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-06-11 16:08:51 +00:00
|
|
|
/**
|
|
|
|
* @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-06-11 16:08:51 +00:00
|
|
|
|
2015-08-18 18:38:06 +00:00
|
|
|
// if in template there are no infoboxes, thay can be hidden, so skip the <includeonly> tags
|
|
|
|
// and parse again to check their presence
|
|
|
|
if ( !$parsedInfoboxes ) {
|
2015-08-18 11:29:51 +00:00
|
|
|
$templateText = $article->fetchContent();
|
2015-08-18 18:38:06 +00:00
|
|
|
$templateText = $parser->getPreloadText($templateText, $article->getTitle(), $parserOptions);
|
2015-08-18 11:29:51 +00:00
|
|
|
|
2015-08-18 18:38:06 +00:00
|
|
|
$infoboxes = $this->validateTemplate( $templateText );
|
|
|
|
|
|
|
|
foreach ( $infoboxes as $infobox ) {
|
|
|
|
PortableInfoboxParserTagController::getInstance()->render($infobox, $parser, $parser->getPreprocessor()->newFrame() );
|
|
|
|
}
|
2015-08-18 11:29:51 +00:00
|
|
|
|
|
|
|
$parsedInfoboxes = $parser->getOutput()->getProperty(PortableInfoboxDataService::INFOBOXES_PROPERTY_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
protected function validateTemplate( $text ) {
|
|
|
|
$infoboxesCount = substr_count($text, '<infobox');
|
|
|
|
$firstInfoboxTagPosition = strpos($text, '<infobox');
|
|
|
|
$firstInfoboxCloseTagPosition = strpos($text, '</infobox>');
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
if ($infoboxesCount === 0 ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $infoboxesCount === 1 && $firstInfoboxTagPosition === 0 && $firstInfoboxCloseTagPosition === strlen($text) - 10) {
|
|
|
|
return [$text];
|
|
|
|
}
|
|
|
|
var_dump($text);
|
|
|
|
while ( $firstInfoboxTagPosition ) {
|
|
|
|
//wyszukaj infoboxy i dodawaj do tablicy wynikowej
|
|
|
|
$cuttedInfobox = substr($text, $firstInfoboxTagPosition, ($firstInfoboxCloseTagPosition + strlen('</infobox>')) - $firstInfoboxTagPosition);
|
|
|
|
|
|
|
|
$text = str_replace($cuttedInfobox, '', $text);
|
|
|
|
$result[] = $cuttedInfobox;
|
|
|
|
|
|
|
|
$firstInfoboxTagPosition = strpos($text, '<infobox');
|
|
|
|
$firstInfoboxCloseTagPosition = strpos($text, '</infobox>');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2015-06-10 09:28:33 +00:00
|
|
|
}
|