3 * Representation for a category.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Category objects are immutable, strictly speaking. If you call methods that change the database,
26 * like to refresh link counts, the objects will be appropriately reinitialized.
27 * Member variables are lazy-initialized.
30 /** Name of the category, normalized to DB-key form */
31 private $mName = null;
37 private $mTitle = null;
38 /** Counts of membership (cat_pages, cat_subcats, cat_files) */
39 private $mPages = null, $mSubcats = null, $mFiles = null;
42 const LAZY_INIT_ROW
= 1;
44 private function __construct() {
48 * Set up all member variables using a database query.
49 * @param int $mode One of (Category::LOAD_ONLY, Category::LAZY_INIT_ROW)
51 * @return bool True on success, false on failure.
53 protected function initialize( $mode = self
::LOAD_ONLY
) {
54 if ( $this->mName
=== null && $this->mID
=== null ) {
55 throw new MWException( __METHOD__
. ' has both names and IDs null' );
56 } elseif ( $this->mID
=== null ) {
57 $where = [ 'cat_title' => $this->mName
];
58 } elseif ( $this->mName
=== null ) {
59 $where = [ 'cat_id' => $this->mID
];
65 $dbr = wfGetDB( DB_REPLICA
);
66 $row = $dbr->selectRow(
68 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
74 # Okay, there were no contents. Nothing to initialize.
75 if ( $this->mTitle
) {
76 # If there is a title object but no record in the category table,
77 # treat this as an empty category.
79 $this->mName
= $this->mTitle
->getDBkey();
84 # If the title exists, call refreshCounts to add a row for it.
85 if ( $mode === self
::LAZY_INIT_ROW
&& $this->mTitle
->exists() ) {
86 DeferredUpdates
::addCallableUpdate( [ $this, 'refreshCounts' ] );
95 $this->mID
= $row->cat_id
;
96 $this->mName
= $row->cat_title
;
97 $this->mPages
= $row->cat_pages
;
98 $this->mSubcats
= $row->cat_subcats
;
99 $this->mFiles
= $row->cat_files
;
101 # (T15683) If the count is negative, then 1) it's obviously wrong
102 # and should not be kept, and 2) we *probably* don't have to scan many
103 # rows to obtain the correct figure, so let's risk a one-time recount.
104 if ( $this->mPages
< 0 ||
$this->mSubcats
< 0 ||
$this->mFiles
< 0 ) {
105 $this->mPages
= max( $this->mPages
, 0 );
106 $this->mSubcats
= max( $this->mSubcats
, 0 );
107 $this->mFiles
= max( $this->mFiles
, 0 );
109 if ( $mode === self
::LAZY_INIT_ROW
) {
110 DeferredUpdates
::addCallableUpdate( [ $this, 'refreshCounts' ] );
120 * @param string $name A category name (no "Category:" prefix). It need
121 * not be normalized, with spaces replaced by underscores.
122 * @return Category|bool Category, or false on a totally invalid name
124 public static function newFromName( $name ) {
126 $title = Title
::makeTitleSafe( NS_CATEGORY
, $name );
128 if ( !is_object( $title ) ) {
132 $cat->mTitle
= $title;
133 $cat->mName
= $title->getDBkey();
141 * @param Title $title Title for the category page
142 * @return Category|bool On a totally invalid name
144 public static function newFromTitle( $title ) {
147 $cat->mTitle
= $title;
148 $cat->mName
= $title->getDBkey();
156 * @param int $id A category id
159 public static function newFromID( $id ) {
161 $cat->mID
= intval( $id );
166 * Factory function, for constructing a Category object from a result set
168 * @param object $row Result set row, must contain the cat_xxx fields. If the
169 * fields are null, the resulting Category object will represent an empty
170 * category if a title object was given. If the fields are null and no
171 * title was given, this method fails and returns false.
172 * @param Title|null $title Optional title object for the category represented by
173 * the given row. May be provided if it is already known, to avoid having
174 * to re-create a title object later.
175 * @return Category|false
177 public static function newFromRow( $row, $title = null ) {
179 $cat->mTitle
= $title;
181 # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
182 # all the cat_xxx fields being null, if the category page exists, but nothing
183 # was ever added to the category. This case should be treated link an empty
184 # category, if possible.
186 if ( $row->cat_title
=== null ) {
187 if ( $title === null ) {
188 # the name is probably somewhere in the row, for example as page_title,
189 # but we can't know that here...
192 # if we have a title object, fetch the category name from there
193 $cat->mName
= $title->getDBkey();
201 $cat->mName
= $row->cat_title
;
202 $cat->mID
= $row->cat_id
;
203 $cat->mSubcats
= $row->cat_subcats
;
204 $cat->mPages
= $row->cat_pages
;
205 $cat->mFiles
= $row->cat_files
;
212 * @return mixed DB key name, or false on failure
214 public function getName() {
215 return $this->getX( 'mName' );
219 * @return mixed Category ID, or false on failure
221 public function getID() {
222 return $this->getX( 'mID' );
226 * @return mixed Total number of member pages, or false on failure
228 public function getPageCount() {
229 return $this->getX( 'mPages' );
233 * @return mixed Number of subcategories, or false on failure
235 public function getSubcatCount() {
236 return $this->getX( 'mSubcats' );
240 * @return mixed Number of member files, or false on failure
242 public function getFileCount() {
243 return $this->getX( 'mFiles' );
247 * @return Title|bool Title for this category, or false on failure.
249 public function getTitle() {
250 if ( $this->mTitle
) {
251 return $this->mTitle
;
254 if ( !$this->initialize( self
::LAZY_INIT_ROW
) ) {
258 $this->mTitle
= Title
::makeTitleSafe( NS_CATEGORY
, $this->mName
);
259 return $this->mTitle
;
263 * Fetch a TitleArray of up to $limit category members, beginning after the
264 * category sort key $offset.
265 * @param int|bool $limit
266 * @param string $offset
267 * @return TitleArray TitleArray object for category members.
269 public function getMembers( $limit = false, $offset = '' ) {
270 $dbr = wfGetDB( DB_REPLICA
);
272 $conds = [ 'cl_to' => $this->getName(), 'cl_from = page_id' ];
273 $options = [ 'ORDER BY' => 'cl_sortkey' ];
276 $options['LIMIT'] = $limit;
279 if ( $offset !== '' ) {
280 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
283 $result = TitleArray
::newFromResult(
285 [ 'page', 'categorylinks' ],
286 [ 'page_id', 'page_namespace', 'page_title', 'page_len',
287 'page_is_redirect', 'page_latest' ],
302 private function getX( $key ) {
303 if ( $this->{$key} === null && !$this->initialize( self
::LAZY_INIT_ROW
) ) {
306 return $this->{$key};
310 * Refresh the counts for this category.
312 * @return bool True on success, false on failure
314 public function refreshCounts() {
315 if ( wfReadOnly() ) {
319 # If we have just a category name, find out whether there is an
320 # existing row. Or if we have just an ID, get the name, because
321 # that's what categorylinks uses.
322 if ( !$this->initialize( self
::LOAD_ONLY
) ) {
326 $dbw = wfGetDB( DB_MASTER
);
327 # Avoid excess contention on the same category (T162121)
328 $name = __METHOD__
. ':' . md5( $this->mName
);
329 $scopedLock = $dbw->getScopedLockAndFlush( $name, __METHOD__
, 0 );
330 if ( !$scopedLock ) {
334 $dbw->startAtomic( __METHOD__
);
336 // Lock the `category` row before locking `categorylinks` rows to try
337 // to avoid deadlocks with LinksDeletionUpdate (T195397)
338 $dbw->lockForUpdate( 'category', [ 'cat_title' => $this->mName
], __METHOD__
);
340 // Lock all the `categorylinks` records and gaps for this category;
341 // this is a separate query due to postgres/oracle limitations
342 $dbw->selectRowCount(
343 [ 'categorylinks', 'page' ],
345 [ 'cl_to' => $this->mName
, 'page_id = cl_from' ],
347 [ 'LOCK IN SHARE MODE' ]
349 // Get the aggregate `categorylinks` row counts for this category
350 $catCond = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY
], 1, 'NULL' );
351 $fileCond = $dbw->conditional( [ 'page_namespace' => NS_FILE
], 1, 'NULL' );
352 $result = $dbw->selectRow(
353 [ 'categorylinks', 'page' ],
355 'pages' => 'COUNT(*)',
356 'subcats' => "COUNT($catCond)",
357 'files' => "COUNT($fileCond)"
359 [ 'cl_to' => $this->mName
, 'page_id = cl_from' ],
363 $shouldExist = $result->pages
> 0 ||
$this->getTitle()->exists();
366 if ( $shouldExist ) {
367 # The category row already exists, so do a plain UPDATE instead
368 # of INSERT...ON DUPLICATE KEY UPDATE to avoid creating a gap
369 # in the cat_id sequence. The row may or may not be "affected".
373 'cat_pages' => $result->pages
,
374 'cat_subcats' => $result->subcats
,
375 'cat_files' => $result->files
377 [ 'cat_title' => $this->mName
],
381 # The category is empty and has no description page, delete it
384 [ 'cat_title' => $this->mName
],
389 } elseif ( $shouldExist ) {
390 # The category row doesn't exist but should, so create it. Use
391 # upsert in case of races.
395 'cat_title' => $this->mName
,
396 'cat_pages' => $result->pages
,
397 'cat_subcats' => $result->subcats
,
398 'cat_files' => $result->files
402 'cat_pages' => $result->pages
,
403 'cat_subcats' => $result->subcats
,
404 'cat_files' => $result->files
408 // @todo: Should we update $this->mID here? Or not since Category
409 // objects tend to be short lived enough to not matter?
412 $dbw->endAtomic( __METHOD__
);
414 # Now we should update our local counts.
415 $this->mPages
= $result->pages
;
416 $this->mSubcats
= $result->subcats
;
417 $this->mFiles
= $result->files
;
423 * Call refreshCounts() if there are no entries in the categorylinks table
424 * or if the category table has a row that states that there are no entries
426 * Due to lock errors or other failures, the precomputed counts can get out of sync,
427 * making it hard to know when to delete the category row without checking the
428 * categorylinks table.
430 * @return bool Whether links were refreshed
433 public function refreshCountsIfEmpty() {
434 $dbw = wfGetDB( DB_MASTER
);
436 $hasLink = $dbw->selectField(
439 [ 'cl_to' => $this->getName() ],
443 $this->refreshCounts(); // delete any category table entry
448 $hasBadRow = $dbw->selectField(
451 [ 'cat_title' => $this->getName(), 'cat_pages <= 0' ],
455 $this->refreshCounts(); // clean up this row