Merge "(bug 36053) Login returnto doesn't work if title isn't in the URI"
[mediawiki.git] / includes / Category.php
blobb9c9609c5b42c77da5095eaf6e5cb0c84728ad45
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() { }
45 /**
46 * Set up all member variables using a database query.
47 * @throws MWException
48 * @return bool True on success, false on failure.
50 protected function initialize() {
51 if ( $this->mName === null && $this->mID === null ) {
52 throw new MWException( __METHOD__ . ' has both names and IDs null' );
53 } elseif ( $this->mID === null ) {
54 $where = array( 'cat_title' => $this->mName );
55 } elseif ( $this->mName === null ) {
56 $where = array( 'cat_id' => $this->mID );
57 } else {
58 # Already initialized
59 return true;
62 wfProfileIn( __METHOD__ );
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__
72 wfProfileOut( __METHOD__ );
74 if ( !$row ) {
75 # Okay, there were no contents. Nothing to initialize.
76 if ( $this->mTitle ) {
77 # If there is a title object but no record in the category table, 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 $name Array: 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 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 $id Integer: 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 $row result set row, must contain the cat_xxx fields. If the fields are null,
158 * the resulting Category object will represent an empty category if a title object
159 * was given. If the fields are null and no title was given, this method fails and returns false.
160 * @param Title $title optional title object for the category represented by the given row.
161 * May be provided if it is already known, to avoid having to re-create a title object later.
162 * @return Category
164 public static function newFromRow( $row, $title = null ) {
165 $cat = new self();
166 $cat->mTitle = $title;
168 # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
169 # all the cat_xxx fields being null, if the category page exists, but nothing
170 # was ever added to the category. This case should be treated linke an empty
171 # category, if possible.
173 if ( $row->cat_title === null ) {
174 if ( $title === null ) {
175 # the name is probably somewhere in the row, for example as page_title,
176 # but we can't know that here...
177 return false;
178 } else {
179 $cat->mName = $title->getDBkey(); # if we have a title object, fetch the category name from there
182 $cat->mID = false;
183 $cat->mSubcats = 0;
184 $cat->mPages = 0;
185 $cat->mFiles = 0;
186 } else {
187 $cat->mName = $row->cat_title;
188 $cat->mID = $row->cat_id;
189 $cat->mSubcats = $row->cat_subcats;
190 $cat->mPages = $row->cat_pages;
191 $cat->mFiles = $row->cat_files;
194 return $cat;
197 /** @return mixed DB key name, or false on failure */
198 public function getName() {
199 return $this->getX( 'mName' );
202 /** @return mixed Category ID, or false on failure */
203 public function getID() {
204 return $this->getX( 'mID' );
207 /** @return mixed Total number of member pages, or false on failure */
208 public function getPageCount() {
209 return $this->getX( 'mPages' );
212 /** @return mixed Number of subcategories, or false on failure */
213 public function getSubcatCount() {
214 return $this->getX( 'mSubcats' );
217 /** @return mixed Number of member files, or false on failure */
218 public function getFileCount() {
219 return $this->getX( 'mFiles' );
223 * @return Title|bool Title for this category, or false on failure.
225 public function getTitle() {
226 if ( $this->mTitle ) {
227 return $this->mTitle;
230 if ( !$this->initialize() ) {
231 return false;
234 $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
235 return $this->mTitle;
239 * Fetch a TitleArray of up to $limit category members, beginning after the
240 * category sort key $offset.
241 * @param $limit integer
242 * @param $offset string
243 * @return TitleArray object for category members.
245 public function getMembers( $limit = false, $offset = '' ) {
246 wfProfileIn( __METHOD__ );
248 $dbr = wfGetDB( DB_SLAVE );
250 $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
251 $options = array( 'ORDER BY' => 'cl_sortkey' );
253 if ( $limit ) {
254 $options['LIMIT'] = $limit;
257 if ( $offset !== '' ) {
258 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
261 $result = TitleArray::newFromResult(
262 $dbr->select(
263 array( 'page', 'categorylinks' ),
264 array( 'page_id', 'page_namespace', 'page_title', 'page_len',
265 'page_is_redirect', 'page_latest' ),
266 $conds,
267 __METHOD__,
268 $options
272 wfProfileOut( __METHOD__ );
274 return $result;
278 * Generic accessor
279 * @return bool
281 private function getX( $key ) {
282 if ( !$this->initialize() ) {
283 return false;
285 return $this->{$key};
289 * Refresh the counts for this category.
291 * @return bool True on success, false on failure
293 public function refreshCounts() {
294 if ( wfReadOnly() ) {
295 return false;
298 # Note, we must use names for this, since categorylinks does.
299 if ( $this->mName === null ) {
300 if ( !$this->initialize() ) {
301 return false;
305 wfProfileIn( __METHOD__ );
307 $dbw = wfGetDB( DB_MASTER );
308 $dbw->begin( __METHOD__ );
310 # Insert the row if it doesn't exist yet (e.g., this is being run via
311 # update.php from a pre-1.16 schema). TODO: This will cause lots and
312 # lots of gaps on some non-MySQL DBMSes if you run populateCategory.php
313 # repeatedly. Plus it's an extra query that's unneeded almost all the
314 # time. This should be rewritten somehow, probably.
315 $seqVal = $dbw->nextSequenceValue( 'category_cat_id_seq' );
316 $dbw->insert(
317 'category',
318 array(
319 'cat_id' => $seqVal,
320 'cat_title' => $this->mName
322 __METHOD__,
323 'IGNORE'
326 $cond1 = $dbw->conditional( array( 'page_namespace' => NS_CATEGORY ), 1, 'NULL' );
327 $cond2 = $dbw->conditional( array( 'page_namespace' => NS_FILE ), 1, 'NULL' );
328 $result = $dbw->selectRow(
329 array( 'categorylinks', 'page' ),
330 array( 'pages' => 'COUNT(*)',
331 'subcats' => "COUNT($cond1)",
332 'files' => "COUNT($cond2)"
334 array( 'cl_to' => $this->mName, 'page_id = cl_from' ),
335 __METHOD__,
336 array( 'LOCK IN SHARE MODE' )
338 $ret = $dbw->update(
339 'category',
340 array(
341 'cat_pages' => $result->pages,
342 'cat_subcats' => $result->subcats,
343 'cat_files' => $result->files
345 array( 'cat_title' => $this->mName ),
346 __METHOD__
348 $dbw->commit( __METHOD__ );
350 wfProfileOut( __METHOD__ );
352 # Now we should update our local counts.
353 $this->mPages = $result->pages;
354 $this->mSubcats = $result->subcats;
355 $this->mFiles = $result->files;
357 return $ret;