2015-06-10 09:28:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
2015-06-16 09:00:39 +00:00
|
|
|
class ApiQueryPortableInfobox extends ApiQueryBase {
|
2015-06-10 09:28:33 +00:00
|
|
|
|
|
|
|
public function __construct( $query, $moduleName ) {
|
|
|
|
parent::__construct( $query, $moduleName, 'ib' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2015-06-11 16:08:51 +00:00
|
|
|
$this->runOnPageSet( $this->getPageSet() );
|
2015-06-10 09:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
return __CLASS__ . '$Id$';
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:08:51 +00:00
|
|
|
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 ) ) {
|
2015-06-11 16:08:51 +00:00
|
|
|
$inf = [ ];
|
2015-08-18 18:38:06 +00:00
|
|
|
foreach ( array_keys( $parsedInfoboxes ) as $k => $v ) {
|
2015-06-11 16:08:51 +00:00
|
|
|
$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 ) {
|
2015-06-11 16:08:51 +00:00
|
|
|
$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-06-10 09:28:33 +00:00
|
|
|
}
|
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();
|
2015-08-19 14:08:04 +00:00
|
|
|
$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 ) {
|
2015-08-19 14:08:04 +00:00
|
|
|
preg_match_all( "/<infobox.+<\/infobox>/sU", $text, $result );
|
2015-08-18 18:38:06 +00:00
|
|
|
|
2015-08-19 14:08:04 +00:00
|
|
|
return $result[0];
|
2015-08-18 18:38:06 +00:00
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
}
|