99d680c971906671fe3a6f5fc05ec4719519864b
[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() { return $this->mEditSection; }
60 function getNumberHeadings() { $this->optionUsed('numberheadings');
61 return $this->mNumberHeadings; }
62 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
63 function getTidy() { return $this->mTidy; }
64 function getInterfaceMessage() { return $this->mInterfaceMessage; }
65 function getTargetLanguage() { return $this->mTargetLanguage; }
66 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
67 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
68 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
69 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
70 function getRemoveComments() { return $this->mRemoveComments; }
71 function getTemplateCallback() { return $this->mTemplateCallback; }
72 function getEnableLimitReport() { return $this->mEnableLimitReport; }
73 function getCleanSignatures() { return $this->mCleanSignatures; }
74 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
75 function getMath() { $this->optionUsed('math');
76 return $this->mMath; }
77 function getThumbSize() { $this->optionUsed('thumbsize');
78 return $this->mThumbSize; }
79
80 function getIsPreview() { return $this->mIsPreview; }
81 function getIsSectionPreview() { return $this->mIsSectionPreview; }
82 function getIsPrintable() { $this->optionUsed('printable');
83 return $this->mIsPrintable; }
84 function getUser() { return $this->mUser; }
85
86 function getSkin( $title = null ) {
87 if ( !isset( $this->mSkin ) ) {
88 $this->mSkin = $this->mUser->getSkin( $title );
89 }
90 return $this->mSkin;
91 }
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
144 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
145 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
146 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
147
148 /**
149 * Extra key that should be present in the parser cache key.
150 */
151 function addExtraKey( $key ) {
152 $this->mExtraKey .= '!' . $key;
153 }
154
155 function __construct( $user = null ) {
156 $this->initialiseFromUser( $user );
157 }
158
159 /**
160 * Get parser options
161 *
162 * @param $user User object
163 * @return ParserOptions object
164 */
165 static function newFromUser( $user ) {
166 return new ParserOptions( $user );
167 }
168
169 /** Get user options */
170 function initialiseFromUser( $userInput ) {
171 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
172 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
173 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
174 global $wgExternalLinkTarget, $wgLang;
175
176 wfProfileIn( __METHOD__ );
177
178 if ( !$userInput ) {
179 global $wgUser;
180 if ( isset( $wgUser ) ) {
181 $user = $wgUser;
182 } else {
183 $user = new User;
184 }
185 } else {
186 $user =& $userInput;
187 }
188
189 $this->mUser = $user;
190
191 $this->mUseDynamicDates = $wgUseDynamicDates;
192 $this->mInterwikiMagic = $wgInterwikiMagic;
193 $this->mAllowExternalImages = $wgAllowExternalImages;
194 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
195 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
196 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
197 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
198 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
199 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
200 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
201 $this->mCleanSignatures = $wgCleanSignatures;
202 $this->mExternalLinkTarget = $wgExternalLinkTarget;
203
204 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
205 $this->mMath = $user->getOption( 'math' );
206 $this->mThumbSize = $user->getOption( 'thumbsize' );
207 $this->mUserLang = $wgLang->getCode();
208
209 wfProfileOut( __METHOD__ );
210 }
211
212 /**
213 * Registers a callback for tracking which ParserOptions which are used.
214 * This is a private API with the parser.
215 */
216 function registerWatcher( $callback ) {
217 $this->onAccessCallback = $callback;
218 }
219
220 /**
221 * Called when an option is accessed.
222 */
223 protected function optionUsed( $optionName ) {
224 if ( $this->onAccessCallback ) {
225 call_user_func( $this->onAccessCallback, $optionName );
226 }
227 }
228
229 /**
230 * Returns the full array of options that would have been used by
231 * in 1.16.
232 * Used to get the old parser cache entries when available.
233 */
234 public static function legacyOptions() {
235 global $wgUseDynamicDates;
236 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
237 if ( $wgUseDynamicDates ) {
238 $legacyOpts[] = 'dateformat';
239 }
240 return $legacyOpts;
241 }
242
243 /**
244 * Generate a hash string with the values set on these ParserOptions
245 * for the keys given in the array.
246 * This will be used as part of the hash key for the parser cache,
247 * so users sharign the options with vary for the same page share
248 * the same cached data safely.
249 *
250 * Replaces User::getPageRenderingHash()
251 *
252 * Extensions which require it should install 'PageRenderingHash' hook,
253 * which will give them a chance to modify this key based on their own
254 * settings.
255 *
256 * @since 1.17
257 * @return \string Page rendering hash
258 */
259 public function optionsHash( $forOptions ) {
260 global $wgContLang, $wgRenderHashAppend;
261
262 $confstr = '';
263
264 if ( in_array( 'math', $forOptions ) )
265 $confstr .= $this->mMath;
266 else
267 $confstr .= '*';
268
269
270 // Space assigned for the stubthreshold but unused
271 // since it disables the parser cache, its value will always
272 // be 0 when this function is called by parsercache.
273 // The conditional is here to avoid a confusing 0
274 if ( in_array( 'stubthreshold', $forOptions ) )
275 $confstr .= '!0' ;
276 else
277 $confstr .= '!*' ;
278
279 if ( in_array( 'dateformat', $forOptions ) )
280 $confstr .= '!' . $this->getDateFormat();
281
282 if ( in_array( 'numberheadings', $forOptions ) )
283 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
284 else
285 $confstr .= '!*';
286
287 if ( in_array( 'userlang', $forOptions ) )
288 $confstr .= '!' . $this->mUserLang;
289 else
290 $confstr .= '!*';
291
292 if ( in_array( 'thumbsize', $forOptions ) )
293 $confstr .= '!' . $this->mThumbSize;
294 else
295 $confstr .= '!*';
296
297 // add in language specific options, if any
298 // FIXME: This is just a way of retrieving the url/user preferred variant
299 $confstr .= $wgContLang->getExtraHashOptions();
300
301 // Since the skin could be overloading link(), it should be
302 // included here but in practice, none of our skins do that.
303 // $confstr .= "!" . $this->mSkin->getSkinName();
304
305 $confstr .= $wgRenderHashAppend;
306
307 if ( !in_array( 'editsection', $forOptions ) ) {
308 $confstr .= '!*';
309 } elseif ( !$this->mEditSection ) {
310 $confstr .= '!edit=0';
311 }
312
313 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
314 $confstr .= '!printable=1';
315
316 if ( $this->mExtraKey != '' )
317 $confstr .= $this->mExtraKey;
318
319 // Give a chance for extensions to modify the hash, if they have
320 // extra options or other effects on the parser cache.
321 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
322
323 // Make it a valid memcached key fragment
324 $confstr = str_replace( ' ', '_', $confstr );
325
326 return $confstr;
327 }
328 }