r36814 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36813‎ | r36814 | r36815 >
Date:14:06, 30 June 2008
Author:daniel
Status:old (Comments)
Tags:
Comment:
Make member counts from the new category table available for subcategories, beef up Category object a bit. This is mainly for use by the CategoryTree extension. NOTE: this changes the query used to fetch category listings!
Modified paths:
  • /trunk/phase3/includes/Category.php (modified) (history)
  • /trunk/phase3/includes/CategoryPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Category.php
@@ -11,6 +11,8 @@
1212 /** Name of the category, normalized to DB-key form */
1313 private $mName = null;
1414 private $mID = null;
 15+ /** Category page title */
 16+ private $mTitle = null;
1517 /** Counts of membership (cat_pages, cat_subcats, cat_files) */
1618 private $mPages = null, $mSubcats = null, $mFiles = null;
1719
@@ -21,11 +23,12 @@
2224 * @return bool True on success, false on failure.
2325 */
2426 protected function initialize() {
 27+ if ( $this->mName === null && $this->mTitle )
 28+ $this->mName = $title->getDBKey();
 29+
2530 if( $this->mName === null && $this->mID === null ) {
2631 throw new MWException( __METHOD__.' has both names and IDs null' );
27 - }
28 - $dbr = wfGetDB( DB_SLAVE );
29 - if( $this->mID === null ) {
 32+ } elseif( $this->mID === null ) {
3033 $where = array( 'cat_title' => $this->mName );
3134 } elseif( $this->mName === null ) {
3235 $where = array( 'cat_id' => $this->mID );
@@ -33,6 +36,7 @@
3437 # Already initialized
3538 return true;
3639 }
 40+ $dbr = wfGetDB( DB_SLAVE );
3741 $row = $dbr->selectRow(
3842 'category',
3943 array( 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats',
@@ -70,10 +74,12 @@
7175 */
7276 public static function newFromName( $name ) {
7377 $cat = new self();
74 - $title = Title::newFromText( "Category:$name" );
 78+ $title = Title::makeTitleSafe( NS_CATEGORY, $name );
7579 if( !is_object( $title ) ) {
7680 return false;
7781 }
 82+
 83+ $cat->mTitle = $title;
7884 $cat->mName = $title->getDBKey();
7985
8086 return $cat;
@@ -82,6 +88,21 @@
8389 /**
8490 * Factory function.
8591 *
 92+ * @param array $title Title for the category page
 93+ * @return mixed Category, or false on a totally invalid name
 94+ */
 95+ public static function newFromTitle( $title ) {
 96+ $cat = new self();
 97+
 98+ $cat->mTitle = $title;
 99+ $cat->mName = $title->getDBKey();
 100+
 101+ return $cat;
 102+ }
 103+
 104+ /**
 105+ * Factory function.
 106+ *
86107 * @param array $id A category id
87108 * @return Category
88109 */
@@ -91,6 +112,50 @@
92113 return $cat;
93114 }
94115
 116+ /**
 117+ * Factory function, for constructing a Category object from a result set
 118+ *
 119+ * @param $row result set row, must contain the cat_xxx fields. If the fields are null,
 120+ * the resulting Category object will represent an empty category if a title object
 121+ * was given. If the fields are null and no title was given, this method fails and returns false.
 122+ * @param $title optional title object for the category represented by the given row.
 123+ * May be provided if it is already known, to avoid having to re-create a title object later.
 124+ * @return Category
 125+ */
 126+ public static function newFromRow( $row, $title = null ) {
 127+ $cat->mTitle = $title;
 128+
 129+ $cat = new self();
 130+
 131+ # NOTE: the row often results from a LEFT JOIN on categorylinks. This may result in
 132+ # all the cat_xxx fields being null, if the category page exists, but nothing
 133+ # was ever added to the category. This case should be treated linke an empty
 134+ # category, if possible.
 135+
 136+ if ( $row->cat_title === null ) {
 137+ if ( $title === null ) {
 138+ # the name is probably somewhere in the row, for example as page_title,
 139+ # but we can't know that here...
 140+ return false;
 141+ } else {
 142+ $cat->mName = $title->getDBKey(); # if we have a title object, fetch the category name from there
 143+ }
 144+
 145+ $cat->mID = false;
 146+ $cat->mSubcats = 0;
 147+ $cat->mPages = 0;
 148+ $cat->mFiles = 0;
 149+ } else {
 150+ $cat->mName = $row->cat_title;
 151+ $cat->mID = $row->cat_id;
 152+ $cat->mSubcats = $row->cat_subcats;
 153+ $cat->mPages = $row->cat_pages;
 154+ $cat->mFiles = $row->cat_files;
 155+ }
 156+
 157+ return $cat;
 158+ }
 159+
95160 /** @return mixed DB key name, or false on failure */
96161 public function getName() { return $this->getX( 'mName' ); }
97162 /** @return mixed Category ID, or false on failure */
@@ -106,10 +171,14 @@
107172 * @return mixed The Title for this category, or false on failure.
108173 */
109174 public function getTitle() {
 175+ if( $this->mTitle ) return $this->mTitle;
 176+
110177 if( !$this->initialize() ) {
111178 return false;
112179 }
113 - return Title::makeTitleSafe( NS_CATEGORY, $this->mName );
 180+
 181+ $this->mTitle = Title::makeTitleSafe( NS_CATEGORY, $this->mName );
 182+ return $this->mTitle;
114183 }
115184
116185 /** Generic accessor */
Index: trunk/phase3/includes/CategoryPage.php
@@ -135,8 +135,17 @@
136136 }
137137
138138 /**
139 - * Add a subcategory to the internal lists
 139+ * Add a subcategory to the internal lists, using a Category object
140140 */
 141+ function addSubcategoryObject( $cat, $sortkey, $pageLength ) {
 142+ $title = $cat->getTitle();
 143+ $this->addSubcategory( $title, $sortkey, $pageLength );
 144+ }
 145+
 146+ /**
 147+ * Add a subcategory to the internal lists, using a title object
 148+ * @deprectated kept for compatibility, please use addSubcategoryObject instead
 149+ */
141150 function addSubcategory( $title, $sortkey, $pageLength ) {
142151 global $wgContLang;
143152 // Subcategory; strip the 'Category' namespace from the link text.
@@ -213,17 +222,17 @@
214223 $this->flip = false;
215224 }
216225 $res = $dbr->select(
217 - array( 'page', 'categorylinks' ),
218 - array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey' ),
 226+ array( 'page', 'categorylinks', 'category' ),
 227+ array( 'page_title', 'page_namespace', 'page_len', 'page_is_redirect', 'cl_sortkey',
 228+ 'cat_id', 'cat_title', 'cat_subcats', 'cat_pages', 'cat_files' ),
219229 array( $pageCondition,
220 - 'cl_from = page_id',
221 - 'cl_to' => $this->title->getDBkey()),
222 - #'page_is_redirect' => 0),
223 - #+ $pageCondition,
 230+ 'cl_to' => $this->title->getDBkey() ),
224231 __METHOD__,
225232 array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
226 - 'USE INDEX' => 'cl_sortkey',
227 - 'LIMIT' => $this->limit + 1 ) );
 233+ 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
 234+ 'LIMIT' => $this->limit + 1 ),
 235+ array( 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
 236+ 'category' => array( 'LEFT JOIN', 'cat_title = page_title AND page_namespace = ' . NS_CATEGORY ) ) );
