Clear user cache before checking userrights conflict
[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 * TODO: Move some stuff from CategoryPage.php to here, and use that.
30 */
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
38 */
39 private $mTitle = null;
40 /** Counts of membership (cat_pages, cat_subcats, cat_files) */
41 private $mPages = null, $mSubcats = null, $mFiles = null;
42
43 private function __construct() {
44 }
45
46 /**
47 * Set up all member variables using a database query.
48 * @throws MWException
49 * @return bool True on success, false on failure.
50 */
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;
61 }
62
63 wfProfileIn( __METHOD__ );
64
65 $dbr = wfGetDB( DB_SLAVE );
66 $row = $dbr->selectRow(
67 'category',
68 array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ),
69 $where,
70 __METHOD__
71 );
72
73 wfProfileOut( __METHOD__ );
74
75 if ( !$row ) {
76 # Okay, there were no contents. Nothing to initialize.
77 if ( $this->mTitle ) {
78 # If there is a title object but no record in the category table, treat this as an empty category
79 $this->mID = false;
80 $this->mName = $this->mTitle->getDBkey();
81 $this->mPages = 0;
82 $this->mSubcats = 0;
83 $this->mFiles = 0;
84
85 return true;
86 } else {
87 return false; # Fail
88 }
89 }
90
91 $this->mID = $row->cat_id;
92 $this->mName = $row->cat_title;
93 $this->mPages = $row->cat_pages;
94 $this->mSubcats = $row->cat_subcats;
95 $this->mFiles = $row->cat_files;
96
97 # (bug 13683) If the count is negative, then 1) it's obviously wrong
98 # and should not be kept, and 2) we *probably* don't have to scan many
99 # rows to obtain the correct figure, so let's risk a one-time recount.
100 if ( $this->mPages < 0 || $this->mSubcats < 0 || $this->mFiles < 0 ) {
101 $this->refreshCounts();
102 }
103
104 return true;
105 }
106
107 /**
108 * Factory function.
109 *
110 * @param array $name A category name (no "Category:" prefix). It need
111 * not be normalized, with spaces replaced by underscores.
112 * @return mixed Category, or false on a totally invalid name
113 */
114 public static function newFromName( $name ) {
115 $cat = new self();
116 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
117
118 if ( !is_object( $title ) ) {
119 return false;
120 }
121
122 $cat->mTitle = $title;
123 $cat->mName = $title->getDBkey();
124
125 return $cat;
126 }
127
128 /**
129 * Factory function.
130 *
131 * @param $title Title for the category page
132 * @return Category|bool on a totally invalid name
133 */
134 public static function newFromTitle( $title ) {
135 $cat = new self();
136
137 $cat->mTitle = $title;
138 $cat->mName = $title->getDBkey();
139
140 return $cat;
141 }
142
143 /**
144 * Factory function.
145 *
146 * @param $id Integer: a category id
147 * @return Category
148 */
149 public static function newFromID( $id ) {
150 $cat = new self();
151 $cat->mID = intval( $id );
152 return $cat;
153 }
154
155 /**
156 * Factory function, for constructing a Category object from a result set
157 *
158 * @param $row result set row, must contain the cat_xxx fields. If the fields are null,
159 * the resulting Category object will represent an empty category if a title object
160 * was given. If the fields are null and no title was given, this method fails and returns false.
161 * @param Title $title optional title object for the category represented by the given row.
162 * May be provided if it is already known, to avoid having to re-create a title object later.
163 * @return Category
164 */
165 public static function newFromRow( $row, $title = null ) {
166 $cat = new self();
167 $cat->mTitle = $title;
168
169 # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
170 # all the cat_xxx fields being null, if the category page exists, but nothing
171 # was ever added to the category. This case should be treated link an empty
172 # category, if possible.
173
174 if ( $row->cat_title === null ) {
175 if ( $title === null ) {
176 # the name is probably somewhere in the row, for example as page_title,
177 # but we can't know that here...
178 return false;
179 } else {
180 $cat->mName = $title->getDBkey(); # if we have a title object, fetch the category name from there
181 }
182
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;
193 }
194
195 return $cat;
196 }
197
198 /** @return mixed DB key name, or false on failure */
199 public function getName() {
200 return $this->getX( 'mName' );
201 }
202
203 /** @return mixed Category ID, or false on failure */
204 public function getID() {
205 return $this->getX( 'mID' );
206 }
207
208 /** @return mixed Total number of member pages, or false on failure */
209 public function getPageCount() {
210 return $this->getX( 'mPages' );
211 }
212
213 /** @return mixed Number of subcategories, or false on failure */
214 public function getSubcatCount() {
215 return $this->getX( 'mSubcats' );
216 }
217
218 /** @return mixed Number of member files, or false on failure */
219 public function getFileCount() {
220 return $this->getX( 'mFiles' );
221 }
222
223 /**
224 * @return Title|bool Title for this category, or false on failure.
225 */
226 public function getTitle() {
227 if ( $this->mTitle ) {
228 return $this->mTitle;
229 }
230
231 if ( !$this->initialize() ) {
232 return false;
233 }
234
235 $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
236 return $this->mTitle;
237 }
238
239 /**
240 * Fetch a TitleArray of up to $limit category members, beginning after the
241 * category sort key $offset.
242 * @param $limit integer
243 * @param $offset string
244 * @return TitleArray object for category members.
245 */
246 public function getMembers( $limit = false, $offset = '' ) {
247 wfProfileIn( __METHOD__ );
248
249 $dbr = wfGetDB( DB_SLAVE );
250
251 $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
252 $options = array( 'ORDER BY' => 'cl_sortkey' );
253
254 if ( $limit ) {
255 $options['LIMIT'] = $limit;
256 }
257
258 if ( $offset !== '' ) {
259 $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
260 }
261
262 $result = TitleArray::newFromResult(
263 $dbr->select(
264 array( 'page', 'categorylinks' ),
265 array( 'page_id', 'page_namespace', 'page_title', 'page_len',
266 'page_is_redirect', 'page_latest' ),
267 $conds,
268 __METHOD__,
269 $options
270 )
271 );
272
273 wfProfileOut( __METHOD__ );
274
275 return $result;
276 }
277
278 /**
279 * Generic accessor
280 * @return bool
281 */
282 private function getX( $key ) {
283 if ( !$this->initialize() ) {
284 return false;
285 }
286 return $this->{$key};
287 }
288
289 /**
290 * Refresh the counts for this category.
291 *
292 * @return bool True on success, false on failure
293 */
294 public function refreshCounts() {
295 if ( wfReadOnly() ) {
296 return false;
297 }
298
299 # Note, we must use names for this, since categorylinks does.
300 if ( $this->mName === null ) {
301 if ( !$this->initialize() ) {
302 return false;
303 }
304 }
305
306 wfProfileIn( __METHOD__ );
307
308 $dbw = wfGetDB( DB_MASTER );
309 $dbw->begin( __METHOD__ );
310
311 # Insert the row if it doesn't exist yet (e.g., this is being run via
312 # update.php from a pre-1.16 schema). TODO: This will cause lots and
313 # lots of gaps on some non-MySQL DBMSes if you run populateCategory.php
314 # repeatedly. Plus it's an extra query that's unneeded almost all the
315 # time. This should be rewritten somehow, probably.
316 $seqVal = $dbw->nextSequenceValue( 'category_cat_id_seq' );
317 $dbw->insert(
318 'category',
319 array(
320 'cat_id' => $seqVal,
321 'cat_title' => $this->mName
322 ),
323 __METHOD__,
324 'IGNORE'
325 );
326
327 $cond1 = $dbw->conditional( array( 'page_namespace' => NS_CATEGORY ), 1, 'NULL' );
328 $cond2 = $dbw->conditional( array( 'page_namespace' => NS_FILE ), 1, 'NULL' );
329 $result = $dbw->selectRow(
330 array( 'categorylinks', 'page' ),
331 array( 'pages' => 'COUNT(*)',
332 'subcats' => "COUNT($cond1)",
333 'files' => "COUNT($cond2)"
334 ),
335 array( 'cl_to' => $this->mName, 'page_id = cl_from' ),
336 __METHOD__,
337 array( 'LOCK IN SHARE MODE' )
338 );
339 $ret = $dbw->update(
340 'category',
341 array(
342 'cat_pages' => $result->pages,
343 'cat_subcats' => $result->subcats,
344 'cat_files' => $result->files
345 ),
346 array( 'cat_title' => $this->mName ),
347 __METHOD__
348 );
349 $dbw->commit( __METHOD__ );
350
351 wfProfileOut( __METHOD__ );
352
353 # Now we should update our local counts.
354 $this->mPages = $result->pages;
355 $this->mSubcats = $result->subcats;
356 $this->mFiles = $result->files;
357
358 return $ret;
359 }
360 }