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