Merge "Made LocalFile move/delete/restore handle network partitions better"
[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
28 * practise this is not the case.
29 *
30 * @ingroup Parser
31 */
32 class ParserOptions {
33
34 /**
35 * Interlanguage links are removed and returned in an array
36 */
37 public $mInterwikiMagic;
38
39 /**
40 * Allow external images inline?
41 */
42 public $mAllowExternalImages;
43
44 /**
45 * If not, any exception?
46 */
47 public $mAllowExternalImagesFrom;
48
49 /**
50 * If not or it doesn't match, should we check an on-wiki whitelist?
51 */
52 public $mEnableImageWhitelist;
53
54 /**
55 * Date format index
56 */
57 public $mDateFormat = null;
58
59 /**
60 * Create "edit section" links?
61 */
62 public $mEditSection = true;
63
64 /**
65 * Allow inclusion of special pages?
66 */
67 public $mAllowSpecialInclusion;
68
69 /**
70 * Use tidy to cleanup output HTML?
71 */
72 public $mTidy = false;
73
74 /**
75 * Which lang to call for PLURAL and GRAMMAR
76 */
77 public $mInterfaceMessage = false;
78
79 /**
80 * Overrides $mInterfaceMessage with arbitrary language
81 */
82 public $mTargetLanguage = null;
83
84 /**
85 * Maximum size of template expansions, in bytes
86 */
87 public $mMaxIncludeSize;
88
89 /**
90 * Maximum number of nodes touched by PPFrame::expand()
91 */
92 public $mMaxPPNodeCount;
93
94 /**
95 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
96 */
97 public $mMaxGeneratedPPNodeCount;
98
99 /**
100 * Maximum recursion depth in PPFrame::expand()
101 */
102 public $mMaxPPExpandDepth;
103
104 /**
105 * Maximum recursion depth for templates within templates
106 */
107 public $mMaxTemplateDepth;
108
109 /**
110 * Maximum number of calls per parse to expensive parser functions
111 */
112 public $mExpensiveParserFunctionLimit;
113
114 /**
115 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
116 */
117 public $mRemoveComments = true;
118
119 /**
120 * Callback for template fetching. Used as first argument to call_user_func().
121 */
122 public $mTemplateCallback =
123 array( 'Parser', 'statelessFetchTemplate' );
124
125 /**
126 * Enable limit report in an HTML comment on output
127 */
128 public $mEnableLimitReport = false;
129
130 /**
131 * Timestamp used for {{CURRENTDAY}} etc.
132 */
133 public $mTimestamp;
134
135 /**
136 * Target attribute for external links
137 */
138 public $mExternalLinkTarget;
139
140 /**
141 * Clean up signature texts?
142 *
143 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
144 * 2) Substitute all transclusions
145 */
146 public $mCleanSignatures;
147
148 /**
149 * Transform wiki markup when saving the page?
150 */
151 public $mPreSaveTransform = true;
152
153 /**
154 * Whether content conversion should be disabled
155 */
156 public $mDisableContentConversion;
157
158 /**
159 * Whether title conversion should be disabled
160 */
161 public $mDisableTitleConversion;
162
163 /**
164 * Automatically number headings?
165 */
166 public $mNumberHeadings;
167
168 /**
169 * Thumb size preferred by the user.
170 */
171 public $mThumbSize;
172
173 /**
174 * Maximum article size of an article to be marked as "stub"
175 */
176 private $mStubThreshold;
177
178 /**
179 * Language object of the User language.
180 */
181 public $mUserLang;
182
183 /**
184 * @var User
185 * Stored user object
186 */
187 public $mUser;
188
189 /**
190 * Parsing the page for a "preview" operation?
191 */
192 public $mIsPreview = false;
193
194 /**
195 * Parsing the page for a "preview" operation on a single section?
196 */
197 public $mIsSectionPreview = false;
198
199 /**
200 * Parsing the printable version of the page?
201 */
202 public $mIsPrintable = false;
203
204 /**
205 * Extra key that should be present in the caching key.
206 */
207 public $mExtraKey = '';
208
209 /**
210 * Function to be called when an option is accessed.
211 */
212 protected $onAccessCallback = null;
213
214 public function getInterwikiMagic() {
215 return $this->mInterwikiMagic;
216 }
217
218 public function getAllowExternalImages() {
219 return $this->mAllowExternalImages;
220 }
221
222 public function getAllowExternalImagesFrom() {
223 return $this->mAllowExternalImagesFrom;
224 }
225
226 public function getEnableImageWhitelist() {
227 return $this->mEnableImageWhitelist;
228 }
229
230 public function getEditSection() {
231 return $this->mEditSection;
232 }
233
234 public function getNumberHeadings() {
235 $this->optionUsed( 'numberheadings' );
236
237 return $this->mNumberHeadings;
238 }
239
240 public function getAllowSpecialInclusion() {
241 return $this->mAllowSpecialInclusion;
242 }
243
244 public function getTidy() {
245 return $this->mTidy;
246 }
247
248 public function getInterfaceMessage() {
249 return $this->mInterfaceMessage;
250 }
251
252 public function getTargetLanguage() {
253 return $this->mTargetLanguage;
254 }
255
256 public function getMaxIncludeSize() {
257 return $this->mMaxIncludeSize;
258 }
259
260 public function getMaxPPNodeCount() {
261 return $this->mMaxPPNodeCount;
262 }
263
264 public function getMaxGeneratedPPNodeCount() {
265 return $this->mMaxGeneratedPPNodeCount;
266 }
267
268 public function getMaxPPExpandDepth() {
269 return $this->mMaxPPExpandDepth;
270 }
271
272 public function getMaxTemplateDepth() {
273 return $this->mMaxTemplateDepth;
274 }
275
276 /* @since 1.20 */
277 public function getExpensiveParserFunctionLimit() {
278 return $this->mExpensiveParserFunctionLimit;
279 }
280
281 public function getRemoveComments() {
282 return $this->mRemoveComments;
283 }
284
285 public function getTemplateCallback() {
286 return $this->mTemplateCallback;
287 }
288
289 public function getEnableLimitReport() {
290 return $this->mEnableLimitReport;
291 }
292
293 public function getCleanSignatures() {
294 return $this->mCleanSignatures;
295 }
296
297 public function getExternalLinkTarget() {
298 return $this->mExternalLinkTarget;
299 }
300
301 public function getDisableContentConversion() {
302 return $this->mDisableContentConversion;
303 }
304
305 public function getDisableTitleConversion() {
306 return $this->mDisableTitleConversion;
307 }
308
309 public function getThumbSize() {
310 $this->optionUsed( 'thumbsize' );
311
312 return $this->mThumbSize;
313 }
314
315 public function getStubThreshold() {
316 $this->optionUsed( 'stubthreshold' );
317
318 return $this->mStubThreshold;
319 }
320
321 public function getIsPreview() {
322 return $this->mIsPreview;
323 }
324
325 public function getIsSectionPreview() {
326 return $this->mIsSectionPreview;
327 }
328
329 public function getIsPrintable() {
330 $this->optionUsed( 'printable' );
331
332 return $this->mIsPrintable;
333 }
334
335 public function getUser() {
336 return $this->mUser;
337 }
338
339 public function getPreSaveTransform() {
340 return $this->mPreSaveTransform;
341 }
342
343 public function getDateFormat() {
344 $this->optionUsed( 'dateformat' );
345 if ( !isset( $this->mDateFormat ) ) {
346 $this->mDateFormat = $this->mUser->getDatePreference();
347 }
348 return $this->mDateFormat;
349 }
350
351 public function getTimestamp() {
352 if ( !isset( $this->mTimestamp ) ) {
353 $this->mTimestamp = wfTimestampNow();
354 }
355 return $this->mTimestamp;
356 }
357
358 /**
359 * Get the user language used by the parser for this page.
360 *
361 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
362 *
363 * To avoid side-effects where the page will be rendered based on the language
364 * of the user who last saved, this function will triger a cache fragmentation.
365 * Usage of this method is discouraged for that reason.
366 *
367 * When saving, this will return the default language instead of the user's.
368 *
369 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
370 *
371 * @return Language
372 * @since 1.19
373 */
374 public function getUserLangObj() {
375 $this->optionUsed( 'userlang' );
376 return $this->mUserLang;
377 }
378
379 /**
380 * Same as getUserLangObj() but returns a string instead.
381 *
382 * @return string Language code
383 * @since 1.17
384 */
385 public function getUserLang() {
386 return $this->getUserLangObj()->getCode();
387 }
388
389 public function setInterwikiMagic( $x ) {
390 return wfSetVar( $this->mInterwikiMagic, $x );
391 }
392
393 public function setAllowExternalImages( $x ) {
394 return wfSetVar( $this->mAllowExternalImages, $x );
395 }
396
397 public function setAllowExternalImagesFrom( $x ) {
398 return wfSetVar( $this->mAllowExternalImagesFrom, $x );
399 }
400
401 public function setEnableImageWhitelist( $x ) {
402 return wfSetVar( $this->mEnableImageWhitelist, $x );
403 }
404
405 public function setDateFormat( $x ) {
406 return wfSetVar( $this->mDateFormat, $x );
407 }
408
409 public function setEditSection( $x ) {
410 return wfSetVar( $this->mEditSection, $x );
411 }
412
413 public function setNumberHeadings( $x ) {
414 return wfSetVar( $this->mNumberHeadings, $x );
415 }
416
417 public function setAllowSpecialInclusion( $x ) {
418 return wfSetVar( $this->mAllowSpecialInclusion, $x );
419 }
420
421 public function setTidy( $x ) {
422 return wfSetVar( $this->mTidy, $x );
423 }
424
425 public function setInterfaceMessage( $x ) {
426 return wfSetVar( $this->mInterfaceMessage, $x );
427 }
428
429 public function setTargetLanguage( $x ) {
430 return wfSetVar( $this->mTargetLanguage, $x, true );
431 }
432
433 public function setMaxIncludeSize( $x ) {
434 return wfSetVar( $this->mMaxIncludeSize, $x );
435 }
436
437 public function setMaxPPNodeCount( $x ) {
438 return wfSetVar( $this->mMaxPPNodeCount, $x );
439 }
440
441 public function setMaxGeneratedPPNodeCount( $x ) {
442 return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x );
443 }
444
445 public function setMaxTemplateDepth( $x ) {
446 return wfSetVar( $this->mMaxTemplateDepth, $x );
447 }
448
449 /* @since 1.20 */
450 public function setExpensiveParserFunctionLimit( $x ) {
451 return wfSetVar( $this->mExpensiveParserFunctionLimit, $x );
452 }
453
454 public function setRemoveComments( $x ) {
455 return wfSetVar( $this->mRemoveComments, $x );
456 }
457
458 public function setTemplateCallback( $x ) {
459 return wfSetVar( $this->mTemplateCallback, $x );
460 }
461
462 public function enableLimitReport( $x = true ) {
463 return wfSetVar( $this->mEnableLimitReport, $x );
464 }
465
466 public function setTimestamp( $x ) {
467 return wfSetVar( $this->mTimestamp, $x );
468 }
469
470 public function setCleanSignatures( $x ) {
471 return wfSetVar( $this->mCleanSignatures, $x );
472 }
473
474 public function setExternalLinkTarget( $x ) {
475 return wfSetVar( $this->mExternalLinkTarget, $x );
476 }
477
478 public function disableContentConversion( $x = true ) {
479 return wfSetVar( $this->mDisableContentConversion, $x );
480 }
481
482 public function disableTitleConversion( $x = true ) {
483 return wfSetVar( $this->mDisableTitleConversion, $x );
484 }
485
486 public function setUserLang( $x ) {
487 if ( is_string( $x ) ) {
488 $x = Language::factory( $x );
489 }
490
491 return wfSetVar( $this->mUserLang, $x );
492 }
493
494 public function setThumbSize( $x ) {
495 return wfSetVar( $this->mThumbSize, $x );
496 }
497
498 public function setStubThreshold( $x ) {
499 return wfSetVar( $this->mStubThreshold, $x );
500 }
501
502 public function setPreSaveTransform( $x ) {
503 return wfSetVar( $this->mPreSaveTransform, $x );
504 }
505
506 public function setIsPreview( $x ) {
507 return wfSetVar( $this->mIsPreview, $x );
508 }
509
510 public function setIsSectionPreview( $x ) {
511 return wfSetVar( $this->mIsSectionPreview, $x );
512 }
513
514 public function setIsPrintable( $x ) {
515 return wfSetVar( $this->mIsPrintable, $x );
516 }
517
518 /**
519 * Extra key that should be present in the parser cache key.
520 * @param string $key
521 */
522 public function addExtraKey( $key ) {
523 $this->mExtraKey .= '!' . $key;
524 }
525
526 /**
527 * Constructor
528 * @param User $user
529 * @param Language $lang
530 */
531 public function __construct( $user = null, $lang = null ) {
532 if ( $user === null ) {
533 global $wgUser;
534 if ( $wgUser === null ) {
535 $user = new User;
536 } else {
537 $user = $wgUser;
538 }
539 }
540 if ( $lang === null ) {
541 global $wgLang;
542 if ( !StubObject::isRealObject( $wgLang ) ) {
543 $wgLang->_unstub();
544 }
545 $lang = $wgLang;
546 }
547 $this->initialiseFromUser( $user, $lang );
548 }
549
550 /**
551 * Get a ParserOptions object from a given user.
552 * Language will be taken from $wgLang.
553 *
554 * @param User $user
555 * @return ParserOptions
556 */
557 public static function newFromUser( $user ) {
558 return new ParserOptions( $user );
559 }
560
561 /**
562 * Get a ParserOptions object from a given user and language
563 *
564 * @param User $user
565 * @param Language $lang
566 * @return ParserOptions
567 */
568 public static function newFromUserAndLang( User $user, Language $lang ) {
569 return new ParserOptions( $user, $lang );
570 }
571
572 /**
573 * Get a ParserOptions object from a IContextSource object
574 *
575 * @param IContextSource $context
576 * @return ParserOptions
577 */
578 public static function newFromContext( IContextSource $context ) {
579 return new ParserOptions( $context->getUser(), $context->getLanguage() );
580 }
581
582 /**
583 * Get user options
584 *
585 * @param User $user
586 * @param Language $lang
587 */
588 private function initialiseFromUser( $user, $lang ) {
589 global $wgInterwikiMagic, $wgAllowExternalImages,
590 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
591 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
592 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
593 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
594
595 wfProfileIn( __METHOD__ );
596
597 $this->mInterwikiMagic = $wgInterwikiMagic;
598 $this->mAllowExternalImages = $wgAllowExternalImages;
599 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
600 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
601 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
602 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
603 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
604 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
605 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
606 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
607 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
608 $this->mCleanSignatures = $wgCleanSignatures;
609 $this->mExternalLinkTarget = $wgExternalLinkTarget;
610 $this->mDisableContentConversion = $wgDisableLangConversion;
611 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
612
613 $this->mUser = $user;
614 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
615 $this->mThumbSize = $user->getOption( 'thumbsize' );
616 $this->mStubThreshold = $user->getStubThreshold();
617 $this->mUserLang = $lang;
618
619 wfProfileOut( __METHOD__ );
620 }
621
622 /**
623 * Registers a callback for tracking which ParserOptions which are used.
624 * This is a private API with the parser.
625 * @param callable $callback
626 */
627 public function registerWatcher( $callback ) {
628 $this->onAccessCallback = $callback;
629 }
630
631 /**
632 * Called when an option is accessed.
633 * @param string $optionName Name of the option
634 */
635 public function optionUsed( $optionName ) {
636 if ( $this->onAccessCallback ) {
637 call_user_func( $this->onAccessCallback, $optionName );
638 }
639 }
640
641 /**
642 * Returns the full array of options that would have been used by
643 * in 1.16.
644 * Used to get the old parser cache entries when available.
645 * @return array
646 */
647 public static function legacyOptions() {
648 return array(
649 'stubthreshold',
650 'numberheadings',
651 'userlang',
652 'thumbsize',
653 'editsection',
654 'printable'
655 );
656 }
657
658 /**
659 * Generate a hash string with the values set on these ParserOptions
660 * for the keys given in the array.
661 * This will be used as part of the hash key for the parser cache,
662 * so users sharing the options with vary for the same page share
663 * the same cached data safely.
664 *
665 * Extensions which require it should install 'PageRenderingHash' hook,
666 * which will give them a chance to modify this key based on their own
667 * settings.
668 *
669 * @since 1.17
670 * @param array $forOptions
671 * @param Title $title Used to get the content language of the page (since r97636)
672 * @return string Page rendering hash
673 */
674 public function optionsHash( $forOptions, $title = null ) {
675 global $wgRenderHashAppend;
676
677 // FIXME: Once the cache key is reorganized this argument
678 // can be dropped. It was used when the math extension was
679 // part of core.
680 $confstr = '*';
681
682 // Space assigned for the stubthreshold but unused
683 // since it disables the parser cache, its value will always
684 // be 0 when this function is called by parsercache.
685 if ( in_array( 'stubthreshold', $forOptions ) ) {
686 $confstr .= '!' . $this->mStubThreshold;
687 } else {
688 $confstr .= '!*';
689 }
690
691 if ( in_array( 'dateformat', $forOptions ) ) {
692 $confstr .= '!' . $this->getDateFormat();
693 }
694
695 if ( in_array( 'numberheadings', $forOptions ) ) {
696 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
697 } else {
698 $confstr .= '!*';
699 }
700
701 if ( in_array( 'userlang', $forOptions ) ) {
702 $confstr .= '!' . $this->mUserLang->getCode();
703 } else {
704 $confstr .= '!*';
705 }
706
707 if ( in_array( 'thumbsize', $forOptions ) ) {
708 $confstr .= '!' . $this->mThumbSize;
709 } else {
710 $confstr .= '!*';
711 }
712
713 // add in language specific options, if any
714 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
715 if ( !is_null( $title ) ) {
716 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
717 } else {
718 global $wgContLang;
719 $confstr .= $wgContLang->getExtraHashOptions();
720 }
721
722 $confstr .= $wgRenderHashAppend;
723
724 if ( !in_array( 'editsection', $forOptions ) ) {
725 $confstr .= '!*';
726 } elseif ( !$this->mEditSection ) {
727 $confstr .= '!edit=0';
728 }
729
730 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
731 $confstr .= '!printable=1';
732 }
733
734 if ( $this->mExtraKey != '' ) {
735 $confstr .= $this->mExtraKey;
736 }
737
738 // Give a chance for extensions to modify the hash, if they have
739 // extra options or other effects on the parser cache.
740 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) );
741
742 // Make it a valid memcached key fragment
743 $confstr = str_replace( ' ', '_', $confstr );
744
745 return $confstr;
746 }
747 }