Merge "Call setTransactionTicket() on DeferredUpdates sub-queue items too"
[lhc/web/wiklou.git] / includes / PageProps.php
index 2d6e535..ed06935 100644 (file)
@@ -33,6 +33,33 @@ class PageProps {
         */
        private static $instance;
 
+       /**
+        * Overrides the default instance of this class
+        * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
+        *
+        * If this method is used it MUST also be called with null after a test to ensure a new
+        * default instance is created next time getInstance is called.
+        *
+        * @since 1.27
+        *
+        * @param PageProps|null $store
+        *
+        * @return ScopedCallback to reset the overridden value
+        * @throws MWException
+        */
+       public static function overrideInstance( PageProps $store = null ) {
+               if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+                       throw new MWException(
+                               'Cannot override ' . __CLASS__ . 'default instance in operation.'
+                       );
+               }
+               $previousValue = self::$instance;
+               self::$instance = $store;
+               return new ScopedCallback( function() use ( $previousValue ) {
+                       self::$instance = $previousValue;
+               } );
+       }
+
        /**
         * @return PageProps
         */
@@ -57,6 +84,16 @@ class PageProps {
                $this->cache = new ProcessCacheLRU( self::CACHE_SIZE );
        }
 
+       /**
+        * Ensure that cache has at least this size
+        * @param int $size
+        */
+       public function ensureCacheSize( $size ) {
+               if ( $this->cache->getSize() < $size ) {
+                       $this->cache->resize( $size );
+               }
+       }
+
        /**
         * Given one or more Titles and one or more names of properties,
         * returns an associative array mapping page ID to property value.
@@ -65,7 +102,7 @@ class PageProps {
         * single Title is provided, it does not need to be passed in an array,
         * but an array will always be returned. If a single property name is
         * provided, it does not need to be passed in an array. In that case,
-        * an associtive array mapping page ID to property value will be
+        * an associative array mapping page ID to property value will be
         * returned; otherwise, an associative array mapping page ID to
         * an associative array mapping property name to property value will be
         * returned. An empty array will be returned if no matching properties
@@ -79,13 +116,13 @@ class PageProps {
                if ( is_array( $propertyNames ) ) {
                        $gotArray = true;
                } else {
-                       $propertyNames = array( $propertyNames );
+                       $propertyNames = [ $propertyNames ];
                        $gotArray = false;
                }
 
-               $values = array();
+               $values = [];
                $goodIDs = $this->getGoodIDs( $titles );
-               $queryIDs = array();
+               $queryIDs = [];
                foreach ( $goodIDs as $pageID ) {
                        foreach ( $propertyNames as $propertyName ) {
                                $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
@@ -103,18 +140,18 @@ class PageProps {
                }
 
                if ( $queryIDs ) {
-                       $dbr = wfGetDB( DB_SLAVE );
+                       $dbr = wfGetDB( DB_REPLICA );
                        $result = $dbr->select(
                                'page_props',
-                               array(
+                               [
                                        'pp_page',
                                        'pp_propname',
                                        'pp_value'
-                               ),
-                               array(
+                               ],
+                               [
                                        'pp_page' => $queryIDs,
                                        'pp_propname' => $propertyNames
-                               ),
+                               ],
                                __METHOD__
                        );
 
@@ -148,9 +185,9 @@ class PageProps {
         * @return array associative array mapping page ID to property value array
         */
        public function getAllProperties( $titles ) {
-               $values = array();
+               $values = [];
                $goodIDs = $this->getGoodIDs( $titles );
-               $queryIDs = array();
+               $queryIDs = [];
                foreach ( $goodIDs as $pageID ) {
                        $pageProperties = $this->getCachedProperties( $pageID );
                        if ( $pageProperties === false ) {
@@ -160,36 +197,36 @@ class PageProps {
                        }
                }
 
-               if ( $queryIDs != array() ) {
-                       $dbr = wfGetDB( DB_SLAVE );
+               if ( $queryIDs != [] ) {
+                       $dbr = wfGetDB( DB_REPLICA );
                        $result = $dbr->select(
                                'page_props',
-                               array(
+                               [
                                        'pp_page',
                                        'pp_propname',
                                        'pp_value'
-                               ),
-                               array(
+                               ],
+                               [
                                        'pp_page' => $queryIDs,
-                               ),
+                               ],
                                __METHOD__
                        );
 
                        $currentPageID = 0;
-                       $pageProperties = array();
+                       $pageProperties = [];
                        foreach ( $result as $row ) {
                                $pageID = $row->pp_page;
                                if ( $currentPageID != $pageID ) {
-                                       if ( $pageProperties != array() ) {
+                                       if ( $pageProperties != [] ) {
                                                $this->cacheProperties( $currentPageID, $pageProperties );
                                                $values[$currentPageID] = $pageProperties;
                                        }
                                        $currentPageID = $pageID;
-                                       $pageProperties = array();
+                                       $pageProperties = [];
                                }
                                $pageProperties[$row->pp_propname] = $row->pp_value;
                        }
-                       if ( $pageProperties != array() ) {
+                       if ( $pageProperties != [] ) {
                                $this->cacheProperties( $pageID, $pageProperties );
                                $values[$pageID] = $pageProperties;
                        }
@@ -203,7 +240,7 @@ class PageProps {
         * @return array array of good page IDs
         */
        private function getGoodIDs( $titles ) {
-               $result = array();
+               $result = [];
                if ( is_array( $titles ) ) {
                        foreach ( $titles as $title ) {
                                $pageID = $title->getArticleID();