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