Merge "Show a warning in edit preview when a template loop is detected"
[lhc/web/wiklou.git] / includes / Category.php
index 1ebd605..629962d 100644 (file)
@@ -40,15 +40,19 @@ class Category {
        /** Counts of membership (cat_pages, cat_subcats, cat_files) */
        private $mPages = null, $mSubcats = null, $mFiles = null;
 
+       const LOAD_ONLY = 0;
+       const LAZY_INIT_ROW = 1;
+
        private function __construct() {
        }
 
        /**
         * Set up all member variables using a database query.
+        * @param int $mode
         * @throws MWException
         * @return bool True on success, false on failure.
         */
-       protected function initialize() {
+       protected function initialize( $mode = self::LOAD_ONLY ) {
                if ( $this->mName === null && $this->mID === null ) {
                        throw new MWException( __METHOD__ . ' has both names and IDs null' );
                } elseif ( $this->mID === null ) {
@@ -80,7 +84,7 @@ class Category {
                                $this->mFiles = 0;
 
                                # If the title exists, call refreshCounts to add a row for it.
-                               if ( $this->mTitle->exists() ) {
+                               if ( $mode === self::LAZY_INIT_ROW && $this->mTitle->exists() ) {
                                        DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
                                }
 
@@ -96,7 +100,7 @@ class Category {
                $this->mSubcats = $row->cat_subcats;
                $this->mFiles = $row->cat_files;
 
-               # (bug 13683) If the count is negative, then 1) it's obviously wrong
+               # (T15683) If the count is negative, then 1) it's obviously wrong
                # and should not be kept, and 2) we *probably* don't have to scan many
                # rows to obtain the correct figure, so let's risk a one-time recount.
                if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
@@ -104,7 +108,9 @@ class Category {
                        $this->mSubcats = max( $this->mSubcats, 0 );
                        $this->mFiles = max( $this->mFiles, 0 );
 
-                       DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
+                       if ( $mode === self::LAZY_INIT_ROW ) {
+                               DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
+                       }
                }
 
                return true;
@@ -168,7 +174,7 @@ class Category {
         * @param Title $title Optional title object for the category represented by
         *   the given row. May be provided if it is already known, to avoid having
         *   to re-create a title object later.
-        * @return Category
+        * @return Category|false
         */
        public static function newFromRow( $row, $title = null ) {
                $cat = new self();
@@ -247,7 +253,7 @@ class Category {
                        return $this->mTitle;
                }
 
-               if ( !$this->initialize() ) {
+               if ( !$this->initialize( self::LAZY_INIT_ROW ) ) {
                        return false;
                }
 
@@ -258,12 +264,11 @@ class Category {
        /**
         * Fetch a TitleArray of up to $limit category members, beginning after the
         * category sort key $offset.
-        * @param int $limit
+        * @param int|bool $limit
         * @param string $offset
         * @return TitleArray TitleArray object for category members.
         */
        public function getMembers( $limit = false, $offset = '' ) {
-
                $dbr = wfGetDB( DB_REPLICA );
 
                $conds = [ 'cl_to' => $this->getName(), 'cl_from = page_id' ];
@@ -297,7 +302,7 @@ class Category {
         * @return bool
         */
        private function getX( $key ) {
-               if ( !$this->initialize() ) {
+               if ( !$this->initialize( self::LAZY_INIT_ROW ) ) {
                        return false;
                }
                return $this->{$key};
@@ -316,11 +321,18 @@ class Category {
                # If we have just a category name, find out whether there is an
                # existing row. Or if we have just an ID, get the name, because
                # that's what categorylinks uses.
-               if ( !$this->initialize() ) {
+               if ( !$this->initialize( self::LOAD_ONLY ) ) {
                        return false;
                }
 
                $dbw = wfGetDB( DB_MASTER );
+               # Avoid excess contention on the same category (T162121)
+               $name = __METHOD__ . ':' . md5( $this->mName );
+               $scopedLock = $dbw->getScopedLockAndFlush( $name, __METHOD__, 1 );
+               if ( !$scopedLock ) {
+                       return false;
+               }
+
                $dbw->startAtomic( __METHOD__ );
 
                $cond1 = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY ], 1, 'NULL' );