Re-commit r34072 with some modifications:
[lhc/web/wiklou.git] / includes / SpecialSpecialpages.php
index 94547ff..b3a5520 100644 (file)
@@ -5,7 +5,7 @@
  */
 
 /**
- * Main function
+ *
  */
 function wfSpecialSpecialpages() {
        global $wgOut, $wgUser, $wgMessageCache;
@@ -13,72 +13,22 @@ function wfSpecialSpecialpages() {
        $wgMessageCache->loadAllMessages();
 
        $wgOut->setRobotpolicy( 'noindex,nofollow' );  # Is this really needed?
+       $sk = $wgUser->getSkin();
 
-       # Read the special pages
-       $pagesRegular = SpecialPage::getRegularPages();
-       $pagesRestricted = SpecialPage::getRestrictedPages();
-       if( count( $pagesRegular ) == 0 && count( $pagesRestricted ) == 0 ) {
-               # Yeah, that was pointless. Thanks for coming.
-               return;
-       }
-
-       # Put regular and restricted special pages into sortable arrays
-       $unsortedPagesRegular = wfSpecialSpecialpages_read( $pagesRegular );
-       $unsortedPagesRestricted = wfSpecialSpecialpages_read( $pagesRestricted );
-
-       # Read the template
-       $tpl = wfMsg( 'specialpages-tpl' );
-       $newSpecialPage = '';
-
-       # Parse the template and create a localized wikitext page
-       foreach ( explode( "\n", $tpl ) as $line ) {
-               # Look for 'special:' in the line
-               $pos = strpos( strtolower( $line ), 'special:' );
-               if( $pos >= 0 ) {
-
-                       # Preserve the line start
-                       $lineStart = ( $pos > 0 ) ? substr( $line, 0, $pos ) : '';
+       /** Pages available to all */
+       wfSpecialSpecialpages_gen( SpecialPage::getRegularPages(), 'spheading', $sk );
 
-                       # Get the canonical special page name
-                       $canonical = strtolower( trim( substr( $line, $pos + strlen( 'special:' ) ) ) );
-
-                       # Check if it is a valid regular special page name
-                       # Restricted pages will be added at the end
-                       if( isset( $unsortedPagesRegular[$canonical] ) ) {
-                               # Add a new line
-                               $newSpecialPage .= $lineStart . "[[special:" . $canonical . "|" . $unsortedPagesRegular[$canonical] . "]]\n";
-                               # Delete this regular special page from the array to avoid double output
-                               unset( $unsortedPagesRegular[$canonical] );
-                       } else {
-                               # Ok, no valid special page, but add the line to the output
-                               $newSpecialPage .= $line . "\n";
-                       }
-               } else {
-                       $newSpecialPage .= $line . "\n";
-               }
-       }
-
-       # Add the rest
-       $newSpecialPage .= wfSpecialSpecialpages_out( 'specialpages-other', $unsortedPagesRegular );
-       # Add the restricted special pages
-       $newSpecialPage .= wfSpecialSpecialpages_out( 'restrictedpheading', $unsortedPagesRestricted );
-
-       # Output the customized special pages list
-       $wgOut->addWikiText( $newSpecialPage );
-
-       if ( $wgUser->isAllowed( 'editinterface' ) ) {
-               # Output a nice link to edit the template
-               $wgOut->addHtml( wfSpecialSpecialpages_edit() );
-       }
+       /** Restricted special pages */
+       wfSpecialSpecialpages_gen( SpecialPage::getRestrictedPages(), 'restrictedpheading', $sk );
 }
 
 /**
- * Output the rest special pages as wikitext
- * @param string $heading Message name for the heading
- * @param array $sortedPages List of other special pages
- * @return string $rest Wikitext
+ * sub function generating the list of pages
+ * @param $pages the list of pages
+ * @param $heading header to be used
+ * @param $sk skin object ???
  */
-function wfSpecialSpecialpages_out( $heading, $pages ) {
+function wfSpecialSpecialpages_gen($pages,$heading,$sk) {
        global $wgOut, $wgSortSpecialPages;
 
        if( count( $pages ) == 0 ) {
@@ -86,44 +36,52 @@ function wfSpecialSpecialpages_out( $heading, $pages ) {
                return;
        }
 
-       # Sort
+       /** Put them into a sortable array */
+       $groups = array();
+       foreach ( $pages as $page ) {
+               if ( $page->isListed() ) {
+                       $group = SpecialPage::getGroup( $page );
+                       if( !isset($groups[$group]) ) {
+                               $groups[$group] = array();
+                       }
+                       $groups[$group][$page->getDescription()] = $page->getTitle();
+               }
+       }
+
+       /** Sort */
        if ( $wgSortSpecialPages ) {
-               asort( $pages );
+               foreach( $groups as $group => $sortedPages ) {
+                       ksort( $groups[$group] );
+               }
        }
 
-       # Now output the rest special pages as wikitext
-       $rest = '== ' . wfMsgExt( $heading, array( 'parseinline' ) ) . " ==\n";
-       foreach ( $pages as $title => $desc ) {
-               $rest .= "* [[special:" . $title . "|" . $desc . "]]\n";
+       /** Always move "other" to end */
+       if( array_key_exists('other',$groups) ) {
+               $other = $groups['other'];
+               unset( $groups['other'] );
+               $groups['other'] = $other;
        }
-       return $rest;
-}
 
-/**
- * Create array with descriptions and names of special pages
- * @param array $pagelist List of special pages
- * @return array $unsortedPagesList Wikitext
- */
-function wfSpecialSpecialpages_read( $pagelist ) {
-       $unsortedPagesList = array();
-       foreach ( $pagelist as $page ) {
-               if ( $page->isListed() ) {
-                       $name = strtolower( $page->getName() );
-                       $unsortedPagesList[$name] = $page->getDescription();
+       /** Now output the HTML */
+       $wgOut->addHTML( '<h2>' . wfMsgHtml( $heading ) . "</h2>\n" );
+       foreach ( $groups as $group => $sortedPages ) {
+               $middle = ceil( count($sortedPages)/2 );
+               $total = count($sortedPages);
+               $count = 0;
+
+               $wgOut->addHTML( "<h4 class='mw-specialpagesgroup'>".wfMsgHtml("specialpages-group-$group")."</h4>\n" );
+               $wgOut->addHTML( "<table style='width: 100%;' class='mw-specialpages-table'><tr>" );
+               $wgOut->addHTML( "<td width='30%' valign='top'><ul>\n" );
+               foreach ( $sortedPages as $desc => $title ) {
+                       $link = $sk->makeKnownLinkObj( $title , htmlspecialchars( $desc ) );
+                       $wgOut->addHTML( "<li>{$link}</li>\n" );
+
+                       # Slit up the larger groups
+                       $count++;
+                       if( $total > 3 && $count == $middle ) {
+                               $wgOut->addHTML( "</ul></td><td width='10%'></td><td width='30%' valign='top'><ul>" );
+                       }
                }
+               $wgOut->addHTML( "</ul></td><td width='30%' valign='top'></td></tr></table>\n" );
        }
-       return $unsortedPagesList;
-}
-
-/**
- * Build edit link to MediaWiki:specialpages-tpl
- *
- * @return string
- */
-function wfSpecialSpecialpages_edit() {
-       global $wgUser, $wgContLang;
-       $align = $wgContLang->isRtl() ? 'left' : 'right';
-       $skin = $wgUser->getSkin();
-       $link = $skin->makeLink ( 'MediaWiki:specialpages-tpl', wfMsgHtml( 'specialpages-edit' ) );
-       return '<p style="float:' . $align . ';" class="mw-specialspecialpages-edit">' . $link . '</p>';
 }