Implement extension registration from an extension.json file
[mediawiki.git] / includes / Category.php
blob2521a65a4a494b27e4be3104b004f47c0404c0ca
1 <?php
2 /**
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
20 * @file
21 * @author Simetrical
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.
29 * @todo Move some stuff from CategoryPage.php to here, and use that.
31 class Category {
32 /** Name of the category, normalized to DB-key form */
33 private $mName = null;
34 private $mID = null;
35 /**
36 * Category page title
37 * @var Title
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() {
46 /**
47 * Set up all member variables using a database query.
48 * @throws MWException
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 = array( 'cat_title' => $this->mName );
56 } elseif ( $this->mName === null ) {
57 $where = array( 'cat_id' => $this->mID );
58 } else {
59 # Already initialized
60 return true;
64 $dbr = wfGetDB( DB_SLAVE );
65 $row = $dbr->selectRow(
66 'category',
67 array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ),
68 $where,
69 __METHOD__
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;
84 return true;
85 } else {
86 return false; # Fail
90 $this->mID = $row->cat_id;
91 $this->mName = $row->cat_title;
92 $this->mPages = $row->cat_pages;
93 $this->mSubcats = $row->cat_subcats;
94 $this->mFiles = $row->cat_files;
96 # (bug 13683) If the count is negative, then 1) it's obviously wrong
97 # and should not be kept, and 2) we *probably* don't have to scan many
98 # rows to obtain the correct figure, so let's risk a one-time recount.
99 if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
100 $this->refreshCounts();
103 return true;
107 * Factory function.
109 * @param array $name A category name (no "Category:" prefix). It need
110 * not be normalized, with spaces replaced by underscores.
111 * @return mixed Category, or false on a totally invalid name
113 public static function newFromName( $name ) {
114 $cat = new self();
115 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
117 if ( !is_object( $title ) ) {
118 return false;
121 $cat->mTitle = $title;
122 $cat->mName = $title->getDBkey();
124 return $cat;
128 * Factory function.
130 * @param Title $title Title for the category page
131 * @return Category|bool On a totally invalid name
133 public static function newFromTitle( $title ) {
134 $cat = new self();
136 $cat->mTitle = $title;
137 $cat->mName = $title->getDBkey();
139 return $cat;
143 * Factory function.
145 * @param int $id A category id
146 * @return Category
148 public static function newFromID( $id ) {
149 $cat = new self();
150 $cat->mID = intval( $id );
151 return $cat;
155 * Factory function, for constructing a Category object from a result set
157 * @param object $row Result set row, must contain the cat_xxx fields. If the
158 * fields are null, the resulting Category object will represent an empty
159 * category if a title object was given. If the fields are null and no
160 * title was given, this method fails and returns false.
161 * @param Title $title Optional title object for the category represented by
162 * the given row. May be provided if it is already known, to avoid having
163 * to re-create a title object later.
164 * @return Category
166 public static function newFromRow( $row, $title = null ) {
167 $cat = new self();
168 $cat->mTitle = $title;
170 # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
171 # all the cat_xxx fields being null, if the category page exists, but nothing
172 # was ever added to the category. This case should be treated link an empty
173 # category, if possible.
175 if ( $row->cat_title === null ) {
176 if ( $title === null ) {
177 # the name is probably somewhere in the row, for example as page_title,
178 # but we can't know that here...
179 return false;
180 } else {
181 # if we have a title object, fetch the category name from there
182 $cat->mName = $title->getDBkey();
185 $cat->mID = false;
186 $cat->mSubcats = 0;
187 $cat->mPages = 0;
188 $cat->mFiles = 0;
189 } else {
190 $cat->mName = $row->cat_title;
191 $cat->mID = $row->cat_id;
192 $cat->mSubcats = $row->cat_subcats;
193 $cat->mPages = $row->cat_pages;
194 $cat->mFiles = $row->cat_files;
197 return $cat;
201 * @return mixed DB key name, or false on failure
203 public function getName() {
204 return $this->getX( 'mName' );
208 * @return mixed Category ID, or false on failure
210 public function getID() {
211 return $this->getX( 'mID' );
215 * @return mixed Total number of member pages, or false on failure
217 public function getPageCount() {
218 return $this->getX( 'mPages' );
222 * @return mixed Number of subcategories, or false on failure
224 public function getSubcatCount() {
225 return $this->getX( 'mSubcats' );
229 * @return mixed Number of member files, or false on failure
231 public function getFileCount() {
232 return $this->getX( 'mFiles' );
236 * @return Title|bool Title for this category, or false on failure.
238 public function getTitle() {
239 if ( $this->mTitle ) {
240 return $this->mTitle;
243 if ( !$this->initialize() ) {
244 return false;
247 $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
248 return $this->mTitle;
252 * Fetch a TitleArray of up to $limit category members, beginning after the
253 * category sort key $offset.
254 * @param int $limit
255 * @param string $offset
256 * @return TitleArray TitleArray object for category members.
258 public function getMembers( $limit = false, $offset = '' ) {
260 $dbr = wfGetDB( DB_SLAVE );
262 $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
263 $options = array( 'ORDER BY' => 'cl_sortkey' );
265 if ( $limit ) {
266 $options['LIMIT'] = $limit;
269 if ( $offset !== '' ) {
270 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
273 $result = TitleArray::newFromResult(
274 $dbr->select(
275 array( 'page', 'categorylinks' ),
276 array( 'page_id', 'page_namespace', 'page_title', 'page_len',
277 'page_is_redirect', 'page_latest' ),
278 $conds,
279 __METHOD__,
280 $options
285 return $result;
289 * Generic accessor
290 * @param string $key
291 * @return bool
293 private function getX( $key ) {
294 if ( !$this->initialize() ) {
295 return false;
297 return $this->{$key};
301 * Refresh the counts for this category.
303 * @return bool True on success, false on failure
305 public function refreshCounts() {
306 if ( wfReadOnly() ) {
307 return false;
310 # Note, we must use names for this, since categorylinks does.
311 if ( $this->mName === null ) {
312 if ( !$this->initialize() ) {
313 return false;
318 $dbw = wfGetDB( DB_MASTER );
319 $dbw->startAtomic( __METHOD__ );
321 # Insert the row if it doesn't exist yet (e.g., this is being run via
322 # update.php from a pre-1.16 schema). TODO: This will cause lots and
323 # lots of gaps on some non-MySQL DBMSes if you run populateCategory.php
324 # repeatedly. Plus it's an extra query that's unneeded almost all the
325 # time. This should be rewritten somehow, probably.
326 $seqVal = $dbw->nextSequenceValue( 'category_cat_id_seq' );
327 $dbw->insert(
328 'category',
329 array(
330 'cat_id' => $seqVal,
331 'cat_title' => $this->mName
333 __METHOD__,
334 'IGNORE'
337 $cond1 = $dbw->conditional( array( 'page_namespace' => NS_CATEGORY ), 1, 'NULL' );
338 $cond2 = $dbw->conditional( array( 'page_namespace' => NS_FILE ), 1, 'NULL' );
339 $result = $dbw->selectRow(
340 array( 'categorylinks', 'page' ),
341 array( 'pages' => 'COUNT(*)',
342 'subcats' => "COUNT($cond1)",
343 'files' => "COUNT($cond2)"
345 array( 'cl_to' => $this->mName, 'page_id = cl_from' ),
346 __METHOD__,
347 array( 'LOCK IN SHARE MODE' )
349 $ret = $dbw->update(
350 'category',
351 array(
352 'cat_pages' => $result->pages,
353 'cat_subcats' => $result->subcats,
354 'cat_files' => $result->files
356 array( 'cat_title' => $this->mName ),
357 __METHOD__
359 $dbw->endAtomic( __METHOD__ );
362 # Now we should update our local counts.
363 $this->mPages = $result->pages;
364 $this->mSubcats = $result->subcats;
365 $this->mFiles = $result->files;
367 return $ret;