PortableInfobox/includes/controllers/ApiQueryPortableInfobox.php

93 lines
2.4 KiB
PHP
Raw Normal View History

<?php
use PortableInfobox\Helpers\PagePropsProxy;
class ApiQueryPortableInfobox extends ApiQueryBase {
protected $propsProxy;
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'ib' );
$this->propsProxy = new PagePropsProxy( true );
}
public function execute() {
$this->runOnPageSet( $this->getPageSet() );
}
protected function runOnPageSet( ApiPageSet $pageSet ) {
2021-12-15 22:01:13 +00:00
$articles = $pageSet->getGoodPages();
2018-08-05 11:45:03 +00:00
$res = $pageSet->getResult();
2015-08-18 11:29:51 +00:00
2015-08-20 14:45:46 +00:00
foreach ( $articles as $id => $articleTitle ) {
2021-12-15 22:01:13 +00:00
$title = Title::castFromPageIdentity( $articleTitle );
if ( $title === null ) {
continue;
}
$parsedInfoboxes = PortableInfoboxDataService::newFromTitle( $title )
->setPagePropsProxy( $this->propsProxy )
->getData();
2015-08-18 11:29:51 +00:00
2015-08-19 20:00:27 +00:00
if ( is_array( $parsedInfoboxes ) && count( $parsedInfoboxes ) ) {
2018-08-16 09:25:53 +00:00
$inf = [];
2015-08-18 18:38:06 +00:00
foreach ( array_keys( $parsedInfoboxes ) as $k => $v ) {
2018-08-16 09:25:53 +00:00
$inf[$k] = [];
}
2018-08-05 11:45:03 +00:00
$res->setIndexedTagName( $inf, 'infobox' );
$res->addValue( [ 'query', 'pages', $id ], 'infoboxes', $inf );
foreach ( $parsedInfoboxes as $count => $infobox ) {
2018-08-05 11:45:03 +00:00
$res->addValue( [ 'query', 'pages', $id, 'infoboxes', $count ], 'id', $count );
2018-08-05 11:45:03 +00:00
$res->addValue(
[ 'query', 'pages', $id, 'infoboxes', $count ],
'parser_tag_version',
$infobox['parser_tag_version']
);
$metadata = $infobox['metadata'];
2018-08-05 11:45:03 +00:00
$res->addValue(
[ 'query', 'pages', $id, 'infoboxes', $count ], 'metadata', $metadata
);
2018-08-05 11:45:03 +00:00
$res->addIndexedTagName(
[ 'query', 'pages', $id, 'infoboxes', $count, 'metadata' ],
'metadata'
);
$this->setIndexedTagNamesForGroupMetadata(
$metadata,
[ 'query', 'pages', $id, 'infoboxes', $count, 'metadata' ],
2018-08-05 11:45:03 +00:00
$res
2016-04-01 11:07:30 +00:00
);
}
}
}
$this->propsProxy->write();
}
/**
* XML format requires all indexed arrays to have _element defined
* This method adds it recursively for all groups
*
* @param array $metadata
* @param array $rootPath
* @param ApiResult $result
*/
2018-10-02 07:41:19 +00:00
private function setIndexedTagNamesForGroupMetadata(
array $metadata, array $rootPath, ApiResult $result
) {
foreach ( $metadata as $nodeCount => $node ) {
if ( $node['type'] === 'group' ) {
$path = array_merge( $rootPath, [ $nodeCount, 'metadata' ] );
2018-08-05 11:45:03 +00:00
$result->addIndexedTagName( $path, 'metadata' );
2018-08-16 09:25:53 +00:00
$this->setIndexedTagNamesForGroupMetadata( $node['metadata'], $path, $result );
}
}
}
}