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