Make PROTECTIONLEVEL count as expensive
authorJackmcbarn <jackmcbarn@gmail.com>
Mon, 13 Jan 2014 20:01:17 +0000 (15:01 -0500)
committerJackmcbarn <jackmcbarn@gmail.com>
Mon, 13 Jan 2014 20:01:17 +0000 (15:01 -0500)
When protection levels for a page have not previously loaded, make calls
to PROTECTIONLEVEL for that page count as expensive. Also, add new
accessors for the protection information.

Change-Id: Ic088a9f482154d5353ccf580bbe5c359371a8cdd

includes/Title.php
includes/parser/CoreParserFunctions.php

index 096a04d..108cf59 100644 (file)
@@ -2655,6 +2655,17 @@ class Title {
                return array( $sources, $pagerestrictions );
        }
 
+       /**
+        * Accessor for mRestrictionsLoaded
+        *
+        * @return bool Whether or not the page's restrictions have already been
+        * loaded from the database
+        * @since 1.23
+        */
+       public function areRestrictionsLoaded() {
+               return $this->mRestrictionsLoaded;
+       }
+
        /**
         * Accessor/initialisation for mRestrictions
         *
@@ -2670,6 +2681,21 @@ class Title {
                                : array();
        }
 
+       /**
+        * Accessor/initialisation for mRestrictions
+        *
+        * @return Array of Arrays of Strings the first level indexed by
+        * action, the second level containing the names of the groups
+        * allowed to perform each action
+        * @since 1.23
+        */
+       public function getAllRestrictions() {
+               if ( !$this->mRestrictionsLoaded ) {
+                       $this->loadRestrictions();
+               }
+               return $this->mRestrictions;
+       }
+
        /**
         * Get the expiry time for the restriction against a given action
         *
index df0f118..87ee1cd 100644 (file)
@@ -704,7 +704,10 @@ class CoreParserFunctions {
        }
 
        /**
-        * Returns the requested protection level for the current page
+        * Returns the requested protection level for the current page. This
+        * is an expensive parser function and can't be called too many times
+        * per page, unless the protection levels for the given title have
+        * already been retrieved
         *
         * @param Parser $parser
         * @param string $type
@@ -717,10 +720,13 @@ class CoreParserFunctions {
                if ( !( $titleObject instanceof Title ) ) {
                        $titleObject = $parser->mTitle;
                }
-               $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
-               # Title::getRestrictions returns an array, its possible it may have
-               # multiple values in the future
-               return implode( $restrictions, ',' );
+               if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
+                       $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
+                       # Title::getRestrictions returns an array, its possible it may have
+                       # multiple values in the future
+                       return implode( $restrictions, ',' );
+               }
+               return '';
        }
 
        /**