Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / Category.php
blob38601725700e8111d3c52849e0b5618422e36f8d
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;
63 $dbr = wfGetDB( DB_SLAVE );
64 $row = $dbr->selectRow(
65 'category',
66 array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ),
67 $where,
68 __METHOD__
71 if ( !$row ) {
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.
76 $this->mID = false;
77 $this->mName = $this->mTitle->getDBkey();
78 $this->mPages = 0;
79 $this->mSubcats = 0;
80 $this->mFiles = 0;
82 return true;
83 } else {
84 return false; # Fail
88 $this->mID = $row->cat_id;
89 $this->mName = $row->cat_title;
90 $this->mPages = $row->cat_pages;
91 $this->mSubcats = $row->cat_subcats;
92 $this->mFiles = $row->cat_files;
94 # (bug 13683) If the count is negative, then 1) it's obviously wrong
95 # and should not be kept, and 2) we *probably* don't have to scan many
96 # rows to obtain the correct figure, so let's risk a one-time recount.
97 if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
98 $this->refreshCounts();
101 return true;
105 * Factory function.
107 * @param array $name A category name (no "Category:" prefix). It need
108 * not be normalized, with spaces replaced by underscores.
109 * @return mixed Category, or false on a totally invalid name
111 public static function newFromName( $name ) {
112 $cat = new self();
113 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
115 if ( !is_object( $title ) ) {
116 return false;
119 $cat->mTitle = $title;
120 $cat->mName = $title->getDBkey();
122 return $cat;
126 * Factory function.
128 * @param Title $title Title for the category page
129 * @return Category|bool On a totally invalid name
131 public static function newFromTitle( $title ) {
132 $cat = new self();
134 $cat->mTitle = $title;
135 $cat->mName = $title->getDBkey();
137 return $cat;
141 * Factory function.
143 * @param int $id A category id
144 * @return Category
146 public static function newFromID( $id ) {
147 $cat = new self();
148 $cat->mID = intval( $id );
149 return $cat;
153 * Factory function, for constructing a Category object from a result set
155 * @param object $row Result set row, must contain the cat_xxx fields. If the
156 * fields are null, the resulting Category object will represent an empty
157 * category if a title object was given. If the fields are null and no
158 * title was given, this method fails and returns false.
159 * @param Title $title Optional title object for the category represented by
160 * the given row. May be provided if it is already known, to avoid having
161 * 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 link 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 # if we have a title object, fetch the category name from there
180 $cat->mName = $title->getDBkey();
183 $cat->mID = false;
184 $cat->mSubcats = 0;
185 $cat->mPages = 0;
186 $cat->mFiles = 0;
187 } else {
188 $cat->mName = $row->cat_title;
189 $cat->mID = $row->cat_id;
190 $cat->mSubcats = $row->cat_subcats;
191 $cat->mPages = $row->cat_pages;
192 $cat->mFiles = $row->cat_files;
195 return $cat;
199 * @return mixed DB key name, or false on failure
201 public function getName() {
202 return $this->getX( 'mName' );
206 * @return mixed Category ID, or false on failure
208 public function getID() {
209 return $this->getX( 'mID' );
213 * @return mixed Total number of member pages, or false on failure
215 public function getPageCount() {
216 return $this->getX( 'mPages' );
220 * @return mixed Number of subcategories, or false on failure
222 public function getSubcatCount() {
223 return $this->getX( 'mSubcats' );
227 * @return mixed Number of member files, or false on failure
229 public function getFileCount() {
230 return $this->getX( 'mFiles' );
234 * @return Title|bool Title for this category, or false on failure.
236 public function getTitle() {
237 if ( $this->mTitle ) {
238 return $this->mTitle;
241 if ( !$this->initialize() ) {
242 return false;
245 $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
246 return $this->mTitle;
250 * Fetch a TitleArray of up to $limit category members, beginning after the
251 * category sort key $offset.
252 * @param int $limit
253 * @param string $offset
254 * @return TitleArray TitleArray object for category members.
256 public function getMembers( $limit = false, $offset = '' ) {
258 $dbr = wfGetDB( DB_SLAVE );
260 $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
261 $options = array( 'ORDER BY' => 'cl_sortkey' );
263 if ( $limit ) {
264 $options['LIMIT'] = $limit;
267 if ( $offset !== '' ) {
268 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
271 $result = TitleArray::newFromResult(
272 $dbr->select(
273 array( 'page', 'categorylinks' ),
274 array( 'page_id', 'page_namespace', 'page_title', 'page_len',
275 'page_is_redirect', 'page_latest' ),
276 $conds,
277 __METHOD__,
278 $options
282 return $result;
286 * Generic accessor
287 * @param string $key
288 * @return bool
290 private function getX( $key ) {
291 if ( !$this->initialize() ) {
292 return false;
294 return $this->{$key};
298 * Refresh the counts for this category.
300 * @return bool True on success, false on failure
302 public function refreshCounts() {
303 if ( wfReadOnly() ) {
304 return false;
307 # If we have just a category name, find out whether there is an
308 # existing row. Or if we have just an ID, get the name, because
309 # that's what categorylinks uses.
310 if ( !$this->initialize() ) {
311 return false;
314 $dbw = wfGetDB( DB_MASTER );
315 $dbw->startAtomic( __METHOD__ );
317 $cond1 = $dbw->conditional( array( 'page_namespace' => NS_CATEGORY ), 1, 'NULL' );
318 $cond2 = $dbw->conditional( array( 'page_namespace' => NS_FILE ), 1, 'NULL' );
319 $result = $dbw->selectRow(
320 array( 'categorylinks', 'page' ),
321 array( 'pages' => 'COUNT(*)',
322 'subcats' => "COUNT($cond1)",
323 'files' => "COUNT($cond2)"
325 array( 'cl_to' => $this->mName, 'page_id = cl_from' ),
326 __METHOD__,
327 array( 'LOCK IN SHARE MODE' )
330 if ( $this->mID ) {
331 # The category row already exists, so do a plain UPDATE instead
332 # of INSERT...ON DUPLICATE KEY UPDATE to avoid creating a gap
333 # in the cat_id sequence. The row may or may not be "affected".
334 $dbw->update(
335 'category',
336 array(
337 'cat_pages' => $result->pages,
338 'cat_subcats' => $result->subcats,
339 'cat_files' => $result->files
341 array( 'cat_title' => $this->mName ),
342 __METHOD__
344 } else {
345 $dbw->upsert(
346 'category',
347 array(
348 'cat_title' => $this->mName,
349 'cat_pages' => $result->pages,
350 'cat_subcats' => $result->subcats,
351 'cat_files' => $result->files
353 array( 'cat_title' ),
354 array(
355 'cat_pages' => $result->pages,
356 'cat_subcats' => $result->subcats,
357 'cat_files' => $result->files
359 __METHOD__
363 $dbw->endAtomic( __METHOD__ );
365 # Now we should update our local counts.
366 $this->mPages = $result->pages;
367 $this->mSubcats = $result->subcats;
368 $this->mFiles = $result->files;
370 return true;