r90760 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90759‎ | r90760 | r90761 >
Date:07:27, 25 June 2011
Author:bawolff
Status:deferred (Comments)
Tags:
Comment:
New extension: CategorySortHeaders, to make custom headers instead of just the first character of sortkey

depends on r90759
Modified paths:
  • /trunk/extensions/CategorySortHeaders (added) (history)
  • /trunk/extensions/CategorySortHeaders/CategorySortHeaders.i18n.php (added) (history)
  • /trunk/extensions/CategorySortHeaders/CategorySortHeaders.php (added) (history)
  • /trunk/extensions/CategorySortHeaders/CategorySortHeaders_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/CategorySortHeaders/CategorySortHeaders_body.php
@@ -0,0 +1,59 @@
 2+<?php
 3+if (!defined( 'MEDIAWIKI' )) die( "Not an entry point" );
 4+
 5+/**
 6+ * Pretty much based on UppercaseCollation from core.
 7+ *
 8+ * Collation that is case-insensitive, and allows specify
 9+ * custom 'first-character' headings for category pages.
 10+ */
 11+class CustomHeaderCollation extends Collation {
 12+
 13+ // The basic idea is we store the sortkey as three parts A^B^C
 14+ // A is the capitalized first letter of the header it falls under.
 15+ // B is the is the header it falls under (In whatever case the user enters)
 16+ // C is the rest of the sortkey.
 17+ //
 18+ // The user enters something like [[category:some cat|^my header^foo]]
 19+ // which gets turned into "^my header^foo\n<page name>"
 20+ // which we turn into "M^my header^FOO\n<PAGE NAME>"
 21+ function getSortKey( $string ) {
 22+ global $wgContLang;
 23+ // UppercaseCollation uses an EN lang object always instead of content lang.
 24+ // I'm not sure why. To me it makes more sense to use $wgContLang.
 25+ // There's minnor differences in some languages (like Turkish)
 26+
 27+ $matches = array();
 28+ if ( preg_match( '/^\^([^\n^]*)\^(.*)$/Ds', $string, $matches ) ) {
 29+ if ( $matches[1] === '' ) $matches[1] = ' ';
 30+ $part1 = $wgContLang->firstChar( $wgContLang->uc( $matches[1] ) );
 31+ $part2 = $matches[1];
 32+ $part3 = $wgContLang->uc( $matches[2] );
 33+
 34+ } else {
 35+ // Ordinay sortkey, no header info.
 36+ $part3 = $wgContLang->uc( $string );
 37+ $part1 = $part2 = $wgContLang->firstChar( $part3 );
 38+ }
 39+
 40+ return $part1 . '^' . $part2 . '^' . $part3;
 41+ }
 42+
 43+ function getFirstLetter( $string ) {
 44+ global $wgContLang;
 45+
 46+ # Stolen from UppercaseCollation
 47+ # not sure when this could actually happen.
 48+ if ( $string[0] === "\0" ) {
 49+ $string = substr( $string, 1 );
 50+ }
 51+
 52+ $m = array();
 53+ if ( preg_match( '/^[^\n^]*\^([^\n^]*)\^/', $string, $m ) ) {
 54+ return $m[1];
 55+ } else {
 56+ // Probably shouldn't happen.
 57+ return $wgContLang->ucfirst( $wgContLang->firstChar( $string ) );
 58+ }
 59+ }
 60+}
Property changes on: trunk/extensions/CategorySortHeaders/CategorySortHeaders_body.php
___________________________________________________________________
Added: svn:eol-style
161 + native
Index: trunk/extensions/CategorySortHeaders/CategorySortHeaders.i18n.php
@@ -0,0 +1,19 @@
 2+<?php
 3+/**
 4+ * i18n file for CategorySortHelpers, which really doesn't have any messages to begin with.
 5+ * @file
 6+ * @ingroup Extensions
 7+ */
 8+
 9+$messages = array();
 10+
 11+/** English
 12+ * @author Brian Wolff
 13+ */
 14+$messages['en'] = array(
 15+ 'categorysortheaders-desc' => 'Allow to specify custom multi-character \'first-character\' sorting headers to list pages under in categories, using syntax like <nowiki>[[category:Foo|^Header^Invisible part of sortkey]]</nowiki>.',
 16+);
 17+
 18+$messages['qqq'] = array(
 19+ 'categorysortheaders-desc' => '{{desc}}',
 20+);
