Extension:Presort

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
Presort
Release status: unmaintained
Implementation Tag
Description Allows to presort wikitables
Author(s) Matthias Blarr (Unsmackedtalk)
Latest version 1.0 (2013-07-23)
Database changes No
License GNU General Public License 2.0
Download See the code section
<presort>
Translate the Presort extension if it is available at translatewiki.net
Check usage and version matrix.

The Presort extension adds a <presort> tag to the wiki markup. This tag can be used around sortable and non-sortable wikitables to presort the table without the need of an ordered wikitext.

Parameter[edit]

mode[edit]

  • column

The table is presorted according to the values in a column.

  • key

The table is presorted according to a key.
Default value is "column".

sortcolumn[edit]

defines the sortcolumn. Can only be used with "column" mode. Numbering starts with 1.
Default value is "1".

order[edit]

  • asc

ascending order

  • desc

descending order
Default value is "asc".

numberingcolumn[edit]

Allows to define a columnnumber, in which automated numbering is filled in. All existing content in this column is overwritten! Default value is -1, which means no numbering.

template[edit]

Allows to define the template used for the key definition. For example templates like dts, sort or hiddensort. Can only be used with "key" mode.
Default value is "dts".

Installation[edit]

  • Download and place the file(s) in a directory called Presort in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/Presort/Presort.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

Presort.php
<?php
 
/**
 * Parser hook extension adds a <presort> tag to wiki markup
 *
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author Matthias Blarr 
 * @copyright 2013 Matthias Blarr
 * @license GNU General Public Licence 2.0
 * @version 1.0
 * thx to kaeptn00 for his extension sort2 where i got the idea from for this and also the skeleton extension code is from there.
 *
 */
 
if( defined( 'MEDIAWIKI' ) ) {
 
        $wgExtensionFunctions[] = 'presort';
        $wgExtensionCredits['parserhook'][] = array( 
        'name' => 'Presort',
        'version' => '1.0.0',
        'author' => 'Matthias Blarr',
        'description' => 'Adds a presort tag to presort (sortable) wikitables',
        'url' => 'https://www.mediawiki.org/wiki/Extension:Presort'
        );
 
        function presort() {
                global $wgParser;
                $wgParser->setHook( 'presort', 'presort2' );
        }
 
        function presort2( $input, $args, $parser ) {
                $sorter2 = new PreSorter( $parser );
        $sorter2->loadSettings( $args );
                return $sorter2->read( $input );
        }
 
        class PreSorter {
 
                var $parser;
                var $order;
                var $mode;
                var $sortcolumn;
				var $template;
				var $numberingcolumn;
        
        function read( $text) {
        //cut end symbol and create line array
        $pos_end=strpos($text,"\n|}");
        $lines = explode( "\n|-", substr($text,0,$pos_end) );
        $prepart = array();
        $sortpart = array();
                        //divide lines into sortable and non-sortable part
            foreach( $lines as $line ) {
            //if( $this->GetSortKey( $line) == false)
            if( $this->GetSortKey( $line) !== false) {break;}
            $prepart[] = $line;
            }
            
            foreach( $lines as $line ) {
            $sortkey = $this->GetSortKey( $line);
                        if( $sortkey !== false)  {
						  $sortpart[$line] = $sortkey;
            }}
            //SORT
            natsort($sortpart);
            if($this->order == 'desc') {$sortpart=array_reverse($sortpart);}
			//NUMBERING
			if($this->numberingcolumn !==-1){
			$i=0;
			foreach( $sortpart as $key => $temp) {
					$pos=stripos($key,"\n|")+2;
                    $text2=substr($key,$pos);
					$cells = preg_split( "/(\n\||\|\|)/", $text2 );
					$cells[$this->numberingcolumn-1]=$i+1;
					$sortpart_temp[]="\n|".implode("\n|",$cells);
					$i++;
					}
				$sortpart=array_flip($sortpart_temp);
			}
            //line array->wikitext
            $part1=implode("\n|-", $prepart);
            $part2=implode("\n|-", array_keys($sortpart));
            $parsetext=$part1."\n|-".$part2."\n|}";
            //PARSE wikitext
        $html = $this->parse( $parsetext );
        return $html;
        }
                function __construct( &$parser ) {
                        $this->parser = &$parser;
            $this->order = 'asc';
                        $this->mode = 'column';
            $this->sortcolumn = 1;
            $this->template = 'dts';
			$this->numberingcolumn=-1;
                }
       
        function GetSortKey ( $text) {
            $sortkeyparsed = "0";
            if($this->mode == 'column') {
                if(stripos($text, "!") !== false||stripos($text,"wikitable")!==false){return false;} else
                {
                    $pos=stripos($text,"\n|")+2;
                    $text2=substr($text,$pos);
                    $cells = preg_split( "/(\n\||\|\|)/", $text2 );
                    if(isset($cells[$this->sortcolumn-1])) {
                        $cell_sort=$cells[$this->sortcolumn-1];
                    } else {
                        $cell_sort=$cells[0];
                    }
                    $pipe_pos=stripos($cell_sort,'|');
                    if($pipe_pos!==false){$temp=trim(strtolower(substr($cell_sort,$pipe_pos+1)));return $temp;}else{
                        return trim(strtolower($cell_sort));}
            }}
            $str_start=stripos($text, $this->template);
            if( $str_start !== false)  {
                $str_end=stripos($text, '}}', $str_start);
                $length=$str_end+2-$str_start;
                $sortkey = substr($text, $str_start, $length);
                $sortkey_parsed = $this->parse( $sortkey );
                return $sortkey_parsed;
            }
            return false;

        }
        
        function loadSettings( $settings ) {
                        if( isset( $settings['order'] ) ){
                                $o = strtolower( $settings['order'] );
                                if( $o == 'asc' || $o == 'desc')
                                        $this->order = $o;
                        }
                        if( isset( $settings['mode'] ) ) {
                                $c = strtolower( $settings['mode'] );
                                if( $c == 'key' || $c == 'column' )
                                        $this->mode = $c;
                        }
            if( isset($settings['sortcolumn']) ){
                                $this->sortcolumn = $settings['sortcolumn'];
            }
            if( isset($settings['template']) ){
                                $this->template = $settings['template'];
            }
			if( isset($settings['numberingcolumn']) ){
                                $this->numberingcolumn = $settings['numberingcolumn'];
            }
        }
 
                function parse( $text ) {
                        $title =& $this->parser->mTitle;
                        $options =& $this->parser->mOptions;
                        $output = $this->parser->parse( $text, $title, $options, true, false );
                        return $output->getText();
                }
 
        }
 
} else {
        echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
        die( -1 );
}