Followup r85244; Define all methods as static, implement a DummyLinker to forward...
[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 $mDateFormat = null; # Date format index
22 var $mEditSection = true; # Create "edit section" links
23 var $mAllowSpecialInclusion; # Allow inclusion of special pages
24 var $mTidy = false; # Ask for tidy cleanup
25 var $mInterfaceMessage = false; # Which lang to call for PLURAL and GRAMMAR
26 var $mTargetLanguage = null; # Overrides above setting with arbitrary language
27 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
28 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
29 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
30 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
31 var $mRemoveComments = true; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
32 var $mTemplateCallback = # Callback for template fetching
33 array( 'Parser', 'statelessFetchTemplate' );
34 var $mEnableLimitReport = false; # Enable limit report in an HTML comment on output
35 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
36 var $mExternalLinkTarget; # Target attribute for external links
37 var $mCleanSignatures; #
38 var $mPreSaveTransform = true; # Transform wiki markup when saving the page.
39
40 var $mNumberHeadings; # Automatically number headings
41 var $mMath; # User math preference (as integer)
42 var $mThumbSize; # Thumb size preferred by the user.
43 private $mStubThreshold; # Maximum article size of an article to be marked as "stub"
44 var $mUserLang; # Language code of the User language.
45
46 /**
47 * @var User
48 */
49 var $mUser; # Stored user object
50 var $mIsPreview = false; # Parsing the page for a "preview" operation
51 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
52 var $mIsPrintable = false; # Parsing the printable version of the page
53
54 var $mExtraKey = ''; # Extra key that should be present in the caching key.
55
56 protected $onAccessCallback = null;
57
58 function getUseDynamicDates() { return $this->mUseDynamicDates; }
59 function getInterwikiMagic() { return $this->mInterwikiMagic; }
60 function getAllowExternalImages() { return $this->mAllowExternalImages; }
61 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
62 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
63 function getEditSection() { return $this->mEditSection; }
64 function getNumberHeadings() { $this->optionUsed('numberheadings');
65 return $this->mNumberHeadings; }
66 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
67 function getTidy() { return $this->mTidy; }
68 function getInterfaceMessage() { return $this->mInterfaceMessage; }
69 function getTargetLanguage() { return $this->mTargetLanguage; }
70 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
71 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
72 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
73 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
74 function getRemoveComments() { return $this->mRemoveComments; }
75 function getTemplateCallback() { return $this->mTemplateCallback; }
76 function getEnableLimitReport() { return $this->mEnableLimitReport; }
77 function getCleanSignatures() { return $this->mCleanSignatures; }
78 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
79 function getMath() { $this->optionUsed('math');
80 return $this->mMath; }
81 function getThumbSize() { $this->optionUsed('thumbsize');
82 return $this->mThumbSize; }
83 function getStubThreshold() { $this->optionUsed('stubthreshold');
84 return $this->mStubThreshold; }
85
86 function getIsPreview() { return $this->mIsPreview; }
87 function getIsSectionPreview() { return $this->mIsSectionPreview; }
88 function getIsPrintable() { $this->optionUsed('printable');
89 return $this->mIsPrintable; }
90 function getUser() { return $this->mUser; }
91 function getPreSaveTransform() { return $this->mPreSaveTransform; }
92
93 function getDateFormat() {
94 $this->optionUsed('dateformat');
95 if ( !isset( $this->mDateFormat ) ) {
96 $this->mDateFormat = $this->mUser->getDatePreference();
97 }
98 return $this->mDateFormat;
99 }
100
101 function getTimestamp() {
102 if ( !isset( $this->mTimestamp ) ) {
103 $this->mTimestamp = wfTimestampNow();
104 }
105 return $this->mTimestamp;
106 }
107
108 /**
109 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
110 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
111 * producing inconsistent tables (Bug 14404).
112 */
113 function getUserLang() {
114 $this->optionUsed('userlang');
115 return $this->mUserLang;
116 }
117
118 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
119 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
120 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
121 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
122 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
123 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
124 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
125 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
126 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
127 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
128 function setSkin( $x ) { $this->mSkin = $x; }
129 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
130 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
131 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
132 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
133 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
134 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
135 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
136 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
137 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
138 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
139 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
140 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
141 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
142 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
143 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
144 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
145
146 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
147 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
148 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
149
150 /**
151 * Extra key that should be present in the parser cache key.
152 */
153 function addExtraKey( $key ) {
154 $this->mExtraKey .= '!' . $key;
155 }
156
157 function __construct( $user = null ) {
158 $this->initialiseFromUser( $user );
159 }
160
161 /**
162 * Get parser options
163 *
164 * @param $user User object
165 * @return ParserOptions object
166 */
167 static function newFromUser( $user ) {
168 return new ParserOptions( $user );
169 }
170
171 /** Get user options */
172 function initialiseFromUser( $userInput ) {
173 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
174 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
175 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
176 global $wgExternalLinkTarget, $wgLang;
177
178 wfProfileIn( __METHOD__ );
179
180 if ( !$userInput ) {
181 global $wgUser;
182 if ( isset( $wgUser ) ) {
183 $user = $wgUser;
184 } else {
185 $user = new User;
186 }
187 } else {
188 $user =& $userInput;
189 }
190
191 $this->mUser = $user;
192
193 $this->mUseDynamicDates = $wgUseDynamicDates;
194 $this->mInterwikiMagic = $wgInterwikiMagic;
195 $this->mAllowExternalImages = $wgAllowExternalImages;
196 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
197 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
198 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
199 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
200 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
201 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
202 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
203 $this->mCleanSignatures = $wgCleanSignatures;
204 $this->mExternalLinkTarget = $wgExternalLinkTarget;
205
206 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
207 $this->mMath = $user->getOption( 'math' );
208 $this->mThumbSize = $user->getOption( 'thumbsize' );
209 $this->mStubThreshold = $user->getStubThreshold();
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 if ( in_array( 'stubthreshold', $forOptions ) )
277 $confstr .= '!' . $this->mStubThreshold;
278 else
279 $confstr .= '!*' ;
280
281 if ( in_array( 'dateformat', $forOptions ) )
282 $confstr .= '!' . $this->getDateFormat();
283
284 if ( in_array( 'numberheadings', $forOptions ) )
285 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
286 else
287 $confstr .= '!*';
288
289 if ( in_array( 'userlang', $forOptions ) )
290 $confstr .= '!' . $this->mUserLang;
291 else
292 $confstr .= '!*';
293
294 if ( in_array( 'thumbsize', $forOptions ) )
295 $confstr .= '!' . $this->mThumbSize;
296 else
297 $confstr .= '!*';
298
299 // add in language specific options, if any
300 // FIXME: This is just a way of retrieving the url/user preferred variant
301 $confstr .= $wgContLang->getExtraHashOptions();
302
303 $confstr .= $wgRenderHashAppend;
304
305 if ( !in_array( 'editsection', $forOptions ) ) {
306 $confstr .= '!*';
307 } elseif ( !$this->mEditSection ) {
308 $confstr .= '!edit=0';
309 }
310
311 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
312 $confstr .= '!printable=1';
313
314 if ( $this->mExtraKey != '' )
315 $confstr .= $this->mExtraKey;
316
317 // Give a chance for extensions to modify the hash, if they have
318 // extra options or other effects on the parser cache.
319 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
320
321 // Make it a valid memcached key fragment
322 $confstr = str_replace( ' ', '_', $confstr );
323
324 return $confstr;
325 }
326 }