Merge "ResourceLoaderModule: Add @since annotations to new methods"
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2 /**
3 * Options for the PHP parser
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * \brief Set options of the Parser
26 *
27 * All member variables are supposed to be private in theory, although in practise this is not the case.
28 *
29 * @ingroup Parser
30 */
31 class ParserOptions {
32
33 /**
34 * Interlanguage links are removed and returned in an array
35 */
36 var $mInterwikiMagic;
37
38 /**
39 * Allow external images inline?
40 */
41 var $mAllowExternalImages;
42
43 /**
44 * If not, any exception?
45 */
46 var $mAllowExternalImagesFrom;
47
48 /**
49 * If not or it doesn't match, should we check an on-wiki whitelist?
50 */
51 var $mEnableImageWhitelist;
52
53 /**
54 * Date format index
55 */
56 var $mDateFormat = null;
57
58 /**
59 * Create "edit section" links?
60 */
61 var $mEditSection = true;
62
63 /**
64 * Allow inclusion of special pages?
65 */
66 var $mAllowSpecialInclusion;
67
68 /**
69 * Use tidy to cleanup output HTML?
70 */
71 var $mTidy = false;
72
73 /**
74 * Which lang to call for PLURAL and GRAMMAR
75 */
76 var $mInterfaceMessage = false;
77
78 /**
79 * Overrides $mInterfaceMessage with arbitrary language
80 */
81 var $mTargetLanguage = null;
82
83 /**
84 * Maximum size of template expansions, in bytes
85 */
86 var $mMaxIncludeSize;
87
88 /**
89 * Maximum number of nodes touched by PPFrame::expand()
90 */
91 var $mMaxPPNodeCount;
92
93 /**
94 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
95 */
96 var $mMaxGeneratedPPNodeCount;
97
98 /**
99 * Maximum recursion depth in PPFrame::expand()
100 */
101 var $mMaxPPExpandDepth;
102
103 /**
104 * Maximum recursion depth for templates within templates
105 */
106 var $mMaxTemplateDepth;
107
108 /**
109 * Maximum number of calls per parse to expensive parser functions
110 */
111 var $mExpensiveParserFunctionLimit;
112
113 /**
114 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
115 */
116 var $mRemoveComments = true;
117
118 /**
119 * Callback for template fetching. Used as first argument to call_user_func().
120 */
121 var $mTemplateCallback =
122 array( 'Parser', 'statelessFetchTemplate' );
123
124 /**
125 * Enable limit report in an HTML comment on output
126 */
127 var $mEnableLimitReport = false;
128
129 /**
130 * Timestamp used for {{CURRENTDAY}} etc.
131 */
132 var $mTimestamp;
133
134 /**
135 * Target attribute for external links
136 */
137 var $mExternalLinkTarget;
138
139 /**
140 * Clean up signature texts?
141 *
142 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
143 * 2) Substitute all transclusions
144 */
145 var $mCleanSignatures;
146
147 /**
148 * Transform wiki markup when saving the page?
149 */
150 var $mPreSaveTransform = true;
151
152 /**
153 * Whether content conversion should be disabled
154 */
155 var $mDisableContentConversion;
156
157 /**
158 * Whether title conversion should be disabled
159 */
160 var $mDisableTitleConversion;
161
162 /**
163 * Automatically number headings?
164 */
165 var $mNumberHeadings;
166
167 /**
168 * Thumb size preferred by the user.
169 */
170 var $mThumbSize;
171
172 /**
173 * Maximum article size of an article to be marked as "stub"
174 */
175 private $mStubThreshold;
176
177 /**
178 * Language object of the User language.
179 */
180 var $mUserLang;
181
182 /**
183 * @var User
184 * Stored user object
185 */
186 var $mUser;
187
188 /**
189 * Parsing the page for a "preview" operation?
190 */
191 var $mIsPreview = false;
192
193 /**
194 * Parsing the page for a "preview" operation on a single section?
195 */
196 var $mIsSectionPreview = false;
197
198 /**
199 * Parsing the printable version of the page?
200 */
201 var $mIsPrintable = false;
202
203 /**
204 * Extra key that should be present in the caching key.
205 */
206 var $mExtraKey = '';
207
208 /**
209 * Function to be called when an option is accessed.
210 */
211 protected $onAccessCallback = null;
212
213 function getInterwikiMagic() { return $this->mInterwikiMagic; }
214 function getAllowExternalImages() { return $this->mAllowExternalImages; }
215 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
216 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
217 function getEditSection() { return $this->mEditSection; }
218 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
219 return $this->mNumberHeadings; }
220 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
221 function getTidy() { return $this->mTidy; }
222 function getInterfaceMessage() { return $this->mInterfaceMessage; }
223 function getTargetLanguage() { return $this->mTargetLanguage; }
224 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
225 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
226 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount; }
227 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
228 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
229 /* @since 1.20 */
230 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit; }
231 function getRemoveComments() { return $this->mRemoveComments; }
232 function getTemplateCallback() { return $this->mTemplateCallback; }
233 function getEnableLimitReport() { return $this->mEnableLimitReport; }
234 function getCleanSignatures() { return $this->mCleanSignatures; }
235 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
236 function getDisableContentConversion() { return $this->mDisableContentConversion; }
237 function getDisableTitleConversion() { return $this->mDisableTitleConversion; }
238 function getThumbSize() { $this->optionUsed( 'thumbsize' );
239 return $this->mThumbSize; }
240 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
241 return $this->mStubThreshold; }
242
243 function getIsPreview() { return $this->mIsPreview; }
244 function getIsSectionPreview() { return $this->mIsSectionPreview; }
245 function getIsPrintable() { $this->optionUsed( 'printable' );
246 return $this->mIsPrintable; }
247 function getUser() { return $this->mUser; }
248 function getPreSaveTransform() { return $this->mPreSaveTransform; }
249
250 function getDateFormat() {
251 $this->optionUsed( 'dateformat' );
252 if ( !isset( $this->mDateFormat ) ) {
253 $this->mDateFormat = $this->mUser->getDatePreference();
254 }
255 return $this->mDateFormat;
256 }
257
258 function getTimestamp() {
259 if ( !isset( $this->mTimestamp ) ) {
260 $this->mTimestamp = wfTimestampNow();
261 }
262 return $this->mTimestamp;
263 }
264
265 /**
266 * Get the user language used by the parser for this page.
267 *
268 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
269 *
270 * To avoid side-effects where the page will be rendered based on the language
271 * of the user who last saved, this function will triger a cache fragmentation.
272 * Usage of this method is discouraged for that reason.
273 *
274 * When saving, this will return the default language instead of the user's.
275 *
276 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
277 *
278 * @return Language
279 * @since 1.19
280 */
281 function getUserLangObj() {
282 $this->optionUsed( 'userlang' );
283 return $this->mUserLang;
284 }
285
286 /**
287 * Same as getUserLangObj() but returns a string instead.
288 *
289 * @return string Language code
290 * @since 1.17
291 */
292 function getUserLang() {
293 return $this->getUserLangObj()->getCode();
294 }
295
296 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
297 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
298 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
299 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
300 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
301 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
302 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
303 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
304 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); }
305
306 /** @deprecated since 1.19 */
307 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); }
308 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); }
309 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); }
310 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
311 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
312 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); }
313 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
314 /* @since 1.20 */
315 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); }
316 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
317 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
318 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
319 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
320 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
321 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
322 function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion, $x ); }
323 function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion, $x ); }
324 function setUserLang( $x ) {
325 if ( is_string( $x ) ) {
326 $x = Language::factory( $x );
327 }
328 return wfSetVar( $this->mUserLang, $x );
329 }
330 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
331 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
332 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
333
334 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
335 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
336 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
337
338 /**
339 * Extra key that should be present in the parser cache key.
340 */
341 function addExtraKey( $key ) {
342 $this->mExtraKey .= '!' . $key;
343 }
344
345 /**
346 * Constructor
347 * @param User $user
348 * @param Language $lang
349 */
350 function __construct( $user = null, $lang = null ) {
351 if ( $user === null ) {
352 global $wgUser;
353 if ( $wgUser === null ) {
354 $user = new User;
355 } else {
356 $user = $wgUser;
357 }
358 }
359 if ( $lang === null ) {
360 global $wgLang;
361 if ( !StubObject::isRealObject( $wgLang ) ) {
362 $wgLang->_unstub();
363 }
364 $lang = $wgLang;
365 }
366 $this->initialiseFromUser( $user, $lang );
367 }
368
369 /**
370 * Get a ParserOptions object from a given user.
371 * Language will be taken from $wgLang.
372 *
373 * @param User $user
374 * @return ParserOptions
375 */
376 public static function newFromUser( $user ) {
377 return new ParserOptions( $user );
378 }
379
380 /**
381 * Get a ParserOptions object from a given user and language
382 *
383 * @param User $user
384 * @param Language $lang
385 * @return ParserOptions
386 */
387 public static function newFromUserAndLang( User $user, Language $lang ) {
388 return new ParserOptions( $user, $lang );
389 }
390
391 /**
392 * Get a ParserOptions object from a IContextSource object
393 *
394 * @param IContextSource $context
395 * @return ParserOptions
396 */
397 public static function newFromContext( IContextSource $context ) {
398 return new ParserOptions( $context->getUser(), $context->getLanguage() );
399 }
400
401 /**
402 * Get user options
403 *
404 * @param User $user
405 * @param Language $lang
406 */
407 private function initialiseFromUser( $user, $lang ) {
408 global $wgInterwikiMagic, $wgAllowExternalImages,
409 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
410 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
411 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
412 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
413
414 wfProfileIn( __METHOD__ );
415
416 $this->mInterwikiMagic = $wgInterwikiMagic;
417 $this->mAllowExternalImages = $wgAllowExternalImages;
418 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
419 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
420 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
421 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
422 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
423 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
424 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
425 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
426 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
427 $this->mCleanSignatures = $wgCleanSignatures;
428 $this->mExternalLinkTarget = $wgExternalLinkTarget;
429 $this->mDisableContentConversion = $wgDisableLangConversion;
430 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
431
432 $this->mUser = $user;
433 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
434 $this->mThumbSize = $user->getOption( 'thumbsize' );
435 $this->mStubThreshold = $user->getStubThreshold();
436 $this->mUserLang = $lang;
437
438 wfProfileOut( __METHOD__ );
439 }
440
441 /**
442 * Registers a callback for tracking which ParserOptions which are used.
443 * This is a private API with the parser.
444 * @param callable $callback
445 */
446 function registerWatcher( $callback ) {
447 $this->onAccessCallback = $callback;
448 }
449
450 /**
451 * Called when an option is accessed.
452 * @param string $optionName Name of the option
453 */
454 public function optionUsed( $optionName ) {
455 if ( $this->onAccessCallback ) {
456 call_user_func( $this->onAccessCallback, $optionName );
457 }
458 }
459
460 /**
461 * Returns the full array of options that would have been used by
462 * in 1.16.
463 * Used to get the old parser cache entries when available.
464 * @return array
465 */
466 public static function legacyOptions() {
467 return array( 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
468 }
469
470 /**
471 * Generate a hash string with the values set on these ParserOptions
472 * for the keys given in the array.
473 * This will be used as part of the hash key for the parser cache,
474 * so users sharign the options with vary for the same page share
475 * the same cached data safely.
476 *
477 * Extensions which require it should install 'PageRenderingHash' hook,
478 * which will give them a chance to modify this key based on their own
479 * settings.
480 *
481 * @since 1.17
482 * @param array $forOptions
483 * @param Title $title Used to get the content language of the page (since r97636)
484 * @return string Page rendering hash
485 */
486 public function optionsHash( $forOptions, $title = null ) {
487 global $wgRenderHashAppend;
488
489 // FIXME: Once the cache key is reorganized this argument
490 // can be dropped. It was used when the math extension was
491 // part of core.
492 $confstr = '*';
493
494 // Space assigned for the stubthreshold but unused
495 // since it disables the parser cache, its value will always
496 // be 0 when this function is called by parsercache.
497 if ( in_array( 'stubthreshold', $forOptions ) ) {
498 $confstr .= '!' . $this->mStubThreshold;
499 } else {
500 $confstr .= '!*';
501 }
502
503 if ( in_array( 'dateformat', $forOptions ) ) {
504 $confstr .= '!' . $this->getDateFormat();
505 }
506
507 if ( in_array( 'numberheadings', $forOptions ) ) {
508 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
509 } else {
510 $confstr .= '!*';
511 }
512
513 if ( in_array( 'userlang', $forOptions ) ) {
514 $confstr .= '!' . $this->mUserLang->getCode();
515 } else {
516 $confstr .= '!*';
517 }
518
519 if ( in_array( 'thumbsize', $forOptions ) ) {
520 $confstr .= '!' . $this->mThumbSize;
521 } else {
522 $confstr .= '!*';
523 }
524
525 // add in language specific options, if any
526 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
527 if ( !is_null( $title ) ) {
528 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
529 } else {
530 global $wgContLang;
531 $confstr .= $wgContLang->getExtraHashOptions();
532 }
533
534 $confstr .= $wgRenderHashAppend;
535
536 if ( !in_array( 'editsection', $forOptions ) ) {
537 $confstr .= '!*';
538 } elseif ( !$this->mEditSection ) {
539 $confstr .= '!edit=0';
540 }
541
542 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
543 $confstr .= '!printable=1';
544 }
545
546 if ( $this->mExtraKey != '' ) {
547 $confstr .= $this->mExtraKey;
548 }
549
550 // Give a chance for extensions to modify the hash, if they have
551 // extra options or other effects on the parser cache.
552 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) );
553
554 // Make it a valid memcached key fragment
555 $confstr = str_replace( ' ', '_', $confstr );
556
557 return $confstr;
558 }
559 }