Extension:CustomCategory

From MediaWiki.org
Jump to: navigation, search


MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
CustomCategory

Release status:Extension status stable

ImplementationTemplate:Extension#type MyWiki
DescriptionTemplate:Extension#description Allows to customize category display, to display sortkey for document instead of their unique title
Author(s)Template:Extension#username Cchanteptalk
Latest versionTemplate:Extension#version 0.1-SNAPSHOT
MediaWikiTemplate:Extension#mediawiki 1.10.x - 1.14.0
LicenseTemplate:Extension#license No license specified
Download Download page

Translate the CustomCategory extension if it is available at translatewiki.net

Check usage and version matrix.

What can this extension do?[edit source]

CustomCategory extension allows to display sortkey, from {{DEFAULTSORT:'''sortkey'''}} or [[Category:Cat|'''sortkey''']], for articles belonging to a given category.

In order to switch between plain category view and this custom view, you just have to add [[Category:DisplaySortkey]] in the category itself, that is to say as parent category. For exemple, you put Category:A in Category:DisplaySortkey, then all articles put in Category:A are displayed in A by their sortkey.

Usage[edit source]

Installation[edit source]

Add a CustomCategory sub-directory in your MediaWiki extensions directory. In this new directory add files CustomCategory.setup.php and CustomCategoryViewer.php.

Finally to setup this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/CustomCategory/CustomCategory.setup.php");

Code[edit source]

File CustomCategory.setup.php[edit source]

<?php
if ( ! defined( 'MEDIAWIKI' ) )
        die();

/**#@+
 *
 * @addtogroup Extensions
 */
$wgExtensionFunctions[] = 'setupCustomCategoryView';
$wgExtensionCredits['parserhook'][] = array(
  'name' => 'CustomCategory',
  'author' => 'Cedric Chantepie',
  'url' => 'http://www.mediawiki.org/wiki/Extension:CustomCategory',
  'description' => 'Allow to display sortkey instead of article name in category view'
);
$wgHooks['CategoryPageView'][] = 'customCategoryView';

require_once ( dirname( __FILE__ ) . '/CustomCategoryViewer.php' );

/**
 */
function setupCustomCategoryView() {
  global $customCategoryViewer;
  $customCategoryViewer = new CustomCategoryViewer();
}

/**
 * Category hook function
 */
function customCategoryView(&$categoryArticle) {
  $meth = new ReflectionMethod('Article', 'view');

  $meth->invoke($categoryArticle);

  if ( NS_CATEGORY != $categoryArticle->mTitle->getNamespace() ) {
    return true; // skip
  }
  // ---

  // Copied from CategoryPage::closeShowCategory
  global $wgOut, $wgRequest, $customCategoryViewer;
  $from = $wgRequest->getVal( 'from' );
  $until = $wgRequest->getVal( 'until' );

  $customCategoryViewer->init( $categoryArticle->mTitle, $from, $until );
  $wgOut->addHTML( $customCategoryViewer->getHTML() );

  return false;
}

File CustomCategoryViewer.php[edit source]

<?php
if ( ! defined( 'MEDIAWIKI' ) )
        die();

require_once( "$IP/includes/Article.php" );
require_once( "$IP/includes/CategoryPage.php" );

/**
 * Category view with custom display.
 */
class CustomCategoryViewer extends CategoryViewer {
        /**
         * No-arg constructor.
         */
        function __construct() {
        }

        /**
         * Initialize
         * Copied from CategoryViewer::__construct
         */
        function init($title, $from = '', $until = '') {
                global $wgCategoryPagingLimit;
                $this->title = $title;
                $this->from = $from;
                $this->until = $until;
                $this->limit = $wgCategoryPagingLimit;
        }

        /**
         * Copied from original CategoryView::doCategoryQuery
         */
        function doCategoryQuery() {
                $dbr = wfGetDB( DB_SLAVE );
                if( $this->from != '' ) {
                        $pageCondition = 'cl_sortkey >= ' . $dbr->addQuotes( $this->from );
                        $this->flip = false;
                } elseif( $this->until != '' ) {
                        $pageCondition = 'cl_sortkey < ' . $dbr->addQuotes( $this->until );
                        $this->flip = true;
                } else {
                        $pageCondition = '1 = 1';
                        $this->flip = false;
                }
                $res = $dbr->select(
                        array( 'page', 'categorylinks' ),
                        array( 'page_title', 'page_namespace', 'page_len', 'cl_sortkey' ),
                        array( $pageCondition,
                               'cl_from          =  page_id',
                               'cl_to'           => $this->title->getDBKey()),
                               #'page_is_redirect' => 0),
                        #+ $pageCondition,
                        __METHOD__,
                        array( 'ORDER BY' => $this->flip ? 'cl_sortkey DESC' : 'cl_sortkey',
                               'USE INDEX' => 'cl_sortkey', 
                               'LIMIT'    => $this->limit + 1 ) );

                $count = 0;
                global $wgContLang;

                $categories = $this->title->getParentCategories();
                $plainView = ($categories[$wgContLang->getNSText ( NS_CATEGORY ) . ":DisplaySortkey"] == NULL);

                // sortkey == '*' | sortkey == page_title
                $this->nextPage = null;
                while( $x = $dbr->fetchObject ( $res ) ) {
                        if( ++$count > $this->limit ) {
                                // We've reached the one extra which shows that there are
                                // additional pages to be had. Stop here...
                                $this->nextPage = $x->cl_sortkey;
                                break;
                        }
                        // HERE
                        $nsDefaultSortkey = $wgContLang->getNSText($x->page_namespace) . ':' . $x->page_title;

                        if (!$plainView && 
                            $x->cl_sortkey != $x->page_title &&
                            $x->cl_sortkey != $nsDefaultSortkey &&
                            $x->cl_sortkey != '*') {

                          $x->cl_sortkey = '_CustomCategory_:' . $x->cl_sortkey;
                        }
                        // _END
                        $title = Title::makeTitle( $x->page_namespace, $x->page_title );

                        if( $title->getNamespace() == NS_CATEGORY ) {
                                $this->addSubcategory( $title, $x->cl_sortkey, $x->page_len );
                        } elseif( $title->getNamespace() == NS_IMAGE ) {
                                $this->addImage( $title, $x->cl_sortkey, $x->page_len );
                        } else {
                                $this->addPage( $title, $x->cl_sortkey, $x->page_len );
                        }
                }
                $dbr->freeResult( $res );
        } 

        function addPage( $title, $sortkey, $pageLength ) {
                $label = $title->getPrefixedText();

                if (strpos($sortkey, 'CustomCategory_:') == 1) {
                  $sortkey = substr($sortkey, 17);
                  $label = $sortkey;
                }

                global $wgContLang;
                $this->articles[] = $this->getSkin()->makeSizeLinkObj(
                  $pageLength, 
                  $title, 
                  $label
                );

                $this->articles_start_char[] = 
                  $wgContLang->convert( $wgContLang->firstChar( $sortkey ) );
        }
}

Mediawiki 1.13.x to 1.19.x[edit source]

This extension need to be reviewed to be compliant with mediawiki 1.13.x to 1.19.x.