Remove HWLDFWordAccumulator, deprecated in 1.28
[lhc/web/wiklou.git] / includes / Category.php
1 <?php
2 /**
3 * Representation for a category.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Simetrical
22 */
23
24 /**
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.
28 */
29 class Category {
30 /** Name of the category, normalized to DB-key form */
31 private $mName = null;
32 private $mID = null;
33 /**
34 * Category page title
35 * @var Title
36 */
37 private $mTitle = null;
38 /** Counts of membership (cat_pages, cat_subcats, cat_files) */
39 private $mPages = null, $mSubcats = null, $mFiles = null;
40
41 const LOAD_ONLY = 0;
42 const LAZY_INIT_ROW = 1;
43
44 private function __construct() {
45 }
46
47 /**
48 * Set up all member variables using a database query.
49 * @param int $mode One of (Category::LOAD_ONLY, Category::LAZY_INIT_ROW)
50 * @throws MWException
51 * @return bool True on success, false on failure.
52 */
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 ];
60 } else {
61 # Already initialized
62 return true;
63 }
64
65 $dbr = wfGetDB( DB_REPLICA );
66 $row = $dbr->selectRow(
67 'category',
68 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
69 $where,
70 __METHOD__
71 );
72
73 if ( !$row ) {
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.
78 $this->mID = false;
79 $this->mName = $this->mTitle->getDBkey();
80 $this->mPages = 0;
81 $this->mSubcats = 0;
82 $this->mFiles = 0;
83
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' ] );
87 }
88
89 return true;
90 } else {
91 return false; # Fail
92 }
93 }
94
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;
100
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 );
108
109 if ( $mode === self::LAZY_INIT_ROW ) {
110 DeferredUpdates::addCallableUpdate( [ $this, 'refreshCounts' ] );
111 }
112 }
113
114 return true;
115 }
116
117 /**
118 * Factory function.
119 *
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
123 */
124 public static function newFromName( $name ) {
125 $cat = new self();
126 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
127
128 if ( !is_object( $title ) ) {
129 return false;
130 }
131
132 $cat->mTitle = $title;
133 $cat->mName = $title->getDBkey();
134
135 return $cat;
136 }
137
138 /**
139 * Factory function.
140 *
141 * @param Title $title Title for the category page
142 * @return Category|bool On a totally invalid name
143 */
144 public static function newFromTitle( $title ) {
145 $cat = new self();
146
147 $cat->mTitle = $title;
148 $cat->mName = $title->getDBkey();
149
150 return $cat;
151 }
152
153 /**
154 * Factory function.
155 *
156 * @param int $id A category id
157 * @return Category
158 */
159 public static function newFromID( $id ) {
160 $cat = new self();
161 $cat->mID = intval( $id );
162 return $cat;
163 }
164
165 /**
166 * Factory function, for constructing a Category object from a result set
167 *
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
176 */
177 public static function newFromRow( $row, $title = null ) {
178 $cat = new self();
179 $cat->mTitle = $title;
180
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.
185
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...
190 return false;
191 } else {
192 # if we have a title object, fetch the category name from there
193 $cat->mName = $title->getDBkey();
194 }
195
196 $cat->mID = false;
197 $cat->mSubcats = 0;
198 $cat->mPages = 0;
199 $cat->mFiles = 0;
200 } else {
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;
206 }
207
208 return $cat;
209 }
210
211 /**
212 * @return mixed DB key name, or false on failure
213 */
214 public function getName() {
215 return $this->getX( 'mName' );
216 }
217
218 /**
219 * @return mixed Category ID, or false on failure
220 */
221 public function getID() {
222 return $this->getX( 'mID' );
223 }
224
225 /**
226 * @return mixed Total number of member pages, or false on failure
227 */
228 public function getPageCount() {
229 return $this->getX( 'mPages' );
230 }
231
232 /**
233 * @return mixed Number of subcategories, or false on failure
234 */
235 public function getSubcatCount() {
236 return $this->getX( 'mSubcats' );
237 }
238
239 /**
240 * @return mixed Number of member files, or false on failure
241 */
242 public function getFileCount() {
243 return $this->getX( 'mFiles' );
244 }
245
246 /**
247 * @return Title|bool Title for this category, or false on failure.
248 */
249 public function getTitle() {
250 if ( $this->mTitle ) {
251 return $this->mTitle;
252 }
253
254 if ( !$this->initialize( self::LAZY_INIT_ROW ) ) {
255 return false;
256 }
257
258 $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
259 return $this->mTitle;
260 }
261
262 /**
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.
268 */
269 public function getMembers( $limit = false, $offset = '' ) {
270 $dbr = wfGetDB( DB_REPLICA );
271
272 $conds = [ 'cl_to' => $this->getName(), 'cl_from = page_id' ];
273 $options = [ 'ORDER BY' => 'cl_sortkey' ];
274
275 if ( $limit ) {
276 $options['LIMIT'] = $limit;
277 }
278
279 if ( $offset !== '' ) {
280 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
281 }
282
283 $result = TitleArray::newFromResult(
284 $dbr->select(
285 [ 'page', 'categorylinks' ],
286 [ 'page_id', 'page_namespace', 'page_title', 'page_len',
287 'page_is_redirect', 'page_latest' ],
288 $conds,
289 __METHOD__,
290 $options
291 )
292 );
293
294 return $result;
295 }
296
297 /**
298 * Generic accessor
299 * @param string $key
300 * @return mixed
301 */
302 private function getX( $key ) {
303 if ( $this->{$key} === null && !$this->initialize( self::LAZY_INIT_ROW ) ) {
304 return false;
305 }
306 return $this->{$key};
307 }
308
309 /**
310 * Refresh the counts for this category.
311 *
312 * @return bool True on success, false on failure
313 */
314 public function refreshCounts() {
315 if ( wfReadOnly() ) {
316 return false;
317 }
318
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 ) ) {
323 return false;
324 }
325
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 ) {
331 return false;
332 }
333
334 $dbw->startAtomic( __METHOD__ );
335
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__ );
339
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' ],
344 '*',
345 [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
346 __METHOD__,
347 [ 'LOCK IN SHARE MODE' ]
348 );
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' ],
354 [
355 'pages' => 'COUNT(*)',
356 'subcats' => "COUNT($catCond)",
357 'files' => "COUNT($fileCond)"
358 ],
359 [ 'cl_to' => $this->mName, 'page_id = cl_from' ],
360 __METHOD__
361 );
362
363 $shouldExist = $result->pages > 0 || $this->getTitle()->exists();
364
365 if ( $this->mID ) {
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".
370 $dbw->update(
371 'category',
372 [
373 'cat_pages' => $result->pages,
374 'cat_subcats' => $result->subcats,
375 'cat_files' => $result->files
376 ],
377 [ 'cat_title' => $this->mName ],
378 __METHOD__
379 );
380 } else {
381 # The category is empty and has no description page, delete it
382 $dbw->delete(
383 'category',
384 [ 'cat_title' => $this->mName ],
385 __METHOD__
386 );
387 $this->mID = false;
388 }
389 } elseif ( $shouldExist ) {
390 # The category row doesn't exist but should, so create it. Use
391 # upsert in case of races.
392 $dbw->upsert(
393 'category',
394 [
395 'cat_title' => $this->mName,
396 'cat_pages' => $result->pages,
397 'cat_subcats' => $result->subcats,
398 'cat_files' => $result->files
399 ],
400 [ 'cat_title' ],
401 [
402 'cat_pages' => $result->pages,
403 'cat_subcats' => $result->subcats,
404 'cat_files' => $result->files
405 ],
406 __METHOD__
407 );
408 // @todo: Should we update $this->mID here? Or not since Category
409 // objects tend to be short lived enough to not matter?
410 }
411
412 $dbw->endAtomic( __METHOD__ );
413
414 # Now we should update our local counts.
415 $this->mPages = $result->pages;
416 $this->mSubcats = $result->subcats;
417 $this->mFiles = $result->files;
418
419 return true;
420 }
421
422 /**
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
425 *
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.
429 *
430 * @return bool Whether links were refreshed
431 * @since 1.32
432 */
433 public function refreshCountsIfEmpty() {
434 $dbw = wfGetDB( DB_MASTER );
435
436 $hasLink = $dbw->selectField(
437 'categorylinks',
438 '1',
439 [ 'cl_to' => $this->getName() ],
440 __METHOD__
441 );
442 if ( !$hasLink ) {
443 $this->refreshCounts(); // delete any category table entry
444
445 return true;
446 }
447
448 $hasBadRow = $dbw->selectField(
449 'category',
450 '1',
451 [ 'cat_title' => $this->getName(), 'cat_pages <= 0' ],
452 __METHOD__
453 );
454 if ( $hasBadRow ) {
455 $this->refreshCounts(); // clean up this row
456
457 return true;
458 }
459
460 return false;
461 }
462 }