From f52af0ba90235fff9aedb5d4b1eabdff9397f41d Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Wed, 1 Jul 2009 19:52:27 +0000 Subject: [PATCH] Clean up CategoryPage::columnList() Should have no functional changes, other than maybe whitespace in the HTML. --- includes/CategoryPage.php | 80 +++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/includes/CategoryPage.php b/includes/CategoryPage.php index b8dab34b0b..0a1d2170ef 100644 --- a/includes/CategoryPage.php +++ b/includes/CategoryPage.php @@ -372,62 +372,60 @@ class CategoryViewer { * 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 ) { - // divide list into three equal chunks - $chunk = (int) ( count( $articles ) / 3 ); - $remaining = count( $articles ) % 3; + $columns = array_combine( $articles, $articles_start_char ); + # Split into three columns + $columns = array_chunk( $columns, ceil( count( $columns )/3 ), true /* preserve keys */ ); - // get and display header - $r = ''; + $ret = '
\n"; + $ret .= ''; + $first = false; + $prevchar = $char; + } + $ret .= "\n
'; + $prevchar = null; - $prev_start_char = 'none'; + foreach ( $columns as $column ) { + $colContents = array(); - // loop through the chunks - for( $startChunk = 0, $endChunk = $chunk, $chunkIndex = 0; - $chunkIndex < 3; - $chunkIndex++, $startChunk = $endChunk, $endChunk += $remaining == 0 ? $chunk : $chunk + 1 ) - { - $r .= "\n"; - $atColumnTop = true; + # 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; + } - // output all articles in category - for ($index = $startChunk ; - $index < $endChunk && $index < count($articles); - $index++ ) - { - // check for change of starting letter or beginning of chunk - if ( ($index == $startChunk) || - ($articles_start_char[$index] != $articles_start_char[$index - 1]) ) - - { - if( $atColumnTop ) { - $atColumnTop = false; - } else { - $r .= "\n"; - } - $cont_msg = ""; - if ( $articles_start_char[$index] == $prev_start_char ) - $cont_msg = ' ' . wfMsgHtml( 'listingcontinuesabbrev' ); - $r .= "

" . htmlspecialchars( $articles_start_char[$index] ) . "$cont_msg

\n
    "; - $prev_start_char = $articles_start_char[$index]; + $first = true; + foreach ( $colContents as $char => $articles ) { + $ret .= '

    ' . 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 .= "

    \n"; - $r .= "
  • {$articles[$index]}
  • "; - } - if( !$atColumnTop ) { - $r .= "
\n"; - } - $r .= "
"; } - $r .= '
'; - return $r; + + $ret .= ''; + return $ret; } /** -- 2.20.1