2020-03-26 19:49:58 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2020-06-05 02:54:51 +00:00
|
|
|
* Magic variable implementations provided by MediaWiki core
|
2020-03-26 19:49:58 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Parser
|
|
|
|
|
*/
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2022-04-10 15:34:45 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
Add new ParserOutput::{get,set}OutputFlag() interface
This is a uniform mechanism to access a number of bespoke boolean
flags in ParserOutput. It allows extensibility in core (by adding new
field names to ParserOutputFlags) without exposing new getter/setter
methods to Parsoid. It replaces the ParserOutput::{get,set}Flag()
interface which (a) doesn't allow access to certain flags, and (b) is
typically called with a string rather than a constant, and (c) has a
very generic name. (Note that Parser::setOutputFlag() already called
these "output flags".)
In the future we might unify the representation so that we store
everything in $mFlags and don't have explicit properties in
ParserOutput, but those representation details should be invisible to
the clients of this API. (We might also use a proper enumeration
for ParserOutputFlags, when PHP supports this.)
There is some overlap with ParserOutput::{get,set}ExtensionData(), but
I've left those methods as-is because (a) they allow for non-boolean
data, unlike the *Flag() methods, and (b) it seems worthwhile to
distingush properties set by extensions from properties used by core.
Code search:
https://codesearch.wmcloud.org/search/?q=%5BOo%5Dut%28put%29%3F%28%5C%28%5C%29%29%3F-%3E%28g%7Cs%29etFlag%5C%28&i=nope&files=&excludeFiles=&repos=
Bug: T292868
Change-Id: I39bc58d207836df6f328c54be9e3330719cebbeb
2021-10-08 20:04:37 +00:00
|
|
|
use MediaWiki\Parser\ParserOutputFlags;
|
2020-03-26 19:49:58 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-03-11 16:21:33 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2020-03-26 19:49:58 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-06-05 02:54:51 +00:00
|
|
|
* Expansions of core magic variables, used by the parser.
|
2020-03-26 19:49:58 +00:00
|
|
|
* @internal
|
|
|
|
|
* @ingroup Parser
|
|
|
|
|
*/
|
2020-06-05 02:54:51 +00:00
|
|
|
class CoreMagicVariables {
|
2020-03-26 19:49:58 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-06-05 02:54:51 +00:00
|
|
|
* Expand the magic variable given by $index.
|
2020-03-26 19:49:58 +00:00
|
|
|
* @internal
|
|
|
|
|
* @param Parser $parser
|
2020-06-05 02:54:51 +00:00
|
|
|
* @param string $id The name of the variable, and equivalently, the magic
|
|
|
|
|
* word ID which was used to match the variable
|
2022-03-11 16:21:33 +00:00
|
|
|
* @param ConvertibleTimestamp $ts Timestamp to use when expanding magic variable
|
2020-03-26 19:49:58 +00:00
|
|
|
* @param NamespaceInfo $nsInfo The NamespaceInfo to use when expanding
|
|
|
|
|
* @param ServiceOptions $svcOptions Service options for the parser
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @return string|null The expanded value, or null to indicate the given
|
2020-06-05 02:54:51 +00:00
|
|
|
* index wasn't a known magic variable.
|
2020-03-26 19:49:58 +00:00
|
|
|
*/
|
|
|
|
|
public static function expand(
|
|
|
|
|
// Fundamental options
|
|
|
|
|
Parser $parser,
|
2020-06-05 02:54:51 +00:00
|
|
|
string $id,
|
2020-03-26 19:49:58 +00:00
|
|
|
// Context passed over from the parser
|
2022-03-11 16:21:33 +00:00
|
|
|
ConvertibleTimestamp $ts,
|
2020-03-26 19:49:58 +00:00
|
|
|
NamespaceInfo $nsInfo,
|
|
|
|
|
ServiceOptions $svcOptions,
|
|
|
|
|
LoggerInterface $logger
|
|
|
|
|
): ?string {
|
|
|
|
|
$pageLang = $parser->getFunctionLang();
|
|
|
|
|
$title = $parser->getTitle();
|
|
|
|
|
|
2020-06-05 02:54:51 +00:00
|
|
|
switch ( $id ) {
|
2020-03-26 19:49:58 +00:00
|
|
|
case '!':
|
|
|
|
|
return '|';
|
2022-05-13 21:10:28 +00:00
|
|
|
case '=':
|
|
|
|
|
return '=';
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentmonth':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( $ts->format( 'm' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentmonth1':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( $ts->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentmonthname':
|
2022-03-01 20:11:40 +00:00
|
|
|
return $pageLang->getMonthName( (int)$ts->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentmonthnamegen':
|
2022-03-01 20:11:40 +00:00
|
|
|
return $pageLang->getMonthNameGen( (int)$ts->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentmonthabbrev':
|
2022-03-01 20:11:40 +00:00
|
|
|
return $pageLang->getMonthAbbreviation( (int)$ts->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentday':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( $ts->format( 'j' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentday2':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( $ts->format( 'd' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localmonth':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'm' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localmonth1':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localmonthname':
|
2022-03-01 20:11:40 +00:00
|
|
|
return $pageLang->getMonthName( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localmonthnamegen':
|
2022-03-01 20:11:40 +00:00
|
|
|
return $pageLang->getMonthNameGen( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localmonthabbrev':
|
2022-03-01 20:11:40 +00:00
|
|
|
return $pageLang->getMonthAbbreviation( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localday':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'j' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localday2':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'd' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'pagename':
|
|
|
|
|
case 'pagenamee':
|
|
|
|
|
case 'fullpagename':
|
|
|
|
|
case 'fullpagenamee':
|
|
|
|
|
case 'subpagename':
|
|
|
|
|
case 'subpagenamee':
|
|
|
|
|
case 'rootpagename':
|
|
|
|
|
case 'rootpagenamee':
|
|
|
|
|
case 'basepagename':
|
|
|
|
|
case 'basepagenamee':
|
|
|
|
|
case 'talkpagename':
|
|
|
|
|
case 'talkpagenamee':
|
|
|
|
|
case 'subjectpagename':
|
|
|
|
|
case 'subjectpagenamee':
|
Unify the "magic variable" and "parser function" form of several built-ins
The following magic variables also have "parser function" forms, where
the first argument is a user-supplied title:
pagename, pagenamee, fullpagename, fullpagenamee, subpagename,
subpagenamee, rootpagename, rootpagenamee, basepagename,
basepagenamee, talkpagename, talkpagenamee, subjectpagename,
subjectpagenamee, pageid, cascadingsources, namespace, namespacee,
namespacenumber, talkspace, talkspacee, subjectspace, subjectspacee
Refactor the code so that the magic variable form invokes the parser
function form with no arguments to reduce code duplication. We also
tweak the behavior of parser function when invoked with no arguments,
although this change will not be directly visible because the parser
always prefers magic variables over parser functions, so the parser
function is never actually invoked with no arguments.
A future patch may allow the parser function to be invoked with a
hash prefix (I895087c546dc820c77c0dda596dfeb72586b87cc) in which case
consistency will be more important.
Note that `revisionuser`, `revisionid`, `revisionday`, `revisionday2`,
`revisionmonth`, `revisionmonth1`, `revisionyear` and
`revisiontimestamp` are also of a similar form and could be included
in this list, but their magic variable and parser function
implementations do not appear to be consistent. This will be
addressed in future patches.
In addition, the following magic variables have a "parser function" form
where the presence or absence of the first argument selects "raw" output:
numberofarticles, numberoffiles, numberofusers, numberofactiveusers,
numberofpages, numberofadmins, numberofedits
Similar to above, refactor the code so that the magic variable form
invokes the parser function form with no arguments to reduce code
duplication (and to support future direct invocation of the parser
function with no arguments).
Bug: T204370
Change-Id: Iaec33fb40a2d9884daf2852ed6a6a3b53c9d3863
2022-08-04 18:37:35 +00:00
|
|
|
case 'pageid':
|
2022-08-01 22:55:10 +00:00
|
|
|
case 'revisionuser':
|
2022-08-01 23:27:51 +00:00
|
|
|
case 'revisionday':
|
|
|
|
|
case 'revisionday2':
|
|
|
|
|
case 'revisionmonth':
|
|
|
|
|
case 'revisionmonth1':
|
|
|
|
|
case 'revisionyear':
|
|
|
|
|
case 'revisiontimestamp':
|
Unify the "magic variable" and "parser function" form of several built-ins
The following magic variables also have "parser function" forms, where
the first argument is a user-supplied title:
pagename, pagenamee, fullpagename, fullpagenamee, subpagename,
subpagenamee, rootpagename, rootpagenamee, basepagename,
basepagenamee, talkpagename, talkpagenamee, subjectpagename,
subjectpagenamee, pageid, cascadingsources, namespace, namespacee,
namespacenumber, talkspace, talkspacee, subjectspace, subjectspacee
Refactor the code so that the magic variable form invokes the parser
function form with no arguments to reduce code duplication. We also
tweak the behavior of parser function when invoked with no arguments,
although this change will not be directly visible because the parser
always prefers magic variables over parser functions, so the parser
function is never actually invoked with no arguments.
A future patch may allow the parser function to be invoked with a
hash prefix (I895087c546dc820c77c0dda596dfeb72586b87cc) in which case
consistency will be more important.
Note that `revisionuser`, `revisionid`, `revisionday`, `revisionday2`,
`revisionmonth`, `revisionmonth1`, `revisionyear` and
`revisiontimestamp` are also of a similar form and could be included
in this list, but their magic variable and parser function
implementations do not appear to be consistent. This will be
addressed in future patches.
In addition, the following magic variables have a "parser function" form
where the presence or absence of the first argument selects "raw" output:
numberofarticles, numberoffiles, numberofusers, numberofactiveusers,
numberofpages, numberofadmins, numberofedits
Similar to above, refactor the code so that the magic variable form
invokes the parser function form with no arguments to reduce code
duplication (and to support future direct invocation of the parser
function with no arguments).
Bug: T204370
Change-Id: Iaec33fb40a2d9884daf2852ed6a6a3b53c9d3863
2022-08-04 18:37:35 +00:00
|
|
|
case 'namespace':
|
|
|
|
|
case 'namespacee':
|
|
|
|
|
case 'namespacenumber':
|
|
|
|
|
case 'talkspace':
|
|
|
|
|
case 'talkspacee':
|
|
|
|
|
case 'subjectspace':
|
|
|
|
|
case 'subjectspacee':
|
|
|
|
|
case 'cascadingsources':
|
|
|
|
|
# First argument of the corresponding parser function
|
|
|
|
|
# (second argument of the PHP implementation) is
|
|
|
|
|
# "title".
|
|
|
|
|
|
|
|
|
|
# Note that for many of these {{FOO}} is subtly different
|
|
|
|
|
# from {{FOO:{{PAGENAME}}}}, so we can't pass $title here
|
|
|
|
|
# we have to explicitly use the "no arguments" form of the
|
|
|
|
|
# parser function by passing `null` to indicate a missing
|
|
|
|
|
# argument (which then defaults to the current page title).
|
|
|
|
|
return CoreParserFunctions::$id( $parser, null );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'revisionid':
|
|
|
|
|
$namespace = $title->getNamespace();
|
|
|
|
|
if (
|
2022-04-26 15:48:03 +00:00
|
|
|
$svcOptions->get( MainConfigNames::MiserMode ) &&
|
2020-03-26 19:49:58 +00:00
|
|
|
!$parser->getOptions()->getInterfaceMessage() &&
|
2020-06-05 02:54:51 +00:00
|
|
|
// @TODO: disallow this variable on all namespaces
|
2020-03-26 19:49:58 +00:00
|
|
|
$nsInfo->isSubject( $namespace )
|
|
|
|
|
) {
|
|
|
|
|
// Use a stub result instead of the actual revision ID in order to avoid
|
|
|
|
|
// double parses on page save but still allow preview detection (T137900)
|
|
|
|
|
if ( $parser->getRevisionId() || $parser->getOptions()->getSpeculativeRevId() ) {
|
|
|
|
|
return '-';
|
|
|
|
|
} else {
|
Add new ParserOutput::{get,set}OutputFlag() interface
This is a uniform mechanism to access a number of bespoke boolean
flags in ParserOutput. It allows extensibility in core (by adding new
field names to ParserOutputFlags) without exposing new getter/setter
methods to Parsoid. It replaces the ParserOutput::{get,set}Flag()
interface which (a) doesn't allow access to certain flags, and (b) is
typically called with a string rather than a constant, and (c) has a
very generic name. (Note that Parser::setOutputFlag() already called
these "output flags".)
In the future we might unify the representation so that we store
everything in $mFlags and don't have explicit properties in
ParserOutput, but those representation details should be invisible to
the clients of this API. (We might also use a proper enumeration
for ParserOutputFlags, when PHP supports this.)
There is some overlap with ParserOutput::{get,set}ExtensionData(), but
I've left those methods as-is because (a) they allow for non-boolean
data, unlike the *Flag() methods, and (b) it seems worthwhile to
distingush properties set by extensions from properties used by core.
Code search:
https://codesearch.wmcloud.org/search/?q=%5BOo%5Dut%28put%29%3F%28%5C%28%5C%29%29%3F-%3E%28g%7Cs%29etFlag%5C%28&i=nope&files=&excludeFiles=&repos=
Bug: T292868
Change-Id: I39bc58d207836df6f328c54be9e3330719cebbeb
2021-10-08 20:04:37 +00:00
|
|
|
self::setOutputFlag(
|
|
|
|
|
$parser,
|
|
|
|
|
$logger,
|
|
|
|
|
ParserOutputFlags::VARY_REVISION_EXISTS,
|
|
|
|
|
'{{REVISIONID}} used'
|
|
|
|
|
);
|
2020-03-26 19:49:58 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Inform the edit saving system that getting the canonical output after
|
|
|
|
|
// revision insertion requires a parse that used that exact revision ID
|
Add new ParserOutput::{get,set}OutputFlag() interface
This is a uniform mechanism to access a number of bespoke boolean
flags in ParserOutput. It allows extensibility in core (by adding new
field names to ParserOutputFlags) without exposing new getter/setter
methods to Parsoid. It replaces the ParserOutput::{get,set}Flag()
interface which (a) doesn't allow access to certain flags, and (b) is
typically called with a string rather than a constant, and (c) has a
very generic name. (Note that Parser::setOutputFlag() already called
these "output flags".)
In the future we might unify the representation so that we store
everything in $mFlags and don't have explicit properties in
ParserOutput, but those representation details should be invisible to
the clients of this API. (We might also use a proper enumeration
for ParserOutputFlags, when PHP supports this.)
There is some overlap with ParserOutput::{get,set}ExtensionData(), but
I've left those methods as-is because (a) they allow for non-boolean
data, unlike the *Flag() methods, and (b) it seems worthwhile to
distingush properties set by extensions from properties used by core.
Code search:
https://codesearch.wmcloud.org/search/?q=%5BOo%5Dut%28put%29%3F%28%5C%28%5C%29%29%3F-%3E%28g%7Cs%29etFlag%5C%28&i=nope&files=&excludeFiles=&repos=
Bug: T292868
Change-Id: I39bc58d207836df6f328c54be9e3330719cebbeb
2021-10-08 20:04:37 +00:00
|
|
|
self::setOutputFlag( $parser, $logger, ParserOutputFlags::VARY_REVISION_ID, '{{REVISIONID}} used' );
|
2020-03-26 19:49:58 +00:00
|
|
|
$value = $parser->getRevisionId();
|
|
|
|
|
if ( $value === 0 ) {
|
2020-04-17 20:29:22 +00:00
|
|
|
$rev = $parser->getRevisionRecordObject();
|
2020-03-26 19:49:58 +00:00
|
|
|
$value = $rev ? $rev->getId() : $value;
|
|
|
|
|
}
|
|
|
|
|
if ( !$value ) {
|
|
|
|
|
$value = $parser->getOptions()->getSpeculativeRevId();
|
|
|
|
|
if ( $value ) {
|
|
|
|
|
$parser->getOutput()->setSpeculativeRevIdUsed( $value );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (string)$value;
|
|
|
|
|
}
|
|
|
|
|
case 'revisionsize':
|
|
|
|
|
return (string)$parser->getRevisionSize();
|
|
|
|
|
case 'currentdayname':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->getWeekdayName( (int)$ts->format( 'w' ) + 1 );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentyear':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( $ts->format( 'Y' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currenttime':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->time( $ts->getTimestamp( TS_MW ), false, false );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currenthour':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( $ts->format( 'H' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentweek':
|
|
|
|
|
// @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
|
|
|
|
|
// int to remove the padding
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNum( (int)$ts->format( 'W' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentdow':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNum( $ts->format( 'w' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localdayname':
|
|
|
|
|
return $pageLang->getWeekdayName(
|
2022-03-11 16:21:33 +00:00
|
|
|
(int)self::makeTsLocal( $svcOptions, $ts )->format( 'w' ) + 1
|
2020-03-26 19:49:58 +00:00
|
|
|
);
|
|
|
|
|
case 'localyear':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'Y' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localtime':
|
|
|
|
|
return $pageLang->time(
|
2022-03-11 16:21:33 +00:00
|
|
|
self::makeTsLocal( $svcOptions, $ts )->format( 'YmdHis' ),
|
2020-03-26 19:49:58 +00:00
|
|
|
false,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
case 'localhour':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'H' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localweek':
|
|
|
|
|
// @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
|
|
|
|
|
// int to remove the padding
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNum( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'W' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localdow':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $pageLang->formatNum( self::makeTsLocal( $svcOptions, $ts )->format( 'w' ) );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'numberofarticles':
|
|
|
|
|
case 'numberoffiles':
|
|
|
|
|
case 'numberofusers':
|
|
|
|
|
case 'numberofactiveusers':
|
|
|
|
|
case 'numberofpages':
|
|
|
|
|
case 'numberofadmins':
|
|
|
|
|
case 'numberofedits':
|
Unify the "magic variable" and "parser function" form of several built-ins
The following magic variables also have "parser function" forms, where
the first argument is a user-supplied title:
pagename, pagenamee, fullpagename, fullpagenamee, subpagename,
subpagenamee, rootpagename, rootpagenamee, basepagename,
basepagenamee, talkpagename, talkpagenamee, subjectpagename,
subjectpagenamee, pageid, cascadingsources, namespace, namespacee,
namespacenumber, talkspace, talkspacee, subjectspace, subjectspacee
Refactor the code so that the magic variable form invokes the parser
function form with no arguments to reduce code duplication. We also
tweak the behavior of parser function when invoked with no arguments,
although this change will not be directly visible because the parser
always prefers magic variables over parser functions, so the parser
function is never actually invoked with no arguments.
A future patch may allow the parser function to be invoked with a
hash prefix (I895087c546dc820c77c0dda596dfeb72586b87cc) in which case
consistency will be more important.
Note that `revisionuser`, `revisionid`, `revisionday`, `revisionday2`,
`revisionmonth`, `revisionmonth1`, `revisionyear` and
`revisiontimestamp` are also of a similar form and could be included
in this list, but their magic variable and parser function
implementations do not appear to be consistent. This will be
addressed in future patches.
In addition, the following magic variables have a "parser function" form
where the presence or absence of the first argument selects "raw" output:
numberofarticles, numberoffiles, numberofusers, numberofactiveusers,
numberofpages, numberofadmins, numberofedits
Similar to above, refactor the code so that the magic variable form
invokes the parser function form with no arguments to reduce code
duplication (and to support future direct invocation of the parser
function with no arguments).
Bug: T204370
Change-Id: Iaec33fb40a2d9884daf2852ed6a6a3b53c9d3863
2022-08-04 18:37:35 +00:00
|
|
|
# second argument is 'raw'; magic variables are "not raw"
|
|
|
|
|
return CoreParserFunctions::$id( $parser, null );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currenttimestamp':
|
2022-03-11 16:21:33 +00:00
|
|
|
return $ts->getTimestamp( TS_MW );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'localtimestamp':
|
2022-03-11 16:21:33 +00:00
|
|
|
return self::makeTsLocal( $svcOptions, $ts )->format( 'YmdHis' );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'currentversion':
|
|
|
|
|
return SpecialVersion::getVersion();
|
|
|
|
|
case 'articlepath':
|
2022-04-10 15:34:45 +00:00
|
|
|
return (string)$svcOptions->get( MainConfigNames::ArticlePath );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'sitename':
|
2022-04-10 15:34:45 +00:00
|
|
|
return (string)$svcOptions->get( MainConfigNames::Sitename );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'server':
|
2022-04-10 15:34:45 +00:00
|
|
|
return (string)$svcOptions->get( MainConfigNames::Server );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'servername':
|
2022-04-10 15:34:45 +00:00
|
|
|
return (string)$svcOptions->get( MainConfigNames::ServerName );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'scriptpath':
|
2022-04-10 15:34:45 +00:00
|
|
|
return (string)$svcOptions->get( MainConfigNames::ScriptPath );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'stylepath':
|
2022-04-10 15:34:45 +00:00
|
|
|
return (string)$svcOptions->get( MainConfigNames::StylePath );
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'directionmark':
|
|
|
|
|
return $pageLang->getDirMark();
|
|
|
|
|
case 'contentlanguage':
|
2021-10-26 22:29:20 +00:00
|
|
|
return $parser->getContentLanguage()->getCode();
|
2020-03-26 19:49:58 +00:00
|
|
|
case 'pagelanguage':
|
|
|
|
|
return $pageLang->getCode();
|
|
|
|
|
default:
|
2020-06-05 02:54:51 +00:00
|
|
|
// This is not one of the core magic variables
|
2020-03-26 19:49:58 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 16:21:33 +00:00
|
|
|
/**
|
|
|
|
|
* Helper to convert a timestamp instance to local time
|
|
|
|
|
* @see MWTimestamp::getLocalInstance()
|
|
|
|
|
* @param ServiceOptions $svcOptions Service options for the parser
|
|
|
|
|
* @param ConvertibleTimestamp $ts Timestamp to convert
|
|
|
|
|
* @return ConvertibleTimestamp
|
|
|
|
|
*/
|
|
|
|
|
private static function makeTsLocal( $svcOptions, $ts ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$localtimezone = $svcOptions->get( MainConfigNames::Localtimezone );
|
2022-03-11 16:21:33 +00:00
|
|
|
$ts->setTimezone( $localtimezone );
|
|
|
|
|
return $ts;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 19:49:58 +00:00
|
|
|
/**
|
|
|
|
|
* Helper method borrowed from Parser.php: sets the flag on the output
|
|
|
|
|
* but also does some debug logging.
|
|
|
|
|
* @param Parser $parser
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param string $flag
|
|
|
|
|
* @param string $reason
|
|
|
|
|
*/
|
|
|
|
|
private static function setOutputFlag(
|
|
|
|
|
Parser $parser,
|
|
|
|
|
LoggerInterface $logger,
|
|
|
|
|
string $flag,
|
|
|
|
|
string $reason
|
|
|
|
|
): void {
|
Add new ParserOutput::{get,set}OutputFlag() interface
This is a uniform mechanism to access a number of bespoke boolean
flags in ParserOutput. It allows extensibility in core (by adding new
field names to ParserOutputFlags) without exposing new getter/setter
methods to Parsoid. It replaces the ParserOutput::{get,set}Flag()
interface which (a) doesn't allow access to certain flags, and (b) is
typically called with a string rather than a constant, and (c) has a
very generic name. (Note that Parser::setOutputFlag() already called
these "output flags".)
In the future we might unify the representation so that we store
everything in $mFlags and don't have explicit properties in
ParserOutput, but those representation details should be invisible to
the clients of this API. (We might also use a proper enumeration
for ParserOutputFlags, when PHP supports this.)
There is some overlap with ParserOutput::{get,set}ExtensionData(), but
I've left those methods as-is because (a) they allow for non-boolean
data, unlike the *Flag() methods, and (b) it seems worthwhile to
distingush properties set by extensions from properties used by core.
Code search:
https://codesearch.wmcloud.org/search/?q=%5BOo%5Dut%28put%29%3F%28%5C%28%5C%29%29%3F-%3E%28g%7Cs%29etFlag%5C%28&i=nope&files=&excludeFiles=&repos=
Bug: T292868
Change-Id: I39bc58d207836df6f328c54be9e3330719cebbeb
2021-10-08 20:04:37 +00:00
|
|
|
$parser->getOutput()->setOutputFlag( $flag );
|
2020-03-26 19:49:58 +00:00
|
|
|
$name = $parser->getTitle()->getPrefixedText();
|
|
|
|
|
// This code was moved from Parser::setOutputFlag and used __METHOD__
|
|
|
|
|
// originally; we've hard-coded that output here so that our refactor
|
|
|
|
|
// doesn't change the messages in the logs.
|
|
|
|
|
$logger->debug( "Parser::setOutputFlag: set $flag flag on '$name'; $reason" );
|
|
|
|
|
}
|
|
|
|
|
}
|