From 1eb24800146a8bfd287d22a0a4aa50d94d10ba9a Mon Sep 17 00:00:00 2001 From: Jens Frank Date: Sun, 22 Aug 2004 09:42:12 +0000 Subject: [PATCH] Moved category stuff to CategoryPage, copying logic of ImagePage --- includes/CategoryPage.php | 298 ++++++++++++++++++++++++++++++++++++++ includes/Parser.php | 280 +---------------------------------- index.php | 8 + 3 files changed, 310 insertions(+), 276 deletions(-) create mode 100644 includes/CategoryPage.php diff --git a/includes/CategoryPage.php b/includes/CategoryPage.php new file mode 100644 index 0000000000..3e630ce020 --- /dev/null +++ b/includes/CategoryPage.php @@ -0,0 +1,298 @@ +mTitle->getNamespace() ) { + $this->openShowCategory(); + } + + Article::view(); + + # If the article we've just shown is in the "Image" namespace, + # follow it with the history list and link list for the image + # it describes. + + if ( NS_CATEGORY == $this->mTitle->getNamespace() ) { + $this->closeShowCategory(); + } + } + + function openShowCategory() + { + # For overloading + } + + # generate a list of subcategories and pages for a category + # depending on wfMsg("usenewcategorypage") it either calls the new + # or the old code. The new code will not work properly for some + # languages due to sorting issues, so they might want to turn it + # off. + + function closeShowCategory() + { + global $wgOut; + $msg = wfMsg('usenewcategorypage'); + if ( '0' == @$msg[0] ) + { + $wgOut->addHTML( $this->oldCategoryMagic() ); + } else { + $wgOut->addHTML( $this->newCategoryMagic() ); + } + } + + # This method generates the list of subcategories and pages for a category + function oldCategoryMagic () { + global $wgLang, $wgUser ; + $fname = 'CategoryPage::oldCategoryMagic'; + + + $sk =& $wgUser->getSkin() ; + + $articles = array() ; + $children = array() ; + $r = ''; + $id = $this->mTitle->getArticleID() ; + + # FIXME: add limits + $dbr =& wfGetDB( DB_SLAVE ); + $cur = $dbr->tableName( 'cur' ); + $categorylinks = $dbr->tableName( 'categorylinks' ); + + $t = $dbr->strencode( $this->mTitle->getDBKey() ); + $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " . + "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ; + $res = $dbr->query( $sql, $fname ) ; + # For all pages that link to this category + while ( $x = $dbr->fetchObject ( $res ) ) + { + $t = $wgLang->getNsText ( $x->cur_namespace ) ; + if ( $t != '' ) $t .= ':' ; + $t .= $x->cur_title ; + + if ( $x->cur_namespace == NS_CATEGORY ) { + array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory + } else { + array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category + } + } + $dbr->freeResult ( $res ) ; + + # Showing subcategories + if ( count ( $children ) > 0 ) { + $r .= '

'.wfMsg('subcategories')."

\n" ; + $r .= implode ( ', ' , $children ) ; + } + + # Showing pages in this category + if ( count ( $articles ) > 0 ) { + $ti = $this->mTitle->getText() ; + $h = wfMsg( 'category_header', $ti ); + $r .= "

$h

\n" ; + $r .= implode ( ', ' , $articles ) ; + } + + return $r ; + } + + function newCategoryMagic () { + global $wgLang,$wgUser; + + $sk =& $wgUser->getSkin(); + + $r = "
\n"; + + $articles = array() ; + $articles_start_char = array(); + $children = array() ; + $children_start_char = array(); + $data = array () ; + $id = $this->mTitle->getArticleID() ; + + # FIXME: add limits + $dbr =& wfGetDB( DB_SLAVE ); + $cur = $dbr->tableName( 'cur' ); + $categorylinks = $dbr->tableName( 'categorylinks' ); + + $t = $dbr->strencode( $this->mTitle->getDBKey() ); + $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " . + "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ; + $res = $dbr->query ( $sql ) ; + while ( $x = $dbr->fetchObject ( $res ) ) + { + $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ; + if ( $t != '' ) $t .= ':' ; + $t .= $x->cur_title ; + + if ( $x->cur_namespace == NS_CATEGORY ) { + $ctitle = str_replace( '_',' ',$x->cur_title ); + array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory + + // If there's a link from Category:A to Category:B, the sortkey of the resulting + // entry in the categorylinks table is Category:A, not A, which it SHOULD be. + // Workaround: If sortkey == "Category:".$title, than use $title for sorting, + // else use sortkey... + if ( ($ns.':'.$ctitle) == $x->cl_sortkey ) { + array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) ); + } else { + array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ; + } + } else { + array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category + array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ; + } + } + $dbr->freeResult ( $res ) ; + + $ti = $this->mTitle->getText() ; + + # Don't show subcategories section if there are none. + if ( count ( $children ) > 0 ) + { + # Showing subcategories + $r .= '

