Merge category paging from REL1_4
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 21 Dec 2004 03:26:43 +0000 (03:26 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 21 Dec 2004 03:26:43 +0000 (03:26 +0000)
includes/CategoryPage.php
includes/ImageGallery.php

index 2bed882..169355f 100644 (file)
@@ -44,286 +44,275 @@ class CategoryPage extends Article {
        # 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 $wgContLang, $wgUser ;
-               $fname = 'CategoryPage::oldCategoryMagic';
-
-
-               $sk =& $wgUser->getSkin() ;
-
-               $articles = array() ;
-               $children = array() ;
-               $r = '';
-               $id = $this->mTitle->getArticleID() ;
-
-               # FIXME: add limits
-               $dbr =& wfGetDB( DB_SLAVE );
-               $page = $dbr->tableName( 'page' );
-               $categorylinks = $dbr->tableName( 'categorylinks' );
-
-               $t = $dbr->strencode( $this->mTitle->getDBKey() );
-               $sql = "SELECT DISTINCT page_title, page_namespace FROM $page,$categorylinks " .
-                               "WHERE cl_to='$t' AND cl_from=page_id AND page_is_redirect=0 ORDER BY cl_sortkey" ;
-               $res = $dbr->query( $sql, $fname ) ;
-               # For all pages that link to this category
-               while ( $x = $dbr->fetchObject ( $res ) )
-               {
-                       $t = $wgContLang->getNsText ( $x->page_namespace ) ;
-                       if ( $t != '' ) $t .= ':' ;
-                       $t .= $x->page_title ;
-
-                       if ( $x->page_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 .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
-                       $r .= implode ( ', ' , $children ) ;
-               }
-
-               # Showing pages in this category
-               if ( count ( $articles ) > 0 ) {
-                       $ti = $this->mTitle->getText() ;
-                       $h =  wfMsg( 'category_header', $ti );
-                       $r .= "<h2>$h</h2>\n" ;
-                       $r .= implode ( ', ' , $articles ) ;
-               }
-
-               # Showing subcategories
-               if ( count ( $children ) > 0 ) {
-                       $r .= '<h2>'.wfMsg('subcategories')."</h2>\n" ;
-                       $r .= implode ( ', ' , $children ) ;
-               }
-
-               # Showing pages in this category
-               if ( count ( $articles ) > 0 ) {
-                       $ti = $this->mTitle->getText() ;
-                       $h =  wfMsg( 'category_header', $ti );
-                       $r .= "<h2>$h</h2>\n" ;
-                       $r .= implode ( ', ' , $articles ) ;
-               }
-
-               return $r ;
+               global $wgOut, $wgRequest;
+               $pageConditions = array();
+               $from = $wgRequest->getVal( 'from' );
+               $until = $wgRequest->getVal( 'until' );
+               $wgOut->addHTML( $this->doCategoryMagic( $from, $until ) );
        }
 
-       function newCategoryMagic () {
+       /**
+        * Format the category data list.
+        *
+        * @param string $from -- return only sort keys from this item on
+        * @param string $until -- don't return keys after this point.
+        * @return string HTML output
+        * @access private
+        */
+       function doCategoryMagic( $from = '', $until = '' ) {
                global $wgContLang,$wgUser, $wgCategoryMagicGallery;
+               $fname = 'CategoryPage::doCategoryMagic';
+               wfProfileIn( $fname );
 
-               $sk =& $wgUser->getSkin();
-
-               $r = "<br style=\"clear:both;\"/>\n";
-
-               $articles = array() ;
+               $articles = array();
                $articles_start_char = array();
-               $children = array() ;
+               $children = array();
                $children_start_char = array();
-               $data = array () ;
-               $id = $this->mTitle->getArticleID() ;
-
-               if ( $wgCategoryMagicGallery ) {
+               $data = array();
+               if( $wgCategoryMagicGallery ) {
                        $ig = new ImageGallery();
                }
 
-               # FIXME: add limits
                $dbr =& wfGetDB( DB_SLAVE );
-               $page = $dbr->tableName( 'page' );
-               $categorylinks = $dbr->tableName( 'categorylinks' );
-
-               $t = $dbr->strencode( $this->mTitle->getDBKey() );
-               $sql = "SELECT DISTINCT page_title,page_namespace,cl_sortkey FROM " .
-                               "$page,$categorylinks WHERE cl_to='$t' AND cl_from=page_id AND page_is_redirect ORDER BY cl_sortkey" ;
-               $res = $dbr->query ( $sql ) ;
-               while ( $x = $dbr->fetchObject ( $res ) )
-               {
-                       $t = $ns = $wgContLang->getNsText ( $x->page_namespace ) ;
-                       if ( $t != '' ) $t .= ':' ;
-                       $t .= $x->page_title ;
-                       $ctitle = str_replace( '_',' ',$x->page_title );
-
-                       if ( $x->page_namespace == NS_CATEGORY ) {
-                               array_push ( $children, $sk->makeKnownLink ( $t, $ctitle ) ) ; # Subcategory
-
+               if( $from != '' ) {
+                       $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $from );
+                       $flip = false;
+               } elseif( $until != '' ) {
+                       $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $until );
+                       $flip = true;
+               } else {
+                       $pageCondition = '1';
+                       $flip = false;
+               }
+               $limit = 200;
+               $res = $dbr->select(
+                       array( 'page', 'categorylinks' ),
+                       array( 'page_title', 'page_namespace', 'cl_sortkey' ),
+                       array( $pageCondition,
+                              'cl_from          =  page_id',
+                              'cl_to'           => $this->mTitle->getDBKey(),
+                              'page_is_redirect' => 0),
+                       #+ $pageCondition,
+                       $fname,
+                       array( 'ORDER BY' => $flip ? 'cl_sortkey DESC' : 'cl_sortkey',
+                              'LIMIT'    => $limit + 1 ) );
+               
+               $sk =& $wgUser->getSkin();
+               $r = "<br style=\"clear:both;\"/>\n";
+               $count = 0;
+               $nextPage = null;
+               while( $x = $dbr->fetchObject ( $res ) ) {
+                       if( ++$count > $limit ) {
+                               // We've reached the one extra which shows that there are
+                               // additional pages to be had. Stop here...
+                               $nextPage = $x->cl_sortkey;
+                               break;
+                       }
+                       
+                       $title = Title::makeTitle( $x->page_namespace, $x->page_title );
+                       
+                       if( $title->getNamespace() == NS_CATEGORY ) {
+                               // Subcategory; strip the 'Category' namespace from the link text.
+                               array_push( $children, $sk->makeKnownLinkObj( $title, $title->getText() ) );
+                               
                                // 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, $wgContLang->firstChar( $x->page_title ) );
+                               if( $title->getPrefixedText() == $x->cl_sortkey ) {
+                                       array_push( $children_start_char, $wgContLang->firstChar( $x->page_title ) );
+                               } else {
+                                       array_push( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
+                               }
+                       } elseif( $wgCategoryMagicGallery && $title->getNamespace() == NS_IMAGE ) {
+                               // Show thumbnails of categorized images, in a separate chunk
+                               if( $flip ) {
+                                       $ig->insert( Image::newFromTitle( $title ) );
                                } else {
-                                       array_push ( $children_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
+                                       $ig->add( Image::newFromTitle( $title ) );
                                }
-                       } elseif ( $wgCategoryMagicGallery && $x->page_namespace == NS_IMAGE ) {
-                               $ig->add( new Image( $x->page_title ) );
                        } else {
-                               array_push ( $articles , $sk->makeKnownLink ( $t ) ) ; # Page in this category
-                               array_push ( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
+                               // Page in this category
+                               array_push( $articles, $sk->makeKnownLinkObj( $title ) ) ;
+                               array_push( $articles_start_char, $wgContLang->firstChar( $x->cl_sortkey ) ) ;
                        }
                }
-               $dbr->freeResult ( $res ) ;
-
-               $ti = $this->mTitle->getText() ;
+               $dbr->freeResult( $res );
 
+               if( $flip ) {
+                       $children            = array_reverse( $children );
+                       $children_start_char = array_reverse( $children_start_char );
+                       $articles            = array_reverse( $articles );
+                       $articles_start_char = array_reverse( $articles_start_char );
+               }
+               
+               if( $until != '' ) {
+                       $r .= $this->pagingLinks( $this->mTitle, $nextPage, $until, $limit );
+               } elseif( $nextPage != '' || $from != '' ) {
+                       $r .= $this->pagingLinks( $this->mTitle, $from, $nextPage, $limit );
+               }
+               
                # Don't show subcategories section if there are none.
-               if ( count ( $children ) > 0 )
-               {
+               if( count( $children ) > 0 ) {
                        # Showing subcategories
                        $r .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\n";
+                       $r .= $this->formatCount( $children, 'subcategorycount' );
+                       $r .= $this->formatList( $children, $children_start_char );
+               }
 
-                       $numchild = count( $children );
-                       if($numchild == 1) {
-                               $r .= wfMsg( 'subcategorycount1', $wgContLang->formatNum( 1 ) );
-                       } else {
-                               $r .= wfMsg( 'subcategorycount' , $wgContLang->formatNum( $numchild ) );
-                       }
-                       unset($numchild);
-
-                       if ( count ( $children ) > 6 ) {
-
-                               // divide list into three equal chunks
-                               $chunk = (int) (count ( $children ) / 3);
-
-                               // get and display header
-                               $r .= '<table width="100%"><tr valign="top">';
-
-                               $startChunk = 0;
-                               $endChunk = $chunk;
-
-                               // loop through the chunks
-                               for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
-                                       $chunkIndex < 3;
-                                       $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
-                               {
-
-                                       $r .= '<td><ul>';
-                                       // 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 ( ($index == $startChunk)
-                                                       || ($children_start_char[$index] != $children_start_char[$index - 1]) )
-                                               {
-                                                       $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
-                                               }
-
-                                               $r .= "<li>{$children[$index]}</li>";
-                                       }
-                                       $r .= '</ul></td>';
-
-
-                               }
-                               $r .= '</tr></table>';
-                       } else {
-                               // for short lists of subcategories to category.
-
-                               $r .= "<h3>{$children_start_char[0]}</h3>\n";
-                               $r .= '<ul><li>'.$children[0].'</li>';
-                               for ($index = 1; $index < count($children); $index++ )
-                               {
-                                       if ($children_start_char[$index] != $children_start_char[$index - 1])
-                                       {
-                                               $r .= "</ul><h3>{$children_start_char[$index]}</h3>\n<ul>";
-                                       }
-                                       $r .= "<li>{$children[$index]}</li>";
-                               }
-                               $r .= '</ul>';
-                       }
-               } # END of if ( count($children) > 0 )
-
+               # Showing articles in this category
+               $ti = htmlspecialchars( $this->mTitle->getText() );
                $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\n";
+               $r .= $this->formatCount( $articles, 'categoryarticlecount' );
+               $r .= $this->formatList( $articles, $articles_start_char );
 
-               $numart = count( $articles );
-               if($numart == 1) {
-                       $r .= wfMsg( 'categoryarticlecount1', $wgContLang->formatNum( 1 ) );
-               } else {
-                       $r .= wfMsg( 'categoryarticlecount' , $wgContLang->formatNum( $numart ) );
+               if( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
+                       $r.= $ig->toHTML();
                }
-               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);
+               wfProfileOut( $fname );
+               return $r;
+       }
 
-                       // get and display header
-                       $r .= '<table width="100%"><tr valign="top">';
+       /**
+        * @param array $articles
+        * @param string $message
+        * @return string
+        * @access private
+        */
+       function formatCount( $articles, $message ) {
+               global $wgContLang;
+               $numart = count( $articles );
+               if( $numart == 1 ) {
+                       # Slightly different message to avoid silly plural
+                       $message .= '1';
+               }
+               return wfMsg( $message, $wgContLang->formatNum( $numart ) );
+       }
+       /**
+        * Format a list of articles chunked by letter, either as a
+        * bullet list or a columnar format, depending on the length.
+        *
+        * @param array $articles
+        * @param array $articles_start_char
+        * @param int   $cutoff
+        * @return string
+        * @access private
+        */
+       function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
+               if ( count ( $articles ) > $cutoff ) {
+                       return $this->columnList( $articles, $articles_start_char );
+               } elseif ( count($articles) > 0) {
+                       // for short lists of articles in categories.
+                       return $this->shortList( $articles, $articles_start_char );
+               }
+               return '';
+       }
+       
+       /**
+        * Format a list of articles chunked by letter in a three-column
+        * list, ordered vertically.
+        *
+        * @param array $articles
+        * @param array $articles_start_char
+        * @return string
+        * @access private
+        */
+       function columnList( $articles, $articles_start_char ) {
+               // divide list into three equal chunks
+               $chunk = (int) (count ( $articles ) / 3);
+
+               // get and display header
+               $r = '<table width="100%"><tr valign="top">';
+
+               $prev_start_char = ""; 
+
+               // loop through the chunks
+               for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
+                       $chunkIndex < 3;
+                       $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
+               {
 
-                       $prev_start_char = ""; 
+                       $r .= '<td><ul>';
 
-                       // loop through the chunks
-                       for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
-                               $chunkIndex < 3;
-                               $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 1)
+                       // 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 .= '<td><ul>';
-
-                               // 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]) )
-
-                                       {
-                                               $cont_msg = "";
-                                               if($articles_start_char[$index] == $prev_start_char)
-                                                       $cont_msg = wfMsg('listingcontinuesabbrev');
-                                               $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
-                                               $prev_start_char = $articles_start_char[$index];
-                                       }
-
-                                       $r .= "<li>{$articles[$index]}</li>";
+                                       $cont_msg = "";
+                                       if($articles_start_char[$index] == $prev_start_char)
+                                               $cont_msg = wfMsg('listingcontinuesabbrev');
+                                       $r .= "</ul><h3>{$articles_start_char[$index]}$cont_msg</h3>\n<ul>";
+                                       $prev_start_char = $articles_start_char[$index];
                                }
-                               $r .= '</ul></td>';
-
 
+                               $r .= "<li>{$articles[$index]}</li>";
                        }
-                       $r .= '</tr></table>';
-               } elseif ( count($articles) > 0) {
-                       // for short lists of articles in categories.
-                       $ti = $this->mTitle->getText() ;
+                       $r .= '</ul></td>';
 
-                       $r .= '<h3>'.$articles_start_char[0]."</h3>\n";
-                       $r .= '<ul><li>'.$articles[0].'</li>';
-                       for ($index = 1; $index < count($articles); $index++ )
-                       {
-                               if ($articles_start_char[$index] != $articles_start_char[$index - 1])
-                               {
-                                       $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
-                               }
 
-                               $r .= "<li>{$articles[$index]}</li>";
-                       }
-                       $r .= '</ul>';
                }
+               $r .= '</tr></table>';
+               return $r;
+       }
+       
+       /**
+        * Format a list of articles chunked by letter in a bullet list.
+        * @param array $articles
+        * @param array $articles_start_char
+        * @return string
+        * @access private
+        */
+       function shortList( $articles, $articles_start_char ) {
+               $r = '<h3>'.$articles_start_char[0]."</h3>\n";
+               $r .= '<ul><li>'.$articles[0].'</li>';
+               for ($index = 1; $index < count($articles); $index++ )
+               {
+                       if ($articles_start_char[$index] != $articles_start_char[$index - 1])
+                       {
+                               $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
+                       }
 
-               if ( $wgCategoryMagicGallery && ! $ig->isEmpty() ) {
-                       $r.= $ig->toHTML();
+                       $r .= "<li>{$articles[$index]}</li>";
                }
-
-               return $r ;
+               $r .= '</ul>';
+               return $r;
+       }
+       
+       /**
+        * @param Title  $title
+        * @param string $first
+        * @param string $last
+        * @param int    $limit
+        * @param array  $query - additional query options to pass
+        * @return string
+        * @access private
+        */
+       function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
+               global $wgUser, $wgLang;
+               $sk =& $wgUser->getSkin();
+               $limitText = $wgLang->formatNum( $limit );
+               
+               $prevLink = htmlspecialchars( wfMsg( 'prevn', $limitText ) );
+               if( $first != '' ) {
+                       $prevLink = $sk->makeLinkObj( $title, $prevLink,
+                               wfArrayToCGI( $query + array( 'until' => $first ) ) );
+               }
+               $nextLink = htmlspecialchars( wfMsg( 'nextn', $limitText ) );
+               if( $last != '' ) {
+                       $nextLink = $sk->makeLinkObj( $title, $nextLink,
+                               wfArrayToCGI( $query + array( 'from' => $last ) ) );
+               }
+               
+               return "($prevLink) ($nextLink)";
        }
 }
+
+
 ?>
index 81af0cb..7f49a79 100644 (file)
@@ -30,7 +30,7 @@ class ImageGallery
        }
 
        /**
-        * Add an image to the gallery.
+        * Add an image at the end of the gallery.
         *
         * @param Image  $image  Image object that is added to the gallery
         * @param string $html   Additional HTML text to be shown. The name and size of the image are always shown.
@@ -38,6 +38,16 @@ class ImageGallery
        function add( $image, $html='' ) {
                $this->mImages[] = array( &$image, $html );
        }
+       
+       /**
+        * Add an image at the beginning of the gallery.
+        *
+        * @param Image  $image  Image object that is added to the gallery
+        * @param string $html   Additional HTML text to be shown. The name and size of the image are always shown.
+        */
+       function insert( $image, $html='' ) {
+               array_unshift( $this->mImages, array( &$image, $html ) );
+       }
 
        /**
         * isEmpty() returns false iff the gallery doesn't contain any images