Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
CoreMagicVariables.php
Go to the documentation of this file.
1<?php
24namespaceMediaWiki\Parser;
25
26use DateTime;
27useMediaWiki\Config\ServiceOptions;
28useMediaWiki\MainConfigNames;
29useMediaWiki\Specials\SpecialVersion;
30useMediaWiki\Utils\MWTimestamp;
31use Psr\Log\LoggerInterface;
32use Wikimedia\Timestamp\ConvertibleTimestamp;
33
39classCoreMagicVariables {
41privateconst CACHE_TTL_BY_ID = [
42'currenttime' => 3600,
43'localtime' => 3600,
44'numberofarticles' => 3600,
45'numberoffiles' => 3600,
46'numberofedits' => 3600,
47'numberofusers' => 3600,
48'numberofactiveusers' => 3600,
49'numberofpages' => 3600,
50'currentversion' => 86400,
51'currenttimestamp' => 3600,
52'localtimestamp' => 3600,
53'pagesinnamespace' => 3600,
54'numberofadmins' => 3600,
55'numberingroup' => 3600,
56 ];
57
59privateconst DEADLINE_DATE_SPEC_BY_UNIT = [
60'Y' =>'first day of January next year midnight',
61'M' =>'first day of next month midnight',
62'D' =>'next day midnight',
63// Note that this relative datetime specifier does not zero out
64// minutes/seconds, but we will do so manually in
65// ::applyUnitTimestampDeadline() when given the unit 'H'
66'H' =>'next hour'
67 ];
69privateconst DEADLINE_TTL_CLOCK_FUDGE = 1;
71privateconst DEADLINE_TTL_STAGGER_MAX = 15;
73privateconst MIN_DEADLINE_TTL = 15;
74
87publicstaticfunctionexpand(
88// Fundamental options
89Parser $parser,
90string $id,
91// Context passed over from the parser
92 ConvertibleTimestamp $ts,
93ServiceOptions $svcOptions,
94 LoggerInterface $logger
95 ): ?string {
96 $pageLang = $parser->getTargetLanguage();
97
98 $cacheTTL = self::CACHE_TTL_BY_ID[$id] ?? -1;
99if ( $cacheTTL > -1 ) {
100 $parser->getOutput()->updateCacheExpiry( $cacheTTL );
101 }
102
103switch ( $id ) {
104case'!':
105return'|';
106case'=':
107return'=';
108case'currentmonth':
109 self::applyUnitTimestampDeadline( $parser, $ts,'M' );
110
111return $pageLang->formatNumNoSeparators( $ts->format('m' ) );
112case'currentmonth1':
113 self::applyUnitTimestampDeadline( $parser, $ts,'M' );
114
115return $pageLang->formatNumNoSeparators( $ts->format('n' ) );
116case'currentmonthname':
117 self::applyUnitTimestampDeadline( $parser, $ts,'M' );
118
119return $pageLang->getMonthName( (int)$ts->format('n' ) );
120case'currentmonthnamegen':
121 self::applyUnitTimestampDeadline( $parser, $ts,'M' );
122
123return $pageLang->getMonthNameGen( (int)$ts->format('n' ) );
124case'currentmonthabbrev':
125 self::applyUnitTimestampDeadline( $parser, $ts,'M' );
126
127return $pageLang->getMonthAbbreviation( (int)$ts->format('n' ) );
128case'currentday':
129 self::applyUnitTimestampDeadline( $parser, $ts,'D' );
130
131return $pageLang->formatNumNoSeparators( $ts->format('j' ) );
132case'currentday2':
133 self::applyUnitTimestampDeadline( $parser, $ts,'D' );
134
135return $pageLang->formatNumNoSeparators( $ts->format('d' ) );
136case'localmonth':
137 $localTs = self::makeTsLocal( $svcOptions, $ts );
138 self::applyUnitTimestampDeadline( $parser, $localTs,'M' );
139
140return $pageLang->formatNumNoSeparators( $localTs->format('m' ) );
141case'localmonth1':
142 $localTs = self::makeTsLocal( $svcOptions, $ts );
143 self::applyUnitTimestampDeadline( $parser, $localTs,'M' );
144
145return $pageLang->formatNumNoSeparators( $localTs->format('n' ) );
146case'localmonthname':
147 $localTs = self::makeTsLocal( $svcOptions, $ts );
148 self::applyUnitTimestampDeadline( $parser, $localTs,'M' );
149
150return $pageLang->getMonthName( (int)$localTs->format('n' ) );
151case'localmonthnamegen':
152 $localTs = self::makeTsLocal( $svcOptions, $ts );
153 self::applyUnitTimestampDeadline( $parser, $localTs,'M' );
154
155return $pageLang->getMonthNameGen( (int)$localTs->format('n' ) );
156case'localmonthabbrev':
157 $localTs = self::makeTsLocal( $svcOptions, $ts );
158 self::applyUnitTimestampDeadline( $parser, $localTs,'M' );
159
160return $pageLang->getMonthAbbreviation( (int)$localTs->format('n' ) );
161case'localday':
162 $localTs = self::makeTsLocal( $svcOptions, $ts );
163 self::applyUnitTimestampDeadline( $parser, $localTs,'D' );
164
165return $pageLang->formatNumNoSeparators( $localTs->format('j' ) );
166case'localday2':
167 $localTs = self::makeTsLocal( $svcOptions, $ts );
168 self::applyUnitTimestampDeadline( $parser, $localTs,'D' );
169
170return $pageLang->formatNumNoSeparators( $localTs->format('d' ) );
171case'pagename':
172case'pagenamee':
173case'fullpagename':
174case'fullpagenamee':
175case'subpagename':
176case'subpagenamee':
177case'rootpagename':
178case'rootpagenamee':
179case'basepagename':
180case'basepagenamee':
181case'talkpagename':
182case'talkpagenamee':
183case'subjectpagename':
184case'subjectpagenamee':
185case'pageid':
186case'revisionid':
187case'revisionuser':
188case'revisionday':
189case'revisionday2':
190case'revisionmonth':
191case'revisionmonth1':
192case'revisionyear':
193case'revisiontimestamp':
194case'namespace':
195case'namespacee':
196case'namespacenumber':
197case'talkspace':
198case'talkspacee':
199case'subjectspace':
200case'subjectspacee':
201case'cascadingsources':
202 # First argument of the corresponding parser function
203 # (second argument of the PHP implementation) is
204 # "title".
205
206 # Note that for many of these {{FOO}} is subtly different
207 # from {{FOO:{{PAGENAME}}}}, so we can't pass $title here
208 # we have to explicitly use the "no arguments" form of the
209 # parser function by passing `null` to indicate a missing
210 # argument (which then defaults to the current page title).
211return CoreParserFunctions::$id( $parser,null );
212case'revisionsize':
213return (string)$parser->getRevisionSize();
214case'currentdayname':
215 self::applyUnitTimestampDeadline( $parser, $ts,'D' );
216
217return $pageLang->getWeekdayName( (int)$ts->format('w' ) + 1 );
218case'currentyear':
219 self::applyUnitTimestampDeadline( $parser, $ts,'Y' );
220
221return $pageLang->formatNumNoSeparators( $ts->format('Y' ) );
222case'currenttime':
223return $pageLang->time( $ts->getTimestamp( TS_MW ),false,false );
224case'currenthour':
225 self::applyUnitTimestampDeadline( $parser, $ts,'H' );
226
227return $pageLang->formatNumNoSeparators( $ts->format('H' ) );
228case'currentweek':
229 self::applyUnitTimestampDeadline( $parser, $ts,'D' );
230// @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
231// int to remove the padding
232return $pageLang->formatNum( (int)$ts->format('W' ) );
233case'currentdow':
234 self::applyUnitTimestampDeadline( $parser, $ts,'D' );
235
236return $pageLang->formatNum( $ts->format('w' ) );
237case'localdayname':
238 $localTs = self::makeTsLocal( $svcOptions, $ts );
239 self::applyUnitTimestampDeadline( $parser, $localTs,'D' );
240
241return $pageLang->getWeekdayName( (int)$localTs->format('w' ) + 1 );
242case'localyear':
243 $localTs = self::makeTsLocal( $svcOptions, $ts );
244 self::applyUnitTimestampDeadline( $parser, $localTs,'Y' );
245
246return $pageLang->formatNumNoSeparators( $localTs->format('Y' ) );
247case'localtime':
248 $localTs = self::makeTsLocal( $svcOptions, $ts );
249
250return $pageLang->time(
251 $localTs->format('YmdHis' ),
252false,
253false
254 );
255case'localhour':
256 $localTs = self::makeTsLocal( $svcOptions, $ts );
257 self::applyUnitTimestampDeadline( $parser, $localTs,'H' );
258
259return $pageLang->formatNumNoSeparators( $localTs->format('H' ) );
260case'localweek':
261 $localTs = self::makeTsLocal( $svcOptions, $ts );
262 self::applyUnitTimestampDeadline( $parser, $localTs,'D' );
263// @bug T6594 PHP5 has it zero padded, PHP4 does not, cast to
264// int to remove the padding
265return $pageLang->formatNum( (int)$localTs->format('W' ) );
266case'localdow':
267 $localTs = self::makeTsLocal( $svcOptions, $ts );
268 self::applyUnitTimestampDeadline( $parser, $localTs,'D' );
269
270return $pageLang->formatNum( $localTs->format('w' ) );
271case'numberofarticles':
272case'numberoffiles':
273case'numberofusers':
274case'numberofactiveusers':
275case'numberofpages':
276case'numberofadmins':
277case'numberofedits':
278 # second argument is 'raw'; magic variables are "not raw"
279return CoreParserFunctions::$id( $parser,null );
280case'currenttimestamp':
281return $ts->getTimestamp( TS_MW );
282case'localtimestamp':
283 $localTs = self::makeTsLocal( $svcOptions, $ts );
284
285return $localTs->format('YmdHis' );
286case'currentversion':
287return SpecialVersion::getVersion();
288case'articlepath':
289return (string)$svcOptions->get(MainConfigNames::ArticlePath );
290case'sitename':
291return (string)$svcOptions->get(MainConfigNames::Sitename );
292case'server':
293return (string)$svcOptions->get(MainConfigNames::Server );
294case'servername':
295return (string)$svcOptions->get(MainConfigNames::ServerName );
296case'scriptpath':
297return (string)$svcOptions->get(MainConfigNames::ScriptPath );
298case'stylepath':
299return (string)$svcOptions->get(MainConfigNames::StylePath );
300case'directionmark':
301return $pageLang->getDirMark();
302case'contentlanguage':
303return $parser->getContentLanguage()->getCode();
304case'pagelanguage':
305return $pageLang->getCode();
306case'userlanguage':
307if ( $parser->getOptions()->isMessage()
309 ) {
310return $parser->getOptions()->getUserLang();
311 }else {
312return $pageLang->getCode();
313 }
314case'bcp47':
315case'dir':
316case'language':
317 # magic variables are the same as empty/default first argument
318return CoreParserFunctions::$id( $parser );
319default:
320// This is not one of the core magic variables
321returnnull;
322 }
323 }
324
332privatestaticfunction makeTsLocal( $svcOptions, $ts ) {
333 $localtimezone = $svcOptions->get(MainConfigNames::Localtimezone );
334 $ts->setTimezone( $localtimezone );
335return $ts;
336 }
337
345privatestaticfunction applyUnitTimestampDeadline(
346 Parser $parser,
347 ConvertibleTimestamp $ts,
348string $unit
349 ) {
350 $tsUnix = (int)$ts->getTimestamp( TS_UNIX );
351
352 $date =new DateTime("@$tsUnix" );
353 $date->setTimezone( $ts->getTimezone() );
354 $date->modify( self::DEADLINE_DATE_SPEC_BY_UNIT[$unit] );
355if ( $unit ==='H' ) {
356// Zero out the minutes/seconds
357 $date->setTime( intval( $date->format('H' ), 10 ), 0, 0 );
358 }else {
359 $date->setTime( 0, 0, 0 );
360 }
361 $deadlineUnix = (int)$date->format('U' );
362
363 $ttl = max( $deadlineUnix - $tsUnix, self::MIN_DEADLINE_TTL );
364 $ttl += self::DEADLINE_TTL_CLOCK_FUDGE;
365 $ttl += ( $tsUnix % self::DEADLINE_TTL_STAGGER_MAX );
366
367 $parser->getOutput()->updateCacheExpiry( $ttl );
368 }
369}
370
372class_alias( CoreMagicVariables::class,'CoreMagicVariables' );
MediaWiki\Config\ServiceOptions
A class for passing options to services.
DefinitionServiceOptions.php:26
MediaWiki\Config\ServiceOptions\get
get( $key)
DefinitionServiceOptions.php:92
MediaWiki\MainConfigNames
A class containing constants representing the names of configuration variables.
DefinitionMainConfigNames.php:22
MediaWiki\MainConfigNames\ServerName
const ServerName
Name constant for the ServerName setting, for use with Config::get()
DefinitionMainConfigNames.php:52
MediaWiki\MainConfigNames\ParserEnableUserLanguage
const ParserEnableUserLanguage
Name constant for the ParserEnableUserLanguage setting, for use with Config::get()
DefinitionMainConfigNames.php:2461
MediaWiki\MainConfigNames\StylePath
const StylePath
Name constant for the StylePath setting, for use with Config::get()
DefinitionMainConfigNames.php:106
MediaWiki\MainConfigNames\Localtimezone
const Localtimezone
Name constant for the Localtimezone setting, for use with Config::get()
DefinitionMainConfigNames.php:1938
MediaWiki\MainConfigNames\Server
const Server
Name constant for the Server setting, for use with Config::get()
DefinitionMainConfigNames.php:40
MediaWiki\MainConfigNames\Sitename
const Sitename
Name constant for the Sitename setting, for use with Config::get()
DefinitionMainConfigNames.php:34
MediaWiki\MainConfigNames\ArticlePath
const ArticlePath
Name constant for the ArticlePath setting, for use with Config::get()
DefinitionMainConfigNames.php:136
MediaWiki\MainConfigNames\ScriptPath
const ScriptPath
Name constant for the ScriptPath setting, for use with Config::get()
DefinitionMainConfigNames.php:76
MediaWiki\Parser\CoreMagicVariables
Expansions of core magic variables, used by the parser.
DefinitionCoreMagicVariables.php:39
MediaWiki\Parser\CoreMagicVariables\expand
static expand(Parser $parser, string $id, ConvertibleTimestamp $ts, ServiceOptions $svcOptions, LoggerInterface $logger)
Expand the magic variable given by $index.
DefinitionCoreMagicVariables.php:87
MediaWiki\Parser\Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
DefinitionParser.php:147
MediaWiki\Parser\Parser\getOptions
getOptions()
DefinitionParser.php:1133
MediaWiki\Parser\Parser\getContentLanguage
getContentLanguage()
Get the content language that this Parser is using.
DefinitionParser.php:1257
MediaWiki\Parser\Parser\getOutput
getOutput()
DefinitionParser.php:1119
MediaWiki\Parser\Parser\getRevisionSize
getRevisionSize()
Get the size of the revision.
DefinitionParser.php:6206
MediaWiki\Specials\SpecialVersion
Version information about MediaWiki (core, extensions, libs), PHP, and the database.
DefinitionSpecialVersion.php:54
MediaWiki\Utils\MWTimestamp
Library for creating and parsing MW-style timestamps.
DefinitionMWTimestamp.php:48
MediaWiki\Parser
DefinitionBlockLevelPass.php:27

[8]ページ先頭

©2009-2025 Movatter.jp