+ /**
+ * Factory function, for constructing a Category object from a result set
+ *
+ * @param $row result set row, must contain the cat_xxx fields. If the fields are null,
+ * the resulting Category object will represent an empty category if a title object
+ * was given. If the fields are null and no title was given, this method fails and returns false.
+ * @param $title optional title object for the category represented by the given row.
+ * May be provided if it is already known, to avoid having to re-create a title object later.
+ * @return Category
+ */
+ public static function newFromRow( $row, $title = null ) {
+ $cat->mTitle = $title;
+
+ $cat = new self();
+
+ # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
+ # all the cat_xxx fields being null, if the category page exists, but nothing
+ # was ever added to the category. This case should be treated linke an empty
+ # category, if possible.
+
+ if ( $row->cat_title === null ) {
+ if ( $title === null ) {
+ # the name is probably somewhere in the row, for example as page_title,
+ # but we can't know that here...
+ return false;
+ } else {
+ $cat->mName = $title->getDBKey(); # if we have a title object, fetch the category name from there
+ }
+
+ $cat->mID = false;
+ $cat->mSubcats = 0;
+ $cat->mPages = 0;
+ $cat->mFiles = 0;
+ } else {
+ $cat->mName = $row->cat_title;
+ $cat->mID = $row->cat_id;
+ $cat->mSubcats = $row->cat_subcats;
+ $cat->mPages = $row->cat_pages;
+ $cat->mFiles = $row->cat_files;
+ }
+
+ return $cat;
+ }
+