diff options
author | Martin Fischer <martin@push-f.com> | 2022-10-08 20:12:14 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2022-10-08 20:13:21 +0200 |
commit | 166d0fbe2f0355d81669b5fde9c2a9bc576da88a (patch) | |
tree | 5e1da8ac1d3b2d8c97be1a074fc1753c56e24fcc | |
parent | d17c17ce925a00bb91d6c596fb457e1104a7b57c (diff) |
add workaround to expand signatures within <vote>
-rw-r--r-- | Vote.php | 21 | ||||
-rw-r--r-- | extension.json | 3 |
2 files changed, 23 insertions, 1 deletions
@@ -8,6 +8,16 @@ class VoteHooks { $parser->setHook( 'vote', [ self::class, 'renderVoteTag' ] ); } + public static function onParserPreSaveTransformComplete( Parser $parser, string &$text ) { + // workaround for https://phabricator.wikimedia.org/T319221 + $sigText = $parser->getUserSig(RequestContext::getMain()->getUser()); + $datetime = getFormattedTimestamp($parser); + $text = preg_replace('/(?<!<nowiki>)(?<!<pre>)~~~~/', "$sigText $datetime", $text); + // NOTE: this regex obviously matches false positives, however since + // it's quite uncommon for ~~~~ to occur in wiki pages this is deemed good enough + // until T319221 is addressed and this workaround is no longer necessary + } + // Render <vote> public static function renderVoteTag( $input, array $args, Parser $parser, PPFrame $frame ) { @@ -322,3 +332,14 @@ function formatError($error) { // The CSS class is provided by MediaWiki. return "<span class=error>$error</span>"; } + +function getFormattedTimestamp($parser) { + // This function exists solely because of the onParserPreSaveTransformComplete workaround for https://phabricator.wikimedia.org/T319221. + // It is supposed to mirror the behavior of https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/refs/tags/1.38.4/includes/parser/Parser.php#4575. + + $ts = $parser->mOptions->getTimestamp(); + $timestamp = MWTimestamp::getLocalInstance( $ts ); + $ts = $timestamp->format( 'YmdHis' ); + $tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text(); + return $parser->getContentLanguage()->timeanddate( $ts, false, false ) . " ($tzMsg)"; +} diff --git a/extension.json b/extension.json index 4cd2418..bdea38d 100644 --- a/extension.json +++ b/extension.json @@ -19,7 +19,8 @@ "InsertVote": "SpecialInsertVote" }, "Hooks": { - "ParserFirstCallInit": "VoteHooks::onParserFirstCallInit" + "ParserFirstCallInit": "VoteHooks::onParserFirstCallInit", + "ParserPreSaveTransformComplete": "VoteHooks::onParserPreSaveTransformComplete" }, "ResourceModules": { "ext.vote-css": { |