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