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
|
|
|
/**
|
2015-10-27 13:09:42 +00:00
|
|
|
* Method used for parsing wikitext provided in infobox that might contain variables
|
2015-06-10 11:55:14 +00:00
|
|
|
*
|
2015-05-07 11:54:26 +00:00
|
|
|
* @param $wikitext
|
2015-06-10 11:55:14 +00:00
|
|
|
*
|
2015-10-27 13:09:42 +00:00
|
|
|
* @return string HTML outcome
|
2015-05-07 11:54:26 +00:00
|
|
|
*/
|
2015-10-27 13:09:42 +00:00
|
|
|
public function parseRecursive( $wikitext ) {
|
2015-05-06 15:04:37 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2015-06-15 10:43:13 +00:00
|
|
|
if ( in_array( substr( $wikitext, 0, 1 ), [ '*', '#' ] ) ) {
|
2015-04-27 14:05:31 +00:00
|
|
|
//fix for first item list elements
|
|
|
|
$wikitext = "\n" . $wikitext;
|
|
|
|
}
|
2015-06-15 10:43:13 +00:00
|
|
|
$parsed = $this->parser->internalParse( $wikitext, false, $this->frame );
|
|
|
|
$output = $this->parser->doBlockLevels( $parsed, false );
|
2015-10-27 13:09:42 +00:00
|
|
|
$ready = $this->parser->mStripState->unstripBoth( $output );
|
|
|
|
$this->parser->replaceLinkHolders( $ready );
|
2015-05-07 11:48:57 +00:00
|
|
|
$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-06-10 11:55:14 +00:00
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
return $marksstripped;
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:57:25 +00:00
|
|
|
public function replaceVariables( $wikitext ) {
|
2015-06-01 13:37:56 +00:00
|
|
|
$output = $this->parser->replaceVariables( $wikitext, $this->frame );
|
2015-06-10 11:55:14 +00:00
|
|
|
|
2015-05-08 15:57:25 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-06-01 13:37:56 +00:00
|
|
|
/**
|
|
|
|
* Add image to parser output for later usage
|
2015-06-10 11:55:14 +00:00
|
|
|
*
|
2015-06-01 13:37:56 +00:00
|
|
|
* @param string $title
|
|
|
|
*/
|
|
|
|
public function addImage( $title ) {
|
|
|
|
$file = wfFindFile( $title );
|
2015-06-01 14:11:36 +00:00
|
|
|
$tmstmp = $file ? $file->getTimestamp() : false;
|
|
|
|
$sha1 = $file ? $file->getSha1() : false;
|
|
|
|
$this->parser->getOutput()->addImage( $title, $tmstmp, $sha1 );
|
2015-06-01 13:37:56 +00:00
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|