Per Platonides; follow-up r78201: add "stubthreshold" to ParserOptions; also provides...
[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 private $mStubThreshold; # Maximum article size of an article to be marked as "stub"
45 var $mUserLang; # Language code of the User language.
46
47 /**
48 * @var User
49 */
50 var $mUser; # Stored user object
51 var $mIsPreview = false; # Parsing the page for a "preview" operation
52 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
53 var $mIsPrintable = false; # Parsing the printable version of the page
54
55 var $mExtraKey = ''; # Extra key that should be present in the caching key.
56
57 protected $onAccessCallback = null;
58
59 function getUseDynamicDates() { return $this->mUseDynamicDates; }
60 function getInterwikiMagic() { return $this->mInterwikiMagic; }
61 function getAllowExternalImages() { return $this->mAllowExternalImages; }
62 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
63 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
64 function getEditSection() { return $this->mEditSection; }
65 function getNumberHeadings() { $this->optionUsed('numberheadings');
66 return $this->mNumberHeadings; }
67 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
68 function getTidy() { return $this->mTidy; }
69 function getInterfaceMessage() { return $this->mInterfaceMessage; }
70 function getTargetLanguage() { return $this->mTargetLanguage; }
71 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
72 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
73 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
74 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
75 function getRemoveComments() { return $this->mRemoveComments; }
76 function getTemplateCallback() { return $this->mTemplateCallback; }
77 function getEnableLimitReport() { return $this->mEnableLimitReport; }
78 function getCleanSignatures() { return $this->mCleanSignatures; }
79 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
80 function getMath() { $this->optionUsed('math');
81 return $this->mMath; }
82 function getThumbSize() { $this->optionUsed('thumbsize');
83 return $this->mThumbSize; }
84 function getStubThreshold() { $this->optionUsed('stubthreshold');
85 return $this->mStubThreshold; }
86
87 function getIsPreview() { return $this->mIsPreview; }
88 function getIsSectionPreview() { return $this->mIsSectionPreview; }
89 function getIsPrintable() { $this->optionUsed('printable');
90 return $this->mIsPrintable; }
91 function getUser() { return $this->mUser; }
92 function getPreSaveTransform() { return $this->mPreSaveTransform; }
93
94 function getSkin( $title = null ) {
95 if ( !isset( $this->mSkin ) ) {
96 $this->mSkin = $this->mUser->getSkin( $title );
97 }
98 return $this->mSkin;
99 }
100
101 function getDateFormat() {
102 $this->optionUsed('dateformat');
103 if ( !isset( $this->mDateFormat ) ) {
104 $this->mDateFormat = $this->mUser->getDatePreference();
105 }
106 return $this->mDateFormat;
107 }
108
109 function getTimestamp() {
110 if ( !isset( $this->mTimestamp ) ) {
111 $this->mTimestamp = wfTimestampNow();
112 }
113 return $this->mTimestamp;
114 }
115
116 /**
117 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
118 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
119 * producing inconsistent tables (Bug 14404).
120 */
121 function getUserLang() {
122 $this->optionUsed('userlang');
123 return $this->mUserLang;
124 }
125
126 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
127 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
128 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
129 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
130 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
131 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
132 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
133 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
134 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
135 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
136 function setSkin( $x ) { $this->mSkin = $x; }
137 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
138 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
139 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
140 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
141 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
142 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
143 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
144 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
145 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
146 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
147 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
148 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
149 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
150 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
151 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
152 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
153
154 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
155 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
156 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
157
158 /**
159 * Extra key that should be present in the parser cache key.
160 */
161 function addExtraKey( $key ) {
162 $this->mExtraKey .= '!' . $key;
163 }
164
165 function __construct( $user = null ) {
166 $this->initialiseFromUser( $user );
167 }
168
169 /**
170 * Get parser options
171 *
172 * @param $user User object
173 * @return ParserOptions object
174 */
175 static function newFromUser( $user ) {
176 return new ParserOptions( $user );
177 }
178
179 /** Get user options */
180 function initialiseFromUser( $userInput ) {
181 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
182 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
183 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
184 global $wgExternalLinkTarget, $wgLang;
185
186 wfProfileIn( __METHOD__ );
187
188 if ( !$userInput ) {
189 global $wgUser;
190 if ( isset( $wgUser ) ) {
191 $user = $wgUser;
192 } else {
193 $user = new User;
194 }
195 } else {
196 $user =& $userInput;
197 }
198
199 $this->mUser = $user;
200
201 $this->mUseDynamicDates = $wgUseDynamicDates;
202 $this->mInterwikiMagic = $wgInterwikiMagic;
203 $this->mAllowExternalImages = $wgAllowExternalImages;
204 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
205 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
206 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
207 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
208 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
209 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
210 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
211 $this->mCleanSignatures = $wgCleanSignatures;
212 $this->mExternalLinkTarget = $wgExternalLinkTarget;
213
214 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
215 $this->mMath = $user->getOption( 'math' );
216 $this->mThumbSize = $user->getOption( 'thumbsize' );
217 $this->mStubThreshold = $user->getStubThreshold();
218 $this->mUserLang = $wgLang->getCode();
219
220 wfProfileOut( __METHOD__ );
221 }
222
223 /**
224 * Registers a callback for tracking which ParserOptions which are used.
225 * This is a private API with the parser.
226 */
227 function registerWatcher( $callback ) {
228 $this->onAccessCallback = $callback;
229 }
230
231 /**
232 * Called when an option is accessed.
233 */
234 protected function optionUsed( $optionName ) {
235 if ( $this->onAccessCallback ) {
236 call_user_func( $this->onAccessCallback, $optionName );
237 }
238 }
239
240 /**
241 * Returns the full array of options that would have been used by
242 * in 1.16.
243 * Used to get the old parser cache entries when available.
244 */
245 public static function legacyOptions() {
246 global $wgUseDynamicDates;
247 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
248 if ( $wgUseDynamicDates ) {
249 $legacyOpts[] = 'dateformat';
250 }
251 return $legacyOpts;
252 }
253
254 /**
255 * Generate a hash string with the values set on these ParserOptions
256 * for the keys given in the array.
257 * This will be used as part of the hash key for the parser cache,
258 * so users sharign the options with vary for the same page share
259 * the same cached data safely.
260 *
261 * Replaces User::getPageRenderingHash()
262 *
263 * Extensions which require it should install 'PageRenderingHash' hook,
264 * which will give them a chance to modify this key based on their own
265 * settings.
266 *
267 * @since 1.17
268 * @return \string Page rendering hash
269 */
270 public function optionsHash( $forOptions ) {
271 global $wgContLang, $wgRenderHashAppend;
272
273 $confstr = '';
274
275 if ( in_array( 'math', $forOptions ) )
276 $confstr .= $this->mMath;
277 else
278 $confstr .= '*';
279
280
281 // Space assigned for the stubthreshold but unused
282 // since it disables the parser cache, its value will always
283 // be 0 when this function is called by parsercache.
284 if ( in_array( 'stubthreshold', $forOptions ) )
285 $confstr .= '!' . $this->mStubThreshold;
286 else
287 $confstr .= '!*' ;
288
289 if ( in_array( 'dateformat', $forOptions ) )
290 $confstr .= '!' . $this->getDateFormat();
291
292 if ( in_array( 'numberheadings', $forOptions ) )
293 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
294 else
295 $confstr .= '!*';
296
297 if ( in_array( 'userlang', $forOptions ) )
298 $confstr .= '!' . $this->mUserLang;
299 else
300 $confstr .= '!*';
301
302 if ( in_array( 'thumbsize', $forOptions ) )
303 $confstr .= '!' . $this->mThumbSize;
304 else
305 $confstr .= '!*';
306
307 // add in language specific options, if any
308 // FIXME: This is just a way of retrieving the url/user preferred variant
309 $confstr .= $wgContLang->getExtraHashOptions();
310
311 // Since the skin could be overloading link(), it should be
312 // included here but in practice, none of our skins do that.
313 // $confstr .= "!" . $this->mSkin->getSkinName();
314
315 $confstr .= $wgRenderHashAppend;
316
317 if ( !in_array( 'editsection', $forOptions ) ) {
318 $confstr .= '!*';
319 } elseif ( !$this->mEditSection ) {
320 $confstr .= '!edit=0';
321 }
322
323 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
324 $confstr .= '!printable=1';
325
326 if ( $this->mExtraKey != '' )
327 $confstr .= $this->mExtraKey;
328
329 // Give a chance for extensions to modify the hash, if they have
330 // extra options or other effects on the parser cache.
331 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
332
333 // Make it a valid memcached key fragment
334 $confstr = str_replace( ' ', '_', $confstr );
335
336 return $confstr;
337 }
338 }