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