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