' . wfMsg( 'subcategories' ) . "

\n"; + + $numchild = count( $children ); + if($numchild == 1) { + $r .= wfMsg( 'subcategorycount1', 1 ); + } else { + $r .= wfMsg( 'subcategorycount' , $numchild ); + } + unset($numchild); + + if ( count ( $children ) > 6 ) { + + // divide list into three equal chunks + $chunk = (int) (count ( $children ) / 3); + + // get and display header + $r .= ''; + + $startChunk = 0; + $endChunk = $chunk; + + // loop through the chunks + for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0; + $chunkIndex < 3; + $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1) + { + + $r .= ''; + + + } + $r .= '
    '; + // output all subcategories to category + for ($index = $startChunk ; + $index < $endChunk && $index < count($children); + $index++ ) + { + // check for change of starting letter or begging of chunk + if ( ($children_start_char[$index] != $children_start_char[$index - 1]) + || ($index == $startChunk) ) + { + $r .= "

{$children_start_char[$index]}

\n
    "; + } + + $r .= "
  • {$children[$index]}
  • "; + } + $r .= '
'; + } else { + // for short lists of subcategories to category. + + $r .= "

{$children_start_char[0]}

\n"; + $r .= '

{$children_start_char[$index]}

\n'; + } + } # END of if ( count($children) > 0 ) + + $r .= '

' . wfMsg( 'category_header', $ti ) . "

\n"; + + $numart = count( $articles ); + if($numart == 1) { + $r .= wfMsg( 'categoryarticlecount1', 1 ); + } else { + $r .= wfMsg( 'categoryarticlecount' , $numart ); + } + unset($numart); + + # Showing articles in this category + if ( count ( $articles ) > 6) { + $ti = $this->mTitle->getText() ; + + // divide list into three equal chunks + $chunk = (int) (count ( $articles ) / 3); + + // get and display header + $r .= ''; + + // loop through the chunks + for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0; + $chunkIndex < 3; + $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1) + { + + $r .= ''; + + + } + $r .= '
    '; + + // output all articles in category + for ($index = $startChunk ; + $index < $endChunk && $index < count($articles); + $index++ ) + { + // check for change of starting letter or begging of chunk + if ( ($index == $startChunk) || + ($articles_start_char[$index] != $articles_start_char[$index - 1]) ) + + { + $r .= "

{$articles_start_char[$index]}

\n
    "; + } + + $r .= "
  • {$articles[$index]}
  • "; + } + $r .= '
'; + } elseif ( count($articles) > 0) { + // for short lists of articles in categories. + $ti = $this->mTitle->getText() ; + + $r .= '

'.$articles_start_char[0]."

\n"; + $r .= '

{$articles_start_char[$index]}

