SpecialPages grouped into categories

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search
Languages:

This code change makes it possible to group the special pages into categories.

Example: http://www.its-wiki.de/index.php/Spezial:Specialpages

Author: thomas(_at_) thomasfischer.biz

Please review and comment :)

Sourcecode (for German categories and MediaWiki 1.7.1; English version for 1.9.3, with more pages and extension-specific pages):
MediaWiki 1.11.0; Chinese version(中文)
File location: ../includes/SpecialSpecialpages.php

<?php
/**
 *
 * @package MediaWiki
 * @subpackage SpecialPage
 */

/**
 *
 */
function wfSpecialSpecialpages() {
        global $wgOut, $wgUser, $wgAvailableRights;

        $wgOut->setRobotpolicy( 'index,nofollow' );
        $sk = $wgUser->getSkin();

        # Get listable pages, in a 2-d array with the first dimension being user right
        $pages = SpecialPage::getPages();

        /** Pages available to all */
        wfSpecialSpecialpages_gen($pages[''],'spheading',$sk);

        /** Restricted special pages */
        $rpages = array();
        foreach($wgAvailableRights as $right) {
                /** only show pages a user can access */
                if( $wgUser->isAllowed($right) ) {
                        /** some rights might not have any special page associated */
                        if(isset($pages[$right])) {
                                $rpages = array_merge( $rpages, $pages[$right] );
                        }
                }
        }
        wfSpecialSpecialpages_gen( $rpages, 'restrictedpheading', $sk );
}

/**
 * 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_gen($pages,$heading,$sk) {
        global $wgOut, $wgSortSpecialPages;

        if( count( $pages ) == 0 ) {
                # Yeah, that was pointless. Thanks for coming.
                return;
        }

        /** Put them into a sortable array */
        $sortedPages = array();
        foreach ( $pages as $name => $page ) {
                if ( $page->isListed() ) {
                        $sortedPages[$page->getDescription()] = $page->getTitle();
                }
        }

        /** Sort */
        if ( $wgSortSpecialPages ) {
                ksort( $sortedPages );
        }


        $kat["Seiten"] = Array();
        $kat["Sonstige"] = Array();
        $kat["Benutzer"] = Array();
        $kat["Dateien"] = Array();
        $kat["Kategorien"] = Array();
        $kat["Weiterleitungen"] = Array();
        $kat["Vorlagen"] = Array();
        $kat["Mediawiki"] = Array();
        $kat["Suche"] = Array();
        $kat["hide"] = Array();

        foreach ( $sortedPages as $desc => $title ) {
                $t = $title->getText();
                $cat = "";
                switch ($t) {
                    case "Allpages":
                    case "Prefixindex":
                    case "Popularpages":
                    case "Randompage":
                    case "Lonelypages":
                    case "Export":
                    case "Deadendpages":
                    case "Mostrevisions":
                    case "Mostlinked":
                    case "Wantedpages":
                    case "Uncategorizedpages":
                    case "Newpages":
                    case "Ancientpages":
                    case "Shortpages":
                    case "Longpages":
                    case "Unwatchedpages":
                    case "Import":
                        $cat = "Seiten";
                        break;
                    case "Booksources":
                    case "Search":
                    case "MIMEsearch":
                        $cat = "Suche";
                        break;
                    case "Listredirects":
                    case "Randomredirect":
                    case "DoubleRedirects":
                    case "BrokenRedirects":
                        $cat = "Weiterleitungen";
                        break;
                    case "Version":
                    case "Statistics":
                    case "Log":
                    case "Allmessages":
                        $cat = "Mediawiki";
                        break;
                    case "Unusedtemplates":
                        $cat = "Vorlagen";
                        break;
                    case "Userlogin":
                    case "Listusers":
                    case "Preferences":
                    case "Watchlist":
                    case "Blockip":
                    case "Userrights":
                    case "Ipblocklist":
                        $cat = "Benutzer";
                        break;
                    case "Imagelist":
                    case "Newimages":
                    case "Unusedimages":
                    case "Upload":
                    case "Uncategorizedimages":
                    case "Mostimages":
                        $cat = "Dateien";
                        break;
                    case "Categories":
                    case "Wantedcategories":
                    case "Mostcategories":
                    case "Unusedcategories":
                    case "Uncategorizedcategories":
                    case "Mostlinkedcategories":
                        $cat = "Kategorien";
                        break;
                    case "Disambiguations":
                        $cat = "hide";
                        break;
                    default:
                        $cat = "Sonstige";
                        break;
                }
                array_push($kat[$cat], $sk->makeKnownLinkObj( $title, $desc ));
        }

        /** Now output the HTML */
        $wgOut->addHTML( '<h2>' . wfMsgHtml( $heading ) . "</h2>\n<ul>" );
        foreach ( $kat as $title => $value ) {
                if ($title == "hide")
                    continue;
                if (sizeof($value) == 0)
                    continue;
                $wgOut->addHTML( '<h2>' .  $title  . "</h2>\n<ul>" );

                foreach ( $value as $link )
                    $wgOut->addHTML( "<li>{$link}</li>\n" );
                $wgOut->addHTML( "</ul>\n" );
        }
        $wgOut->addHTML( "</ul>\n" );
}

?>

Source(s): Source Code solutions