PortableInfobox/controllers/ApiQueryPortableInfobox.class.php

77 lines
2.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 ) {
2015-08-19 20:00:27 +00:00
$articles = $pageSet->getGoodTitles();
2015-08-18 11:29:51 +00:00
2015-08-20 14:45:46 +00:00
foreach ( $articles as $id => $articleTitle ) {
2015-08-19 20:00:27 +00:00
$parsedInfoboxes = PortableInfoboxDataService::newFromTitle( $articleTitle )->getData();
2015-08-18 11:29:51 +00:00
2015-08-19 20:00:27 +00:00
if ( is_array( $parsedInfoboxes ) && count( $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 );
foreach ( $parsedInfoboxes as $count => $infobox ) {
$pageSet->getResult()->addValue( [ 'query', 'pages', $id, 'infoboxes', $count ], 'id', $count );
$metadata = $infobox['metadata'] ?? $this->metadataFallback( $infobox, $articleTitle );
$pageSet->getResult()->setIndexedTagName( $metadata, 'metadata' );
2016-04-01 11:07:30 +00:00
$pageSet->getResult()->addValue(
[ 'query', 'pages', $id, 'infoboxes', $count ], 'metadata', $metadata
2016-04-01 11:07:30 +00:00
);
}
}
}
}
2016-04-07 09:29:26 +00:00
/**
* We still have old infobox sources in page properties, so we need this fallback.
* Monitor kibana and remove it after logs stop appear
*
* @param $infobox
* @param $title
* @return array
*/
private function metadataFallback( $infobox, $title ) {
global $wgCityId;
Wikia\Logger\WikiaLogger::instance()->info( 'Portable Infobox ApiQuery metadata fallback' );
$task = new Wikia\Tasks\Tasks\RefreshLinksForTitleTask();
$task->setTitle( $title );
$task->title( $title );
$task->call( 'refresh' );
$task->wikiId( $wgCityId );
$task->queue();
// TODO reparse and get fresh data
$data = PortableInfoboxDataService::newFromTitle( $title )->purge()->getData();
var_dump($infobox);
var_dump($data);
die;
return $infobox[ 'sources' ] ? array_fill_keys( $infobox[ 'sources' ], '' ) : [ ];
}
}