/** Name of the category, normalized to DB-key form */
private $mName = null;
private $mID = null;
+ /** Category page title */
+ private $mTitle = null;
/** Counts of membership (cat_pages, cat_subcats, cat_files) */
private $mPages = null, $mSubcats = null, $mFiles = null;
* @return bool True on success, false on failure.
*/
protected function initialize() {
+ if ( $this->mName === null && $this->mTitle )
+ $this->mName = $title->getDBKey();
+
if( $this->mName === null && $this->mID === null ) {
throw new MWException( __METHOD__.' has both names and IDs null' );
- }
- $dbr = wfGetDB( DB_SLAVE );
- if( $this->mID === null ) {
+ } elseif( $this->mID === null ) {
$where = array( 'cat_title' => $this->mName );
} elseif( $this->mName === null ) {
$where = array( 'cat_id' => $this->mID );
# Already initialized
return true;
}
+ $dbr = wfGetDB( DB_SLAVE );
$row = $dbr->selectRow(
'category',
array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats',
);
if( !$row ) {
# Okay, there were no contents. Nothing to initialize.
- return false;
+ if ( $this->mTitle ) {
+ # If there is a title object but no record in the category table, treat this as an empty category
+ $this->mID = false;
+ $this->mName = $this->mTitle->getDBKey();
+ $this->mPages = 0;
+ $this->mSubcats = 0;
+ $this->mFiles = 0;
+
+ return true;
+ } else {
+ return false; # Fail
+ }
}
$this->mID = $row->cat_id;
$this->mName = $row->cat_title;
*/
public static function newFromName( $name ) {
$cat = new self();
- $title = Title::newFromText( "Category:$name" );
+ $title = Title::makeTitleSafe( NS_CATEGORY, $name );
if( !is_object( $title ) ) {
return false;
}
+
+ $cat->mTitle = $title;
+ $cat->mName = $title->getDBKey();
+
+ return $cat;
+ }
+
+ /**
+ * Factory function.
+ *
+ * @param array $title Title for the category page
+ * @return mixed Category, or false on a totally invalid name
+ */
+ public static function newFromTitle( $title ) {
+ $cat = new self();
+
+ $cat->mTitle = $title;
$cat->mName = $title->getDBKey();
return $cat;
return $cat;
}
+ /**
+ * 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 = new self();
+ $cat->mTitle = $title;
+
+
+ # 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;
+ }
+
/** @return mixed DB key name, or false on failure */
public function getName() { return $this->getX( 'mName' ); }
/** @return mixed Category ID, or false on failure */
* @return mixed The Title for this category, or false on failure.
*/
public function getTitle() {
+ if( $this->mTitle ) return $this->mTitle;
+
if( !$this->initialize() ) {
return false;
}
- return Title::makeTitleSafe( NS_CATEGORY, $this->mName );
+
+ $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
+ return $this->mTitle;
+ }
+
+ /**
+ * Fetch a TitleArray of up to $limit category members, beginning after the
+ * category sort key $offset.
+ * @param $limit integer
+ * @param $offset string
+ * @return TitleArray object for category members.
+ */
+ public function getMembers( $limit = false, $offset = '' ) {
+ $dbr = wfGetDB( DB_SLAVE );
+
+ $conds = array( 'cl_to' => $this->getName(), 'cl_from = page_id' );
+ $options = array( 'ORDER BY' => 'cl_sortkey' );
+ if( $limit ) $options[ 'LIMIT' ] = $limit;
+ if( $offset !== '' ) $conds[] = 'cl_sortkey > ' . $dbr->addQuotes( $offset );
+
+ return TitleArray::newFromResult(
+ $dbr->select(
+ array( 'page', 'categorylinks' ),
+ array( 'page_id', 'page_namespace','page_title', 'page_len',
+ 'page_is_redirect', 'page_latest' ),
+ $conds,
+ __METHOD__,
+ $options
+ )
+ );
}
/** Generic accessor */