Merge "Make it slightly easier for extensions to hook into page protection."
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
index 29b3500..5cb70cb 100644 (file)
@@ -52,6 +52,8 @@ class ParserOutput extends CacheTime {
                private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
                private $mSecondaryDataUpdates = array(); # List of DataUpdate, used to save info from the page somewhere else.
                private $mExtensionData = array(); # extra data used by extensions
+               private $mLimitReportData = array(); # Parser limit report data
+               private $mParseStartTime = array(); # Timestamps for getTimeSinceStart()
 
        const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
 
@@ -67,22 +69,8 @@ class ParserOutput extends CacheTime {
 
        function getText() {
                if ( $this->mEditSectionTokens ) {
-                       $text = $this->mText;
-
-                       // If there's old output with misplaced editsections links cached, mangle it to put them in
-                       // the right position. We can assume that there is no '</hN>' inside header tags, making this
-                       // possible to do with a regex.
-                       $text = preg_replace(
-                               //            [ this part is like EDITSECTION_REGEX, but with non-capturing groups                           ]
-                               //                                                                                  note the space here ------v
-                               '#(<[hH](\d)>)(<(?:mw:)?editsection page="(?:.*?)" section="(?:.*?)"(?:/>|>(?:.*?)(?:</(?:mw:)?editsection>))) ([\s\S]*?)(</[hH]\2>)#',
-                               // swap the order of content and editsection link - $2 is ignored since it's the number in hN's tag name
-                               '$1$4 $3$5',
-                               $text
-                       );
-
                        return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
-                               array( &$this, 'replaceEditSectionLinksCallback' ), $text );
+                               array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
                }
                return preg_replace( ParserOutput::EDITSECTION_REGEX, '', $this->mText );
        }
@@ -134,6 +122,7 @@ class ParserOutput extends CacheTime {
        function getIndexPolicy()            { return $this->mIndexPolicy; }
        function getTOCHTML()                { return $this->mTOCHTML; }
        function getTimestamp()              { return $this->mTimestamp; }
+       function getLimitReportData()        { return $this->mLimitReportData; }
 
        function setText( $text )            { return wfSetVar( $this->mText, $text ); }
        function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
@@ -558,4 +547,67 @@ class ParserOutput extends CacheTime {
                return null;
        }
 
+       private static function getTimes( $clock = null ) {
+               $ret = array();
+               if ( !$clock || $clock === 'wall' ) {
+                       $ret['wall'] = microtime( true );
+               }
+               if ( ( !$clock || $clock === 'cpu' ) && function_exists( 'getrusage' ) ) {
+                       $ru = getrusage();
+                       $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
+                       $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
+               }
+               return $ret;
+       }
+
+       /**
+        * Resets the parse start timestamps for future calls to getTimeSinceStart()
+        * @since 1.22
+        */
+       function resetParseStartTime() {
+               $this->mParseStartTime = self::getTimes();
+       }
+
+       /**
+        * Returns the time since resetParseStartTime() was last called
+        *
+        * Clocks available are:
+        *  - wall: Wall clock time
+        *  - cpu: CPU time (requires getrusage)
+        *
+        * @since 1.22
+        * @param string $clock
+        * @return float|null
+        */
+       function getTimeSinceStart( $clock ) {
+               if ( !isset( $this->mParseStartTime[$clock] ) ) {
+                       return null;
+               }
+
+               $end = self::getTimes( $clock );
+               return $end[$clock] - $this->mParseStartTime[$clock];
+       }
+
+       /**
+        * Sets parser limit report data for a key
+        *
+        * The key is used as the prefix for various messages used for formatting:
+        *  - $key: The label for the field in the limit report
+        *  - $key-value-text: Message used to format the value in the "NewPP limit
+        *      report" HTML comment. If missing, uses $key-format.
+        *  - $key-value-html: Message used to format the value in the preview
+        *      limit report table. If missing, uses $key-format.
+        *  - $key-value: Message used to format the value. If missing, uses "$1".
+        *
+        * Note that all values are interpreted as wikitext, and so should be
+        * encoded with htmlspecialchars() as necessary, but should avoid complex
+        * HTML for sanity of display in the "NewPP limit report" comment.
+        *
+        * @since 1.22
+        * @param string $key Message key
+        * @param mixed $value Appropriate for Message::params()
+        */
+       function setLimitReportData( $key, $value ) {
+               $this->mLimitReportData[$key] = $value;
+       }
 }