Merge "Make it slightly easier for extensions to hook into page protection."
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
index 3af3b7a..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>))#';
 
@@ -120,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 ); }
@@ -176,10 +179,10 @@ class ParserOutput extends CacheTime {
                global $wgServer, $wgRegisterInternalExternals;
 
                $registerExternalLink = true;
-               if( !$wgRegisterInternalExternals ) {
+               if ( !$wgRegisterInternalExternals ) {
                        $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
                }
-               if( $registerExternalLink ) {
+               if ( $registerExternalLink ) {
                        $this->mExternalLinks[$url] = 1;
                }
        }
@@ -201,11 +204,11 @@ class ParserOutput extends CacheTime {
                if ( $ns == NS_MEDIA ) {
                        // Normalize this pseudo-alias if it makes it down here...
                        $ns = NS_FILE;
-               } elseif( $ns == NS_SPECIAL ) {
+               } elseif ( $ns == NS_SPECIAL ) {
                        // We don't record Special: links currently
                        // It might actually be wise to, but we'd need to do some normalization.
                        return;
-               } elseif( $dbk === '' ) {
+               } elseif ( $dbk === '' ) {
                        // Don't record self links -  [[#Foo]]
                        return;
                }
@@ -258,7 +261,7 @@ class ParserOutput extends CacheTime {
         */
        function addInterwikiLink( $title ) {
                $prefix = $title->getInterwiki();
-               if( $prefix == '' ) {
+               if ( $prefix == '' ) {
                        throw new MWException( 'Non-interwiki link passed, internal parser error.' );
                }
                if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
@@ -329,7 +332,7 @@ class ParserOutput extends CacheTime {
         */
        public function getDisplayTitle() {
                $t = $this->getTitleText();
-               if( $t === '' ) {
+               if ( $t === '' ) {
                        return false;
                }
                return $t;
@@ -544,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;
+       }
 }