]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/parser/ParserOptions.php
MediaWiki 1.17.1
[autoinstallsdev/mediawiki.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;                      # Reference to the preferred skin
22         var $mDateFormat;                # Date format index
23         var $mEditSection;               # Create "edit section" links
24         var $mNumberHeadings;            # Automatically number headings
25         var $mAllowSpecialInclusion;     # Allow inclusion of special pages
26         var $mTidy;                      # Ask for tidy cleanup
27         var $mInterfaceMessage;          # Which lang to call for PLURAL and GRAMMAR
28         var $mTargetLanguage;            # Overrides above setting with arbitrary language
29         var $mMaxIncludeSize;            # Maximum size of template expansions, in bytes
30         var $mMaxPPNodeCount;            # Maximum number of nodes touched by PPFrame::expand()
31         var $mMaxPPExpandDepth;          # Maximum recursion depth in PPFrame::expand()
32         var $mMaxTemplateDepth;          # Maximum recursion depth for templates within templates
33         var $mRemoveComments;            # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
34         var $mTemplateCallback;          # Callback for template fetching
35         var $mEnableLimitReport;         # 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 $mMath;                      # User math preference (as integer)
39         var $mUserLang;                  # Language code of the User language.
40         var $mThumbSize;                 # Thumb size preferred by the user.
41         var $mCleanSignatures;           #
42
43         var $mUser;                      # Stored user object, just used to initialise the skin
44         var $mIsPreview;                 # Parsing the page for a "preview" operation
45         var $mIsSectionPreview;          # Parsing the page for a "preview" operation on a single section
46         var $mIsPrintable;               # Parsing the printable version of the page
47         
48         var $mExtraKey = '';             # Extra key that should be present in the caching key.
49         
50         protected $onAccessCallback = null;
51         
52         function getUseDynamicDates()               { return $this->mUseDynamicDates; }
53         function getInterwikiMagic()                { return $this->mInterwikiMagic; }
54         function getAllowExternalImages()           { return $this->mAllowExternalImages; }
55         function getAllowExternalImagesFrom()       { return $this->mAllowExternalImagesFrom; }
56         function getEnableImageWhitelist()          { return $this->mEnableImageWhitelist; }
57         function getEditSection()                   { $this->optionUsed('editsection');
58                                                       return $this->mEditSection; }
59         function getNumberHeadings()                { $this->optionUsed('numberheadings');
60                                                       return $this->mNumberHeadings; }
61         function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
62         function getTidy()                          { return $this->mTidy; }
63         function getInterfaceMessage()              { return $this->mInterfaceMessage; }
64         function getTargetLanguage()                { return $this->mTargetLanguage; }
65         function getMaxIncludeSize()                { return $this->mMaxIncludeSize; }
66         function getMaxPPNodeCount()                { return $this->mMaxPPNodeCount; }
67         function getMaxPPExpandDepth()              { return $this->mMaxPPExpandDepth; }
68         function getMaxTemplateDepth()              { return $this->mMaxTemplateDepth; }
69         function getRemoveComments()                { return $this->mRemoveComments; }
70         function getTemplateCallback()              { return $this->mTemplateCallback; }
71         function getEnableLimitReport()             { return $this->mEnableLimitReport; }
72         function getCleanSignatures()               { return $this->mCleanSignatures; }
73         function getExternalLinkTarget()            { return $this->mExternalLinkTarget; }
74         function getMath()                          { $this->optionUsed('math');
75                                                       return $this->mMath; }
76         function getThumbSize()                     { $this->optionUsed('thumbsize');
77                                                       return $this->mThumbSize; }
78         
79         function getIsPreview()                     { return $this->mIsPreview; }
80         function getIsSectionPreview()              { return $this->mIsSectionPreview; }
81         function getIsPrintable()                   { $this->optionUsed('printable');
82                                                       return $this->mIsPrintable; }
83
84         function getSkin( $title = null ) {
85                 if ( !isset( $this->mSkin ) ) {
86                         $this->mSkin = $this->mUser->getSkin( $title );
87                 }
88                 return $this->mSkin;
89         }
90
91         function getDateFormat() {
92                 $this->optionUsed('dateformat');
93                 if ( !isset( $this->mDateFormat ) ) {
94                         $this->mDateFormat = $this->mUser->getDatePreference();
95                 }
96                 return $this->mDateFormat;
97         }
98
99         function getTimestamp() {
100                 if ( !isset( $this->mTimestamp ) ) {
101                         $this->mTimestamp = wfTimestampNow();
102                 }
103                 return $this->mTimestamp;
104         }
105
106         /**
107          * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
108          * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
109          * producing inconsistent tables (Bug 14404).
110          */
111         function getUserLang() {
112                 $this->optionUsed('userlang');
113                 return $this->mUserLang;
114         }
115
116         function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
117         function setInterwikiMagic( $x )            { return wfSetVar( $this->mInterwikiMagic, $x ); }
118         function setAllowExternalImages( $x )       { return wfSetVar( $this->mAllowExternalImages, $x ); }
119         function setAllowExternalImagesFrom( $x )   { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
120         function setEnableImageWhitelist( $x )      { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
121         function setDateFormat( $x )                { return wfSetVar( $this->mDateFormat, $x ); }
122         function setEditSection( $x )               { return wfSetVar( $this->mEditSection, $x ); }
123         function setNumberHeadings( $x )            { return wfSetVar( $this->mNumberHeadings, $x ); }
124         function setAllowSpecialInclusion( $x )     { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
125         function setTidy( $x )                      { return wfSetVar( $this->mTidy, $x); }
126         function setSkin( $x )                      { $this->mSkin = $x; }
127         function setInterfaceMessage( $x )          { return wfSetVar( $this->mInterfaceMessage, $x); }
128         function setTargetLanguage( $x )            { return wfSetVar( $this->mTargetLanguage, $x, true ); }
129         function setMaxIncludeSize( $x )            { return wfSetVar( $this->mMaxIncludeSize, $x ); }
130         function setMaxPPNodeCount( $x )            { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
131         function setMaxTemplateDepth( $x )          { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
132         function setRemoveComments( $x )            { return wfSetVar( $this->mRemoveComments, $x ); }
133         function setTemplateCallback( $x )          { return wfSetVar( $this->mTemplateCallback, $x ); }
134         function enableLimitReport( $x = true )     { return wfSetVar( $this->mEnableLimitReport, $x ); }
135         function setTimestamp( $x )                 { return wfSetVar( $this->mTimestamp, $x ); }
136         function setCleanSignatures( $x )           { return wfSetVar( $this->mCleanSignatures, $x ); }
137         function setExternalLinkTarget( $x )        { return wfSetVar( $this->mExternalLinkTarget, $x ); }
138         function setMath( $x )                      { return wfSetVar( $this->mMath, $x ); }
139         function setUserLang( $x )                  { return wfSetVar( $this->mUserLang, $x ); }
140         function setThumbSize( $x )                 { return wfSetVar( $this->mThumbSize, $x ); }
141         
142         function setIsPreview( $x )                 { return wfSetVar( $this->mIsPreview, $x ); }
143         function setIsSectionPreview( $x )          { return wfSetVar( $this->mIsSectionPreview, $x ); }
144         function setIsPrintable( $x )               { return wfSetVar( $this->mIsPrintable, $x ); }
145
146         /**
147          * Extra key that should be present in the parser cache key.
148          */
149         function addExtraKey( $key ) {
150                 $this->mExtraKey .= '!' . $key;
151         }
152
153         function __construct( $user = null ) {
154                 $this->initialiseFromUser( $user );
155         }
156
157         /**
158          * Get parser options
159          *
160          * @param $user User object
161          * @return ParserOptions object
162          */
163         static function newFromUser( $user ) {
164                 return new ParserOptions( $user );
165         }
166
167         /** Get user options */
168         function initialiseFromUser( $userInput ) {
169                 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
170                 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
171                 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
172                 global $wgExternalLinkTarget, $wgLang;
173
174                 wfProfileIn( __METHOD__ );
175
176                 if ( !$userInput ) {
177                         global $wgUser;
178                         if ( isset( $wgUser ) ) {
179                                 $user = $wgUser;
180                         } else {
181                                 $user = new User;
182                         }
183                 } else {
184                         $user =& $userInput;
185                 }
186
187                 $this->mUser = $user;
188
189                 $this->mUseDynamicDates = $wgUseDynamicDates;
190                 $this->mInterwikiMagic = $wgInterwikiMagic;
191                 $this->mAllowExternalImages = $wgAllowExternalImages;
192                 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
193                 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
194                 $this->mSkin = null; # Deferred
195                 $this->mDateFormat = null; # Deferred
196                 $this->mEditSection = true;
197                 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
198                 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
199                 $this->mTidy = false;
200                 $this->mInterfaceMessage = false;
201                 $this->mTargetLanguage = null; // default depends on InterfaceMessage setting
202                 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
203                 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
204                 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
205                 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
206                 $this->mRemoveComments = true;
207                 $this->mTemplateCallback = array( 'Parser', 'statelessFetchTemplate' );
208                 $this->mEnableLimitReport = false;
209                 $this->mCleanSignatures = $wgCleanSignatures;
210                 $this->mExternalLinkTarget = $wgExternalLinkTarget;
211                 $this->mMath = $user->getOption( 'math' );
212                 $this->mUserLang = $wgLang->getCode();
213                 $this->mThumbSize = $user->getOption( 'thumbsize' );
214                 
215                 $this->mIsPreview = false;
216                 $this->mIsSectionPreview = false;
217                 $this->mIsPrintable = false;
218
219                 wfProfileOut( __METHOD__ );
220         }
221
222         /**
223          * Registers a callback for tracking which ParserOptions which are used.
224          * This is a private API with the parser.
225          */
226         function registerWatcher( $callback ) {
227                 $this->onAccessCallback = $callback;
228         }
229         
230         /**
231          * Called when an option is accessed.
232          */
233         protected function optionUsed( $optionName ) {
234                 if ( $this->onAccessCallback ) {
235                         call_user_func( $this->onAccessCallback, $optionName );
236                 }
237         }
238         
239         /**
240          * Returns the full array of options that would have been used by 
241          * in 1.16.
242          * Used to get the old parser cache entries when available.
243          */
244         public static function legacyOptions() {
245                 global $wgUseDynamicDates;
246                 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
247                 if ( $wgUseDynamicDates ) {
248                         $legacyOpts[] = 'dateformat';
249                 }
250                 return $legacyOpts;
251         }
252         
253         /**
254          * Generate a hash string with the values set on these ParserOptions
255          * for the keys given in the array.
256          * This will be used as part of the hash key for the parser cache,
257          * so users sharign the options with vary for the same page share 
258          * the same cached data safely.
259          * 
260          * Replaces User::getPageRenderingHash()
261          *
262          * Extensions which require it should install 'PageRenderingHash' hook,
263          * which will give them a chance to modify this key based on their own
264          * settings.
265          *
266          * @since 1.17
267          * @return \string Page rendering hash
268          */
269         public function optionsHash( $forOptions ) {
270                 global $wgContLang, $wgRenderHashAppend;
271
272                 $confstr = '';
273                 
274                 if ( in_array( 'math', $forOptions ) )
275                         $confstr .= $this->mMath;
276                 else
277                         $confstr .= '*';
278                         
279
280                 // Space assigned for the stubthreshold but unused
281                 // since it disables the parser cache, its value will always 
282                 // be 0 when this function is called by parsercache.
283                 // The conditional is here to avoid a confusing 0
284                 if ( true || in_array( 'stubthreshold', $forOptions ) )
285                         $confstr .= '!0' ;
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 }