Index: trunk/extensions/Sort.php |
— | — | @@ -0,0 +1,103 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Parser hook extension adds a <sort> tag to wiki markup |
| 6 | + * |
| 7 | + * <sort> |
| 8 | + * <sort order="asc" class="ol"> |
| 9 | + * |
| 10 | + * Both attributes are optional; the default is for an ascending |
| 11 | + * sort using an unordered list. Order can be ASC or DESC, case |
| 12 | + * insensitive. Class can be OL or UL, also case insensitive. |
| 13 | + * |
| 14 | + * @package MediaWiki |
| 15 | + * @subpackage Extensions |
| 16 | + * @author Rob Church <robchur@gmail.com> |
| 17 | + * @copyright © 2006 Rob Church |
| 18 | + * @licence GNU General Public Licence 2.0 |
| 19 | + */ |
| 20 | + |
| 21 | +if( defined( 'MEDIAWIKI' ) ) { |
| 22 | + |
| 23 | + $wgExtensionFunctions[] = 'efSort'; |
| 24 | + |
| 25 | + function efSort() { |
| 26 | + global $wgParser; |
| 27 | + $wgParser->setHook( 'sort', 'efRenderSort' ); |
| 28 | + } |
| 29 | + |
| 30 | + function efRenderSort( $input, $args, &$parser ) { |
| 31 | + $sorter = new Sorter( $parser ); |
| 32 | + $sorter->loadSettings( $args ); |
| 33 | + return $sorter->sortToHtml( $input ); |
| 34 | + } |
| 35 | + |
| 36 | + class Sorter { |
| 37 | + |
| 38 | + var $parser; |
| 39 | + var $order; |
| 40 | + var $class; |
| 41 | + |
| 42 | + function Sorter( &$parser ) { |
| 43 | + $this->parser =& $parser; |
| 44 | + $this->order = 'asc'; |
| 45 | + $this->class = 'ul'; |
| 46 | + } |
| 47 | + |
| 48 | + function loadSettings( $settings ) { |
| 49 | + if( isset( $settings['order'] ) ) |
| 50 | + $this->order = strtolower( $settings['order'] ) == 'desc' ? 'desc' : 'asc'; |
| 51 | + if( isset( $settings['class'] ) ) { |
| 52 | + $c = strtolower( $settings['class'] ); |
| 53 | + if( $c == 'ul' || $c == 'ol' ) |
| 54 | + $this->class = $c; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + function internalSort( $text ) { |
| 59 | + $raw = explode( "\n", $text ); |
| 60 | + $lines = array(); |
| 61 | + foreach( $raw as $line ) { |
| 62 | + if( trim( $line ) != '' ) { |
| 63 | + $html = $this->parse( $line ); |
| 64 | + $lines[ $this->stripHtml( $html ) ] = $html; |
| 65 | + } |
| 66 | + } |
| 67 | + if( $this->order == 'desc' ) { |
| 68 | + krsort( $lines ); |
| 69 | + } else { |
| 70 | + ksort( $lines ); |
| 71 | + } |
| 72 | + return $lines; |
| 73 | + } |
| 74 | + |
| 75 | + function makeList( $sorted ) { |
| 76 | + $tag = $this->class == 'ol' ? 'ol' : 'ul'; |
| 77 | + foreach( $sorted as $item ) |
| 78 | + $list[] = wfOpenElement( 'li' ) . $item . wfCloseElement( 'li' ); |
| 79 | + return wfOpenElement( $tag ) . implode( "\n", $list ) . wfCloseElement( $tag ); |
| 80 | + } |
| 81 | + |
| 82 | + function sortToHtml( $text ) { |
| 83 | + return $this->makeList( $this->internalSort( $text ) ); |
| 84 | + } |
| 85 | + |
| 86 | + function stripHtml( $text ) { |
| 87 | + return preg_replace( '@<[\/\!]*?[^<>]*?>@si', '', $text ); |
| 88 | + } |
| 89 | + |
| 90 | + function parse( $text ) { |
| 91 | + $title =& $this->parser->mTitle; |
| 92 | + $options =& $this->parser->mOptions; |
| 93 | + $output = $this->parser->parse( $text, $title, $options, false, false ); |
| 94 | + return $output->getText(); |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} else { |
| 100 | + echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" ); |
| 101 | + die( -1 ); |
| 102 | +} |
| 103 | + |
| 104 | +?> |
\ No newline at end of file |
Property changes on: trunk/extensions/Sort.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 105 | + native |