Extension:CustomCategory
From MediaWiki.org
|
CustomCategory Release status: beta |
|
|---|---|
| Implementation | MyWiki |
| Description | Allows to customize category display, to display sortkey for document instead of their unique title |
| Author(s) | Cchantep Talk |
| Version | 0.1-SNAPSHOT |
| MediaWiki | 1.10.x |
| Download | Download page |
Contents |
[edit] What can this extension do?
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 article put in Category:A are displayed in A by their sortkey.
[edit] Usage
[edit] Installation
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");
[edit] Code
[edit] File CustomCategory.setup.php
<?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; }
[edit] File CustomCategoryViewer.php
<?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 ) ); } }

