Tweak for r79018: fatal errors due to something calling parser clearState when mOptio...
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2 /**
3 * Options for the PHP parser
4 *
5 * @file
6 * @ingroup Parser
7 */
8
9 /**
10 * Set options of the Parser
11 * @todo document
12 * @ingroup Parser
13 */
14 class ParserOptions {
15 # All variables are supposed to be private in theory, although in practise this is not the case.
16 var $mUseDynamicDates; # Use DateFormatter to format dates
17 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
18 var $mAllowExternalImages; # Allow external images inline
19 var $mAllowExternalImagesFrom; # If not, any exception?
20 var $mEnableImageWhitelist; # If not or it doesn't match, should we check an on-wiki whitelist?
21 var $mSkin = null; # Reference to the preferred skin
22 var $mDateFormat = null; # Date format index
23 var $mEditSection = true; # Create "edit section" links
24 var $mAllowSpecialInclusion; # Allow inclusion of special pages
25 var $mTidy = false; # Ask for tidy cleanup
26 var $mInterfaceMessage = false; # Which lang to call for PLURAL and GRAMMAR
27 var $mTargetLanguage = null; # Overrides above setting with arbitrary language
28 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
29 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
30 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
31 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
32 var $mRemoveComments = true; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
33 var $mTemplateCallback = # Callback for template fetching
34 array( 'Parser', 'statelessFetchTemplate' );
35 var $mEnableLimitReport = false; # Enable limit report in an HTML comment on output
36 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
37 var $mExternalLinkTarget; # Target attribute for external links
38 var $mCleanSignatures; #
39 var $mPreSaveTransform = true; # Transform wiki markup when saving the page.
40
41 var $mNumberHeadings; # Automatically number headings
42 var $mMath; # User math preference (as integer)
43 var $mThumbSize; # Thumb size preferred by the user.
44 var $mUserLang; # Language code of the User language.
45
46 var $mUser; # Stored user object
47 var $mIsPreview = false; # Parsing the page for a "preview" operation
48 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
49 var $mIsPrintable = false; # Parsing the printable version of the page
50
51 var $mExtraKey = ''; # Extra key that should be present in the caching key.
52
53 protected $onAccessCallback = null;
54
55 function getUseDynamicDates() { return $this->mUseDynamicDates; }
56 function getInterwikiMagic() { return $this->mInterwikiMagic; }
57 function getAllowExternalImages() { return $this->mAllowExternalImages; }
58 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
59 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
60 function getEditSection() { return $this->mEditSection; }
61 function getNumberHeadings() { $this->optionUsed('numberheadings');
62 return $this->mNumberHeadings; }
63 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
64 function getTidy() { return $this->mTidy; }
65 function getInterfaceMessage() { return $this->mInterfaceMessage; }
66 function getTargetLanguage() { return $this->mTargetLanguage; }
67 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
68 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
69 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
70 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
71 function getRemoveComments() { return $this->mRemoveComments; }
72 function getTemplateCallback() { return $this->mTemplateCallback; }
73 function getEnableLimitReport() { return $this->mEnableLimitReport; }
74 function getCleanSignatures() { return $this->mCleanSignatures; }
75 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
76 function getMath() { $this->optionUsed('math');
77 return $this->mMath; }
78 function getThumbSize() { $this->optionUsed('thumbsize');
79 return $this->mThumbSize; }
80
81 function getIsPreview() { return $this->mIsPreview; }
82 function getIsSectionPreview() { return $this->mIsSectionPreview; }
83 function getIsPrintable() { $this->optionUsed('printable');
84 return $this->mIsPrintable; }
85 function getUser() { return $this->mUser; }
86 function getPreSaveTransform() { return $this->mPreSaveTransform; }
87
88 function getSkin( $title = null ) {
89 if ( !isset( $this->mSkin ) ) {
90 $this->mSkin = $this->mUser->getSkin( $title );
91 }
92 return $this->mSkin;
93 }
94
95 function getDateFormat() {
96 $this->optionUsed('dateformat');
97 if ( !isset( $this->mDateFormat ) ) {
98 $this->mDateFormat = $this->mUser->getDatePreference();
99 }
100 return $this->mDateFormat;
101 }
102
103 function getTimestamp() {
104 if ( !isset( $this->mTimestamp ) ) {
105 $this->mTimestamp = wfTimestampNow();
106 }
107 return $this->mTimestamp;
108 }
109
110 /**
111 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
112 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
113 * producing inconsistent tables (Bug 14404).
114 */
115 function getUserLang() {
116 $this->optionUsed('userlang');
117 return $this->mUserLang;
118 }
119
120 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
121 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
122 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
123 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
124 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
125 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
126 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
127 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
128 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
129 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
130 function setSkin( $x ) { $this->mSkin = $x; }
131 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
132 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
133 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
134 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
135 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
136 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
137 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
138 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
139 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
140 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
141 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
142 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
143 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
144 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
145 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
146
147 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
148 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
149 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
150
151 /**
152 * Extra key that should be present in the parser cache key.
153 */
154 function addExtraKey( $key ) {
155 $this->mExtraKey .= '!' . $key;
156 }
157
158 function __construct( $user = null ) {
159 $this->initialiseFromUser( $user );
160 }
161
162 /**
163 * Get parser options
164 *
165 * @param $user User object
166 * @return ParserOptions object
167 */
168 static function newFromUser( $user ) {
169 return new ParserOptions( $user );
170 }
171
172 /** Get user options */
173 function initialiseFromUser( $userInput ) {
174 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
175 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
176 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
177 global $wgExternalLinkTarget, $wgLang;
178
179 wfProfileIn( __METHOD__ );
180
181 if ( !$userInput ) {
182 global $wgUser;
183 if ( isset( $wgUser ) ) {
184 $user = $wgUser;
185 } else {
186 $user = new User;
187 }
188 } else {
189 $user =& $userInput;
190 }
191
192 $this->mUser = $user;
193
194 $this->mUseDynamicDates = $wgUseDynamicDates;
195 $this->mInterwikiMagic = $wgInterwikiMagic;
196 $this->mAllowExternalImages = $wgAllowExternalImages;
197 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
198 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
199 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
200 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
201 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
202 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
203 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
204 $this->mCleanSignatures = $wgCleanSignatures;
205 $this->mExternalLinkTarget = $wgExternalLinkTarget;
206
207 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
208 $this->mMath = $user->getOption( 'math' );
209 $this->mThumbSize = $user->getOption( 'thumbsize' );
210 $this->mUserLang = $wgLang->getCode();
211
212 wfProfileOut( __METHOD__ );
213 }
214
215 /**
216 * Registers a callback for tracking which ParserOptions which are used.
217 * This is a private API with the parser.
218 */
219 function registerWatcher( $callback ) {
220 $this->onAccessCallback = $callback;
221 }
222
223 /**
224 * Called when an option is accessed.
225 */
226 protected function optionUsed( $optionName ) {
227 if ( $this->onAccessCallback ) {
228 call_user_func( $this->onAccessCallback, $optionName );
229 }
230 }
231
232 /**
233 * Returns the full array of options that would have been used by
234 * in 1.16.
235 * Used to get the old parser cache entries when available.
236 */
237 public static function legacyOptions() {
238 global $wgUseDynamicDates;
239 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
240 if ( $wgUseDynamicDates ) {
241 $legacyOpts[] = 'dateformat';
242 }
243 return $legacyOpts;
244 }
245
246 /**
247 * Generate a hash string with the values set on these ParserOptions
248 * for the keys given in the array.
249 * This will be used as part of the hash key for the parser cache,
250 * so users sharign the options with vary for the same page share
251 * the same cached data safely.
252 *
253 * Replaces User::getPageRenderingHash()
254 *
255 * Extensions which require it should install 'PageRenderingHash' hook,
256 * which will give them a chance to modify this key based on their own
257 * settings.
258 *
259 * @since 1.17
260 * @return \string Page rendering hash
261 */
262 public function optionsHash( $forOptions ) {
263 global $wgContLang, $wgRenderHashAppend;
264
265 $confstr = '';
266
267 if ( in_array( 'math', $forOptions ) )
268 $confstr .= $this->mMath;
269 else
270 $confstr .= '*';
271
272
273 // Space assigned for the stubthreshold but unused
274 // since it disables the parser cache, its value will always
275 // be 0 when this function is called by parsercache.
276 // The conditional is here to avoid a confusing 0
277 if ( in_array( 'stubthreshold', $forOptions ) )
278 $confstr .= '!0' ;
279 else
280 $confstr .= '!*' ;
281
282 if ( in_array( 'dateformat', $forOptions ) )
283 $confstr .= '!' . $this->getDateFormat();
284
285 if ( in_array( 'numberheadings', $forOptions ) )
286 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
287 else
288 $confstr .= '!*';
289
290 if ( in_array( 'userlang', $forOptions ) )
291 $confstr .= '!' . $this->mUserLang;
292 else
293 $confstr .= '!*';
294
295 if ( in_array( 'thumbsize', $forOptions ) )
296 $confstr .= '!' . $this->mThumbSize;
297 else
298 $confstr .= '!*';
299
300 // add in language specific options, if any
301 // FIXME: This is just a way of retrieving the url/user preferred variant
302 $confstr .= $wgContLang->getExtraHashOptions();
303
304 // Since the skin could be overloading link(), it should be
305 // included here but in practice, none of our skins do that.
306 // $confstr .= "!" . $this->mSkin->getSkinName();
307
308 $confstr .= $wgRenderHashAppend;
309
310 if ( !in_array( 'editsection', $forOptions ) ) {
311 $confstr .= '!*';
312 } elseif ( !$this->mEditSection ) {
313 $confstr .= '!edit=0';
314 }
315
316 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
317 $confstr .= '!printable=1';
318
319 if ( $this->mExtraKey != '' )
320 $confstr .= $this->mExtraKey;
321
322 // Give a chance for extensions to modify the hash, if they have
323 // extra options or other effects on the parser cache.
324 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
325
326 // Make it a valid memcached key fragment
327 $confstr = str_replace( ' ', '_', $confstr );
328
329 return $confstr;
330 }
331 }