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.
7 * TODO: Move some stuff from CategoryPage.php to here, and use that.
13 /** Name of the category, normalized to DB-key form */
14 private $mName = 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;
21 private function __construct() {}
24 * Set up all member variables using a database query.
25 * @return bool True on success, false on failure.
27 protected function initialize() {
28 if ( $this->mName
=== null && $this->mTitle
)
29 $this->mName
= $title->getDBkey();
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
);
41 $dbr = wfGetDB( DB_SLAVE
);
42 $row = $dbr->selectRow(
44 array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ),
49 # Okay, there were no contents. Nothing to initialize.
50 if ( $this->mTitle
) {
51 # If there is a title object but no record in the category table, treat this as an empty category
53 $this->mName
= $this->mTitle
->getDBkey();
63 $this->mID
= $row->cat_id
;
64 $this->mName
= $row->cat_title
;
65 $this->mPages
= $row->cat_pages
;
66 $this->mSubcats
= $row->cat_subcats
;
67 $this->mFiles
= $row->cat_files
;
69 # (bug 13683) If the count is negative, then 1) it's obviously wrong
70 # and should not be kept, and 2) we *probably* don't have to scan many
71 # rows to obtain the correct figure, so let's risk a one-time recount.
72 if( $this->mPages
< 0 ||
$this->mSubcats
< 0 ||
$this->mFiles
< 0 ) {
73 $this->refreshCounts();
82 * @param $name Array: A category name (no "Category:" prefix). It need
83 * not be normalized, with spaces replaced by underscores.
84 * @return mixed Category, or false on a totally invalid name
86 public static function newFromName( $name ) {
88 $title = Title
::makeTitleSafe( NS_CATEGORY
, $name );
89 if( !is_object( $title ) ) {
93 $cat->mTitle
= $title;
94 $cat->mName
= $title->getDBkey();
102 * @param $title Title for the category page
103 * @return Mixed: category, or false on a totally invalid name
105 public static function newFromTitle( $title ) {
108 $cat->mTitle
= $title;
109 $cat->mName
= $title->getDBkey();
117 * @param $id Integer: a category id
120 public static function newFromID( $id ) {
122 $cat->mID
= intval( $id );
127 * Factory function, for constructing a Category object from a result set
129 * @param $row result set row, must contain the cat_xxx fields. If the fields are null,
130 * the resulting Category object will represent an empty category if a title object
131 * was given. If the fields are null and no title was given, this method fails and returns false.
132 * @param $title optional title object for the category represented by the given row.
133 * May be provided if it is already known, to avoid having to re-create a title object later.
136 public static function newFromRow( $row, $title = null ) {
138 $cat->mTitle
= $title;
141 # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
142 # all the cat_xxx fields being null, if the category page exists, but nothing
143 # was ever added to the category. This case should be treated linke an empty
144 # category, if possible.
146 if ( $row->cat_title
=== null ) {
147 if ( $title === null ) {
148 # the name is probably somewhere in the row, for example as page_title,
149 # but we can't know that here...
152 $cat->mName
= $title->getDBkey(); # if we have a title object, fetch the category name from there
160 $cat->mName
= $row->cat_title
;
161 $cat->mID
= $row->cat_id
;
162 $cat->mSubcats
= $row->cat_subcats
;
163 $cat->mPages
= $row->cat_pages
;
164 $cat->mFiles
= $row->cat_files
;
170 /** @return mixed DB key name, or false on failure */
171 public function getName() { return $this->getX( 'mName' ); }
172 /** @return mixed Category ID, or false on failure */
173 public function getID() { return $this->getX( 'mID' ); }
174 /** @return mixed Total number of member pages, or false on failure */
175 public function getPageCount() { return $this->getX( 'mPages' ); }
176 /** @return mixed Number of subcategories, or false on failure */
177 public function getSubcatCount() { return $this->getX( 'mSubcats' ); }
178 /** @return mixed Number of member files, or false on failure */
179 public function getFileCount() { return $this->getX( 'mFiles' ); }
182 * @return mixed The Title for this category, or false on failure.
184 public function getTitle() {
185 if( $this->mTitle
) return $this->mTitle
;
187 if( !$this->initialize() ) {
191 $this->mTitle
= Title
::makeTitleSafe( NS_CATEGORY
, $this->mName
);
192 return $this->mTitle
;
196 * Fetch a TitleArray of up to $limit category members, beginning after the
197 * category sort key $offset.
198 * @param $limit integer
199 * @param $offset string
200 * @return TitleArray object for category members.
202 public function getMembers( $limit = false, $offset = '' ) {
203 $dbr = wfGetDB( DB_SLAVE
);
205 $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
206 $options = array( 'ORDER BY' => 'cl_sortkey' );
207 if( $limit ) $options[ 'LIMIT' ] = $limit;
208 if( $offset !== '' ) $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
210 return TitleArray
::newFromResult(
212 array( 'page', 'categorylinks' ),
213 array( 'page_id', 'page_namespace','page_title', 'page_len',
214 'page_is_redirect', 'page_latest' ),
222 /** Generic accessor */
223 private function getX( $key ) {
224 if( !$this->initialize() ) {
227 return $this->{$key};
231 * Refresh the counts for this category.
233 * @return bool True on success, false on failure
235 public function refreshCounts() {
239 $dbw = wfGetDB( DB_MASTER
);
241 # Note, we must use names for this, since categorylinks does.
242 if( $this->mName
=== null ) {
243 if( !$this->initialize() ) {
247 # Let's be sure that the row exists in the table. We don't need to
248 # do this if we got the row from the table in initialization!
251 array( 'cat_title' => $this->mName
),
257 $cond1 = $dbw->conditional( 'page_namespace='.NS_CATEGORY
, 1, 'NULL' );
258 $cond2 = $dbw->conditional( 'page_namespace='.NS_FILE
, 1, 'NULL' );
259 $result = $dbw->selectRow(
260 array( 'categorylinks', 'page' ),
261 array( 'COUNT(*) AS pages',
262 "COUNT($cond1) AS subcats",
263 "COUNT($cond2) AS files"
265 array( 'cl_to' => $this->mName
, 'page_id = cl_from' ),
272 'cat_pages' => $result->pages
,
273 'cat_subcats' => $result->subcats
,
274 'cat_files' => $result->files
276 array( 'cat_title' => $this->mName
),
281 # Now we should update our local counts.
282 $this->mPages
= $result->pages
;
283 $this->mSubcats
= $result->subcats
;
284 $this->mFiles
= $result->files
;