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