PortableInfobox/services/PortableInfoboxDataService.class.php

159 lines
3.7 KiB
PHP
Raw Normal View History

2015-06-09 10:14:00 +00:00
<?php
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';
const INFOBOXES_PROPERTY_NAME = 'infoboxes';
2015-06-09 10:14:00 +00:00
/**
* @var Title $title
*/
protected $title;
protected $templateHelper;
protected $cache;
protected $cachekey;
/**
* @param $title Title
* @param $helper
*/
protected function __construct( $title, $helper ) {
$this->title = $title;
$this->templateHelper = $helper ? $helper : new PortableInfoboxTemplatesHelper();
$this->cachekey = wfMemcKey( $title->getArticleID(), self::INFOBOXES_PROPERTY_NAME );
}
public static function newFromTitle( $title, $helper = null ) {
return new PortableInfoboxDataService( $title, $helper );
}
public static function newFromPageID( $pageid, $helper = null ) {
return new PortableInfoboxDataService( Title::newFromID( $pageid ), $helper );
}
/**
* Returns infobox data
*
* @return array in format [ 'data' => [], 'sources' => [] ] or [] will be returned
*/
public function getData() {
if ( $this->title && $this->title->exists() && $this->title->inNamespace( NS_TEMPLATE ) ) {
$hidden = $this->templateHelper->parseInfoboxes( $this->title );
if ( $hidden ) {
$this->delete();
$this->set( json_decode( $hidden, true ) );
};
2015-06-09 10:14:00 +00:00
}
return $this->get();
2015-06-09 10:14:00 +00:00
}
2015-06-09 12:31:04 +00:00
/**
* Get image list from infobox data
*
2015-06-09 12:31:04 +00:00
* @return array
*/
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 ) {
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-09 10:14:00 +00:00
return array_keys( $images );
}
2015-08-19 20:00:27 +00:00
/**
* Save infobox data, permanently
*
* @param NodeInfobox $raw infobox parser output
2015-08-19 20:00:27 +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
}
}
/**
* Remove infobox data from page props and memcache
2015-08-19 20:00:27 +00:00
*/
public function delete() {
$this->clear();
unset( $this->cache );
2015-08-19 20:00:27 +00:00
}
2015-08-20 13:52:36 +00:00
/**
* Purge mem cache and local cache
2015-08-20 13:52:36 +00:00
*/
public function purge() {
wfDebug( __CLASS__ . ": PURGE" );
global $wgMemc;
$wgMemc->delete( $this->cachekey );
unset( $this->cache );
}
2015-08-20 13:52:36 +00:00
// soft cache handlers
protected function get() {
if ( !isset( $this->cache ) ) {
$this->cache = $this->load();
2015-08-25 12:27:01 +00:00
}
return $this->cache;
}
2015-08-25 12:27:01 +00:00
protected function set( $data ) {
$this->store( $data );
$this->cache = $data;
2015-08-25 12:27:01 +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 ) {
$data = json_decode( Wikia::getProps( $id, self::INFOBOXES_PROPERTY_NAME ), true );
$wgMemc->set( $this->cachekey, $data, WikiaResponse::CACHE_STANDARD );
}
return $data;
}
2015-08-25 12:27:01 +00:00
return [ ];
}
protected function store( $data ) {
$id = $this->title->getArticleID();
if ( $id ) {
global $wgMemc;
$wgMemc->set( $this->cachekey, $data, WikiaResponse::CACHE_STANDARD );
Wikia::setProps( $id, [ self::INFOBOXES_PROPERTY_NAME => json_encode( $data ) ] );
}
}
protected function clear() {
$id = $this->title->getArticleID();
if ( $id ) {
global $wgMemc;
$wgMemc->set( $this->cachekey, '', WikiaResponse::CACHE_STANDARD );
Wikia::setProps( $id, [ self::INFOBOXES_PROPERTY_NAME => '' ] );
}
2015-08-20 13:52:36 +00:00
}
2015-06-09 10:14:00 +00:00
}