PortableInfobox/services/Parser/MediaWikiParserService.php

81 lines
2.3 KiB
PHP
Raw Normal View History

<?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
*/
public function parse( $wikitext ) {
2015-05-06 15:04:37 +00:00
wfProfileIn( __METHOD__ );
if ( substr( $wikitext, 0, 1 ) == "*" ) {
//fix for first item list elements
$wikitext = "\n" . $wikitext;
}
//save current options state, as it'll be overridden by new instance when parse is invoked
$options = $this->getParserOptions();
$tmpOptions = clone $options;
$tmpOptions->setIsPartialParse( true );
$output = $this->parser->parse( $wikitext, $this->getParserTitle(), $tmpOptions, false, false )->getText();
//restore options state
$this->parser->Options( $options );
2015-05-06 15:04:37 +00:00
wfProfileOut( __METHOD__ );
return $output;
}
/**
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
*/
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 );
$marksstripped = preg_replace( '|{{{.*}}}|Us', '', $newlinesstripped );
2015-05-06 15:04:37 +00:00
wfProfileOut( __METHOD__ );
return $marksstripped;
}
public function replaceVariables( $wikitext ) {
$output = $this->parser->replaceVariables( $wikitext, $this->frame );
return $output;
}
/**
* Add image to parser output for later usage
* @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 );
}
private function getParserTitle() {
return $this->parser->getTitle();
}
private function getParserOptions() {
$options = $this->parser->getOptions();
$options->enableLimitReport( false );
return $options;
}
}