Merge "Re-remove experiment code for moduleStorage"
[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 * User math preference (as integer). Not used (1.19)
169 */
170 var $mMath;
171
172 /**
173 * Thumb size preferred by the user.
174 */
175 var $mThumbSize;
176
177 /**
178 * Maximum article size of an article to be marked as "stub"
179 */
180 private $mStubThreshold;
181
182 /**
183 * Language object of the User language.
184 */
185 var $mUserLang;
186
187 /**
188 * @var User
189 * Stored user object
190 */
191 var $mUser;
192
193 /**
194 * Parsing the page for a "preview" operation?
195 */
196 var $mIsPreview = false;
197
198 /**
199 * Parsing the page for a "preview" operation on a single section?
200 */
201 var $mIsSectionPreview = false;
202
203 /**
204 * Parsing the printable version of the page?
205 */
206 var $mIsPrintable = false;
207
208 /**
209 * Extra key that should be present in the caching key.
210 */
211 var $mExtraKey = '';
212
213 /**
214 * Function to be called when an option is accessed.
215 */
216 protected $onAccessCallback = null;
217
218 function getInterwikiMagic() { return $this->mInterwikiMagic; }
219 function getAllowExternalImages() { return $this->mAllowExternalImages; }
220 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
221 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
222 function getEditSection() { return $this->mEditSection; }
223 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
224 return $this->mNumberHeadings; }
225 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
226 function getTidy() { return $this->mTidy; }
227 function getInterfaceMessage() { return $this->mInterfaceMessage; }
228 function getTargetLanguage() { return $this->mTargetLanguage; }
229 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
230 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
231 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount; }
232 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
233 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
234 /* @since 1.20 */
235 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit; }
236 function getRemoveComments() { return $this->mRemoveComments; }
237 function getTemplateCallback() { return $this->mTemplateCallback; }
238 function getEnableLimitReport() { return $this->mEnableLimitReport; }
239 function getCleanSignatures() { return $this->mCleanSignatures; }
240 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
241 function getDisableContentConversion() { return $this->mDisableContentConversion; }
242 function getDisableTitleConversion() { return $this->mDisableTitleConversion; }
243 /** @deprecated since 1.22 use User::getOption('math') instead */
244 function getMath() { $this->optionUsed( 'math' );
245 return $this->mMath; }
246 function getThumbSize() { $this->optionUsed( 'thumbsize' );
247 return $this->mThumbSize; }
248 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
249 return $this->mStubThreshold; }
250
251 function getIsPreview() { return $this->mIsPreview; }
252 function getIsSectionPreview() { return $this->mIsSectionPreview; }
253 function getIsPrintable() { $this->optionUsed( 'printable' );
254 return $this->mIsPrintable; }
255 function getUser() { return $this->mUser; }
256 function getPreSaveTransform() { return $this->mPreSaveTransform; }
257
258 function getDateFormat() {
259 $this->optionUsed( 'dateformat' );
260 if ( !isset( $this->mDateFormat ) ) {
261 $this->mDateFormat = $this->mUser->getDatePreference();
262 }
263 return $this->mDateFormat;
264 }
265
266 function getTimestamp() {
267 if ( !isset( $this->mTimestamp ) ) {
268 $this->mTimestamp = wfTimestampNow();
269 }
270 return $this->mTimestamp;
271 }
272
273 /**
274 * Get the user language used by the parser for this page.
275 *
276 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
277 *
278 * To avoid side-effects where the page will be rendered based on the language
279 * of the user who last saved, this function will triger a cache fragmentation.
280 * Usage of this method is discouraged for that reason.
281 *
282 * When saving, this will return the default language instead of the user's.
283 *
284 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
285 *
286 * @return Language object
287 * @since 1.19
288 */
289 function getUserLangObj() {
290 $this->optionUsed( 'userlang' );
291 return $this->mUserLang;
292 }
293
294 /**
295 * Same as getUserLangObj() but returns a string instead.
296 *
297 * @return String Language code
298 * @since 1.17
299 */
300 function getUserLang() {
301 return $this->getUserLangObj()->getCode();
302 }
303
304 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
305 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
306 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
307 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
308 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
309 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
310 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
311 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
312 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); }
313
314 /** @deprecated in 1.19 */
315 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); }
316 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); }
317 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); }
318 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
319 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
320 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); }
321 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
322 /* @since 1.20 */
323 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); }
324 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
325 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
326 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
327 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
328 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
329 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
330 function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion, $x ); }
331 function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion, $x ); }
332 /** @deprecated since 1.22 */
333 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
334 function setUserLang( $x ) {
335 if ( is_string( $x ) ) {
336 $x = Language::factory( $x );
337 }
338 return wfSetVar( $this->mUserLang, $x );
339 }
340 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
341 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
342 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
343
344 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
345 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
346 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
347
348 /**
349 * Extra key that should be present in the parser cache key.
350 */
351 function addExtraKey( $key ) {
352 $this->mExtraKey .= '!' . $key;
353 }
354
355 /**
356 * Constructor
357 * @param $user User object
358 * @param $lang Language object
359 */
360 function __construct( $user = null, $lang = null ) {
361 if ( $user === null ) {
362 global $wgUser;
363 if ( $wgUser === null ) {
364 $user = new User;
365 } else {
366 $user = $wgUser;
367 }
368 }
369 if ( $lang === null ) {
370 global $wgLang;
371 if ( !StubObject::isRealObject( $wgLang ) ) {
372 $wgLang->_unstub();
373 }
374 $lang = $wgLang;
375 }
376 $this->initialiseFromUser( $user, $lang );
377 }
378
379 /**
380 * Get a ParserOptions object from a given user.
381 * Language will be taken from $wgLang.
382 *
383 * @param $user User object
384 * @return ParserOptions object
385 */
386 public static function newFromUser( $user ) {
387 return new ParserOptions( $user );
388 }
389
390 /**
391 * Get a ParserOptions object from a given user and language
392 *
393 * @param $user User object
394 * @param $lang Language object
395 * @return ParserOptions object
396 */
397 public static function newFromUserAndLang( User $user, Language $lang ) {
398 return new ParserOptions( $user, $lang );
399 }
400
401 /**
402 * Get a ParserOptions object from a IContextSource object
403 *
404 * @param $context IContextSource object
405 * @return ParserOptions object
406 */
407 public static function newFromContext( IContextSource $context ) {
408 return new ParserOptions( $context->getUser(), $context->getLanguage() );
409 }
410
411 /**
412 * Get user options
413 *
414 * @param $user User object
415 * @param $lang Language object
416 */
417 private function initialiseFromUser( $user, $lang ) {
418 global $wgInterwikiMagic, $wgAllowExternalImages,
419 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
420 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
421 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
422 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
423
424 wfProfileIn( __METHOD__ );
425
426 $this->mInterwikiMagic = $wgInterwikiMagic;
427 $this->mAllowExternalImages = $wgAllowExternalImages;
428 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
429 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
430 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
431 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
432 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
433 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
434 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
435 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
436 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
437 $this->mCleanSignatures = $wgCleanSignatures;
438 $this->mExternalLinkTarget = $wgExternalLinkTarget;
439 $this->mDisableContentConversion = $wgDisableLangConversion;
440 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
441
442 $this->mUser = $user;
443 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
444 $this->mMath = $user->getOption( 'math' );
445 $this->mThumbSize = $user->getOption( 'thumbsize' );
446 $this->mStubThreshold = $user->getStubThreshold();
447 $this->mUserLang = $lang;
448
449 wfProfileOut( __METHOD__ );
450 }
451
452 /**
453 * Registers a callback for tracking which ParserOptions which are used.
454 * This is a private API with the parser.
455 */
456 function registerWatcher( $callback ) {
457 $this->onAccessCallback = $callback;
458 }
459
460 /**
461 * Called when an option is accessed.
462 */
463 protected function optionUsed( $optionName ) {
464 if ( $this->onAccessCallback ) {
465 call_user_func( $this->onAccessCallback, $optionName );
466 }
467 }
468
469 /**
470 * Returns the full array of options that would have been used by
471 * in 1.16.
472 * Used to get the old parser cache entries when available.
473 * @return array
474 */
475 public static function legacyOptions() {
476 return array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
477 }
478
479 /**
480 * Generate a hash string with the values set on these ParserOptions
481 * for the keys given in the array.
482 * This will be used as part of the hash key for the parser cache,
483 * so users sharign the options with vary for the same page share
484 * the same cached data safely.
485 *
486 * Extensions which require it should install 'PageRenderingHash' hook,
487 * which will give them a chance to modify this key based on their own
488 * settings.
489 *
490 * @since 1.17
491 * @param $forOptions Array
492 * @param $title Title: used to get the content language of the page (since r97636)
493 * @return string Page rendering hash
494 */
495 public function optionsHash( $forOptions, $title = null ) {
496 global $wgRenderHashAppend;
497
498 $confstr = '';
499
500 if ( in_array( 'math', $forOptions ) ) {
501 $confstr .= $this->mMath;
502 } else {
503 $confstr .= '*';
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
562 // Give a chance for extensions to modify the hash, if they have
563 // extra options or other effects on the parser cache.
564 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser() ) );
565
566 // Make it a valid memcached key fragment
567 $confstr = str_replace( ' ', '_', $confstr );
568
569 return $confstr;
570 }
571 }