Property changes on: trunk/extensions/CategorySortHeaders/CategorySortHeaders.i18n.php
___________________________________________________________________
Added: svn:eol-style
121 + native
Index: trunk/extensions/CategorySortHeaders/CategorySortHeaders.php
@@ -0,0 +1,60 @@
 2+<?php
 3+if (!defined( 'MEDIAWIKI' ) ) die('Not an entry point');
 4+
 5+/**
 6+ * Extension to allow specifying custom multi-character 'first-character'
 7+ * sorting headers to list pages under in categories, using syntax like
 8+ * [[category:Foo|^Header^Invisible part of sortkey]] or even just
 9+ * [[category:Foo|^Header^]].
 10+ *
 11+ * Note, this extension is incompatible with changing $wgCategoryCollation
 12+ * in LocalSettings.php. It defines its own Collation that is
 13+ * roughly equivalent to 'uppercase', and thus can't be used
 14+ * with 'uca-default' or any other custom collation.
 15+ * Additionally, this extension requires at least MediaWiki 1.17.
 16+ *
 17+ * To install:
 18+ * Add to the bottom of LocalSettings.php:
 19+ * require_once( "$IP/extensions/CategorySortHeaders/CategorySortHeaders.php" );
 20+ * Run either update.php or updateCollation.php
 21+ * (update.php can be run from the web installer if need be.)
 22+ *
 23+ **************************************************************
 24+ *
 25+ * Copyright © 2011 Brian Wolff
 26+ *
 27+ * This program is free software; you can redistribute it and/or modify
 28+ * it under the terms of the GNU General Public License as published by
 29+ * the Free Software Foundation; either version 2 of the License, or
 30+ * (at your option) any later version.
 31+ *
 32+ * This program is distributed in the hope that it will be useful,
 33+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 34+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 35+ * GNU General Public License for more details.
 36+ *
 37+ * You should have received a copy of the GNU General Public License along
 38+ * with this program; if not, write to the Free Software Foundation, Inc.,
 39+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 40+ * http://www.gnu.org/copyleft/gpl.html
 41+ *
 42+ * @author Brian Wolff
 43+ */
 44+
 45+
 46+$wgExtensionCredits['other'][] = array(
 47+ 'path' => __FILE__,
 48+ 'name' => 'CategorySortHeaders',
 49+ 'author' => '[http://mediawiki.org/wiki/User:Bawolff Brian Wolff]',
 50+ 'descriptionmsg' => 'categorysortheaders-desc',
 51+ 'url' => 'http://www.mediawiki.org/wiki/Extension:CategorySortHeaders',
 52+ 'version' => 0.1,
 53+);
 54+
 55+$dir = dirname( __FILE__ ) . '/';
 56+$wgExtensionMessagesFiles['CategorySortHeaders'] = $dir . 'CategorySortHeaders.i18n.php';
 57+$wgAutoloadClasses['CustomHeaderCollation'] = $dir . 'CategorySortHeaders_body.php';
 58+
 59+$wgCategoryCollation = 'CustomHeaderCollation';
 60+
 61+
Property changes on: trunk/extensions/CategorySortHeaders/CategorySortHeaders.php
___________________________________________________________________
Added: svn:eol-style
162 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r90800(Follow up r90760) fix comment about mw version required and fix getFirstLett...bawolff21:28, 25 June 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90759Let $wgCategoryCollation take a class name as a value so that extensions...bawolff07:21, 25 June 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:54, 25 June 2011

It would be nice to use some char which is not allowed in the titles... but of course the parser doesn't support it. Maybe with a magic word?

You mention that 1.17 is the oldest supported, even though you just committed a change in trunk?

#Comment by Bawolff (talk | contribs)   21:12, 25 June 2011

>You mention that 1.17 is the oldest supported, even though you just committed a change in trunk?

Yeah, I'm planning to change that next commit. I thought I could already do this in 1.17, but found out I couldn't when MediaWiki started throwing exceptions at me. I also have to change CustomHeaderCollation::getFirstLetter. At the time I thought it worked on the encoded cl_sortkey field, but actually it works on the human-readable field (the function still works by coincidence though)

>It would be nice to use some char which is not allowed in the titles... but of course the parser doesn't support it. Maybe with a magic word?

Yes, I used ^ because its so rarely used. I also considered using some obscure unicode character, but thought that'd be more trouble then it was worth since it'd be hard to type. The magic word idea might be cool, something similar to #defaultsort. Could have {{#catheader:header|sortkey}} and then converts that to a sortkey with the parts separated by control characters. This extension was originally inspired by the patch at Help_talk:Categories#Sort_by_more_than_one_character where they use parenthesis, but I think those are much too common a character.

Status & tagging log