PortableInfobox/includes/services/Parser/MediaWikiParserService.php

74 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace PortableInfobox\Parser;
class MediaWikiParserService implements ExternalParser {
protected $parser;
protected $frame;
protected $localParser;
2018-08-16 13:59:49 +00:00
protected $tidyDriver;
2018-08-27 12:23:23 +00:00
protected $cache = [];
public function __construct( \Parser $parser, \PPFrame $frame ) {
global $wgPortableInfoboxUseTidy;
2018-08-16 13:59:49 +00:00
$this->parser = $parser;
$this->frame = $frame;
2018-08-16 13:59:49 +00:00
2018-08-26 10:13:26 +00:00
if ( $wgPortableInfoboxUseTidy && class_exists( '\MediaWiki\Tidy\RemexDriver' ) ) {
$this->tidyDriver = \MWTidy::factory( [
'driver' => 'RemexHtml',
'pwrap' => false
] );
2018-08-16 13:59:49 +00:00
}
}
2015-05-07 11:54:26 +00:00
/**
* Method used for parsing wikitext provided in infobox that might contain variables
*
2018-08-16 09:25:53 +00:00
* @param string $wikitext
*
* @return string HTML outcome
2015-05-07 11:54:26 +00:00
*/
public function parseRecursive( $wikitext ) {
2018-08-27 12:23:23 +00:00
if( isset( $this->cache[$wikitext] ) ) {
return $this->cache[$wikitext];
}
$parsed = $this->parser->internalParse( $wikitext, false, $this->frame );
if ( in_array( substr( $parsed, 0, 1 ), [ '*', '#' ] ) ) {
//fix for first item list elements
$parsed = "\n" . $parsed;
}
$output = $this->parser->doBlockLevels( $parsed, false );
$ready = $this->parser->mStripState->unstripBoth( $output );
$this->parser->replaceLinkHolders( $ready );
2018-08-16 13:59:49 +00:00
if ( isset( $this->tidyDriver ) ) {
$ready = $this->tidyDriver->tidy( $ready );
}
2015-05-07 11:48:57 +00:00
$newlinesstripped = preg_replace( '|[\n\r]|Us', '', $ready );
$marksstripped = preg_replace( '|{{{.*}}}|Us', '', $newlinesstripped );
2018-08-27 12:23:23 +00:00
$this->cache[$wikitext] = $marksstripped;
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 );
}
}