external parser approach changed, tests fixed, refactoring

This commit is contained in:
Adam Robak 2015-05-07 14:37:13 +02:00
parent ce1b842110
commit 242bada43d
12 changed files with 79 additions and 78 deletions

View file

@ -19,6 +19,7 @@ $wgAutoloadClasses[ 'PortableInfoboxRenderService' ] = $dir . 'services/Portable
// parser
$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\\DummyParser'] = $dir . 'services/Parser/DummyParser.php';
$wgAutoloadClasses[ 'Wikia\\PortableInfobox\\Parser\\MediaWikiParserService'] = $dir . 'services/Parser/MediaWikiParserService.php';

View file

@ -2,6 +2,7 @@
namespace Wikia\PortableInfobox\Parser\Nodes;
use Wikia\PortableInfobox\Parser\ExternalParser;
use Wikia\PortableInfobox\Parser\SimpleParser;
class Node {
@ -20,6 +21,16 @@ class Node {
$this->infoboxData = $infoboxData;
}
/**
* @return ExternalParser
*/
public function getExternalParser() {
if ( !isset( $this->externalParser ) ) {
$this->setExternalParser( new SimpleParser() );
}
return $this->externalParser;
}
/**
* @param mixed $externalParser
*/
@ -48,7 +59,7 @@ class Node {
if ( !$value ) {
if ( $xmlNode->{self::DEFAULT_TAG_NAME} ) {
$value = (string)$xmlNode->{self::DEFAULT_TAG_NAME};
$value = $this->parseWithExternalParser( $value, true );
$value = $this->getExternalParser()->parseRecursive( $value );
}
}
return $value;
@ -60,23 +71,8 @@ class Node {
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 ) {
$data = isset( $this->infoboxData[ $key ] ) ? $this->infoboxData[ $key ] : null;
return $this->parseWithExternalParser( $data );
return $this->getExternalParser()->parse( $data );
}
}

View file

@ -10,9 +10,7 @@ class NodeComparison extends Node {
if ( $this->externalParser ) {
$nodeFactory->setExternalParser( $this->externalParser );
}
$data = [];
$data['value'] = $nodeFactory->getDataFromNodes( $this->xmlNode );
return $data;
return [ 'value' => $nodeFactory->getDataFromNodes( $this->xmlNode ) ];
}
public function isEmpty( $data ) {
@ -23,6 +21,4 @@ class NodeComparison extends Node {
}
return true;
}
}

View file

@ -4,9 +4,9 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
class NodeData extends Node {
public function getData() {
$data = [];
$data['label'] = $this->parseWithExternalParser( (string) $this->xmlNode->{self::LABEL_TAG_NAME}, true );
$data['value'] = $this->getValueWithDefault( $this->xmlNode );
return $data;
return [
'label' => $this->getExternalParser()->parseRecursive( (string) $this->xmlNode->{self::LABEL_TAG_NAME} ),
'value' => $this->getValueWithDefault( $this->xmlNode )
];
}
}

View file

@ -4,9 +4,7 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
class NodeFooter extends Node {
public function getData() {
$data = [];
$data['value'] = $this->parseWithExternalParser( (string) $this->xmlNode, true );
return $data;
return [ 'value' => $this->getExternalParser()->parseRecursive( (string)$this->xmlNode ) ];
}
public function isEmpty( $data ) {

View file

@ -1,5 +1,6 @@
<?php
namespace Wikia\PortableInfobox\Parser\Nodes;
use Wikia\PortableInfobox\Parser\XmlParser;
class NodeGroup extends Node {
@ -9,9 +10,7 @@ class NodeGroup extends Node {
if ( $this->externalParser ) {
$nodeFactory->setExternalParser( $this->externalParser );
}
$data = [];
$data['value'] = $nodeFactory->getDataFromNodes( $this->xmlNode );
return $data;
return [ 'value' => $nodeFactory->getDataFromNodes( $this->xmlNode ) ];
}
public function isEmpty( $data ) {
@ -22,6 +21,4 @@ class NodeGroup extends Node {
}
return true;
}
}

View file

@ -2,8 +2,8 @@
namespace Wikia\PortableInfobox\Parser\Nodes;
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 ) ];
}
}

View file

@ -1,23 +1,24 @@
<?php
namespace Wikia\PortableInfobox\Parser\Nodes;
use Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer;
class NodeImage extends Node {
const ALT_TAG_NAME = 'alt';
public function getData() {
$node = [];
$imageName = $this->getValueWithDefault( $this->xmlNode );
$node['value'] = $this->resolveImageUrl( $imageName );
$node['alt'] = $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} );
return $node;
return [
'value' => $this->resolveImageUrl( $this->getValueWithDefault( $this->xmlNode ) ),
'alt' => $this->getValueWithDefault( $this->xmlNode->{self::ALT_TAG_NAME} )
];
}
public function resolveImageUrl( $filename ) {
global $wgContLang;
$title = \Title::newFromText( \Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer::getInstance()
->sanitizeImageFileName($filename, $wgContLang), NS_FILE );
$title = \Title::newFromText(
ImageFilenameSanitizer::getInstance()->sanitizeImageFileName( $filename, $wgContLang ),
NS_FILE
);
if ( $title && $title->exists() ) {
return \WikiaFileHelper::getFileFromTitle( $title )->getUrlGenerator()->url();
} else {

View file

@ -10,9 +10,7 @@ class NodeSet extends Node {
if ( $this->externalParser ) {
$nodeFactory->setExternalParser( $this->externalParser );
}
$data = [];
$data['value'] = $nodeFactory->getDataFromNodes( $this->xmlNode );
return $data;
return [ 'value' => $nodeFactory->getDataFromNodes( $this->xmlNode ) ];
}
public function isEmpty( $data ) {

View 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 );
}
}

View file

@ -1,11 +1,11 @@
<?php
namespace Wikia\PortableInfobox\Parser;
//interface moved here, because of $wgAutoloadClass issue
interface ExternalParser {
public function parse( $text );
public function parseRecursive( $text );
}
////interface moved here, because of $wgAutoloadClass issue
//interface ExternalParser {
// public function parse( $text );
// public function parseRecursive( $text );
//}
class XmlParser {

View file

@ -25,15 +25,15 @@ class XmlParserTest extends WikiaBaseTest {
';
$data = $parser->getDataFromXmlString( $markup );
// 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 }
$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 }
$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
$this->assertTrue( $data[0]['data']['value']['isEmpty'] == false );
$this->assertFalse( $data[ 0 ][ 'data' ][ 'value' ][ 0 ][ 'isEmpty' ] );
// infobox -> comparison
$this->assertTrue( $data[0]['isEmpty'] == false );
$this->assertFalse( $data[ 0 ][ 'isEmpty' ] );
}
public function testExternalParser() {
@ -57,7 +57,8 @@ class XmlParserTest extends WikiaBaseTest {
</infobox>
';
$data = $parser->getDataFromXmlString( $markup );
$this->assertTrue( $data[0]['data']['value'] == 'parseRecursive(ABB)' );
$this->assertTrue( $data[1]['data']['value'][0]['data']['value'][2]['data']['value'] == 'parseRecursive(LALALA)');
$this->assertEquals( 'parseRecursive(ABB)', $data[ 0 ][ 'data' ][ 'value' ] );
$this->assertEquals( 'parse(LALALA)',
$data[ 1 ][ 'data' ][ 'value' ][ 0 ][ 'data' ][ 'value' ][ 2 ][ 'data' ][ 'value' ] );
}
}