Merge "Correct error message for missing permission to get patrol information"
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
index e4c867a..729b9db 100644 (file)
@@ -120,13 +120,13 @@ class ParserOptions {
         * Callback for current revision fetching. Used as first argument to call_user_func().
         */
        private $mCurrentRevisionCallback =
-               array( 'Parser', 'statelessFetchRevision' );
+               [ 'Parser', 'statelessFetchRevision' ];
 
        /**
         * Callback for template fetching. Used as first argument to call_user_func().
         */
        private $mTemplateCallback =
-               array( 'Parser', 'statelessFetchTemplate' );
+               [ 'Parser', 'statelessFetchTemplate' ];
 
        /**
         * Enable limit report in an HTML comment on output
@@ -372,18 +372,17 @@ class ParserOptions {
        }
 
        /**
-        * Get the user language used by the parser for this page and record the
-        * userlang parser option, which splits parser cache.
+        * Get the user language used by the parser for this page and split the parser cache.
         *
-        * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
+        * @warning: Calling this causes the parser cache to be fragmented by user language!
+        * To avoid cache fragmentation, output should not depend on the user language.
+        * Use Parser::getFunctionLang() or Parser::getTargetLanguage() instead!
         *
-        * To avoid side-effects where the page will be rendered based on the language
-        * of the user who last saved, this function will trigger a cache fragmentation.
-        * For that reason, usage of this method is discouraged unless it is desired to
-        * split caches based on user language, such as for multilingual content.
-        *
-        * When saving, this will return the default language instead of the user's.
+        * @note This function will trigger a cache fragmentation by recording the
+        * 'userlang' option, see optionUsed(). This is done to avoid cache pollution
+        * when the page is rendered based on the language of the user.
         *
+        * @note When saving, this will return the default language instead of the user's.
         * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
         *
         * @return Language
@@ -397,6 +396,12 @@ class ParserOptions {
        /**
         * Same as getUserLangObj() but returns a string instead.
         *
+        * @warning: Calling this causes the parser cache to be fragmented by user language!
+        * To avoid cache fragmentation, output should not depend on the user language.
+        * Use Parser::getFunctionLang() or Parser::getTargetLanguage() instead!
+        *
+        * @see getUserLangObj()
+        *
         * @return string Language code
         * @since 1.17
         */
@@ -594,6 +599,16 @@ class ParserOptions {
                $this->initialiseFromUser( $user, $lang );
        }
 
+       /**
+        * Get a ParserOptions object for an anonymous user
+        * @since 1.27
+        * @return ParserOptions
+        */
+       public static function newFromAnon() {
+               global $wgContLang;
+               return new ParserOptions( new User, $wgContLang );
+       }
+
        /**
         * Get a ParserOptions object from a given user.
         * Language will be taken from $wgLang.
@@ -675,10 +690,10 @@ class ParserOptions {
         */
        public function matches( ParserOptions $other ) {
                $fields = array_keys( get_class_vars( __CLASS__ ) );
-               $fields = array_diff( $fields, array(
+               $fields = array_diff( $fields, [
                        'mEnableLimitReport', // only effects HTML comments
                        'onAccessCallback', // only used for ParserOutput option tracking
-               ) );
+               ] );
                foreach ( $fields as $field ) {
                        if ( !is_object( $this->$field ) && $this->$field !== $other->$field ) {
                                return false;
@@ -702,6 +717,10 @@ class ParserOptions {
 
        /**
         * Called when an option is accessed.
+        * Calls the watcher that was set using registerWatcher().
+        * Typically, the watcher callback is ParserOutput::registerOption().
+        * The information registered that way will be used by ParserCache::save().
+        *
         * @param string $optionName Name of the option
         */
        public function optionUsed( $optionName ) {
@@ -717,14 +736,14 @@ class ParserOptions {
         * @return array
         */
        public static function legacyOptions() {
-               return array(
+               return [
                        'stubthreshold',
                        'numberheadings',
                        'userlang',
                        'thumbsize',
                        'editsection',
                        'printable'
-               );
+               ];
        }
 
        /**
@@ -793,6 +812,10 @@ class ParserOptions {
 
                $confstr .= $wgRenderHashAppend;
 
+               // @note: as of Feb 2015, core never sets the editsection flag, since it uses
+               // <mw:editsection> tags to inject editsections on the fly. However, extensions
+               // may be using it by calling ParserOption::optionUsed resp. ParserOutput::registerOption
+               // directly. At least Wikibase does at this point in time.
                if ( !in_array( 'editsection', $forOptions ) ) {
                        $confstr .= '!*';
                } elseif ( !$this->mEditSection ) {
@@ -809,7 +832,7 @@ class ParserOptions {
 
                // Give a chance for extensions to modify the hash, if they have
                // extra options or other effects on the parser cache.
-               Hooks::run( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) );
+               Hooks::run( 'PageRenderingHash', [ &$confstr, $this->getUser(), &$forOptions ] );
 
                // Make it a valid memcached key fragment
                $confstr = str_replace( ' ', '_', $confstr );
@@ -818,8 +841,8 @@ class ParserOptions {
        }
 
        /**
-        * Sets a hook to force that a page exists, and sets a current revision callback to return a
-        * revision with custom content when the current revision of the page is requested.
+        * Sets a hook to force that a page exists, and sets a current revision callback to return
+        * revision with custom content when the current revision of the page is requested.
         *
         * @since 1.25
         * @param Title $title
@@ -828,20 +851,25 @@ class ParserOptions {
         * @return ScopedCallback to unset the hook
         */
        public function setupFakeRevision( $title, $content, $user ) {
-               $oldCallback = $this->setCurrentRevisionCallback( function ( $titleToCheck, $parser = false ) use ( $title, $content, $user, &$oldCallback ) {
-                       if ( $titleToCheck->equals( $title ) ) {
-                               return new Revision( array(
-                                       'page' => $title->getArticleID(),
-                                       'user_text' => $user->getName(),
-                                       'user' => $user->getId(),
-                                       'parent_id' => $title->getLatestRevId(),
-                                       'title' => $title,
-                                       'content' => $content
-                               ) );
-                       } else {
-                               return call_user_func( $oldCallback, $titleToCheck, $parser );
+               $oldCallback = $this->setCurrentRevisionCallback(
+                       function (
+                               $titleToCheck, $parser = false ) use ( $title, $content, $user, &$oldCallback
+                       ) {
+                               if ( $titleToCheck->equals( $title ) ) {
+                                       return new Revision( [
+                                               'page' => $title->getArticleID(),
+                                               'user_text' => $user->getName(),
+                                               'user' => $user->getId(),
+                                               'parent_id' => $title->getLatestRevID(),
+                                               'title' => $title,
+                                               'content' => $content
+                                       ] );
+                               } else {
+                                       return call_user_func( $oldCallback, $titleToCheck, $parser );
+                               }
                        }
-               } );
+               );
+
                global $wgHooks;
                $wgHooks['TitleExists'][] =
                        function ( $titleToCheck, &$exists ) use ( $title ) {