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.
29 * @todo Move some stuff from CategoryPage.php to here, and use that.
32 /** Name of the category, normalized to DB-key form */
33 private $mName = null;
39 private $mTitle = null;
40 /** Counts of membership (cat_pages, cat_subcats, cat_files) */
41 private $mPages = null, $mSubcats = null, $mFiles = null;
43 private function __construct() {
47 * Set up all member variables using a database query.
49 * @return bool True on success, false on failure.
51 protected function initialize() {
52 if ( $this->mName
=== null && $this->mID
=== null ) {
53 throw new MWException( __METHOD__
. ' has both names and IDs null' );
54 } elseif ( $this->mID
=== null ) {
55 $where = [ 'cat_title' => $this->mName
];
56 } elseif ( $this->mName
=== null ) {
57 $where = [ 'cat_id' => $this->mID
];
63 $dbr = wfGetDB( DB_REPLICA
);
64 $row = $dbr->selectRow(
66 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
72 # Okay, there were no contents. Nothing to initialize.
73 if ( $this->mTitle
) {
74 # If there is a title object but no record in the category table,
75 # treat this as an empty category.
77 $this->mName
= $this->mTitle
->getDBkey();
82 # If the title exists, call refreshCounts to add a row for it.
83 if ( $this->mTitle
->exists() ) {
84 DeferredUpdates
::addCallableUpdate( [ $this, 'refreshCounts' ] );
93 $this->mID
= $row->cat_id
;
94 $this->mName
= $row->cat_title
;
95 $this->mPages
= $row->cat_pages
;
96 $this->mSubcats
= $row->cat_subcats
;
97 $this->mFiles
= $row->cat_files
;
99 # (bug 13683) If the count is negative, then 1) it's obviously wrong
100 # and should not be kept, and 2) we *probably* don't have to scan many
101 # rows to obtain the correct figure, so let's risk a one-time recount.
102 if ( $this->mPages
< 0 ||
$this->mSubcats
< 0 ||
$this->mFiles
< 0 ) {
103 $this->mPages
= max( $this->mPages
, 0 );
104 $this->mSubcats
= max( $this->mSubcats
, 0 );
105 $this->mFiles
= max( $this->mFiles
, 0 );
107 DeferredUpdates
::addCallableUpdate( [ $this, 'refreshCounts' ] );
116 * @param array $name A category name (no "Category:" prefix). It need
117 * not be normalized, with spaces replaced by underscores.
118 * @return mixed Category, or false on a totally invalid name
120 public static function newFromName( $name ) {
122 $title = Title
::makeTitleSafe( NS_CATEGORY
, $name );
124 if ( !is_object( $title ) ) {
128 $cat->mTitle
= $title;
129 $cat->mName
= $title->getDBkey();
137 * @param Title $title Title for the category page
138 * @return Category|bool On a totally invalid name
140 public static function newFromTitle( $title ) {
143 $cat->mTitle
= $title;
144 $cat->mName
= $title->getDBkey();
152 * @param int $id A category id
155 public static function newFromID( $id ) {
157 $cat->mID
= intval( $id );
162 * Factory function, for constructing a Category object from a result set
164 * @param object $row Result set row, must contain the cat_xxx fields. If the
165 * fields are null, the resulting Category object will represent an empty
166 * category if a title object was given. If the fields are null and no
167 * title was given, this method fails and returns false.
168 * @param Title $title Optional title object for the category represented by
169 * the given row. May be provided if it is already known, to avoid having
170 * to re-create a title object later.
171 * @return Category|false
173 public static function newFromRow( $row, $title = null ) {
175 $cat->mTitle
= $title;
177 # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
178 # all the cat_xxx fields being null, if the category page exists, but nothing
179 # was ever added to the category. This case should be treated link an empty
180 # category, if possible.
182 if ( $row->cat_title
=== null ) {
183 if ( $title === null ) {
184 # the name is probably somewhere in the row, for example as page_title,
185 # but we can't know that here...
188 # if we have a title object, fetch the category name from there
189 $cat->mName
= $title->getDBkey();
197 $cat->mName
= $row->cat_title
;
198 $cat->mID
= $row->cat_id
;
199 $cat->mSubcats
= $row->cat_subcats
;
200 $cat->mPages
= $row->cat_pages
;
201 $cat->mFiles
= $row->cat_files
;
208 * @return mixed DB key name, or false on failure
210 public function getName() {
211 return $this->getX( 'mName' );
215 * @return mixed Category ID, or false on failure
217 public function getID() {
218 return $this->getX( 'mID' );
222 * @return mixed Total number of member pages, or false on failure
224 public function getPageCount() {
225 return $this->getX( 'mPages' );
229 * @return mixed Number of subcategories, or false on failure
231 public function getSubcatCount() {
232 return $this->getX( 'mSubcats' );
236 * @return mixed Number of member files, or false on failure
238 public function getFileCount() {
239 return $this->getX( 'mFiles' );
243 * @return Title|bool Title for this category, or false on failure.
245 public function getTitle() {
246 if ( $this->mTitle
) {
247 return $this->mTitle
;
250 if ( !$this->initialize() ) {
254 $this->mTitle
= Title
::makeTitleSafe( NS_CATEGORY
, $this->mName
);
255 return $this->mTitle
;
259 * Fetch a TitleArray of up to $limit category members, beginning after the
260 * category sort key $offset.
262 * @param string $offset
263 * @return TitleArray TitleArray object for category members.
265 public function getMembers( $limit = false, $offset = '' ) {
267 $dbr = wfGetDB( DB_REPLICA
);
269 $conds = [ 'cl_to' => $this->getName(), 'cl_from = page_id' ];
270 $options = [ 'ORDER BY' => 'cl_sortkey' ];
273 $options['LIMIT'] = $limit;
276 if ( $offset !== '' ) {
277 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
280 $result = TitleArray
::newFromResult(
282 [ 'page', 'categorylinks' ],
283 [ 'page_id', 'page_namespace', 'page_title', 'page_len',
284 'page_is_redirect', 'page_latest' ],
299 private function getX( $key ) {
300 if ( !$this->initialize() ) {
303 return $this->{$key};
307 * Refresh the counts for this category.
309 * @return bool True on success, false on failure
311 public function refreshCounts() {
312 if ( wfReadOnly() ) {
316 # If we have just a category name, find out whether there is an
317 # existing row. Or if we have just an ID, get the name, because
318 # that's what categorylinks uses.
319 if ( !$this->initialize() ) {
323 $dbw = wfGetDB( DB_MASTER
);
324 $dbw->startAtomic( __METHOD__
);
326 $cond1 = $dbw->conditional( [ 'page_namespace' => NS_CATEGORY
], 1, 'NULL' );
327 $cond2 = $dbw->conditional( [ 'page_namespace' => NS_FILE
], 1, 'NULL' );
328 $result = $dbw->selectRow(
329 [ 'categorylinks', 'page' ],
330 [ 'pages' => 'COUNT(*)',
331 'subcats' => "COUNT($cond1)",
332 'files' => "COUNT($cond2)"
334 [ 'cl_to' => $this->mName
, 'page_id = cl_from' ],
336 [ 'LOCK IN SHARE MODE' ]
339 $shouldExist = $result->pages
> 0 ||
$this->getTitle()->exists();
342 if ( $shouldExist ) {
343 # The category row already exists, so do a plain UPDATE instead
344 # of INSERT...ON DUPLICATE KEY UPDATE to avoid creating a gap
345 # in the cat_id sequence. The row may or may not be "affected".
349 'cat_pages' => $result->pages
,
350 'cat_subcats' => $result->subcats
,
351 'cat_files' => $result->files
353 [ 'cat_title' => $this->mName
],
357 # The category is empty and has no description page, delete it
360 [ 'cat_title' => $this->mName
],
365 } elseif ( $shouldExist ) {
366 # The category row doesn't exist but should, so create it. Use
367 # upsert in case of races.
371 'cat_title' => $this->mName
,
372 'cat_pages' => $result->pages
,
373 'cat_subcats' => $result->subcats
,
374 'cat_files' => $result->files
378 'cat_pages' => $result->pages
,
379 'cat_subcats' => $result->subcats
,
380 'cat_files' => $result->files
384 // @todo: Should we update $this->mID here? Or not since Category
385 // objects tend to be short lived enough to not matter?
388 $dbw->endAtomic( __METHOD__
);
390 # Now we should update our local counts.
391 $this->mPages
= $result->pages
;
392 $this->mSubcats
= $result->subcats
;
393 $this->mFiles
= $result->files
;