1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
<?php
/**
* Magic variable implementations provided by MediaWiki core
*
* 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;
use Psr\Log\LoggerInterface;
/**
* Expansions of core magic variables, used by the parser.
* @internal
* @ingroup Parser
*/
class CoreMagicVariables {
/** @var int Assume that no output will later be saved this many seconds after parsing */
private const MAX_TTS = 900;
/**
* Expand the magic variable given by $index.
* @internal
* @param Parser $parser
* @param string $id The name of the variable, and equivalently, the magic
* word ID which was used to match the variable
* @param int $ts Timestamp to use when expanding magic variable
* @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
* index wasn't a known magic variable.
*/
public static function expand(
// Fundamental options
Parser $parser,
string $id,
// Context passed over from the parser
int $ts,
NamespaceInfo $nsInfo,
ServiceOptions $svcOptions,
LoggerInterface $logger
): ?string {
$pageLang = $parser->getFunctionLang();
$title = $parser->getTitle();
switch ( $id ) {
case '!':
return '|';
case 'currentmonth':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'm' ), true );
case 'currentmonth1':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'n' ), true );
case 'currentmonthname':
return $pageLang->getMonthName( MWTimestamp::getInstance( $ts )->format( 'n' ) );
case 'currentmonthnamegen':
return $pageLang->getMonthNameGen( MWTimestamp::getInstance( $ts )->format( 'n' ) );
case 'currentmonthabbrev':
return $pageLang->getMonthAbbreviation( MWTimestamp::getInstance( $ts )->format( 'n' ) );
case 'currentday':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'j' ), true );
case 'currentday2':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'd' ), true );
case 'localmonth':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'm' ), true );
case 'localmonth1':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'n' ), true );
case 'localmonthname':
return $pageLang->getMonthName( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
case 'localmonthnamegen':
return $pageLang->getMonthNameGen( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
case 'localmonthabbrev':
return $pageLang->getMonthAbbreviation( MWTimestamp::getLocalInstance( $ts )->format( 'n' ) );
case 'localday':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'j' ), true );
case 'localday2':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'd' ), true );
case 'pagename':
return wfEscapeWikiText( $title->getText() );
case 'pagenamee':
return wfEscapeWikiText( $title->getPartialURL() );
case 'fullpagename':
return wfEscapeWikiText( $title->getPrefixedText() );
case 'fullpagenamee':
return wfEscapeWikiText( $title->getPrefixedURL() );
case 'subpagename':
return wfEscapeWikiText( $title->getSubpageText() );
case 'subpagenamee':
return wfEscapeWikiText( $title->getSubpageUrlForm() );
case 'rootpagename':
return wfEscapeWikiText( $title->getRootText() );
case 'rootpagenamee':
return wfEscapeWikiText( wfUrlencode( str_replace(
' ',
'_',
$title->getRootText()
) ) );
case 'basepagename':
return wfEscapeWikiText( $title->getBaseText() );
case 'basepagenamee':
return wfEscapeWikiText( wfUrlencode( str_replace(
' ',
'_',
$title->getBaseText()
) ) );
case 'talkpagename':
if ( $title->canHaveTalkPage() ) {
$talkPage = $title->getTalkPage();
return wfEscapeWikiText( $talkPage->getPrefixedText() );
}
return '';
case 'talkpagenamee':
if ( $title->canHaveTalkPage() ) {
$talkPage = $title->getTalkPage();
return wfEscapeWikiText( $talkPage->getPrefixedURL() );
}
return '';
case 'subjectpagename':
$subjPage = $title->getSubjectPage();
return wfEscapeWikiText( $subjPage->getPrefixedText() );
case 'subjectpagenamee':
$subjPage = $title->getSubjectPage();
return wfEscapeWikiText( $subjPage->getPrefixedURL() );
case 'pageid': // requested in T25427
// Inform the edit saving system that getting the canonical output
// after page insertion requires a parse that used that exact page ID
self::setOutputFlag( $parser, $logger, 'vary-page-id', '{{PAGEID}} used' );
$value = $title->getArticleID();
if ( !$value ) {
$value = $parser->getOptions()->getSpeculativePageId();
if ( $value ) {
$parser->getOutput()->setSpeculativePageIdUsed( $value );
}
}
return (string)$value;
case 'revisionid':
$namespace = $title->getNamespace();
if (
$svcOptions->get( 'MiserMode' ) &&
!$parser->getOptions()->getInterfaceMessage() &&
// @TODO: disallow this variable on all namespaces
$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 {
self::setOutputFlag( $parser, $logger, 'vary-revision-exists', '{{REVISIONID}} used' );
return '';
}
} else {
// Inform the edit saving system that getting the canonical output after
// revision insertion requires a parse that used that exact revision ID
self::setOutputFlag( $parser, $logger, 'vary-revision-id', '{{REVISIONID}} used' );
$value = $parser->getRevisionId();
if ( $value === 0 ) {
$rev = $parser->getRevisionRecordObject();
$value = $rev ? $rev->getId() : $value;
}
if ( !$value ) {
$value = $parser->getOptions()->getSpeculativeRevId();
if ( $value ) {
$parser->getOutput()->setSpeculativeRevIdUsed( $value );
}
}
return (string)$value;
}
case 'revisionday':
return strval( (int)self::getRevisionTimestampSubstring(
$parser, $logger, 6, 2, self::MAX_TTS, $id
) );
case 'revisionday2':
return self::getRevisionTimestampSubstring(
$parser, $logger, 6, 2, self::MAX_TTS, $id
);
case 'revisionmonth':
return self::getRevisionTimestampSubstring(
$parser, $logger, 4, 2, self::MAX_TTS, $id
);
case 'revisionmonth1':
return strval( (int)self::getRevisionTimestampSubstring(
$parser, $logger, 4, 2, self::MAX_TTS, $id
) );
case 'revisionyear':
return self::getRevisionTimestampSubstring(
$parser, $logger, 0, 4, self::MAX_TTS, $id
);
case 'revisiontimestamp':
return self::getRevisionTimestampSubstring(
$parser, $logger, 0, 14, self::MAX_TTS, $id
);
case 'revisionuser':
// Inform the edit saving system that getting the canonical output after
// revision insertion requires a parse that used the actual user ID
self::setOutputFlag( $parser, $logger, 'vary-user', '{{REVISIONUSER}} used' );
// Note that getRevisionUser() can return null; we need to
// be sure to cast this to (an empty) string, since 'null'
// means "magic variable not handled here".
return (string)$parser->getRevisionUser();
case 'revisionsize':
return (string)$parser->getRevisionSize();
case 'namespace':
return str_replace( '_', ' ',
$parser->getContentLanguage()->getNsText( $title->getNamespace() ) );
case 'namespacee':
return wfUrlencode( $parser->getContentLanguage()->getNsText( $title->getNamespace() ) );
case 'namespacenumber':
return $title->getNamespace();
case 'talkspace':
return $title->canHaveTalkPage()
? str_replace( '_', ' ', $title->getTalkNsText() )
: '';
case 'talkspacee':
return $title->canHaveTalkPage()
? wfUrlencode( $title->getTalkNsText() )
: '';
case 'subjectspace':
return str_replace( '_', ' ', $title->getSubjectNsText() );
case 'subjectspacee':
return ( wfUrlencode( $title->getSubjectNsText() ) );
case 'currentdayname':
return $pageLang->getWeekdayName( (int)MWTimestamp::getInstance( $ts )->format( 'w' ) + 1 );
case 'currentyear':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'Y' ), true );
case 'currenttime':
return $pageLang->time( wfTimestamp( TS_MW, $ts ), false, false );
case 'currenthour':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'H' ), true );
case 'currentweek':
// @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
// int to remove the padding
return $pageLang->formatNum( (int)MWTimestamp::getInstance( $ts )->format( 'W' ) );
case 'currentdow':
return $pageLang->formatNum( MWTimestamp::getInstance( $ts )->format( 'w' ) );
case 'localdayname':
return $pageLang->getWeekdayName(
(int)MWTimestamp::getLocalInstance( $ts )->format( 'w' ) + 1
);
case 'localyear':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'Y' ), true );
case 'localtime':
return $pageLang->time(
MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' ),
false,
false
);
case 'localhour':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'H' ), true );
case 'localweek':
// @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
// int to remove the padding
return $pageLang->formatNum( (int)MWTimestamp::getLocalInstance( $ts )->format( 'W' ) );
case 'localdow':
return $pageLang->formatNum( MWTimestamp::getLocalInstance( $ts )->format( 'w' ) );
case 'numberofarticles':
return $pageLang->formatNum( SiteStats::articles() );
case 'numberoffiles':
return $pageLang->formatNum( SiteStats::images() );
case 'numberofusers':
return $pageLang->formatNum( SiteStats::users() );
case 'numberofactiveusers':
return $pageLang->formatNum( SiteStats::activeUsers() );
case 'numberofpages':
return $pageLang->formatNum( SiteStats::pages() );
case 'numberofadmins':
return $pageLang->formatNum( SiteStats::numberingroup( 'sysop' ) );
case 'numberofedits':
return $pageLang->formatNum( SiteStats::edits() );
case 'currenttimestamp':
return wfTimestamp( TS_MW, $ts );
case 'localtimestamp':
return MWTimestamp::getLocalInstance( $ts )->format( 'YmdHis' );
case 'currentversion':
return SpecialVersion::getVersion();
case 'articlepath':
return (string)$svcOptions->get( 'ArticlePath' );
case 'sitename':
return (string)$svcOptions->get( 'Sitename' );
case 'server':
return (string)$svcOptions->get( 'Server' );
case 'servername':
return (string)$svcOptions->get( 'ServerName' );
case 'scriptpath':
return (string)$svcOptions->get( 'ScriptPath' );
case 'stylepath':
return (string)$svcOptions->get( 'StylePath' );
case 'directionmark':
return $pageLang->getDirMark();
case 'contentlanguage':
return (string)$svcOptions->get( 'LanguageCode' );
case 'pagelanguage':
return $pageLang->getCode();
case 'cascadingsources':
return CoreParserFunctions::cascadingsources( $parser );
default:
// This is not one of the core magic variables
return null;
}
}
/**
* @param Parser $parser
* @param LoggerInterface $logger
* @param int $start
* @param int $len
* @param int $mtts Max time-till-save; sets vary-revision-timestamp if result changes by then
* @param string $variable Parser variable name
* @return string
*/
private static function getRevisionTimestampSubstring(
Parser $parser,
LoggerInterface $logger,
int $start,
int $len,
int $mtts,
string $variable
): string {
// Get the timezone-adjusted timestamp to be used for this revision
$resNow = substr( $parser->getRevisionTimestamp(), $start, $len );
// Possibly set vary-revision if there is not yet an associated revision
if ( !$parser->getRevisionRecordObject() ) {
// Get the timezone-adjusted timestamp $mtts seconds in the future.
// This future is relative to the current time and not that of the
// parser options. The rendered timestamp can be compared to that
// of the timestamp specified by the parser options.
$resThen = substr(
$parser->getContentLanguage()->userAdjust( wfTimestamp( TS_MW, time() + $mtts ), '' ),
$start,
$len
);
if ( $resNow !== $resThen ) {
// Inform the edit saving system that getting the canonical output after
// revision insertion requires a parse that used an actual revision timestamp
self::setOutputFlag( $parser, $logger, 'vary-revision-timestamp', "$variable used" );
}
}
return $resNow;
}
/**
* 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 {
$parser->getOutput()->setFlag( $flag );
$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" );
}
}
|