\n'; + } + + + return $r ; + } + + +} + + +?> diff --git a/includes/Parser.php b/includes/Parser.php index 79c9c1eec2..369f04f759 100644 --- a/includes/Parser.php +++ b/includes/Parser.php @@ -373,278 +373,6 @@ class Parser return $rnd; } - # categoryMagic - # generate a list of subcategories and pages for a category - # depending on wfMsg("usenewcategorypage") it either calls the new - # or the old code. The new code will not work properly for some - # languages due to sorting issues, so they might want to turn it - # off. - function categoryMagic() { - $msg = wfMsg('usenewcategorypage'); - if ( '0' == @$msg[0] ) - { - return $this->oldCategoryMagic(); - } else { - return $this->newCategoryMagic(); - } - } - - # This method generates the list of subcategories and pages for a category - function oldCategoryMagic () { - global $wgLang ; - $fname = 'Parser::oldCategoryMagic'; - - if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all - - if ( $this->mTitle->getNamespace() != NS_CATEGORY ) return "" ; # This ain't a category page - - $r = "
\n"; - - - $sk =& $this->mOptions->getSkin() ; - - $articles = array() ; - $children = array() ; - $data = array () ; - $id = $this->mTitle->getArticleID() ; - - # FIXME: add limits - $dbr =& wfGetDB( DB_SLAVE ); - $cur = $dbr->tableName( 'cur' ); - $categorylinks = $dbr->tableName( 'categorylinks' ); - - $t = $dbr->strencode( $this->mTitle->getDBKey() ); - $sql = "SELECT DISTINCT cur_title,cur_namespace FROM $cur,$categorylinks " . - "WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ; - $res = $dbr->query( $sql, $fname ) ; - while ( $x = $dbr->fetchObject ( $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 ; - - if ( $x->cur_namespace == NS_CATEGORY ) { - array_push ( $children , $sk->makeLink ( $t ) ) ; # Subcategory - } else { - array_push ( $articles , $sk->makeLink ( $t ) ) ; # Page in this category - } - } - $dbr->freeResult ( $res ) ; - - # Showing subcategories - if ( count ( $children ) > 0 ) { - $r .= '

'.wfMsg('subcategories')."

\n" ; - $r .= implode ( ', ' , $children ) ; - } - - # Showing pages in this category - if ( count ( $articles ) > 0 ) { - $ti = $this->mTitle->getText() ; - $h = wfMsg( 'category_header', $ti ); - $r .= "

$h

\n" ; - $r .= implode ( ', ' , $articles ) ; - } - - return $r ; - } - - function newCategoryMagic () { - global $wgLang; - if ( !$this->mOptions->getUseCategoryMagic() ) return ; # Doesn't use categories at all - - if ( $this->mTitle->getNamespace() != NS_CATEGORY ) return '' ; # This ain't a category page - - $r = "
\n"; - - - $sk =& $this->mOptions->getSkin() ; - - $articles = array() ; - $articles_start_char = array(); - $children = array() ; - $children_start_char = array(); - $data = array () ; - $id = $this->mTitle->getArticleID() ; - - # FIXME: add limits - $dbr =& wfGetDB( DB_SLAVE ); - $cur = $dbr->tableName( 'cur' ); - $categorylinks = $dbr->tableName( 'categorylinks' ); - - $t = $dbr->strencode( $this->mTitle->getDBKey() ); - $sql = "SELECT DISTINCT cur_title,cur_namespace,cl_sortkey FROM " . - "$cur,$categorylinks WHERE cl_to='$t' AND cl_from=cur_id ORDER BY cl_sortkey" ; - $res = $dbr->query ( $sql ) ; - while ( $x = $dbr->fetchObject ( $res ) ) - { - $t = $ns = $wgLang->getNsText ( $x->cur_namespace ) ; - if ( $t != '' ) $t .= ':' ; - $t .= $x->cur_title ; - - if ( $x->cur_namespace == NS_CATEGORY ) { - $ctitle = str_replace( '_',' ',$x->cur_title ); - array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory - - // If there's a link from Category:A to Category:B, the sortkey of the resulting - // entry in the categorylinks table is Category:A, not A, which it SHOULD be. - // Workaround: If sortkey == "Category:".$title, than use $title for sorting, - // else use sortkey... - if ( ($ns.':'.$ctitle) == $x->cl_sortkey ) { - array_push ( $children_start_char, $wgLang->firstChar( $x->cur_title ) ); - } else { - array_push ( $children_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ; - } - } else { - array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category - array_push ( $articles_start_char, $wgLang->firstChar( $x->cl_sortkey ) ) ; - } - } - $dbr->freeResult ( $res ) ; - - $ti = $this->mTitle->getText() ; - - # Don't show subcategories section if there are none. - if ( count ( $children ) > 0 ) - { - # Showing subcategories - $r .= '

' . wfMsg( 'subcategories' ) . "

\n"; - - $numchild = count( $children ); - if($numchild == 1) { - $r .= wfMsg( 'subcategorycount1', 1 ); - } else { - $r .= wfMsg( 'subcategorycount' , $numchild ); - } - unset($numchild); - - if ( count ( $children ) > 6 ) { - - // divide list into three equal chunks - $chunk = (int) (count ( $children ) / 3); - - // get and display header - $r .= ''; - - $startChunk = 0; - $endChunk = $chunk; - - // loop through the chunks - for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0; - $chunkIndex < 3; - $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1) - { - - $r .= ''; - - - } - $r .= '
    '; - // output all subcategories to category - for ($index = $startChunk ; - $index < $endChunk && $index < count($children); - $index++ ) - { - // check for change of starting letter or begging of chunk - if ( ($children_start_char[$index] != $children_start_char[$index - 1]) - || ($index == $startChunk) ) - { - $r .= "

{$children_start_char[$index]}

\n
    "; - } - - $r .= "
  • {$children[$index]}
  • "; - } - $r .= '
'; - } else { - // for short lists of subcategories to category. - - $r .= "

{$children_start_char[0]}

\n"; - $r .= '

{$children_start_char[$index]}

\n'; - } - } # END of if ( count($children) > 0 ) - - $r .= '

' . wfMsg( 'category_header', $ti ) . "

\n"; - - $numart = count( $articles ); - if($numart == 1) { - $r .= wfMsg( 'categoryarticlecount1', 1 ); - } else { - $r .= wfMsg( 'categoryarticlecount' , $numart ); - } - unset($numart); - - # Showing articles in this category - if ( count ( $articles ) > 6) { - $ti = $this->mTitle->getText() ; - - // divide list into three equal chunks - $chunk = (int) (count ( $articles ) / 3); - - // get and display header - $r .= ''; - - // loop through the chunks - for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0; - $chunkIndex < 3; - $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1) - { - - $r .= ''; - - - } - $r .= '
    '; - - // output all articles in category - for ($index = $startChunk ; - $index < $endChunk && $index < count($articles); - $index++ ) - { - // check for change of starting letter or begging of chunk - if ( ($index == $startChunk) || - ($articles_start_char[$index] != $articles_start_char[$index - 1]) ) - - { - $r .= "

{$articles_start_char[$index]}

\n
    "; - } - - $r .= "
  • {$articles[$index]}
  • "; - } - $r .= '
'; - } elseif ( count($articles) > 0) { - // for short lists of articles in categories. - $ti = $this->mTitle->getText() ; - - $r .= '

'.$articles_start_char[0]."

\n"; - $r .= '

{$articles_start_char[$index]}

\n'; - } - - - return $r ; - } - # Return allowed HTML attributes function getHTMLattrs () { $htmlattrs = array( # Allowed attributes--no scripting, etc. @@ -870,10 +598,10 @@ class Parser $sk =& $this->mOptions->getSkin(); $text = $sk->transformContent( $text ); - if ( $isMain && !isset ( $this->categoryMagicDone ) ) { - $text .= $this->categoryMagic () ; - $this->categoryMagicDone = true ; - } + // if ( $isMain && !isset ( $this->categoryMagicDone ) ) { + // $text .= $this->categoryMagic () ; + // $this->categoryMagicDone = true ; + // } wfProfileOut( $fname ); return $text; diff --git a/index.php b/index.php index 3ea90f6be8..ad36e52758 100644 --- a/index.php +++ b/index.php @@ -104,6 +104,14 @@ if( $search = $wgRequest->getText( 'search' ) ) { require_once( "includes/ImagePage.php" ); $wgArticle = new ImagePage( $wgTitle ); break; + case NS_CATEGORY: + if ( $wgUseCategoryMagic ) { + require_once( "includes/CategoryPage.php" ); + $wgArticle = new CategoryPage( $wgTitle ); + break; + } + # NO break if wgUseCategoryMagic is false, drop through to next (default). + # Don't insert other cases between NS_CATEGORY and default. default: $wgArticle = new Article( $wgTitle ); } -- 2.20.1