2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
2021-06-02 17:55:12 +00:00
|
|
|
|
2018-08-09 09:49:10 +00:00
|
|
|
namespace PortableInfobox\Parser;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2022-03-11 20:35:51 +00:00
|
|
|
use BlockLevelPass;
|
2024-04-01 18:52:06 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
use MediaWiki\MainConfigNames;
|
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
|
|
|
|
2015-04-27 14:05:31 +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 = [];
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2022-03-11 20:35:51 +00:00
|
|
|
public function __construct( Parser $parser, PPFrame $frame ) {
|
2018-08-18 16:40:36 +00:00
|
|
|
global $wgPortableInfoboxUseTidy;
|
2018-08-16 13:59:49 +00:00
|
|
|
|
2015-04-27 14:05:31 +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 ) ) {
|
2024-04-01 18:52:06 +00:00
|
|
|
$this->tidyDriver = new RemexDriver(
|
|
|
|
new ServiceOptions(
|
|
|
|
// @phan-suppress-next-line PhanAccessClassConstantInternal
|
|
|
|
RemexDriver::CONSTRUCTOR_OPTIONS,
|
|
|
|
[
|
|
|
|
MainConfigNames::TidyConfig => [
|
|
|
|
'driver' => 'RemexHtml',
|
|
|
|
'pwrap' => false,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[ MainConfigNames::ParserEnableLegacyMediaDOM => false ]
|
|
|
|
)
|
|
|
|
);
|
2018-08-16 13:59:49 +00:00
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2018-08-16 09:25:53 +00:00
|
|
|
* @param string $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 ) {
|
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];
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:08:52 +00:00
|
|
|
$parsed = $this->parser->recursiveTagParse( $wikitext ?? '', $this->frame );
|
2016-11-14 10:24:44 +00:00
|
|
|
if ( in_array( substr( $parsed, 0, 1 ), [ '*', '#' ] ) ) {
|
2021-09-10 02:52:19 +00:00
|
|
|
// fix for first item list elements
|
2016-11-14 10:24:44 +00:00
|
|
|
$parsed = "\n" . $parsed;
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
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 );
|
2021-09-12 21:07:34 +00:00
|
|
|
$ready = $this->parser->getStripState()->unstripBoth( $output );
|
|
|
|
|
|
|
|
// @phan-suppress-next-line PhanDeprecatedFunction
|
2015-10-27 13:09:42 +00:00
|
|
|
$this->parser->replaceLinkHolders( $ready );
|
2021-09-12 21:07:34 +00:00
|
|
|
|
2018-08-16 13:59:49 +00:00
|
|
|
if ( isset( $this->tidyDriver ) ) {
|
|
|
|
$ready = $this->tidyDriver->tidy( $ready );
|
|
|
|
}
|
2021-09-12 21:07:34 +00:00
|
|
|
|
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-06-10 11:55:14 +00:00
|
|
|
|
2018-08-27 12:23:23 +00:00
|
|
|
$this->cache[$wikitext] = $marksstripped;
|
2021-09-12 21:07:34 +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
|
|
|
*
|
2022-03-11 20:35:51 +00:00
|
|
|
* @param Title $title
|
2023-07-24 17:13:33 +00:00
|
|
|
* @return ?string PageImages markers, if any.
|
2015-06-01 13:37:56 +00:00
|
|
|
*/
|
2023-07-24 17:13:33 +00:00
|
|
|
public function addImage( $title ): ?string {
|
2023-07-01 17:44:34 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
|
|
|
$repoGroup = $services->getRepoGroup();
|
2021-09-12 21:07:34 +00:00
|
|
|
|
|
|
|
$file = $repoGroup->findFile( $title );
|
2019-07-20 16:12:47 +00:00
|
|
|
$tmstmp = $file ? $file->getTimestamp() : null;
|
|
|
|
$sha1 = $file ? $file->getSha1() : null;
|
2019-09-01 11:02:06 +00:00
|
|
|
$this->parser->getOutput()->addImage( $title->getDBkey(), $tmstmp, $sha1 );
|
2018-12-26 22:36:08 +00:00
|
|
|
|
2023-07-24 17:13:33 +00:00
|
|
|
// Pass PI images to PageImages extension if available (Popups and og:image). Since 1.38, this produces an HTML
|
|
|
|
// comment that must be present in the rendered HTML for the image to qualify for selection.
|
2022-03-11 20:35:51 +00:00
|
|
|
if ( method_exists(
|
2022-03-11 20:50:45 +00:00
|
|
|
ParserFileProcessingHookHandlers::class, 'onParserModifyImageHTML'
|
|
|
|
) ) {
|
2024-11-21 18:01:11 +00:00
|
|
|
if ( version_compare( MW_VERSION, '1.43', '>=' ) ) {
|
|
|
|
// @phan-suppress-next-line PhanParamTooMany
|
|
|
|
$handler = new ParserFileProcessingHookHandlers(
|
|
|
|
$services->getMainConfig(),
|
|
|
|
$repoGroup,
|
|
|
|
$services->getMainWANObjectCache(),
|
|
|
|
$services->getHttpRequestFactory(),
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod
|
|
|
|
$services->getConnectionProvider(),
|
|
|
|
$services->getTitleFactory(),
|
|
|
|
$services->getLinksMigration()
|
|
|
|
);
|
|
|
|
} elseif ( version_compare( MW_VERSION, '1.42', '>=' ) ) {
|
2024-04-13 16:59:53 +00:00
|
|
|
// @phan-suppress-next-line PhanParamTooMany
|
|
|
|
$handler = new ParserFileProcessingHookHandlers(
|
|
|
|
$repoGroup,
|
|
|
|
$services->getMainWANObjectCache(),
|
|
|
|
$services->getHttpRequestFactory(),
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod
|
|
|
|
$services->getConnectionProvider(),
|
|
|
|
$services->getTitleFactory(),
|
|
|
|
$services->getLinksMigration()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// @phan-suppress-next-line PhanParamTooMany
|
|
|
|
$handler = new ParserFileProcessingHookHandlers(
|
|
|
|
$repoGroup,
|
|
|
|
$services->getMainWANObjectCache(),
|
|
|
|
$services->getHttpRequestFactory(),
|
|
|
|
$services->getDBLoadBalancerFactory(),
|
|
|
|
$services->getTitleFactory()
|
|
|
|
);
|
|
|
|
}
|
2022-12-07 00:19:24 +00:00
|
|
|
|
2022-03-11 20:50:45 +00:00
|
|
|
$params = [];
|
|
|
|
$html = '';
|
|
|
|
|
2022-12-07 00:19:24 +00:00
|
|
|
$handler->onParserModifyImageHTML(
|
2022-03-11 20:50:45 +00:00
|
|
|
$this->parser, $file, $params, $html
|
|
|
|
);
|
2023-07-24 17:13:33 +00:00
|
|
|
|
|
|
|
return $html;
|
2018-12-26 22:36:08 +00:00
|
|
|
}
|
2023-07-24 17:13:33 +00:00
|
|
|
|
|
|
|
return null;
|
2015-06-01 13:37:56 +00:00
|
|
|
}
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|