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