Merge "(bug 40257) action=info no longer shows subpages where disabled"
[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 * Use DateFormatter to format dates
35 */
36 var $mUseDynamicDates;
37
38 /**
39 * Interlanguage links are removed and returned in an array
40 */
41 var $mInterwikiMagic;
42
43 /**
44 * Allow external images inline?
45 */
46 var $mAllowExternalImages;
47
48 /**
49 * If not, any exception?
50 */
51 var $mAllowExternalImagesFrom;
52
53 /**
54 * If not or it doesn't match, should we check an on-wiki whitelist?
55 */
56 var $mEnableImageWhitelist;
57
58 /**
59 * Date format index
60 */
61 var $mDateFormat = null;
62
63 /**
64 * Create "edit section" links?
65 */
66 var $mEditSection = true;
67
68 /**
69 * Allow inclusion of special pages?
70 */
71 var $mAllowSpecialInclusion;
72
73 /**
74 * Use tidy to cleanup output HTML?
75 */
76 var $mTidy = false;
77
78 /**
79 * Which lang to call for PLURAL and GRAMMAR
80 */
81 var $mInterfaceMessage = false;
82
83 /**
84 * Overrides $mInterfaceMessage with arbitrary language
85 */
86 var $mTargetLanguage = null;
87
88 /**
89 * Maximum size of template expansions, in bytes
90 */
91 var $mMaxIncludeSize;
92
93 /**
94 * Maximum number of nodes touched by PPFrame::expand()
95 */
96 var $mMaxPPNodeCount;
97
98 /**
99 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
100 */
101 var $mMaxGeneratedPPNodeCount;
102
103 /**
104 * Maximum recursion depth in PPFrame::expand()
105 */
106 var $mMaxPPExpandDepth;
107
108 /**
109 * Maximum recursion depth for templates within templates
110 */
111 var $mMaxTemplateDepth;
112
113 /**
114 * Maximum number of calls per parse to expensive parser functions
115 */
116 var $mExpensiveParserFunctionLimit;
117
118 /**
119 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
120 */
121 var $mRemoveComments = true;
122
123 /**
124 * Callback for template fetching. Used as first argument to call_user_func().
125 */
126 var $mTemplateCallback =
127 array( 'Parser', 'statelessFetchTemplate' );
128
129 /**
130 * Enable limit report in an HTML comment on output
131 */
132 var $mEnableLimitReport = false;
133
134 /**
135 * Timestamp used for {{CURRENTDAY}} etc.
136 */
137 var $mTimestamp;
138
139 /**
140 * Target attribute for external links
141 */
142 var $mExternalLinkTarget;
143
144 /**
145 * Clean up signature texts?
146 *
147 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
148 * 2) Substitute all transclusions
149 */
150 var $mCleanSignatures;
151
152 /**
153 * Transform wiki markup when saving the page?
154 */
155 var $mPreSaveTransform = true;
156
157 /**
158 * Automatically number headings?
159 */
160 var $mNumberHeadings;
161
162 /**
163 * User math preference (as integer). Not used (1.19)
164 */
165 var $mMath;
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 getUseDynamicDates() { return $this->mUseDynamicDates; }
214 function getInterwikiMagic() { return $this->mInterwikiMagic; }
215 function getAllowExternalImages() { return $this->mAllowExternalImages; }
216 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
217 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
218 function getEditSection() { return $this->mEditSection; }
219 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
220 return $this->mNumberHeadings; }
221 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
222 function getTidy() { return $this->mTidy; }
223 function getInterfaceMessage() { return $this->mInterfaceMessage; }
224 function getTargetLanguage() { return $this->mTargetLanguage; }
225 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
226 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
227 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount; }
228 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
229 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
230 /* @since 1.20 */
231 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit; }
232 function getRemoveComments() { return $this->mRemoveComments; }
233 function getTemplateCallback() { return $this->mTemplateCallback; }
234 function getEnableLimitReport() { return $this->mEnableLimitReport; }
235 function getCleanSignatures() { return $this->mCleanSignatures; }
236 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
237 function getMath() { $this->optionUsed( 'math' );
238 return $this->mMath; }
239 function getThumbSize() { $this->optionUsed( 'thumbsize' );
240 return $this->mThumbSize; }
241 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
242 return $this->mStubThreshold; }
243
244 function getIsPreview() { return $this->mIsPreview; }
245 function getIsSectionPreview() { return $this->mIsSectionPreview; }
246 function getIsPrintable() { $this->optionUsed( 'printable' );
247 return $this->mIsPrintable; }
248 function getUser() { return $this->mUser; }
249 function getPreSaveTransform() { return $this->mPreSaveTransform; }
250
251 /**
252 * @param $title Title
253 * @return Skin
254 * @deprecated since 1.18 Use Linker::* instead
255 */
256 function getSkin( $title = null ) {
257 wfDeprecated( __METHOD__, '1.18' );
258 return new DummyLinker;
259 }
260
261 function getDateFormat() {
262 $this->optionUsed( 'dateformat' );
263 if ( !isset( $this->mDateFormat ) ) {
264 $this->mDateFormat = $this->mUser->getDatePreference();
265 }
266 return $this->mDateFormat;
267 }
268
269 function getTimestamp() {
270 if ( !isset( $this->mTimestamp ) ) {
271 $this->mTimestamp = wfTimestampNow();
272 }
273 return $this->mTimestamp;
274 }
275
276 /**
277 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
278 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
279 * producing inconsistent tables (Bug 14404).
280 *
281 * @return Language object
282 * @since 1.19
283 */
284 function getUserLangObj() {
285 $this->optionUsed( 'userlang' );
286 return $this->mUserLang;
287 }
288
289 /**
290 * Same as getUserLangObj() but returns a string instead.
291 *
292 * @return String Language code
293 * @since 1.17
294 */
295 function getUserLang() {
296 return $this->getUserLangObj()->getCode();
297 }
298
299 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
300 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
301 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
302 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
303 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
304 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
305 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
306 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
307 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
308 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); }
309
310 /** @deprecated in 1.19; will be removed in 1.20 */
311 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); }
312 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); }
313 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); }
314 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
315 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
316 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); }
317 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
318 /* @since 1.20 */
319 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); }
320 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
321 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
322 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
323 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
324 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
325 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
326 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
327 function setUserLang( $x ) {
328 if ( is_string( $x ) ) {
329 $x = Language::factory( $x );
330 }
331 return wfSetVar( $this->mUserLang, $x );
332 }
333 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
334 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
335 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
336
337 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
338 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
339 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
340
341 /**
342 * Extra key that should be present in the parser cache key.
343 */
344 function addExtraKey( $key ) {
345 $this->mExtraKey .= '!' . $key;
346 }
347
348 /**
349 * Constructor
350 * @param $user User object
351 * @param $lang Language object
352 */
353 function __construct( $user = null, $lang = null ) {
354 if ( $user === null ) {
355 global $wgUser;
356 if ( $wgUser === null ) {
357 $user = new User;
358 } else {
359 $user = $wgUser;
360 }
361 }
362 if ( $lang === null ) {
363 global $wgLang;
364 if ( !StubObject::isRealObject( $wgLang ) ) {
365 $wgLang->_unstub();
366 }
367 $lang = $wgLang;
368 }
369 $this->initialiseFromUser( $user, $lang );
370 }
371
372 /**
373 * Get a ParserOptions object from a given user.
374 * Language will be taken from $wgLang.
375 *
376 * @param $user User object
377 * @return ParserOptions object
378 */
379 public static function newFromUser( $user ) {
380 return new ParserOptions( $user );
381 }
382
383 /**
384 * Get a ParserOptions object from a given user and language
385 *
386 * @param $user User object
387 * @param $lang Language object
388 * @return ParserOptions object
389 */
390 public static function newFromUserAndLang( User $user, Language $lang ) {
391 return new ParserOptions( $user, $lang );
392 }
393
394 /**
395 * Get a ParserOptions object from a IContextSource object
396 *
397 * @param $context IContextSource object
398 * @return ParserOptions object
399 */
400 public static function newFromContext( IContextSource $context ) {
401 return new ParserOptions( $context->getUser(), $context->getLanguage() );
402 }
403
404 /**
405 * Get user options
406 *
407 * @param $user User object
408 * @param $lang Language object
409 */
410 private function initialiseFromUser( $user, $lang ) {
411 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
412 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
413 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
414 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
415 $wgMaxGeneratedPPNodeCount;
416
417 wfProfileIn( __METHOD__ );
418
419 $this->mUseDynamicDates = $wgUseDynamicDates;
420 $this->mInterwikiMagic = $wgInterwikiMagic;
421 $this->mAllowExternalImages = $wgAllowExternalImages;
422 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
423 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
424 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
425 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
426 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
427 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
428 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
429 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
430 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
431 $this->mCleanSignatures = $wgCleanSignatures;
432 $this->mExternalLinkTarget = $wgExternalLinkTarget;
433
434 $this->mUser = $user;
435 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
436 $this->mMath = $user->getOption( 'math' );
437 $this->mThumbSize = $user->getOption( 'thumbsize' );
438 $this->mStubThreshold = $user->getStubThreshold();
439 $this->mUserLang = $lang;
440
441 wfProfileOut( __METHOD__ );
442 }
443
444 /**
445 * Registers a callback for tracking which ParserOptions which are used.
446 * This is a private API with the parser.
447 */
448 function registerWatcher( $callback ) {
449 $this->onAccessCallback = $callback;
450 }
451
452 /**
453 * Called when an option is accessed.
454 */
455 protected function optionUsed( $optionName ) {
456 if ( $this->onAccessCallback ) {
457 call_user_func( $this->onAccessCallback, $optionName );
458 }
459 }
460
461 /**
462 * Returns the full array of options that would have been used by
463 * in 1.16.
464 * Used to get the old parser cache entries when available.
465 * @return array
466 */
467 public static function legacyOptions() {
468 global $wgUseDynamicDates;
469 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
470 if ( $wgUseDynamicDates ) {
471 $legacyOpts[] = 'dateformat';
472 }
473 return $legacyOpts;
474 }
475
476 /**
477 * Generate a hash string with the values set on these ParserOptions
478 * for the keys given in the array.
479 * This will be used as part of the hash key for the parser cache,
480 * so users sharign the options with vary for the same page share
481 * the same cached data safely.
482 *
483 * Replaces User::getPageRenderingHash()
484 *
485 * Extensions which require it should install 'PageRenderingHash' hook,
486 * which will give them a chance to modify this key based on their own
487 * settings.
488 *
489 * @since 1.17
490 * @param $forOptions Array
491 * @param $title Title: used to get the content language of the page (since r97636)
492 * @return string Page rendering hash
493 */
494 public function optionsHash( $forOptions, $title = null ) {
495 global $wgRenderHashAppend;
496
497 $confstr = '';
498
499 if ( in_array( 'math', $forOptions ) ) {
500 $confstr .= $this->mMath;
501 } else {
502 $confstr .= '*';
503 }
504
505
506 // Space assigned for the stubthreshold but unused
507 // since it disables the parser cache, its value will always
508 // be 0 when this function is called by parsercache.
509 if ( in_array( 'stubthreshold', $forOptions ) ) {
510 $confstr .= '!' . $this->mStubThreshold;
511 } else {
512 $confstr .= '!*' ;
513 }
514
515 if ( in_array( 'dateformat', $forOptions ) ) {
516 $confstr .= '!' . $this->getDateFormat();
517 }
518
519 if ( in_array( 'numberheadings', $forOptions ) ) {
520 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
521 } else {
522 $confstr .= '!*';
523 }
524
525 if ( in_array( 'userlang', $forOptions ) ) {
526 $confstr .= '!' . $this->mUserLang->getCode();
527 } else {
528 $confstr .= '!*';
529 }
530
531 if ( in_array( 'thumbsize', $forOptions ) ) {
532 $confstr .= '!' . $this->mThumbSize;
533 } else {
534 $confstr .= '!*';
535 }
536
537 // add in language specific options, if any
538 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
539 if( !is_null( $title ) ) {
540 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
541 } else {
542 global $wgContLang;
543 $confstr .= $wgContLang->getExtraHashOptions();
544 }
545
546 $confstr .= $wgRenderHashAppend;
547
548 if ( !in_array( 'editsection', $forOptions ) ) {
549 $confstr .= '!*';
550 } elseif ( !$this->mEditSection ) {
551 $confstr .= '!edit=0';
552 }
553
554 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
555 $confstr .= '!printable=1';
556 }
557
558 if ( $this->mExtraKey != '' )
559 $confstr .= $this->mExtraKey;
560
561 // Give a chance for extensions to modify the hash, if they have
562 // extra options or other effects on the parser cache.
563 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
564
565 // Make it a valid memcached key fragment
566 $confstr = str_replace( ' ', '_', $confstr );
567
568 return $confstr;
569 }
570 }