From c69c442310827ff0ec1914a5b017100dd54ec0eb Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Wed, 28 Nov 2012 21:16:29 +0100 Subject: [PATCH] Use natural string ordering for sorting pages in namespace includes --- _test/namespace_includes.test.php | 4 ++-- helper.php | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/_test/namespace_includes.test.php b/_test/namespace_includes.test.php index f077d07..4283438 100644 --- a/_test/namespace_includes.test.php +++ b/_test/namespace_includes.test.php @@ -34,8 +34,8 @@ public function setup() { // for page ordering saveWikiText('inclorder:page1', 'Page 1', 'Created page 1'); saveWikiText('inclorder:page2', 'Page 2', 'Created page 2'); - saveWikiText('inclorder:page3', '{{include_n>1}} Page 3/1', 'created page 3/1'); - saveWikiText('inclorder:page4', '{{include_n>0}} Page 4/0', 'created page 4/0'); + saveWikiText('inclorder:page3', '{{include_n>10}} Page 3/10', 'created page 3/1'); + saveWikiText('inclorder:page4', '{{include_n>2}} Page 4/2', 'created page 4/0'); } /** diff --git a/helper.php b/helper.php index 0d80b55..ebaafd3 100644 --- a/helper.php +++ b/helper.php @@ -625,9 +625,9 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { if (count($pages) > 1) { if ($flags['order'] === 'id') { if ($flags['rsort']) { - rsort($pages); + usort($pages, array($this, '_r_strnatcasecmp')); } else { - sort($pages); + natcasesort($pages); } } else { $ordered_pages = array(); @@ -658,9 +658,9 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { $ordered_pages[$key] = $page; } if ($flags['rsort']) { - krsort($ordered_pages); + uksort($ordered_pages, array($this, '_r_strnatcasecmp')); } else { - ksort($ordered_pages); + uksort($ordered_pages, 'strnatcasecmp'); } $pages = $ordered_pages; } @@ -674,6 +674,21 @@ function _get_included_pages($mode, $page, $sect, $parent_id, $flags) { return $result; } + /** + * String comparisons using a "natural order" algorithm in reverse order + * + * @link http://php.net/manual/en/function.strnatcmp.php + * @param string $a First string + * @param string $b Second string + * @return int Similar to other string comparison functions, this one returns < 0 if + * str1 is greater than str2; > + * 0 if str1 is lesser than + * str2, and 0 if they are equal. + */ + function _r_strnatcasecmp($a, $b) { + return strnatcasecmp($b, $a); + } + /** * This function generates the list of all included pages from a list of metadata * instructions.