72e4242d7a594aed6927483690305db3cdb0ba6a
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2
3 /**
4 * Set options of the Parser
5 * @todo document
6 * @ingroup Parser
7 */
8 class ParserOptions {
9 # All variables are supposed to be private in theory, although in practise this is not the case.
10 var $mUseDynamicDates; # Use DateFormatter to format dates
11 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
12 var $mAllowExternalImages; # Allow external images inline
13 var $mAllowExternalImagesFrom; # If not, any exception?
14 var $mEnableImageWhitelist; # If not or it doesn't match, should we check an on-wiki whitelist?
15 var $mSkin; # Reference to the preferred skin
16 var $mDateFormat; # Date format index
17 var $mEditSection; # Create "edit section" links
18 var $mNumberHeadings; # Automatically number headings
19 var $mAllowSpecialInclusion; # Allow inclusion of special pages
20 var $mTidy; # Ask for tidy cleanup
21 var $mInterfaceMessage; # Which lang to call for PLURAL and GRAMMAR
22 var $mTargetLanguage; # Overrides above setting with arbitrary language
23 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
24 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
25 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
26 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
27 var $mRemoveComments; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
28 var $mTemplateCallback; # Callback for template fetching
29 var $mEnableLimitReport; # Enable limit report in an HTML comment on output
30 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
31 var $mExternalLinkTarget; # Target attribute for external links
32 var $mMath; # User math preference (as integer)
33 var $mUserLang; # Language code of the User language.
34 var $mThumbSize; # Thumb size preferred by the user.
35
36 var $mUser; # Stored user object, just used to initialise the skin
37 var $mIsPreview; # Parsing the page for a "preview" operation
38 var $mIsSectionPreview; # Parsing the page for a "preview" operation on a single section
39 var $mIsPrintable; # Parsing the printable version of the page
40
41
42 protected $accessedOptions;
43
44 function getUseDynamicDates() { return $this->mUseDynamicDates; }
45 function getInterwikiMagic() { return $this->mInterwikiMagic; }
46 function getAllowExternalImages() { return $this->mAllowExternalImages; }
47 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
48 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
49 function getEditSection() { $this->accessedOptions['editsection'] = true;
50 return $this->mEditSection; }
51 function getNumberHeadings() { $this->accessedOptions['numberheadings'] = true;
52 return $this->mNumberHeadings; }
53 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
54 function getTidy() { return $this->mTidy; }
55 function getInterfaceMessage() { return $this->mInterfaceMessage; }
56 function getTargetLanguage() { return $this->mTargetLanguage; }
57 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
58 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
59 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
60 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
61 function getRemoveComments() { return $this->mRemoveComments; }
62 function getTemplateCallback() { return $this->mTemplateCallback; }
63 function getEnableLimitReport() { return $this->mEnableLimitReport; }
64 function getCleanSignatures() { return $this->mCleanSignatures; }
65 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
66 function getMath() { $this->accessedOptions['math'] = true;
67 return $this->mMath; }
68 function getThumbSize() { $this->accessedOptions['thumbsize'] = true;
69 return $this->mThumbSize; }
70
71 function getIsPreview() { return $this->mIsPreview; }
72 function getIsSectionPreview() { return $this->mIsSectionPreview; }
73 function getIsPrintable() { $this->accessedOptions['printable'] = true;
74 return $this->mIsPrintable; }
75
76 function getSkin() {
77 if ( !isset( $this->mSkin ) ) {
78 $this->mSkin = $this->mUser->getSkin();
79 }
80 return $this->mSkin;
81 }
82
83 function getDateFormat() {
84 $this->accessedOptions['dateformat'] = true;
85 if ( !isset( $this->mDateFormat ) ) {
86 $this->mDateFormat = $this->mUser->getDatePreference();
87 }
88 return $this->mDateFormat;
89 }
90
91 function getTimestamp() {
92 if ( !isset( $this->mTimestamp ) ) {
93 $this->mTimestamp = wfTimestampNow();
94 }
95 return $this->mTimestamp;
96 }
97
98 # You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
99 # Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
100 # producing inconsistent tables (Bug 14404).
101 function getUserLang() {
102 $this->accessedOptions['userlang'] = true;
103 return $this->mUserLang;
104 }
105
106 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
107 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
108 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
109 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
110 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
111 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
112 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
113 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
114 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
115 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
116 function setSkin( $x ) { $this->mSkin = $x; }
117 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
118 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
119 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
120 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
121 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
122 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
123 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
124 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
125 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
126 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
127 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
128 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
129 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
130 function setThumbSize() { return wfSetVar( $this->mThumbSize, $x ); }
131
132 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
133 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
134 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
135
136 function __construct( $user = null ) {
137 $this->initialiseFromUser( $user );
138 }
139
140 /**
141 * Get parser options
142 *
143 * @param $user User object
144 * @return ParserOptions object
145 */
146 static function newFromUser( $user ) {
147 return new ParserOptions( $user );
148 }
149
150 /** Get user options */
151 function initialiseFromUser( $userInput ) {
152 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
153 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
154 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
155 global $wgExternalLinkTarget, $wgLang;
156
157 wfProfileIn( __METHOD__ );
158
159 if ( !$userInput ) {
160 global $wgUser;
161 if ( isset( $wgUser ) ) {
162 $user = $wgUser;
163 } else {
164 $user = new User;
165 }
166 } else {
167 $user =& $userInput;
168 }
169
170 $this->mUser = $user;
171
172 $this->mUseDynamicDates = $wgUseDynamicDates;
173 $this->mInterwikiMagic = $wgInterwikiMagic;
174 $this->mAllowExternalImages = $wgAllowExternalImages;
175 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
176 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
177 $this->mSkin = null; # Deferred
178 $this->mDateFormat = null; # Deferred
179 $this->mEditSection = true;
180 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
181 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
182 $this->mTidy = false;
183 $this->mInterfaceMessage = false;
184 $this->mTargetLanguage = null; // default depends on InterfaceMessage setting
185 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
186 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
187 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
188 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
189 $this->mRemoveComments = true;
190 $this->mTemplateCallback = array( 'Parser', 'statelessFetchTemplate' );
191 $this->mEnableLimitReport = false;
192 $this->mCleanSignatures = $wgCleanSignatures;
193 $this->mExternalLinkTarget = $wgExternalLinkTarget;
194 $this->mMath = $user->getOption( 'math' );
195 $this->mUserLang = $wgLang->getCode();
196 $this->mThumbSize = $user->getOption( 'thumbsize' );
197
198 $this->mIsPreview = false;
199 $this->mIsSectionPreview = false;
200 $this->mIsPrintable = false;
201
202 wfProfileOut( __METHOD__ );
203 }
204
205 /**
206 * Returns the options from this ParserOptions which have been used.
207 */
208 public function usedOptions() {
209 return array_keys( $this->accessedOptions );
210 }
211
212 /**
213 * Resets the memory of options usage.
214 */
215 public function resetUsage() {
216 $this->accessedOptions = array();
217 }
218
219 /**
220 * Returns the full array of options that would have been used by
221 * in 1.16.
222 * Used to get the old parser cache entries when available.
223 */
224 public static function legacyOptions() {
225 global $wgUseDynamicDates;
226 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
227 if ( $wgUseDynamicDates ) {
228 $legacyOpts[] = 'dateformat';
229 }
230 return $legacyOpts;
231 }
232
233 /**
234 * Generate a hash string with the values set on these ParserOptions
235 * for the keys given in the array.
236 * This will be used as part of the hash key for the parser cache,
237 * so users sharign the options with vary for the same page share
238 * the same cached data safely.
239 *
240 * Replaces User::getPageRenderingHash()
241 *
242 * Extensions which require it should install 'PageRenderingHash' hook,
243 * which will give them a chance to modify this key based on their own
244 * settings.
245 *
246 * @since 1.17
247 * @return \string Page rendering hash
248 */
249 public function optionsHash( $forOptions ) {
250 global $wgContLang, $wgRenderHashAppend;
251
252 $confstr = '';
253
254 if ( in_array( 'math', $forOptions ) )
255 $confstr .= $this->mMath;
256 else
257 $confstr .= '*';
258
259
260 // Space assigned for the stubthreshold but unused
261 // since it disables the parser cache, its value will always
262 // be 0 when this function is called by parsercache.
263 // The conditional is here to avoid a confusing 0
264 if ( in_array( 'stubthreshold', $forOptions ) )
265 $confstr .= '!0' ;
266 else
267 $confstr .= '!*' ;
268
269 if ( in_array( 'dateformat', $forOptions ) )
270 $confstr .= '!' . $this->mDateFormat;
271
272 if ( in_array( 'numberheadings', $forOptions ) )
273 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
274 else
275 $confstr .= '!*';
276
277 if ( in_array( 'userlang', $forOptions ) )
278 $confstr .= '!' . $this->mUserLang;
279 else
280 $confstr .= '!*';
281
282 if ( in_array( 'thumbsize', $forOptions ) )
283 $confstr .= '!' . $this->mThumbSize;
284 else
285 $confstr .= '!*';
286
287 // add in language specific options, if any
288 // FIXME: This is just a way of retrieving the url/user preferred variant
289 $confstr .= $wgContLang->getExtraHashOptions();
290
291 // Since the skin could be overloading link(), it should be
292 // included here but in practice, none of our skins do that.
293 // $confstr .= "!" . $this->mSkin->getSkinName();
294
295 $confstr .= $wgRenderHashAppend;
296
297 if ( !$this->mEditSection && in_array( 'editsection', $forOptions ) )
298 $confstr .= '!edit=0';
299 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
300 $confstr .= '!printable=1';
301
302 // Give a chance for extensions to modify the hash, if they have
303 // extra options or other effects on the parser cache.
304 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
305
306 // Make it a valid memcached key fragment
307 $confstr = str_replace( ' ', '_', $confstr );
308
309 return $confstr;
310 }
311 }