mirror of
https://github.com/Universal-Omega/PortableInfobox.git
synced 2024-11-15 11:59:56 +00:00
external parser approach changed, tests fixed, refactoring
This commit is contained in:
parent
ce1b842110
commit
242bada43d
|
@ -19,6 +19,7 @@ $wgAutoloadClasses[ 'PortableInfoboxRenderService' ] = $dir . 'services/Portable
|
||||||
|
|
||||||
// parser
|
// parser
|
||||||
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\ExternalParser'] = $dir . 'services/Parser/ExternalParser.php';
|
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\ExternalParser'] = $dir . 'services/Parser/ExternalParser.php';
|
||||||
|
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\SimpleParser'] = $dir . 'services/Parser/SimpleParser.php';
|
||||||
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\XmlParser'] = $dir . 'services/Parser/XmlParser.php';
|
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\XmlParser'] = $dir . 'services/Parser/XmlParser.php';
|
||||||
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\DummyParser'] = $dir . 'services/Parser/DummyParser.php';
|
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\DummyParser'] = $dir . 'services/Parser/DummyParser.php';
|
||||||
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\MediaWikiParserService'] = $dir . 'services/Parser/MediaWikiParserService.php';
|
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\MediaWikiParserService'] = $dir . 'services/Parser/MediaWikiParserService.php';
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
namespace Wikia\PortableInfobox\Parser\Nodes;
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
|
|
||||||
use Wikia\PortableInfobox\Parser\ExternalParser;
|
use Wikia\PortableInfobox\Parser\ExternalParser;
|
||||||
|
use Wikia\PortableInfobox\Parser\SimpleParser;
|
||||||
|
|
||||||
class Node {
|
class Node {
|
||||||
|
|
||||||
|
@ -20,6 +21,16 @@ class Node {
|
||||||
$this->infoboxData = $infoboxData;
|
$this->infoboxData = $infoboxData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ExternalParser
|
||||||
|
*/
|
||||||
|
public function getExternalParser() {
|
||||||
|
if ( !isset( $this->externalParser ) ) {
|
||||||
|
$this->setExternalParser( new SimpleParser() );
|
||||||
|
}
|
||||||
|
return $this->externalParser;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $externalParser
|
* @param mixed $externalParser
|
||||||
*/
|
*/
|
||||||
|
@ -32,7 +43,7 @@ class Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getData() {
|
public function getData() {
|
||||||
return [ 'value' => (string) $this->xmlNode ];
|
return [ 'value' => (string)$this->xmlNode ];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty( $data ) {
|
public function isEmpty( $data ) {
|
||||||
|
@ -47,36 +58,21 @@ class Node {
|
||||||
}
|
}
|
||||||
if ( !$value ) {
|
if ( !$value ) {
|
||||||
if ( $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
if ( $xmlNode->{self::DEFAULT_TAG_NAME} ) {
|
||||||
$value = (string) $xmlNode->{self::DEFAULT_TAG_NAME};
|
$value = (string)$xmlNode->{self::DEFAULT_TAG_NAME};
|
||||||
$value = $this->parseWithExternalParser( $value, true );
|
$value = $this->getExternalParser()->parseRecursive( $value );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getXmlAttribute( \SimpleXMLElement $xmlNode, $attribute ) {
|
protected function getXmlAttribute( \SimpleXMLElement $xmlNode, $attribute ) {
|
||||||
if( isset( $xmlNode[ $attribute ] ) )
|
if ( isset( $xmlNode[ $attribute ] ) )
|
||||||
return (string) $xmlNode[ $attribute ];
|
return (string)$xmlNode[ $attribute ];
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @FIXME: regardless of what is the final approach, this code needs to be explained
|
|
||||||
* WHY it does the things it does. Here. In docblock. Or by phrasing it explicitly with
|
|
||||||
* class and method names.
|
|
||||||
*/
|
|
||||||
protected function parseWithExternalParser( $data, $recursive = true ) {
|
|
||||||
if ( !empty( $data ) && !empty( $this->externalParser ) ) {
|
|
||||||
if ( $recursive ) {
|
|
||||||
return $this->externalParser->parseRecursive( $data );
|
|
||||||
}
|
|
||||||
return $this->externalParser->parse( $data );
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getInfoboxData( $key ) {
|
protected function getInfoboxData( $key ) {
|
||||||
$data = isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ] : null;
|
$data = isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ] : null;
|
||||||
return $this->parseWithExternalParser( $data );
|
return $this->getExternalParser()->parse( $data );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,26 +3,22 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
|
|
||||||
use Wikia\PortableInfobox\Parser\XmlParser;
|
use Wikia\PortableInfobox\Parser\XmlParser;
|
||||||
|
|
||||||
class NodeComparison extends Node {
|
class NodeComparison extends Node {
|
||||||
|
|
||||||
public function getData() {
|
public function getData() {
|
||||||
$nodeFactory = new XmlParser( $this->infoboxData );
|
$nodeFactory = new XmlParser( $this->infoboxData );
|
||||||
if ( $this->externalParser ) {
|
if ( $this->externalParser ) {
|
||||||
$nodeFactory->setExternalParser( $this->externalParser );
|
$nodeFactory->setExternalParser( $this->externalParser );
|
||||||
}
|
}
|
||||||
$data = [];
|
return [ 'value' => $nodeFactory->getDataFromNodes( $this->xmlNode ) ];
|
||||||
$data['value'] = $nodeFactory->getDataFromNodes( $this->xmlNode );
|
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty( $data ) {
|
public function isEmpty( $data ) {
|
||||||
foreach ( $data['value'] as $group ) {
|
foreach ( $data[ 'value' ] as $group ) {
|
||||||
if ( $group['isEmpty'] == false ) {
|
if ( $group[ 'isEmpty' ] == false ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
class NodeData extends Node {
|
class NodeData extends Node {
|
||||||
|
|
||||||
public function getData() {
|
public function getData() {
|
||||||
$data = [];
|
return [
|
||||||
$data['label'] = $this->parseWithExternalParser( (string) $this->xmlNode->{self::LABEL_TAG_NAME}, true );
|
'label' => $this->getExternalParser()->parseRecursive( (string) $this->xmlNode->{self::LABEL_TAG_NAME} ),
|
||||||
$data['value'] = $this->getValueWithDefault( $this->xmlNode );
|
'value' => $this->getValueWithDefault( $this->xmlNode )
|
||||||
return $data;
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,11 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
class NodeFooter extends Node {
|
class NodeFooter extends Node {
|
||||||
|
|
||||||
public function getData() {
|
public function getData() {
|
||||||
$data = [];
|
return [ 'value' => $this->getExternalParser()->parseRecursive( (string)$this->xmlNode ) ];
|
||||||
$data['value'] = $this->parseWithExternalParser( (string) $this->xmlNode, true );
|
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty( $data ) {
|
public function isEmpty( $data ) {
|
||||||
$links = trim( $data['value'] );
|
$links = trim( $data[ 'value' ] );
|
||||||
return empty( $links );
|
return empty( $links );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Wikia\PortableInfobox\Parser\Nodes;
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
|
|
||||||
use Wikia\PortableInfobox\Parser\XmlParser;
|
use Wikia\PortableInfobox\Parser\XmlParser;
|
||||||
|
|
||||||
class NodeGroup extends Node {
|
class NodeGroup extends Node {
|
||||||
|
@ -9,19 +10,15 @@ class NodeGroup extends Node {
|
||||||
if ( $this->externalParser ) {
|
if ( $this->externalParser ) {
|
||||||
$nodeFactory->setExternalParser( $this->externalParser );
|
$nodeFactory->setExternalParser( $this->externalParser );
|
||||||
}
|
}
|
||||||
$data = [];
|
return [ 'value' => $nodeFactory->getDataFromNodes( $this->xmlNode ) ];
|
||||||
$data['value'] = $nodeFactory->getDataFromNodes( $this->xmlNode );
|
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty( $data ) {
|
public function isEmpty( $data ) {
|
||||||
foreach ( $data['value'] as $elem ) {
|
foreach ( $data[ 'value' ] as $elem ) {
|
||||||
if ( $elem['type'] != 'header' && !($elem['isEmpty']) ) {
|
if ( $elem[ 'type' ] != 'header' && !( $elem[ 'isEmpty' ] ) ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
namespace Wikia\PortableInfobox\Parser\Nodes;
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
|
|
||||||
class NodeHeader extends Node {
|
class NodeHeader extends Node {
|
||||||
public function getData() {
|
|
||||||
return [ 'value' => $this->parseWithExternalParser( (string) $this->xmlNode, true ) ];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public function getData() {
|
||||||
|
return [ 'value' => $this->getExternalParser()->parseRecursive( (string)$this->xmlNode ) ];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,24 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Wikia\PortableInfobox\Parser\Nodes;
|
namespace Wikia\PortableInfobox\Parser\Nodes;
|
||||||
|
|
||||||
|
use Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer;
|
||||||
|
|
||||||
class NodeImage extends Node {
|
class NodeImage extends Node {
|
||||||
const ALT_TAG_NAME = 'alt';
|
const ALT_TAG_NAME = 'alt';
|
||||||
|
|
||||||
public function getData() {
|
public function getData() {
|
||||||
$node = [];
|
return [
|
||||||
|
'value' => $this->resolveImageUrl( $this->getValueWithDefault( $this->xmlNode ) ),
|
||||||
$imageName = $this->getValueWithDefault( $this->xmlNode );
|
'alt' => $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} )
|
||||||
$node['value'] = $this->resolveImageUrl( $imageName );
|
];
|
||||||
$node['alt'] = $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} );
|
|
||||||
|
|
||||||
return $node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolveImageUrl( $filename ) {
|
public function resolveImageUrl( $filename ) {
|
||||||
global $wgContLang;
|
global $wgContLang;
|
||||||
$title = \Title::newFromText( \Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer::getInstance()
|
$title = \Title::newFromText(
|
||||||
->sanitizeImageFileName($filename, $wgContLang), NS_FILE );
|
ImageFilenameSanitizer::getInstance()->sanitizeImageFileName( $filename, $wgContLang ),
|
||||||
|
NS_FILE
|
||||||
|
);
|
||||||
if ( $title && $title->exists() ) {
|
if ( $title && $title->exists() ) {
|
||||||
return \WikiaFileHelper::getFileFromTitle( $title )->getUrlGenerator()->url();
|
return \WikiaFileHelper::getFileFromTitle( $title )->getUrlGenerator()->url();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -10,14 +10,12 @@ class NodeSet extends Node {
|
||||||
if ( $this->externalParser ) {
|
if ( $this->externalParser ) {
|
||||||
$nodeFactory->setExternalParser( $this->externalParser );
|
$nodeFactory->setExternalParser( $this->externalParser );
|
||||||
}
|
}
|
||||||
$data = [];
|
return [ 'value' => $nodeFactory->getDataFromNodes( $this->xmlNode ) ];
|
||||||
$data['value'] = $nodeFactory->getDataFromNodes( $this->xmlNode );
|
|
||||||
return $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isEmpty( $data ) {
|
public function isEmpty( $data ) {
|
||||||
foreach ( $data['value'] as $elem ) {
|
foreach ( $data[ 'value' ] as $elem ) {
|
||||||
if ( $elem['type'] != 'header' && !($elem['isEmpty']) ) {
|
if ( $elem[ 'type' ] != 'header' && !( $elem[ 'isEmpty' ] ) ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
13
services/Parser/SimpleParser.php
Normal file
13
services/Parser/SimpleParser.php
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
namespace Wikia\PortableInfobox\Parser;
|
||||||
|
|
||||||
|
class SimpleParser implements ExternalParser {
|
||||||
|
public function parse( $text ) {
|
||||||
|
return $text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parseRecursive( $text ) {
|
||||||
|
return $this->parse( $text );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Wikia\PortableInfobox\Parser;
|
namespace Wikia\PortableInfobox\Parser;
|
||||||
|
|
||||||
//interface moved here, because of $wgAutoloadClass issue
|
////interface moved here, because of $wgAutoloadClass issue
|
||||||
interface ExternalParser {
|
//interface ExternalParser {
|
||||||
public function parse( $text );
|
// public function parse( $text );
|
||||||
public function parseRecursive( $text );
|
// public function parseRecursive( $text );
|
||||||
}
|
//}
|
||||||
|
|
||||||
class XmlParser {
|
class XmlParser {
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,10 @@ class XmlParserTest extends WikiaBaseTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsEmpty() {
|
public function testIsEmpty() {
|
||||||
$parser = new \Wikia\PortableInfobox\Parser\XmlParser([
|
$parser = new \Wikia\PortableInfobox\Parser\XmlParser( [
|
||||||
'elem2' => 'ELEM2',
|
'elem2' => 'ELEM2',
|
||||||
'lado2' => 'LALALA'
|
'lado2' => 'LALALA'
|
||||||
]);
|
] );
|
||||||
$markup = '
|
$markup = '
|
||||||
<infobox>
|
<infobox>
|
||||||
<comparison>
|
<comparison>
|
||||||
|
@ -25,22 +25,22 @@ class XmlParserTest extends WikiaBaseTest {
|
||||||
';
|
';
|
||||||
$data = $parser->getDataFromXmlString( $markup );
|
$data = $parser->getDataFromXmlString( $markup );
|
||||||
// infobox -> comparison -> set -> header
|
// infobox -> comparison -> set -> header
|
||||||
$this->assertTrue( $data[0]['data']['value'][0]['value'][0]['isEmpty'] == false );
|
$this->assertFalse( $data[ 0 ][ 'data' ][ 'value' ][ 0 ][ 'data' ][ 'value' ][ 0 ][ 'isEmpty' ] );
|
||||||
// infobox -> comparison -> set -> data { lado1 }
|
// infobox -> comparison -> set -> data { lado1 }
|
||||||
$this->assertTrue( $data[0]['data']['value'][0]['data']['value'][1]['isEmpty'] == true );
|
$this->assertTrue( $data[ 0 ][ 'data' ][ 'value' ][ 0 ][ 'data' ][ 'value' ][ 1 ][ 'isEmpty' ] );
|
||||||
// infobox -> comparison -> set -> data { lado2 }
|
// infobox -> comparison -> set -> data { lado2 }
|
||||||
$this->assertTrue( $data[0]['data']['value'][0]['data']['value'][2]['isEmpty'] == false );
|
$this->assertFalse( $data[ 0 ][ 'data' ][ 'value' ][ 0 ][ 'data' ][ 'value' ][ 2 ][ 'isEmpty' ] );
|
||||||
// infobox -> comparison -> set
|
// infobox -> comparison -> set
|
||||||
$this->assertTrue( $data[0]['data']['value']['isEmpty'] == false );
|
$this->assertFalse( $data[ 0 ][ 'data' ][ 'value' ][ 0 ][ 'isEmpty' ] );
|
||||||
// infobox -> comparison
|
// infobox -> comparison
|
||||||
$this->assertTrue( $data[0]['isEmpty'] == false );
|
$this->assertFalse( $data[ 0 ][ 'isEmpty' ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testExternalParser() {
|
public function testExternalParser() {
|
||||||
$parser = new \Wikia\PortableInfobox\Parser\XmlParser([
|
$parser = new \Wikia\PortableInfobox\Parser\XmlParser( [
|
||||||
'elem2' => 'ELEM2',
|
'elem2' => 'ELEM2',
|
||||||
'lado2' => 'LALALA'
|
'lado2' => 'LALALA'
|
||||||
]);
|
] );
|
||||||
$externalParser = new \Wikia\PortableInfobox\Parser\DummyParser();
|
$externalParser = new \Wikia\PortableInfobox\Parser\DummyParser();
|
||||||
$parser->setExternalParser( $externalParser );
|
$parser->setExternalParser( $externalParser );
|
||||||
$markup = '
|
$markup = '
|
||||||
|
@ -57,7 +57,8 @@ class XmlParserTest extends WikiaBaseTest {
|
||||||
</infobox>
|
</infobox>
|
||||||
';
|
';
|
||||||
$data = $parser->getDataFromXmlString( $markup );
|
$data = $parser->getDataFromXmlString( $markup );
|
||||||
$this->assertTrue( $data[0]['data']['value'] == 'parseRecursive(ABB)' );
|
$this->assertEquals( 'parseRecursive(ABB)', $data[ 0 ][ 'data' ][ 'value' ] );
|
||||||
$this->assertTrue( $data[1]['data']['value'][0]['data']['value'][2]['data']['value'] == 'parseRecursive(LALALA)');
|
$this->assertEquals( 'parse(LALALA)',
|
||||||
|
$data[ 1 ][ 'data' ][ 'value' ][ 0 ][ 'data' ][ 'value' ][ 2 ][ 'data' ][ 'value' ] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue