Add/update doc blocks for MWTidy
[lhc/web/wiklou.git] / includes / PageProps.php
index 0a3a324..3654384 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
         */
@@ -58,53 +85,76 @@ class PageProps {
        }
 
        /**
-        * Given one or more Titles and the name of a property, returns an
-        * associative array mapping page ID to property value. Pages in the
-        * provided set of Titles that do not have a value for the given
-        * property will not appear in the returned array. If a single Title
-        * is provided, it does not need to be passed in an array, but an array
-        * will always be returned. An empty array will be returned if no
-        * matching properties were found.
-        *
-        * @param array|Title $titles
-        * @param string $propertyName
+        * Given one or more Titles and one or more names of properties,
+        * returns an associative array mapping page ID to property value.
+        * Pages in the provided set of Titles that do not have a value for
+        * the given properties will not appear in the returned array. If a
+        * 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
+        * 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
+        * were found.
         *
+        * @param Title[]|Title $titles
+        * @param string[]|string $propertyNames
         * @return array associative array mapping page ID to property value
-        *
         */
-       public function getProperty( $titles, $propertyName ) {
-               $values = array();
+       public function getProperties( $titles, $propertyNames ) {
+               if ( is_array( $propertyNames ) ) {
+                       $gotArray = true;
+               } else {
+                       $propertyNames = [ $propertyNames ];
+                       $gotArray = false;
+               }
+
+               $values = [];
                $goodIDs = $this->getGoodIDs( $titles );
-               $queryIDs = array();
+               $queryIDs = [];
                foreach ( $goodIDs as $pageID ) {
-                       $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
-                       if ( $propertyValue === false ) {
-                               $queryIDs[] = $pageID;
-                       } else {
-                               $values[$pageID] = $propertyValue;
+                       foreach ( $propertyNames as $propertyName ) {
+                               $propertyValue = $this->getCachedProperty( $pageID, $propertyName );
+                               if ( $propertyValue === false ) {
+                                       $queryIDs[] = $pageID;
+                                       break;
+                               } else {
+                                       if ( $gotArray ) {
+                                               $values[$pageID][$propertyName] = $propertyValue;
+                                       } else {
+                                               $values[$pageID] = $propertyValue;
+                                       }
+                               }
                        }
                }
 
-               if ( $queryIDs != array() ) {
+               if ( $queryIDs ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        $result = $dbr->select(
                                'page_props',
-                               array(
+                               [
                                        'pp_page',
+                                       'pp_propname',
                                        'pp_value'
-                               ),
-                               array(
+                               ],
+                               [
                                        'pp_page' => $queryIDs,
-                                       'pp_propname' => $propertyName
-                               ),
+                                       'pp_propname' => $propertyNames
+                               ],
                                __METHOD__
                        );
 
                        foreach ( $result as $row ) {
                                $pageID = $row->pp_page;
+                               $propertyName = $row->pp_propname;
                                $propertyValue = $row->pp_value;
                                $this->cacheProperty( $pageID, $propertyName, $propertyValue );
-                               $values[$pageID] = $propertyValue;
+                               if ( $gotArray ) {
+                                       $values[$pageID][$propertyName] = $propertyValue;
+                               } else {
+                                       $values[$pageID] = $propertyValue;
+                               }
                        }
                }
 
@@ -121,15 +171,13 @@ class PageProps {
         * will always be returned. An empty array will be returned if no
         * matching properties were found.
         *
-        * @param array|Title $titles
-        *
+        * @param Title[]|Title $titles
         * @return array associative array mapping page ID to property value array
-        *
         */
-       public function getProperties( $titles ) {
-               $values = array();
+       public function getAllProperties( $titles ) {
+               $values = [];
                $goodIDs = $this->getGoodIDs( $titles );
-               $queryIDs = array();
+               $queryIDs = [];
                foreach ( $goodIDs as $pageID ) {
                        $pageProperties = $this->getCachedProperties( $pageID );
                        if ( $pageProperties === false ) {
@@ -139,36 +187,36 @@ class PageProps {
                        }
                }
 
-               if ( $queryIDs != array() ) {
+               if ( $queryIDs != [] ) {
                        $dbr = wfGetDB( DB_SLAVE );
                        $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;
                        }
@@ -178,13 +226,11 @@ class PageProps {
        }
 
        /**
-        * @param array|Title $titles
-        *
+        * @param Title[]|Title $titles
         * @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();
@@ -206,9 +252,7 @@ class PageProps {
         *
         * @param int $pageID page ID of page being queried
         * @param string $propertyName name of property being queried
-        *
         * @return string|bool property value array or false if not found
-        *
         */
        private function getCachedProperty( $pageID, $propertyName ) {
                if ( $this->cache->has( $pageID, $propertyName, self::CACHE_TTL ) ) {
@@ -227,9 +271,7 @@ class PageProps {
         * Get properties from the cache.
         *
         * @param int $pageID page ID of page being queried
-        *
         * @return string|bool property value array or false if not found
-        *
         */
        private function getCachedProperties( $pageID ) {
                if ( $this->cache->has( 0, $pageID, self::CACHE_TTL ) ) {
@@ -244,7 +286,6 @@ class PageProps {
         * @param int $pageID page ID of page being cached
         * @param string $propertyName name of property being cached
         * @param mixed $propertyValue value of property
-        *
         */
        private function cacheProperty( $pageID, $propertyName, $propertyValue ) {
                $this->cache->set( $pageID, $propertyName, $propertyValue );
@@ -254,8 +295,7 @@ class PageProps {
         * Save properties to the cache.
         *
         * @param int $pageID page ID of page being cached
-        * @param array $pageProperties associative array of page properties to be cached
-        *
+        * @param string[] $pageProperties associative array of page properties to be cached
         */
        private function cacheProperties( $pageID, $pageProperties ) {
                $this->cache->clear( $pageID );