2015-06-09 10:14:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PortableInfoboxDataService {
|
|
|
|
|
|
|
|
const IMAGE_FIELD_TYPE = 'image';
|
2015-07-02 07:54:06 +00:00
|
|
|
const INFOBOXES_PROPERTY_NAME = 'infoboxes';
|
2015-06-09 10:14:00 +00:00
|
|
|
|
2015-06-22 15:15:26 +00:00
|
|
|
/**
|
|
|
|
* @var Title $title
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
|
2015-06-23 08:41:15 +00:00
|
|
|
protected function __construct( $title ) {
|
2015-06-22 15:15:26 +00:00
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
2015-07-01 13:17:52 +00:00
|
|
|
public static function newFromTitle( $title ) {
|
2015-06-23 08:41:15 +00:00
|
|
|
return new PortableInfoboxDataService( $title );
|
|
|
|
}
|
|
|
|
|
2015-06-22 15:15:26 +00:00
|
|
|
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() ) {
|
2015-07-01 10:05:08 +00:00
|
|
|
$parserOutput = Article::newFromTitle( $this->title, RequestContext::getMain() )
|
2015-06-09 10:14:00 +00:00
|
|
|
//on empty parser cache this should be regenerated, see WikiPage.php:2996
|
2015-07-01 10:05:08 +00:00
|
|
|
->getParserOutput();
|
|
|
|
$data = $parserOutput ?
|
2015-07-02 07:54:06 +00:00
|
|
|
$parserOutput->getProperty( self::INFOBOXES_PROPERTY_NAME )
|
2015-07-01 10:05:08 +00:00
|
|
|
: false;
|
2015-06-22 15:15:26 +00:00
|
|
|
|
|
|
|
//return empty [] to prevent false on non existing infobox data
|
|
|
|
return $data ? $data : [ ];
|
2015-06-09 10:14:00 +00:00
|
|
|
}
|
2015-06-22 15:15:26 +00:00
|
|
|
|
|
|
|
return [ ];
|
2015-06-09 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:31:04 +00:00
|
|
|
/**
|
|
|
|
* Get image list from infobox data
|
2015-06-22 15:15:26 +00:00
|
|
|
*
|
2015-06-09 12:31:04 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-06-22 15:15:26 +00:00
|
|
|
public function getImages() {
|
|
|
|
$images = [ ];
|
|
|
|
|
|
|
|
foreach ( $this->getData() as $infobox ) {
|
2015-07-01 13:03:54 +00:00
|
|
|
// ensure data array exists
|
|
|
|
$data = is_array( $infobox[ 'data' ] ) ? $infobox[ 'data' ] : [ ];
|
|
|
|
foreach ( $data as $field ) {
|
2015-06-22 15:15:26 +00:00
|
|
|
if ( $field[ 'type' ] == self::IMAGE_FIELD_TYPE && isset( $field[ 'data' ] ) && !empty( $field[ 'data' ][ 'key' ] ) ) {
|
|
|
|
$images[ $field[ 'data' ][ 'key' ] ] = true;
|
2015-06-09 10:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 15:15:26 +00:00
|
|
|
|
2015-06-09 10:14:00 +00:00
|
|
|
return array_keys( $images );
|
|
|
|
}
|
|
|
|
}
|