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