htaccessing some directories for dev using cvs tree as www tree ;)
[lhc/web/wiklou.git] / includes / Parser.php
index dd17a17..ac119e3 100644 (file)
@@ -256,52 +256,51 @@ class Parser
                return $rnd;
        }
 
+       # This method generates the list of subcategories and pages for a category
        function categoryMagic ()
        {
                global $wgLang , $wgUser ;
-               if ( !$this->mOptions->getUseCategoryMagic() ) return ;
-               $id = $this->mTitle->getArticleID() ;
-               $cat = $wgLang->ucfirst ( wfMsg ( "category" ) ) ;
-               $ti = $this->mTitle->getText() ;
-               $ti = explode ( ":" , $ti , 2 ) ;
-               if ( $cat != $ti[0] ) return "" ;
-               $r = "<br style=\"clear:both;\"/>\n";
+               if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all
 
-               $articles = array() ;
-               $parents = array () ;
-               $children = array() ;
+               $cns = Namespace::getCategory() ;
+               if ( $this->mTitle->getNamespace() != $cns ) return "" ; # This ain't a category page
+
+               $r = "<br style=\"clear:both;\"/>\n";
 
 
-#              $sk =& $this->mGetSkin();
                $sk =& $wgUser->getSkin() ;
 
+               $articles = array() ;
+               $children = array() ;
                $data = array () ;
-               $sql1 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
-               $sql2 = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
+               $id = $this->mTitle->getArticleID() ;
 
-               $res = wfQuery ( $sql1, DB_READ ) ;
+               # For existing categories
+               $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,links WHERE l_to={$id} AND l_from=cur_id";
+               $res = wfQuery ( $sql, DB_READ ) ;
                while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
 
-               $res = wfQuery ( $sql2, DB_READ ) ;
+               # For non-existing categories
+               $sql = "SELECT DISTINCT cur_title,cur_namespace FROM cur,brokenlinks WHERE bl_to={$id} AND bl_from=cur_id" ;
+               $res = wfQuery ( $sql, DB_READ ) ;
                while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
 
-
+               # For all pages that link to this category
                foreach ( $data AS $x )
                {
                        $t = $wgLang->getNsText ( $x->cur_namespace ) ;
                        if ( $t != "" ) $t .= ":" ;
                        $t .= $x->cur_title ;
 
-                       $y = explode ( ":" , $t , 2 ) ;
-                       if ( count ( $y ) == 2 && $y[0] == $cat ) {
-                               array_push ( $children , $sk->makeLink ( $t , $y[1] ) ) ;
+                       if ( $x->cur_namespace == $cns ) {
+                               array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory
                        } else {
-                               array_push ( $articles , $sk->makeLink ( $t ) ) ;
+                               array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category
                        }
                }
                wfFreeResult ( $res ) ;
 
-               # Children
+               # Showing subcategories
                if ( count ( $children ) > 0 )
                {
                        asort ( $children ) ;
@@ -309,11 +308,12 @@ class Parser
                        $r .= implode ( ", " , $children ) ;
                }
 
-               # Articles
+               # Showing pages in this category
                if ( count ( $articles ) > 0 )
                {
+                       $ti = $this->mTitle->getText() ;
                        asort ( $articles ) ;
-                       $h =  wfMsg( "category_header", $ti[1] );
+                       $h =  wfMsg( "category_header", $ti );
                        $r .= "<h2>{$h}</h2>\n" ;
                        $r .= implode ( ", " , $articles ) ;
                }
@@ -484,7 +484,10 @@ class Parser
                $sk =& $this->mOptions->getSkin();
                $text = $sk->transformContent( $text );
 
-               $text .= $this->categoryMagic () ;
+               if ( !isset ( $this->categoryMagicDone ) ) {
+                  $text .= $this->categoryMagic () ;
+                  $this->categoryMagicDone = true ;
+                  }
 
                wfProfileOut( $fname );
                return $text;
@@ -829,7 +832,7 @@ class Parser
                if ( !$image ) { $image = Namespace::getImage(); }
                if ( !$special ) { $special = Namespace::getSpecial(); }
                if ( !$media ) { $media = Namespace::getMedia(); }
-               if ( !$category ) { $category = wfMsg ( "category" ) ; }
+               if ( !$category ) { $category = Namespace::getCategory(); ; }
 
                $nottalk = !Namespace::isTalk( $this->mTitle->getNamespace() );
 
@@ -892,6 +895,14 @@ class Parser
                                $wgLinkCache->addImageLinkObj( $nt );
                                return $s;
                        }
+                       if ( $ns == $category ) {
+                               $t = $nt->getText() ;
+                               $nnt = Title::newFromText ( Namespace::getCanonicalName($category).":".$t ) ;
+                               $t = $sk->makeLinkObj( $nnt, $t, "", "" , $prefix );
+                               $this->mOutput->mCategoryLinks[] = $t ;
+                               $s .= $prefix . $trail ;
+                               return $s ;
+                       }
                }
                if( ( $nt->getPrefixedText() == $this->mTitle->getPrefixedText() ) &&
                    ( strpos( $link, "#" ) == FALSE ) ) {
@@ -899,23 +910,6 @@ class Parser
                        return $s;
                }
 
-               # Category feature
-               $catns = strtoupper ( $nt->getDBkey () ) ;
-               $catns = explode ( ":" , $catns ) ;
-               if ( count ( $catns ) > 1 ) $catns = array_shift ( $catns ) ;
-               else $catns = "" ;
-               if ( $catns == strtoupper($category) && $this->mOptions->getUseCategoryMagic() ) {
-                       $t = explode ( ":" , $nt->getText() ) ;
-                       array_shift ( $t ) ;
-                       $t = implode ( ":" , $t ) ;
-                       $t = $wgLang->ucFirst ( $t ) ;
-                       $nnt = Title::newFromText ( $category.":".$t ) ;
-                       $t = $sk->makeLinkObj( $nnt, $t, "", $trail , $prefix );
-                       $this->mOutput->mCategoryLinks[] = $t ;
-                       $s .= $prefix . $trail ;
-                       return $s ;
-               }
-
                if( $ns == $media ) {
                        $s .= $prefix . $sk->makeMediaLinkObj( $nt, $text ) . $trail;
                        $wgLinkCache->addImageLinkObj( $nt );