228237
229238 $count = 0;
230239 $this->nextPage = null;
@@ -238,7 +247,8 @@
239248 $title = Title::makeTitle( $x->page_namespace, $x->page_title );
240249
241250 if( $title->getNamespace() == NS_CATEGORY ) {
242 - $this->addSubcategory( $title, $x->cl_sortkey, $x->page_len );
 251+ $cat = Category::newFromRow( $x, $title );
 252+ $this->addSubcategoryObject( $cat, $x->cl_sortkey, $x->page_len );
243253 } elseif( $this->showGallery && $title->getNamespace() == NS_IMAGE ) {
244254 $this->addImage( $title, $x->cl_sortkey, $x->page_len, $x->page_is_redirect );
245255 } else {

Follow-up revisions

RevisionCommit summaryAuthorDate
r36815Show member counts in category tree. Requires r36814 of Category.php and Cate...daniel14:09, 30 June 2008
r70030Remove dead code added in r36814...simetrical19:55, 27 July 2010
r72388Follow up r36814. Make the deprecated function call the non-deprecated one in...platonides18:46, 4 September 2010

Comments

#Comment by MaxSem (talk | contribs)   08:01, 7 August 2010

Deprecating addSubcategory(), you're recommending to use a newer addSubcategoryObject(), which in turn calls addSubcategory() itself. This doesn't make sense:)

#Comment by Platonides (talk | contribs)   22:39, 21 August 2010

Watching since I agree with MaxSem.

#Comment by Nikerabbit (talk | contribs)   07:58, 23 August 2010

There is also typo in deprectated. I don't see the problem however. It should just mention that it is deprecated for public use and should not be called directly.

#Comment by Platonides (talk | contribs)   18:47, 4 September 2010

Fixed in r72388.

Nikerabbit, the typo had been fixed in r44046.

#Comment by Duesentrieb (talk | contribs)   06:54, 23 August 2010

Just for the record: I hope you are not waiting for me to do anything abotu this... I don't really understand what's going on :)

Status & tagging log