PortableInfobox/includes/services/Parser/MediaWikiParserService.php

117 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2021-06-02 17:55:12 +00:00
namespace PortableInfobox\Parser;
2022-03-11 20:35:51 +00:00
use BlockLevelPass;
2021-06-02 17:55:12 +00:00
use MediaWiki\MediaWikiServices;
2022-03-11 20:35:51 +00:00
use MediaWiki\Tidy\RemexDriver;
use PageImages\Hooks\ParserFileProcessingHookHandlers;
use Parser;
use PPFrame;
use Title;
2021-06-02 17:55:12 +00:00
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 = [];
2022-03-11 20:35:51 +00:00
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
2022-03-11 20:35:51 +00:00
if ( $wgPortableInfoboxUseTidy && class_exists( RemexDriver::class ) ) {
2021-09-12 17:43:20 +00:00
global $wgTidyConfig;
$wgTidyConfig = [
'driver' => 'RemexHtml',
'pwrap' => false
];
2021-09-10 02:52:19 +00:00
$this->tidyDriver = MediaWikiServices::getInstance()->getTidy();
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-10-02 07:41:19 +00:00
if ( isset( $this->cache[$wikitext] ) ) {
2018-08-27 12:23:23 +00:00
return $this->cache[$wikitext];
}
$parsed = $this->parser->recursiveTagParse( $wikitext ?? '', $this->frame );
if ( in_array( substr( $parsed, 0, 1 ), [ '*', '#' ] ) ) {
2021-09-10 02:52:19 +00:00
// fix for first item list elements
$parsed = "\n" . $parsed;
}
2021-12-03 19:28:38 +00:00
// @phan-suppress-next-line PhanAccessMethodInternal
2022-03-11 20:35:51 +00:00
$output = BlockLevelPass::doBlockLevels( $parsed, false );
$ready = $this->parser->getStripState()->unstripBoth( $output );
// @phan-suppress-next-line PhanDeprecatedFunction
$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
*
2022-03-11 20:35:51 +00:00
* @param Title $title
*/
public function addImage( $title ) {
$services = MediaWikiServices::getInstance();
$repoGroup = $services->getRepoGroup();
$file = $repoGroup->findFile( $title );
$tmstmp = $file ? $file->getTimestamp() : null;
$sha1 = $file ? $file->getSha1() : null;
$this->parser->getOutput()->addImage( $title->getDBkey(), $tmstmp, $sha1 );
// Pass PI images to PageImages extension if available (Popups and og:image)
2022-03-11 20:35:51 +00:00
if ( method_exists(
ParserFileProcessingHookHandlers::class, 'onParserModifyImageHTML'
) ) {
// @phan-suppress-next-line PhanParamTooMany
$handler = new ParserFileProcessingHookHandlers(
$repoGroup,
$services->getMainWANObjectCache(),
$services->getHttpRequestFactory()
);
2022-12-07 00:19:24 +00:00
$params = [];
$html = '';
2022-12-07 00:19:24 +00:00
$handler->onParserModifyImageHTML(
$this->parser, $file, $params, $html
);
}
}
}