PortableInfobox/includes/controllers/ApiQueryAllInfoboxes.php

62 lines
1.4 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\MediaWikiServices;
2021-09-12 16:42:37 +00:00
class ApiQueryAllInfoboxes extends ApiQueryBase {
2021-09-10 02:52:19 +00:00
private const CACHE_TTL = 86400;
2021-09-12 16:42:37 +00:00
2021-09-10 02:52:19 +00:00
public const MCACHE_KEY = 'allinfoboxes-list';
/**
* @param ApiQuery $query
* @param string $moduleName
*/
public function __construct( ApiQuery $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'api' );
}
public function execute() {
2018-08-05 11:06:26 +00:00
$db = $this->getDB();
$res = $this->getResult();
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
2018-08-06 12:27:25 +00:00
$cachekey = $cache->makeKey( self::MCACHE_KEY );
2018-08-05 11:06:26 +00:00
$data = $cache->getWithSetCallback( $cachekey, self::CACHE_TTL, function () use ( $db ) {
$out = [];
$res = ( new AllInfoboxesQueryPage() )->doQuery();
2021-09-10 02:52:19 +00:00
foreach ( $res as $row ) {
2018-08-05 11:06:26 +00:00
$out[] = [
2018-08-06 13:16:36 +00:00
'pageid' => $row->value,
'title' => $row->title,
'label' => $this->createLabel( $row->title ),
'ns' => $row->namespace
2018-08-05 11:06:26 +00:00
];
}
return $out;
} );
foreach ( $data as $id => $infobox ) {
2018-08-05 11:06:26 +00:00
$res->addValue( [ 'query', 'allinfoboxes' ], null, $infobox );
}
2018-08-05 11:06:26 +00:00
$res->addIndexedTagName( [ 'query', 'allinfoboxes' ], 'i' );
}
/**
2018-10-02 07:41:19 +00:00
* As a infobox template label we want to return a nice, clean text, without e.g. '_' signs
* @param $text infobox template title
2018-10-02 07:41:19 +00:00
* @return string
*/
private function createLabel( $text ) {
2018-08-16 09:25:53 +00:00
$title = Title::newFromText( $text, NS_TEMPLATE );
if ( $title ) {
return $title->getText();
}
return $text;
}
}