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