DAT-2874 refactor infoboxes data service, push to top infobox item

This commit is contained in:
idradm 2015-06-22 17:15:26 +02:00
parent 2b6d2f60e6
commit 02a2d0c3c1
2 changed files with 37 additions and 20 deletions

View file

@ -13,14 +13,12 @@ class PortableInfoboxHooks {
static public function onImageServingCollectImages( &$imageNamesArray, $articleTitle ) {
if ( $articleTitle ) {
$dataService = new PortableInfoboxDataService();
$infoboxData = $dataService->getInfoboxDataByTitle( $articleTitle );
$infoboxImages = $dataService->getImageListFromInfoboxesData( $infoboxData );
$infoboxImages = ( new PortableInfoboxDataService( $articleTitle ) )->getImages();
if ( !empty( $infoboxImages ) ) {
$imageNamesArray = array_merge( $infoboxImages, (array) $imageNamesArray );
$imageNamesArray = array_merge( $infoboxImages, (array)$imageNamesArray );
}
}
return true;
}
}

View file

@ -4,35 +4,54 @@ class PortableInfoboxDataService {
const IMAGE_FIELD_TYPE = 'image';
public function getInfoboxDataByTitle( $title ) {
$data = [];
if ( $title && $title->exists() ) {
$data = Article::newFromTitle( $title, RequestContext::getMain() )
/**
* @var Title $title
*/
protected $title;
public function __construct( $title ) {
$this->title = $title;
}
public static function newFromPageID( $pageid ) {
return new PortableInfoboxDataService( Title::newFromID( $pageid ) );
}
/**
* Returns infobox data
*
* @return array in format [ 'data' => [], 'sources' => [] ] or [] will be returned
*/
public function getData() {
if ( $this->title && $this->title->exists() ) {
$data = Article::newFromTitle( $this->title, RequestContext::getMain() )
//on empty parser cache this should be regenerated, see WikiPage.php:2996
->getParserOutput()
->getProperty( PortableInfoboxParserTagController::INFOBOXES_PROPERTY_NAME );
//return empty [] to prevent false on non existing infobox data
return $data ? $data : [ ];
}
return $data;
return [ ];
}
/**
* Get image list from infobox data
* @param $data array in format returned by PortableInfoboxDataService::getInfoboxDataByTitle which is
* an array of arrays returned by Wikia\PortableInfobox\Parser\XmlParser::getDataFromXmlString
*
* @return array
*/
public function getImageListFromInfoboxesData( $data ) {
$images = [];
public function getImages() {
$images = [ ];
if ( is_array( $data ) ) {
foreach ( $data as $infobox ) {
foreach ( $infobox as $field ) {
if ( $field['type'] == self::IMAGE_FIELD_TYPE && isset( $field['data'] ) && !empty( $field['data']['key'] ) ) {
$images[ $field['data']['key'] ] = true;
}
foreach ( $this->getData() as $infobox ) {
foreach ( $infobox[ 'data' ] as $field ) {
if ( $field[ 'type' ] == self::IMAGE_FIELD_TYPE && isset( $field[ 'data' ] ) && !empty( $field[ 'data' ][ 'key' ] ) ) {
$images[ $field[ 'data' ][ 'key' ] ] = true;
}
}
}
return array_keys( $images );
}
}