MediaWiki  master
CoreMagicVariables.php
Go to the documentation of this file.
1 <?php
26 use Psr\Log\LoggerInterface;
27 use Wikimedia\Timestamp\ConvertibleTimestamp;
28 
35 
49  public static function expand(
50  // Fundamental options
51  Parser $parser,
52  string $id,
53  // Context passed over from the parser
54  ConvertibleTimestamp $ts,
55  NamespaceInfo $nsInfo,
56  ServiceOptions $svcOptions,
57  LoggerInterface $logger
58  ): ?string {
59  $pageLang = $parser->getTargetLanguage();
60  $title = $parser->getTitle();
61 
62  switch ( $id ) {
63  case '!':
64  return '|';
65  case '=':
66  return '=';
67  case 'currentmonth':
68  return $pageLang->formatNumNoSeparators( $ts->format( 'm' ) );
69  case 'currentmonth1':
70  return $pageLang->formatNumNoSeparators( $ts->format( 'n' ) );
71  case 'currentmonthname':
72  return $pageLang->getMonthName( (int)$ts->format( 'n' ) );
73  case 'currentmonthnamegen':
74  return $pageLang->getMonthNameGen( (int)$ts->format( 'n' ) );
75  case 'currentmonthabbrev':
76  return $pageLang->getMonthAbbreviation( (int)$ts->format( 'n' ) );
77  case 'currentday':
78  return $pageLang->formatNumNoSeparators( $ts->format( 'j' ) );
79  case 'currentday2':
80  return $pageLang->formatNumNoSeparators( $ts->format( 'd' ) );
81  case 'localmonth':
82  return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'm' ) );
83  case 'localmonth1':
84  return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
85  case 'localmonthname':
86  return $pageLang->getMonthName( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
87  case 'localmonthnamegen':
88  return $pageLang->getMonthNameGen( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
89  case 'localmonthabbrev':
90  return $pageLang->getMonthAbbreviation( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'n' ) );
91  case 'localday':
92  return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'j' ) );
93  case 'localday2':
94  return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'd' ) );
95  case 'pagename':
96  case 'pagenamee':
97  case 'fullpagename':
98  case 'fullpagenamee':
99  case 'subpagename':
100  case 'subpagenamee':
101  case 'rootpagename':
102  case 'rootpagenamee':
103  case 'basepagename':
104  case 'basepagenamee':
105  case 'talkpagename':
106  case 'talkpagenamee':
107  case 'subjectpagename':
108  case 'subjectpagenamee':
109  case 'pageid':
110  case 'revisionday':
111  case 'revisionday2':
112  case 'revisionmonth':
113  case 'revisionmonth1':
114  case 'revisionyear':
115  case 'revisiontimestamp':
116  case 'namespace':
117  case 'namespacee':
118  case 'namespacenumber':
119  case 'talkspace':
120  case 'talkspacee':
121  case 'subjectspace':
122  case 'subjectspacee':
123  case 'cascadingsources':
124  # First argument of the corresponding parser function
125  # (second argument of the PHP implementation) is
126  # "title".
127 
128  # Note that for many of these {{FOO}} is subtly different
129  # from {{FOO:{{PAGENAME}}}}, so we can't pass $title here
130  # we have to explicitly use the "no arguments" form of the
131  # parser function by passing `null` to indicate a missing
132  # argument (which then defaults to the current page title).
133  return CoreParserFunctions::$id( $parser, null );
134  case 'revisionid':
135  $namespace = $title->getNamespace();
136  if (
137  $svcOptions->get( MainConfigNames::MiserMode ) &&
138  !$parser->getOptions()->getInterfaceMessage() &&
139  // @TODO: disallow this variable on all namespaces
140  $nsInfo->isSubject( $namespace )
141  ) {
142  // Use a stub result instead of the actual revision ID in order to avoid
143  // double parses on page save but still allow preview detection (T137900)
144  if ( $parser->getRevisionId() || $parser->getOptions()->getSpeculativeRevId() ) {
145  return '-';
146  } else {
147  self::setOutputFlag(
148  $parser,
149  $logger,
150  ParserOutputFlags::VARY_REVISION_EXISTS,
151  '{{REVISIONID}} used'
152  );
153  return '';
154  }
155  } else {
156  // Inform the edit saving system that getting the canonical output after
157  // revision insertion requires a parse that used that exact revision ID
158  self::setOutputFlag( $parser, $logger, ParserOutputFlags::VARY_REVISION_ID, '{{REVISIONID}} used' );
159  $value = $parser->getRevisionId();
160  if ( $value === 0 ) {
161  $rev = $parser->getRevisionRecordObject();
162  $value = $rev ? $rev->getId() : $value;
163  }
164  if ( !$value ) {
165  $value = $parser->getOptions()->getSpeculativeRevId();
166  if ( $value ) {
167  $parser->getOutput()->setSpeculativeRevIdUsed( $value );
168  }
169  }
170  return (string)$value;
171  }
172  case 'revisionuser':
173  // Inform the edit saving system that getting the canonical output after
174  // revision insertion requires a parse that used the actual user ID
175  self::setOutputFlag( $parser, $logger, ParserOutputFlags::VARY_USER, '{{REVISIONUSER}} used' );
176  // Note that getRevisionUser() can return null; we need to
177  // be sure to cast this to (an empty) string, since 'null'
178  // means "magic variable not handled here".
179  return (string)$parser->getRevisionUser();
180  case 'revisionsize':
181  return (string)$parser->getRevisionSize();
182  case 'currentdayname':
183  return $pageLang->getWeekdayName( (int)$ts->format( 'w' ) + 1 );
184  case 'currentyear':
185  return $pageLang->formatNumNoSeparators( $ts->format( 'Y' ) );
186  case 'currenttime':
187  return $pageLang->time( $ts->getTimestamp( TS_MW ), false, false );
188  case 'currenthour':
189  return $pageLang->formatNumNoSeparators( $ts->format( 'H' ) );
190  case 'currentweek':
191  // @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
192  // int to remove the padding
193  return $pageLang->formatNum( (int)$ts->format( 'W' ) );
194  case 'currentdow':
195  return $pageLang->formatNum( $ts->format( 'w' ) );
196  case 'localdayname':
197  return $pageLang->getWeekdayName(
198  (int)self::makeTsLocal( $svcOptions, $ts )->format( 'w' ) + 1
199  );
200  case 'localyear':
201  return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'Y' ) );
202  case 'localtime':
203  return $pageLang->time(
204  self::makeTsLocal( $svcOptions, $ts )->format( 'YmdHis' ),
205  false,
206  false
207  );
208  case 'localhour':
209  return $pageLang->formatNumNoSeparators( self::makeTsLocal( $svcOptions, $ts )->format( 'H' ) );
210  case 'localweek':
211  // @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
212  // int to remove the padding
213  return $pageLang->formatNum( (int)self::makeTsLocal( $svcOptions, $ts )->format( 'W' ) );
214  case 'localdow':
215  return $pageLang->formatNum( self::makeTsLocal( $svcOptions, $ts )->format( 'w' ) );
216  case 'numberofarticles':
217  case 'numberoffiles':
218  case 'numberofusers':
219  case 'numberofactiveusers':
220  case 'numberofpages':
221  case 'numberofadmins':
222  case 'numberofedits':
223  # second argument is 'raw'; magic variables are "not raw"
224  return CoreParserFunctions::$id( $parser, null );
225  case 'currenttimestamp':
226  return $ts->getTimestamp( TS_MW );
227  case 'localtimestamp':
228  return self::makeTsLocal( $svcOptions, $ts )->format( 'YmdHis' );
229  case 'currentversion':
231  case 'articlepath':
232  return (string)$svcOptions->get( MainConfigNames::ArticlePath );
233  case 'sitename':
234  return (string)$svcOptions->get( MainConfigNames::Sitename );
235  case 'server':
236  return (string)$svcOptions->get( MainConfigNames::Server );
237  case 'servername':
238  return (string)$svcOptions->get( MainConfigNames::ServerName );
239  case 'scriptpath':
240  return (string)$svcOptions->get( MainConfigNames::ScriptPath );
241  case 'stylepath':
242  return (string)$svcOptions->get( MainConfigNames::StylePath );
243  case 'directionmark':
244  return $pageLang->getDirMark();
245  case 'contentlanguage':
246  return $parser->getContentLanguage()->getCode();
247  case 'pagelanguage':
248  return $pageLang->getCode();
249  default:
250  // This is not one of the core magic variables
251  return null;
252  }
253  }
254 
262  private static function makeTsLocal( $svcOptions, $ts ) {
263  $localtimezone = $svcOptions->get( MainConfigNames::Localtimezone );
264  $ts->setTimezone( $localtimezone );
265  return $ts;
266  }
267 
276  private static function setOutputFlag(
277  Parser $parser,
278  LoggerInterface $logger,
279  string $flag,
280  string $reason
281  ): void {
282  $parser->getOutput()->setOutputFlag( $flag );
283  $name = $parser->getTitle()->getPrefixedText();
284  // This code was moved from Parser::setOutputFlag and used __METHOD__
285  // originally; we've hard-coded that output here so that our refactor
286  // doesn't change the messages in the logs.
287  $logger->debug( "Parser::setOutputFlag: set $flag flag on '$name'; $reason" );
288  }
289 }
Expansions of core magic variables, used by the parser.
static expand(Parser $parser, string $id, ConvertibleTimestamp $ts, NamespaceInfo $nsInfo, ServiceOptions $svcOptions, LoggerInterface $logger)
Expand the magic variable given by $index.
A class for passing options to services.
A class containing constants representing the names of configuration variables.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
isSubject( $index)
Is the given namespace is a subject (non-talk) namespace?
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:97
getOptions()
Definition: Parser.php:1117
getRevisionUser()
Get the name of the user that edited the last revision.
Definition: Parser.php:6088
getRevisionSize()
Get the size of the revision.
Definition: Parser.php:6112
getTitle()
Definition: Parser.php:1028
getContentLanguage()
Get the content language that this Parser is using.
Definition: Parser.php:1241
getOutput()
Definition: Parser.php:1109
getRevisionId()
Get the ID of the revision we are parsing.
Definition: Parser.php:5995
getRevisionRecordObject()
Get the revision record object for $this->mRevisionId.
Definition: Parser.php:6005
static getVersion( $flags='', $lang=null)
Return a string of the MediaWiki version with Git revision if available.