Clump has uploaded a new change for review. https://gerrit.wikimedia.org/r/320301
Change subject: SimpleSort extension: Populate with initial files ...................................................................... SimpleSort extension: Populate with initial files This all files for the initial version of this extension. Change-Id: I17d8b7016265c1ff8da8795aad2cc2c6b55421e9 --- A SimpleSort.hooks.php A SimpleSort.i18n.magic.php A SimpleSort.php A extension.json A i18n/en.json A i18n/qqq.json 6 files changed, 184 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SimpleSort refs/changes/01/320301/1 diff --git a/SimpleSort.hooks.php b/SimpleSort.hooks.php new file mode 100644 index 0000000..21323a4 --- /dev/null +++ b/SimpleSort.hooks.php @@ -0,0 +1,115 @@ +<?php +/** + * Hooks for SimpleSort extension + * + * @file + * @ingroup Extensions + */ + +class SimpleSortHooks { + + // Register any render callbacks with the parser + public static function onParserFirstCallInit( Parser &$parser ) { + + // Create a function hook associating the "simplesort" magic word + $parser->setFunctionHook( 'simplesort', [ __CLASS__, 'renderSort' ] ); + + return true; + } + + // Render the output of {{#simplesort:}}. + // If two arguments are supplied, the 1st specifies options, whitespace-separated: + // desc Sort in descending order + // alpha Alphabetic sorting (regular, not natural) + // num Numeric sorting + // case Case-sensitive sorting + // insep="x" Use x as a list separator in order to identify individual elements in the input. + // Note that whitespace on either side of x is ignored, and discarded. + // The quotes are required. + // outsep="x" Use x as a list separator in the output. + // The quotes are required. + // Default sorting options are to use php's "natural", case-insensitive sort order, + // and a comma-separator for both input and output. + // The remaining argument is the sortable list. + public static function renderSort( $parser ) { + // defaults + $asc = true; // Ascending or descending. + $nat = true; // Natural sorting. + $alpha = true; // Alphabetic or numeric, overridden by nat. + $cs = false; // Case-sensitive + $insep = ","; // Input separator. + $outsep = ","; // Output separator. + + $numargs = func_num_args(); + $arglist = func_get_args(); + $input = ""; + + if ( $numargs >= 3 ) { + // Options specified, extract them. + $options = $arglist[1]; + + // Outsep and insep options are complicated by the potential for using + // whitespace as a separator. So first look for them. + preg_match( '/insep="([^"]*)"/', $options, $msep ); + if ( $msep ) { + $insep = $msep[1]; + // Remove the option from the string of options. + $options = str_replace( $msep[0], "", $options ); + } + + preg_match( '/outsep="([^"]*)"/', $options, $osep ); + if ( $osep ) { + $outsep = $osep[1]; + // Remove the option from the string of options. + $options = str_replace( $osep[0], "", $options ); + } else { + $outsep = $insep; + } + + // Only 3 options actually possible, but excessively, parse up to 4 to + // catch and isolate trailing debris. + $opts = preg_split( "/[\s]+/", trim( $options ), 4 ); + + // Check for each option. Ignore blank options if they somehow got in there. + for ( $i=0; $i < count( $opts ); $i++ ) { + if ( $opts[$i] === "desc" ) { + $asc = false; + } else if ( $opts[$i] === "alpha" ) { + $alpha = true; + $nat = false; + } else if ( $opts[$i] === "num" ) { + $alpha = false; + $nat = false; + } else if ( $opts[$i] === "case" ) { + $cs = true; + } else if ( $opts[$i] !== "" ) { + return wfMessage( 'simplesort-err', $opts[$i] )->text(); + } + } + $input = $arglist[2]; + } else if ( $numargs == 2 ) { + $input = $arglist[1]; + } + + if ( $input === "" ) { + $output = ""; + } else { + if ( $insep === "" ) + $ilist = str_split( preg_replace( "/[\s]+/", "", $input ) ); + else + $ilist = preg_split( "/[\s]*" . preg_quote( $insep ) . "[\s]*/", $input ); + + $flags = ( ( $nat ) ? SORT_NATURAL : ( ( $alpha ) ? SORT_STRING : SORT_NUMERIC ) ) + | ( ( $cs ) ? 0 : SORT_FLAG_CASE ); + + if ( $asc ) + sort( $ilist, $flags ); + else + rsort( $ilist, $flags ); + + $output = implode( $outsep, $ilist ); + } + + return [ $output, 'noparse' => false ]; + } +} diff --git a/SimpleSort.i18n.magic.php b/SimpleSort.i18n.magic.php new file mode 100644 index 0000000..0db1a3b --- /dev/null +++ b/SimpleSort.i18n.magic.php @@ -0,0 +1,12 @@ +<?php +/** + * Magic words for SimpleSort + * + * @author Clark Verbrugge (clump) + */ + +$magicWords = []; + +$magicWords['en'] = [ + 'simplesort' => [ 0, 'simplesort' ], +]; diff --git a/SimpleSort.php b/SimpleSort.php new file mode 100644 index 0000000..2ee5ef6 --- /dev/null +++ b/SimpleSort.php @@ -0,0 +1,15 @@ +<?php + +if ( function_exists( 'wfLoadExtension' ) ) { + wfLoadExtension( 'SimpleSort' ); + // Keep i18n globals so mergeMessageFileList.php doesn't break + $wgMessagesDirs['SimpleSort'] = __DIR__ . '/i18n'; + $wgExtensionMessagesFiles['SimpleSort'] = __DIR__ . '/SimpleSort.i18n.magic.php'; + wfWarn( + 'Deprecated PHP entry point used for SimpleSort extension. Please use wfLoadExtension ' . + 'instead, see https://www.mediawiki.org/wiki/Extension_registration for more details.' + ); + return true; +} else { + die( 'This version of the SimpleSort extension requires MediaWiki 1.25+' ); +} diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..ea99274 --- /dev/null +++ b/extension.json @@ -0,0 +1,28 @@ +{ + "name": "SimpleSort", + "version": "1.0.0", + "author": [ + "Clark Verbrugge" + ], + "url": "https://www.mediawiki.org/wiki/Extension:SimpleSort", + "descriptionmsg": "simplesort-desc", + "license-name": "CC0-1.0", + "type": "parserhook", + "AutoloadClasses": { + "SimpleSortHooks": "SimpleSort.hooks.php" + }, + "ExtensionMessagesFiles": { + "SimpleSortMagic": "SimpleSort.i18n.magic.php" + }, + "Hooks": { + "ParserFirstCallInit": [ + "SimpleSortHooks::onParserFirstCallInit" + ] + }, + "MessagesDirs": { + "SimpleSort": [ + "i18n" + ] + }, + "manifest_version": 1 +} diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..ff41a88 --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,7 @@ +{ + "@metadata": { + "authors": ["Clark Verbrugge"] + }, + "simplesort-desc": "Simple sorting of a string of items based on a given delimiter.", + "simplesort-err": "Unrecognized option to SimpleSort: \"$1\"" +} diff --git a/i18n/qqq.json b/i18n/qqq.json new file mode 100644 index 0000000..c9b97b1 --- /dev/null +++ b/i18n/qqq.json @@ -0,0 +1,7 @@ +{ + "@metadata": { + "authors": ["Clark Verbrugge"] + }, + "simplesort-desc": "{{desc|name=SimpleSort|url=https://www.mediawiki.org/wiki/Extension:SimpleSort}}", + "simplesort-err": "Error message for unrecognized sorting options" +} -- To view, visit https://gerrit.wikimedia.org/r/320301 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I17d8b7016265c1ff8da8795aad2cc2c6b55421e9 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/SimpleSort Gerrit-Branch: master Gerrit-Owner: Clump <cl...@snoopycat.ca> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits