2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser;
|
|
|
|
|
|
|
|
class MediaWikiParserService implements ExternalParser {
|
|
|
|
|
|
|
|
protected $parser;
|
|
|
|
protected $frame;
|
|
|
|
protected $localParser;
|
|
|
|
|
|
|
|
public function __construct( \Parser $parser, \PPFrame $frame ) {
|
|
|
|
$this->parser = $parser;
|
|
|
|
$this->frame = $frame;
|
|
|
|
}
|
|
|
|
|
2015-05-07 11:54:26 +00:00
|
|
|
/**
|
|
|
|
* Method used for parsing wikitext provided through variable
|
|
|
|
* @param $wikitext
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-04-27 14:05:31 +00:00
|
|
|
public function parse( $wikitext ) {
|
2015-05-06 15:04:37 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2015-04-27 14:05:31 +00:00
|
|
|
if ( substr( $wikitext, 0, 1 ) == "*" ) {
|
|
|
|
//fix for first item list elements
|
|
|
|
$wikitext = "\n" . $wikitext;
|
|
|
|
}
|
|
|
|
$parsedText = $this->getParserInstance()
|
|
|
|
->parse( $wikitext, $this->getParserTitle(), $this->getParserOptions(), false )
|
|
|
|
->getText();
|
2015-05-06 15:04:37 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2015-04-27 14:05:31 +00:00
|
|
|
return $parsedText;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-07 11:54:26 +00:00
|
|
|
* Method used for parsing wikitext provided in infobox that might contain variables
|
|
|
|
* @param $wikitext
|
|
|
|
* @return string HTML outcome
|
2015-04-27 14:05:31 +00:00
|
|
|
*/
|
|
|
|
public function parseRecursive( $wikitext ) {
|
2015-05-06 15:04:37 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2015-05-07 11:48:57 +00:00
|
|
|
$withVars = $this->parser->replaceVariables( $wikitext, $this->frame );
|
|
|
|
$parsed = $this->parse( $withVars );
|
|
|
|
$ready = $this->parser->mStripState->unstripBoth( $parsed );
|
|
|
|
$newlinesstripped = preg_replace( '|[\n\r]|Us', '', $ready );
|
2015-04-27 14:05:31 +00:00
|
|
|
$marksstripped = preg_replace( '|{{{.*}}}|Us', '', $newlinesstripped );
|
2015-05-06 15:04:37 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2015-04-27 14:05:31 +00:00
|
|
|
return $marksstripped;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getParserTitle() {
|
|
|
|
return $this->parser->getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getParserOptions() {
|
|
|
|
$options = $this->parser->getOptions();
|
|
|
|
$options->enableLimitReport( false );
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getParserInstance() {
|
|
|
|
if ( !isset( $this->localParser ) ) {
|
|
|
|
$this->localParser = new \Parser();
|
|
|
|
}
|
|
|
|
return $this->localParser;
|
|
|
|
}
|
|
|
|
}
|