- function newCategoryMagic () {
- global $wgLang,$wgUser;
-
- $sk =& $wgUser->getSkin();
-
- $r = "<br style=\"clear:both;\"/>\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 .= '<h2>' . wfMsg( 'subcategories' ) . "</h2>\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 .= '<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 )
-
- $r .= '<h2>' . wfMsg( 'category_header', $ti ) . "</h2>\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 .= '<table width="100%"><tr valign="top">';
-
- // loop through the chunks
- for($startChunk = 0, $endChunk = $chunk, $chunkIndex = 0;
- $chunkIndex < 3;
- $chunkIndex++, $startChunk = $endChunk, $endChunk += $chunk + 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]) )
-
- {
- $r .= "</ul><h3>{$articles_start_char[$index]}</h3>\n<ul>";
- }
-
- $r .= "<li>{$articles[$index]}</li>";
- }
- $r .= '</ul></td>';
-
-
- }
- $r .= '</tr></table>';
- } elseif ( count($articles) > 0) {
- // for short lists of articles in categories.
- $ti = $this->mTitle->getText() ;
-
- $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>';
- }
- return $r ;
- }
+ /**
+ * Format a list of articles chunked by letter in a three-column
+ * list, ordered vertically.
+ *
+ * TODO: Take the headers into account when creating columns, so they're
+ * more visually equal.
+ *
+ * More distant TODO: Scrap this and use CSS columns, whenever IE finally
+ * supports those.
+ *
+ * @param $articles Array
+ * @param $articles_start_char Array
+ * @return String
+ * @private
+ */
+ function columnList( $articles, $articles_start_char ) {
+ $columns = array_combine( $articles, $articles_start_char );
+ # Split into three columns
+ $columns = array_chunk( $columns, ceil( count( $columns )/3 ), true /* preserve keys */ );
+
+ $ret = '<table width="100%"><tr valign="top"><td>';
+ $prevchar = null;
+
+ foreach ( $columns as $column ) {
+ $colContents = array();
+
+ # Kind of like array_flip() here, but we keep duplicates in an
+ # array instead of dropping them.
+ foreach ( $column as $article => $char ) {
+ if ( !isset( $colContents[$char] ) ) {
+ $colContents[$char] = array();
+ }
+ $colContents[$char][] = $article;
+ }
+
+ $first = true;
+ foreach ( $colContents as $char => $articles ) {
+ $ret .= '<h3>' . htmlspecialchars( $char );
+ if ( $first && $char === $prevchar ) {
+ # We're continuing a previous chunk at the top of a new
+ # column, so add " cont." after the letter.
+ $ret .= ' ' . wfMsgHtml( 'listingcontinuesabbrev' );
+ }
+ $ret .= "</h3>\n";
+
+ $ret .= '<ul><li>';
+ $ret .= implode( "</li>\n<li>", $articles );
+ $ret .= '</li></ul>';
+
+ $first = false;
+ $prevchar = $char;
+ }
+
+ $ret .= "</td>\n<td>";
+ }
+
+ $ret .= '</td></tr></table>';
+ return $ret;
+ }
+
+ /**
+ * Format a list of articles chunked by letter in a bullet list.
+ * @param $articles Array
+ * @param $articles_start_char Array
+ * @return String
+ * @private
+ */
+ function shortList( $articles, $articles_start_char ) {
+ $r = '<h3>' . htmlspecialchars( $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>" . htmlspecialchars( $articles_start_char[$index] ) . "</h3>\n<ul>";
+ }
+
+ $r .= "<li>{$articles[$index]}</li>";
+ }
+ $r .= '</ul>';
+ return $r;
+ }
+
+ /**
+ * @param $title Title object
+ * @param $first String
+ * @param $last String
+ * @param $limit Int
+ * @param $query Array: additional query options to pass
+ * @return String
+ * @private
+ */
+ function pagingLinks( $title, $first, $last, $limit, $query = array() ) {
+ global $wgLang;
+ $sk = $this->getSkin();
+ $limitText = $wgLang->formatNum( $limit );
+
+ $prevLink = wfMsgExt( 'prevn', array( 'escape', 'parsemag' ), $limitText );
+ if( $first != '' ) {
+ $prevQuery = $query;
+ $prevQuery['until'] = $first;
+ $prevLink = $sk->linkKnown(
+ $title,
+ $prevLink,
+ array(),
+ $prevQuery
+ );
+ }
+ $nextLink = wfMsgExt( 'nextn', array( 'escape', 'parsemag' ), $limitText );
+ if( $last != '' ) {
+ $lastQuery = $query;
+ $lastQuery['from'] = $last;
+ $nextLink = $sk->linkKnown(
+ $title,
+ $nextLink,
+ array(),
+ $lastQuery
+ );
+ }
+
+ return "($prevLink) ($nextLink)";
+ }
+
+ /**
+ * What to do if the category table conflicts with the number of results
+ * returned? This function says what. It works the same whether the
+ * things being counted are articles, subcategories, or files.
+ *
+ * Note for grepping: uses the messages category-article-count,
+ * category-article-count-limited, category-subcat-count,
+ * category-subcat-count-limited, category-file-count,
+ * category-file-count-limited.
+ *
+ * @param $rescnt Int: The number of items returned by our database query.
+ * @param $dbcnt Int: The number of items according to the category table.
+ * @param $type String: 'subcat', 'article', or 'file'
+ * @return String: A message giving the number of items, to output to HTML.
+ */
+ private function getCountMessage( $rescnt, $dbcnt, $type ) {
+ global $wgLang;
+ # There are three cases:
+ # 1) The category table figure seems sane. It might be wrong, but
+ # we can't do anything about it if we don't recalculate it on ev-
+ # ery category view.
+ # 2) The category table figure isn't sane, like it's smaller than the
+ # number of actual results, *but* the number of results is less
+ # than $this->limit and there's no offset. In this case we still
+ # know the right figure.
+ # 3) We have no idea.
+ $totalrescnt = count( $this->articles ) + count( $this->children ) +
+ ($this->showGallery ? $this->gallery->count() : 0);
+ if($dbcnt == $rescnt || (($totalrescnt == $this->limit || $this->from
+ || $this->until) && $dbcnt > $rescnt)){
+ # Case 1: seems sane.
+ $totalcnt = $dbcnt;
+ } elseif($totalrescnt < $this->limit && !$this->from && !$this->until){
+ # Case 2: not sane, but salvageable. Use the number of results.
+ # Since there are fewer than 200, we can also take this opportunity
+ # to refresh the incorrect category table entry -- which should be
+ # quick due to the small number of entries.
+ $totalcnt = $rescnt;
+ $this->cat->refreshCounts();
+ } else {
+ # Case 3: hopeless. Don't give a total count at all.
+ return wfMsgExt("category-$type-count-limited", 'parse',
+ $wgLang->formatNum( $rescnt ) );
+ }
+ return wfMsgExt( "category-$type-count", 'parse', $wgLang->formatNum( $rescnt ),
+ $wgLang->formatNum( $totalcnt ) );
+ }