2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
|
|
|
|
|
|
|
use Wikia\PortableInfobox\Parser\ExternalParser;
|
2015-05-07 12:37:13 +00:00
|
|
|
use Wikia\PortableInfobox\Parser\SimpleParser;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
|
|
|
class Node {
|
|
|
|
|
|
|
|
const DATA_SRC_ATTR_NAME = 'source';
|
|
|
|
const DEFAULT_TAG_NAME = 'default';
|
|
|
|
const VALUE_TAG_NAME = 'value';
|
|
|
|
const LABEL_TAG_NAME = 'label';
|
|
|
|
|
|
|
|
protected $xmlNode;
|
2015-05-21 16:07:49 +00:00
|
|
|
protected $parent = null;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
|
|
|
/* @var $externalParser ExternalParser */
|
|
|
|
protected $externalParser;
|
|
|
|
|
|
|
|
public function __construct( \SimpleXMLElement $xmlNode, $infoboxData ) {
|
|
|
|
$this->xmlNode = $xmlNode;
|
|
|
|
$this->infoboxData = $infoboxData;
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:07:49 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getParent() {
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $parent
|
|
|
|
*/
|
|
|
|
public function setParent( Node $parent ) {
|
|
|
|
$this->parent = $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ignoreNodeWhenEmpty() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
/**
|
|
|
|
* @return ExternalParser
|
|
|
|
*/
|
|
|
|
public function getExternalParser() {
|
|
|
|
if ( !isset( $this->externalParser ) ) {
|
|
|
|
$this->setExternalParser( new SimpleParser() );
|
|
|
|
}
|
|
|
|
return $this->externalParser;
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $externalParser
|
|
|
|
*/
|
|
|
|
public function setExternalParser( ExternalParser $externalParser ) {
|
|
|
|
$this->externalParser = $externalParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType() {
|
2015-05-21 16:07:49 +00:00
|
|
|
return strtolower( $this->xmlNode->getName() );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getData() {
|
2015-05-07 12:37:13 +00:00
|
|
|
return [ 'value' => (string)$this->xmlNode ];
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 18:14:39 +00:00
|
|
|
/**
|
|
|
|
* @desc Check if node is empty.
|
|
|
|
* Note that a '0' value cannot be treated like a null
|
|
|
|
*/
|
2015-04-27 14:05:31 +00:00
|
|
|
public function isEmpty( $data ) {
|
2015-05-18 13:54:25 +00:00
|
|
|
$value = $data[ 'value' ];
|
|
|
|
return !( isset( $value ) ) || (empty( $value ) && $value != '0');
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getValueWithDefault( \SimpleXMLElement $xmlNode ) {
|
|
|
|
$source = $this->getXmlAttribute( $xmlNode, self::DATA_SRC_ATTR_NAME );
|
|
|
|
$value = null;
|
|
|
|
if ( !empty( $source ) ) {
|
|
|
|
$value = $this->getInfoboxData( $source );
|
|
|
|
}
|
|
|
|
if ( !$value ) {
|
|
|
|
if ( $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
2015-05-08 14:56:44 +00:00
|
|
|
/*
|
|
|
|
* <default> tag can contain <ref> or other WikiText parser hooks
|
|
|
|
* We should not parse it's contents as XML but return pure text in order to let MediaWiki Parser
|
|
|
|
* parse it.
|
|
|
|
*/
|
2015-05-13 11:51:07 +00:00
|
|
|
$value = \Wikia\PortableInfobox\Helpers\SimpleXmlUtil::getInstance()->getInnerXML(
|
|
|
|
$xmlNode->{self::DEFAULT_TAG_NAME}
|
|
|
|
);
|
2015-05-07 12:37:13 +00:00
|
|
|
$value = $this->getExternalParser()->parseRecursive( $value );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:57:25 +00:00
|
|
|
protected function getRawValueWithDefault( \SimpleXMLElement $xmlNode ) {
|
|
|
|
$source = $this->getXmlAttribute( $xmlNode, self::DATA_SRC_ATTR_NAME );
|
|
|
|
$value = null;
|
|
|
|
if ( !empty( $source ) ) {
|
2015-05-11 08:44:24 +00:00
|
|
|
$value = $this->getRawInfoboxData( $source );
|
2015-05-08 15:57:25 +00:00
|
|
|
}
|
|
|
|
if ( !$value ) {
|
|
|
|
if ( $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
|
|
|
$value = (string)$xmlNode->{self::DEFAULT_TAG_NAME};
|
|
|
|
$value = $this->getExternalParser()->replaceVariables( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
protected function getXmlAttribute( \SimpleXMLElement $xmlNode, $attribute ) {
|
|
|
|
if ( isset( $xmlNode[ $attribute ] ) )
|
|
|
|
return (string)$xmlNode[ $attribute ];
|
2015-04-27 14:05:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:57:25 +00:00
|
|
|
protected function getRawInfoboxData ( $key ) {
|
|
|
|
$data = isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ] : null;
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
protected function getInfoboxData( $key ) {
|
2015-05-11 08:44:24 +00:00
|
|
|
return $this->getExternalParser()->parseRecursive( $this->getRawInfoboxData ( $key ) );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|