PortableInfobox/controllers/ApiQueryPortableInfobox.class.php

56 lines
1.9 KiB
PHP
Raw Normal View History

<?php
class ApiQueryPortableInfobox extends ApiQueryBase {
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'ib' );
}
public function execute() {
$this->runOnPageSet( $this->getPageSet() );
}
public function getVersion() {
return __CLASS__ . '$Id$';
}
protected function runOnPageSet( ApiPageSet $pageSet ) {
$articles = array_map( function ( Title $item ) {
return Article::newFromTitle( $item, RequestContext::getMain() );
}, $pageSet->getGoodTitles() );
/**
* @var Article $article
*/
foreach ( $articles as $id => $article ) {
2015-08-18 11:29:51 +00:00
$parsedInfoboxes = $article->getParserOutput()->getProperty( PortableInfoboxDataService::INFOBOXES_PROPERTY_NAME );
2015-08-18 11:29:51 +00:00
//if in template there are no infoboxes, skip the <includeonly> tags and parse again
if (!$parsedInfoboxes) {
$templateText = $article->fetchContent();
$parser = new Parser();
$templateText = $parser->getPreloadText($templateText, $article->getTitle(), new ParserOptions());
PortableInfoboxParserTagController::getInstance()->render($templateText, $parser, $parser->getPreprocessor()->newFrame() );
$parsedInfoboxes = $parser->getOutput()->getProperty(PortableInfoboxDataService::INFOBOXES_PROPERTY_NAME);
}
if ( is_array( $parsedInfoboxes ) ) {
$inf = [ ];
foreach ( array_keys( $d ) as $k => $v ) {
$inf[ $k ] = [ ];
}
$pageSet->getResult()->setIndexedTagName( $inf, 'infobox' );
$pageSet->getResult()->addValue( [ 'query', 'pages', $id ], 'infoboxes', $inf );
2015-08-18 11:29:51 +00:00
foreach ( $parsedInfoboxes as $count => $infobox ) {
$s = isset( $infobox[ 'sources' ] ) ? $infobox[ 'sources' ] : [ ];
$pageSet->getResult()->addValue( [ 'query', 'pages', $id, 'infoboxes', $count ], 'id', $count );
$pageSet->getResult()->setIndexedTagName( $s, "source" );
$pageSet->getResult()->addValue( [ 'query', 'pages', $id, 'infoboxes', $count ], 'sources', $s );
}
}
}
}
}