2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
|
|
|
|
2015-06-08 13:56:08 +00:00
|
|
|
use Wikia\PortableInfobox\Helpers\SimpleXmlUtil;
|
2015-04-27 14:05:31 +00:00
|
|
|
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-06-09 08:41:45 +00:00
|
|
|
/** @return mixed */
|
2015-05-21 16:07:49 +00:00
|
|
|
public function getParent() {
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
2015-06-09 08:41:45 +00:00
|
|
|
/** @param mixed $parent */
|
2015-05-21 16:07:49 +00:00
|
|
|
public function setParent( Node $parent ) {
|
|
|
|
$this->parent = $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ignoreNodeWhenEmpty() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-09 08:41:45 +00:00
|
|
|
/** @return ExternalParser */
|
2015-05-07 12:37:13 +00:00
|
|
|
public function getExternalParser() {
|
|
|
|
if ( !isset( $this->externalParser ) ) {
|
|
|
|
$this->setExternalParser( new SimpleParser() );
|
|
|
|
}
|
2015-06-08 13:56:08 +00:00
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
return $this->externalParser;
|
|
|
|
}
|
|
|
|
|
2015-06-09 08:41:45 +00:00
|
|
|
/** @param mixed $externalParser */
|
2015-04-27 14:05:31 +00:00
|
|
|
public function setExternalParser( ExternalParser $externalParser ) {
|
|
|
|
$this->externalParser = $externalParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType() {
|
2015-05-25 13:03:45 +00:00
|
|
|
/*
|
|
|
|
* Node type generation is based on XML tag name.
|
|
|
|
* It's worth to remember that SimpleXMLElement::getName method is
|
|
|
|
* case - sensitive ( "<Data>" != "<data>" ), so we need to sanitize Node Type
|
|
|
|
* by using strtolower function
|
|
|
|
*/
|
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-06-09 08:41:45 +00:00
|
|
|
*
|
|
|
|
* @param $data
|
|
|
|
*
|
|
|
|
* @return bool
|
2015-05-19 18:14:39 +00:00
|
|
|
*/
|
2015-04-27 14:05:31 +00:00
|
|
|
public function isEmpty( $data ) {
|
2015-05-18 13:54:25 +00:00
|
|
|
$value = $data[ 'value' ];
|
2015-06-08 13:56:08 +00:00
|
|
|
|
|
|
|
return !( isset( $value ) ) || ( empty( $value ) && $value != '0' );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getValueWithDefault( \SimpleXMLElement $xmlNode ) {
|
2015-06-08 13:56:08 +00:00
|
|
|
$value = $this->extractDataFromSource( $xmlNode );
|
2015-06-09 08:41:45 +00:00
|
|
|
if ( !$value && $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
|
|
|
$value = $this->extractDataFromNode( $xmlNode->{self::DEFAULT_TAG_NAME} );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
2015-06-08 13:56:08 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:57:25 +00:00
|
|
|
protected function getRawValueWithDefault( \SimpleXMLElement $xmlNode ) {
|
2015-06-09 08:41:45 +00:00
|
|
|
$value = $this->getRawInfoboxData( $this->getXmlAttribute( $xmlNode, self::DATA_SRC_ATTR_NAME ) );
|
|
|
|
if ( !$value && $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
|
|
|
$value = $this->getExternalParser()->replaceVariables( (string)$xmlNode->{self::DEFAULT_TAG_NAME} );
|
2015-05-08 15:57:25 +00:00
|
|
|
}
|
2015-06-08 13:56:08 +00:00
|
|
|
|
2015-05-08 15:57:25 +00:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:56:08 +00:00
|
|
|
protected function getValueWithData( \SimpleXMLElement $xmlNode ) {
|
|
|
|
$value = $this->extractDataFromSource( $xmlNode );
|
|
|
|
|
2015-06-09 08:41:45 +00:00
|
|
|
return $value ? $value
|
|
|
|
: $this->extractDataFromNode( $xmlNode );
|
2015-06-08 13:56:08 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 12:37:13 +00:00
|
|
|
protected function getXmlAttribute( \SimpleXMLElement $xmlNode, $attribute ) {
|
2015-06-09 08:41:45 +00:00
|
|
|
return ( isset( $xmlNode[ $attribute ] ) ) ? (string)$xmlNode[ $attribute ]
|
|
|
|
: null;
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 13:56:08 +00:00
|
|
|
protected function getRawInfoboxData( $key ) {
|
2015-06-09 08:41:45 +00:00
|
|
|
return isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ]
|
|
|
|
: null;
|
2015-05-08 15:57:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
protected function getInfoboxData( $key ) {
|
2015-06-08 13:56:08 +00:00
|
|
|
return $this->getExternalParser()->parseRecursive( $this->getRawInfoboxData( $key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \SimpleXMLElement $xmlNode
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function extractDataFromSource( \SimpleXMLElement $xmlNode ) {
|
|
|
|
$source = $this->getXmlAttribute( $xmlNode, self::DATA_SRC_ATTR_NAME );
|
|
|
|
|
2015-06-09 08:41:45 +00:00
|
|
|
return ( !empty( $source ) ) ? $this->getInfoboxData( $source )
|
|
|
|
: null;
|
2015-06-08 13:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \SimpleXMLElement $xmlNode
|
|
|
|
*
|
2015-06-09 08:41:45 +00:00
|
|
|
* @return string
|
2015-06-08 13:56:08 +00:00
|
|
|
*/
|
|
|
|
protected function extractDataFromNode( \SimpleXMLElement $xmlNode ) {
|
|
|
|
/*
|
|
|
|
* <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.
|
|
|
|
*/
|
|
|
|
return $this->getExternalParser()->parseRecursive( SimpleXmlUtil::getInstance()->getInnerXML( $xmlNode ) );
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
}
|