PortableInfobox/controllers/ApiQueryPortableInfobox.class.php

87 lines
3.1 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() );
2015-08-18 18:38:06 +00:00
$parser = new Parser();
$parserOptions = new ParserOptions();
2015-08-19 09:56:07 +00:00
$frame = $parser->getPreprocessor()->newFrame();
2015-08-18 11:29:51 +00:00
2015-08-19 13:26:36 +00:00
foreach ( $articles as $id => $article ) {
$parsedInfoboxes = $this->getParsedInfoboxes( $article, $parser, $parserOptions, $frame );
2015-08-18 11:29:51 +00:00
if ( is_array( $parsedInfoboxes ) ) {
$inf = [ ];
2015-08-18 18:38:06 +00:00
foreach ( array_keys( $parsedInfoboxes ) 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 );
}
}
}
}
2015-08-18 18:38:06 +00:00
2015-08-19 13:26:36 +00:00
/**
* @desc For given Article, get property 'infoboxes' from parser output. If property is empty, this may mean that
* template is inside the <noinclude> tag. In this case, we want to skip the <includeonly> tags, get from this only
* infoboxes and parse them again to check their presence and get params.
* @param $article
* @param $parser
* @param $parserOptions
* @param $frame
* @return mixed
*/
protected function getParsedInfoboxes( $article, $parser, $parserOptions, $frame ) {
$parsedInfoboxes = $article->getParserOutput()->getProperty( PortableInfoboxDataService::INFOBOXES_PROPERTY_NAME );
if ( !$parsedInfoboxes ) {
$templateText = $article->fetchContent();
$templateTextWithoutIncludeonly = $parser->getPreloadText( $templateText, $article->getTitle(), $parserOptions );
$infoboxes = $this->processTemplate( $templateTextWithoutIncludeonly );
2015-08-19 13:26:36 +00:00
foreach ( $infoboxes as $infobox ) {
PortableInfoboxParserTagController::getInstance()->render( $infobox, $parser, $frame );
}
$parsedInfoboxes = $parser->getOutput()->getProperty( PortableInfoboxDataService::INFOBOXES_PROPERTY_NAME );
}
return $parsedInfoboxes;
}
2015-08-19 09:56:07 +00:00
/**
* @desc From the template string with removed <includeonly> tags, creates an array of
* strings containing only infoboxes. All template content which is not an infobox is removed.
*
* @param $text string Content of template which uses the <includeonly> tags
* @return array of striped infoboxes ready to parse
*/
protected function processTemplate( $text ) {
preg_match_all( "/<infobox.+<\/infobox>/sU", $text, $result );
2015-08-18 18:38:06 +00:00
return $result[0];
2015-08-18 18:38:06 +00:00
}
}