r4913 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r4912‎ | r4913 | r4914 >
Date:00:49, 23 August 2004
Author:hashar
Status:old
Tags:
Comment:
rewrite of the dmoz-like category browser. Known bugs: makeKnownLink instead of makeLink, <br> is put at beginning instead of the end
Modified paths:
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Title.php
@@ -1252,82 +1252,54 @@
12531253
12541254 # Get categories to wich belong this title and return an array of
12551255 # categories names.
1256 - function getParentCategories( ) {
 1256+ # Return an array of parents in the form:
 1257+ # $parent => $currentarticle
 1258+ function getParentCategories() {
12571259 global $wgLang,$wgUser;
12581260
12591261 $titlekey = $this->getArticleId();
1260 - $cns = Namespace::getCategory();
12611262 $sk =& $wgUser->getSkin();
12621263 $parents = array();
12631264 $dbr =& wfGetDB( DB_SLAVE );
12641265 $cur = $dbr->tableName( 'cur' );
12651266 $categorylinks = $dbr->tableName( 'categorylinks' );
12661267
1267 - # get the parents categories of this title from the database
1268 - $sql = "SELECT DISTINCT cur_id FROM $cur,$categorylinks
1269 - WHERE cl_from='$titlekey' AND cl_to=cur_title AND cur_namespace='$cns'
1270 - ORDER BY cl_sortkey" ;
 1268+ # NEW SQL
 1269+ $sql = "SELECT * FROM categorylinks"
 1270+ ." WHERE cl_from='$titlekey'"
 1271+ ." AND cl_from <> '0'"
 1272+ ." ORDER BY cl_sortkey";
 1273+
12711274 $res = $dbr->query ( $sql ) ;
12721275
12731276 if($dbr->numRows($res) > 0) {
1274 - while ( $x = $dbr->fetchObject ( $res ) ) $data[] = $x ;
 1277+ while ( $x = $dbr->fetchObject ( $res ) )
 1278+ //$data[] = Title::newFromText($wgLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
 1279+ $data[$wgLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
12751280 $dbr->freeResult ( $res ) ;
12761281 } else {
12771282 $data = '';
12781283 }
12791284 return $data;
12801285 }
1281 -
1282 - # will get the parents and grand-parents
1283 - # TODO : not sure what's happening when a loop happen like:
1284 - # Encyclopedia > Astronomy > Encyclopedia
1285 - function getAllParentCategories(&$stack) {
1286 - global $wgUser,$wgLang;
1287 - $result = '';
1288 -
1289 - # getting parents
1290 - $parents = $this->getParentCategories( );
12911286
1292 - if($parents == '')
1293 - {
1294 - # The current element has no more parent so we dump the stack
1295 - # and make a clean line of categories
1296 - $sk =& $wgUser->getSkin() ;
1297 -
1298 - foreach ( array_reverse($stack) as $child => $parent )
 1287+ # Go through all parents
 1288+ function getCategorieBrowser() {
 1289+ $parents = $this->getParentCategories();
 1290+
 1291+ if($parents != '') {
 1292+ foreach($parents as $parent => $current)
12991293 {
1300 - # make a link of that parent
1301 - $result .= $sk->makeLink($wgLang->getNSText ( Namespace::getCategory() ).':'.$parent,$parent);
1302 - $result .= ' &gt; ';
1303 - $lastchild = $child;
 1294+ $nt = Title::newFromText($parent);
 1295+ $stack[$parent] = $nt->getCategorieBrowser();
13041296 }
1305 - # append the last child.
1306 - # TODO : We should have a last child unless there is an error in the
1307 - # "categorylinks" table.
1308 - if(isset($lastchild)) { $result .= $lastchild; }
1309 -
1310 - $result .= "<br/>\n";
1311 -
1312 - # now we can empty the stack
1313 - $stack = array();
1314 -
 1297+ return $stack;
13151298 } else {
1316 - # look at parents of current category
1317 - foreach($parents as $parent)
1318 - {
1319 - # create a title object for the parent
1320 - $tpar = Title::newFromID($parent->cur_id);
1321 - # add it to the stack
1322 - $stack[$this->getText()] = $tpar->getText();
1323 - # grab its parents
1324 - $result .= $tpar->getAllParentCategories($stack);
1325 - }
 1299+ return array();
13261300 }
1327 -
1328 - if(isset($result)) { return $result; }
1329 - else { return ''; };
13301301 }
13311302
 1303+
13321304 # Returns an associative array for selecting this title from cur
13331305 function curCond() {
13341306 return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
Index: trunk/phase3/includes/Skin.php
@@ -413,10 +413,36 @@
414414 wfMsg( 'categories' ), 'article=' . urlencode( $wgTitle->getPrefixedDBkey() ) )
415415 . ': ' . $t;
416416
 417+ # optional 'dmoz-like' category browser. Will be shown under the list
 418+ # of categories an article belong to
417419 if($wgUseCategoryBrowser) {
418420 $s .= '<br/><hr/>';
419 - $catstack = array();
420 - $s.= $wgTitle->getAllParentCategories($catstack);
 421+
 422+ # get a big array of the parents tree
 423+ $parenttree = $wgTitle->getCategorieBrowser();
 424+
 425+ # Render the array as a serie of links
 426+ function walkThrough ($tree) {
 427+ global $wgUser;
 428+ $sk = $wgUser->getSkin();
 429+ $return = '';
 430+ foreach($tree as $element => $parent) {
 431+ if(empty($parent)) {
 432+ # element start a new list
 433+ $return .= '<br />';
 434+ } else {
 435+ # grab the others elements
 436+ $return .= walkThrough($parent);
 437+ }
 438+ # add our current element to the list
 439+ $eltitle = Title::NewFromText($element);
 440+ # FIXME : should be makeLink() [AV]
 441+ $return .= $sk->makeKnownLink($element, $eltitle->getText()).' &gt; ';
 442+ }
 443+ return $return;
 444+ }
 445+
 446+ $s .= walkThrough($parenttree);
421447 }
422448
423449 return $s;

Status & tagging log