2015-06-09 10:14:00 +00:00
|
|
|
<?php
|
|
|
|
|
2015-09-23 13:51:41 +00:00
|
|
|
use Wikia\PortableInfobox\Helpers\PagePropsProxy;
|
2015-09-22 15:48:38 +00:00
|
|
|
use Wikia\PortableInfobox\Helpers\PortableInfoboxTemplatesHelper;
|
|
|
|
use Wikia\PortableInfobox\Parser\Nodes\NodeInfobox;
|
|
|
|
|
2015-06-09 10:14:00 +00:00
|
|
|
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
|
|
|
protected $title;
|
2015-09-22 15:48:38 +00:00
|
|
|
protected $templateHelper;
|
2015-09-23 13:51:41 +00:00
|
|
|
protected $propsProxy;
|
2015-09-22 15:48:38 +00:00
|
|
|
protected $cache;
|
|
|
|
protected $cachekey;
|
2015-06-22 15:15:26 +00:00
|
|
|
|
2015-09-22 15:48:38 +00:00
|
|
|
/**
|
|
|
|
* @param $title Title
|
2015-09-23 13:51:41 +00:00
|
|
|
*
|
|
|
|
* @internal param $helper
|
2015-09-22 15:48:38 +00:00
|
|
|
*/
|
2015-09-23 13:51:41 +00:00
|
|
|
protected function __construct( $title ) {
|
|
|
|
$this->title = $title !== null ? $title : new Title();
|
|
|
|
$this->templateHelper = new PortableInfoboxTemplatesHelper();
|
|
|
|
$this->propsProxy = new PagePropsProxy();
|
|
|
|
$this->cachekey = wfMemcKey( $this->title->getArticleID(), self::INFOBOXES_PROPERTY_NAME );
|
2015-06-22 15:15:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 13:51:41 +00:00
|
|
|
public static function newFromTitle( $title ) {
|
|
|
|
return new PortableInfoboxDataService( $title );
|
2015-06-23 08:41:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 13:51:41 +00:00
|
|
|
public static function newFromPageID( $pageid ) {
|
|
|
|
return new PortableInfoboxDataService( Title::newFromID( $pageid ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// set internal helpers methods
|
|
|
|
public function setTemplatesHelper( $helper ) {
|
|
|
|
$this->templateHelper = $helper;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPagePropsProxy( $proxy ) {
|
|
|
|
$this->propsProxy = $proxy;
|
|
|
|
|
|
|
|
return $this;
|
2015-06-22 15:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-23 13:51:41 +00:00
|
|
|
* Returns infobox data, terminal method
|
2015-06-22 15:15:26 +00:00
|
|
|
*
|
|
|
|
* @return array in format [ 'data' => [], 'sources' => [] ] or [] will be returned
|
|
|
|
*/
|
|
|
|
public function getData() {
|
2015-09-22 15:48:38 +00:00
|
|
|
if ( $this->title && $this->title->exists() && $this->title->inNamespace( NS_TEMPLATE ) ) {
|
|
|
|
$hidden = $this->templateHelper->parseInfoboxes( $this->title );
|
|
|
|
if ( $hidden ) {
|
|
|
|
$this->delete();
|
2015-09-23 13:51:41 +00:00
|
|
|
$this->set( $hidden );
|
2015-09-22 15:48:38 +00:00
|
|
|
};
|
2015-06-09 10:14:00 +00:00
|
|
|
}
|
2015-09-23 13:51:41 +00:00
|
|
|
$result = $this->get();
|
2015-06-22 15:15:26 +00:00
|
|
|
|
2015-09-23 13:51:41 +00:00
|
|
|
return $result !== null ? $result : [ ];
|
2015-06-09 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:31:04 +00:00
|
|
|
/**
|
2015-09-23 13:51:41 +00:00
|
|
|
* Get image list from infobox data, terminal method
|
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-09-22 15:48:38 +00:00
|
|
|
if ( $field[ 'type' ] == self::IMAGE_FIELD_TYPE &&
|
|
|
|
isset( $field[ 'data' ] ) &&
|
|
|
|
!empty( $field[ 'data' ][ 'key' ] )
|
|
|
|
) {
|
2015-06-22 15:15:26 +00:00
|
|
|
$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 );
|
|
|
|
}
|
2015-08-19 20:00:27 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-22 15:48:38 +00:00
|
|
|
* Save infobox data, permanently
|
|
|
|
*
|
|
|
|
* @param NodeInfobox $raw infobox parser output
|
2015-09-23 13:51:41 +00:00
|
|
|
*
|
|
|
|
* @return $this
|
2015-08-19 20:00:27 +00:00
|
|
|
*/
|
2015-09-22 15:48:38 +00:00
|
|
|
public function save( NodeInfobox $raw ) {
|
|
|
|
if ( $raw ) {
|
|
|
|
$stored = $this->get();
|
|
|
|
$stored[] = [ 'data' => $raw->getRenderData(), 'sources' => $raw->getSource() ];
|
|
|
|
$this->set( $stored );
|
2015-08-19 20:00:27 +00:00
|
|
|
}
|
2015-09-23 13:51:41 +00:00
|
|
|
|
|
|
|
return $this;
|
2015-08-19 20:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-22 15:48:38 +00:00
|
|
|
* Remove infobox data from page props and memcache
|
2015-08-19 20:00:27 +00:00
|
|
|
*/
|
2015-09-22 15:48:38 +00:00
|
|
|
public function delete() {
|
|
|
|
$this->clear();
|
|
|
|
unset( $this->cache );
|
2015-09-23 13:51:41 +00:00
|
|
|
|
|
|
|
return $this;
|
2015-08-19 20:00:27 +00:00
|
|
|
}
|
2015-08-20 13:52:36 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-22 15:48:38 +00:00
|
|
|
* Purge mem cache and local cache
|
2015-08-20 13:52:36 +00:00
|
|
|
*/
|
2015-09-22 15:48:38 +00:00
|
|
|
public function purge() {
|
|
|
|
global $wgMemc;
|
|
|
|
$wgMemc->delete( $this->cachekey );
|
|
|
|
unset( $this->cache );
|
2015-09-23 13:51:41 +00:00
|
|
|
|
|
|
|
return $this;
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
2015-08-20 13:52:36 +00:00
|
|
|
|
2015-09-22 15:48:38 +00:00
|
|
|
// soft cache handlers
|
|
|
|
protected function get() {
|
|
|
|
if ( !isset( $this->cache ) ) {
|
|
|
|
$this->cache = $this->load();
|
2015-08-25 12:27:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 15:48:38 +00:00
|
|
|
return $this->cache;
|
|
|
|
}
|
2015-08-25 12:27:01 +00:00
|
|
|
|
2015-09-22 15:48:38 +00:00
|
|
|
protected function set( $data ) {
|
|
|
|
$this->store( $data );
|
|
|
|
$this->cache = $data;
|
2015-08-25 12:27:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 15:48:38 +00:00
|
|
|
// PageProps handlers with memcache wrappers
|
|
|
|
protected function load() {
|
|
|
|
$id = $this->title->getArticleID();
|
|
|
|
if ( $id ) {
|
|
|
|
global $wgMemc;
|
|
|
|
// first try memcache, then go to props
|
|
|
|
$data = $wgMemc->get( $this->cachekey );
|
|
|
|
if ( $data === false ) {
|
2015-09-23 13:51:41 +00:00
|
|
|
$data = json_decode( $this->propsProxy->get( $id, self::INFOBOXES_PROPERTY_NAME ), true );
|
2015-09-22 15:48:38 +00:00
|
|
|
$wgMemc->set( $this->cachekey, $data, WikiaResponse::CACHE_STANDARD );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2015-08-25 12:27:01 +00:00
|
|
|
|
2015-09-22 15:48:38 +00:00
|
|
|
return [ ];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function store( $data ) {
|
|
|
|
$id = $this->title->getArticleID();
|
|
|
|
if ( $id ) {
|
|
|
|
global $wgMemc;
|
|
|
|
$wgMemc->set( $this->cachekey, $data, WikiaResponse::CACHE_STANDARD );
|
2015-09-23 13:51:41 +00:00
|
|
|
$this->propsProxy->set( $id, [ self::INFOBOXES_PROPERTY_NAME => json_encode( $data ) ] );
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function clear() {
|
|
|
|
$id = $this->title->getArticleID();
|
|
|
|
if ( $id ) {
|
|
|
|
global $wgMemc;
|
|
|
|
$wgMemc->set( $this->cachekey, '', WikiaResponse::CACHE_STANDARD );
|
2015-09-23 13:51:41 +00:00
|
|
|
$this->propsProxy->set( $id, [ self::INFOBOXES_PROPERTY_NAME => '' ] );
|
2015-09-22 15:48:38 +00:00
|
|
|
}
|
2015-08-20 13:52:36 +00:00
|
|
|
}
|
2015-06-09 10:14:00 +00:00
|
|
|
}
|