Skip to content

Commit

Permalink
Use natural string ordering for sorting pages in namespace includes
Browse files Browse the repository at this point in the history
  • Loading branch information
michitux committed Nov 28, 2012
1 parent d0eed7e commit c69c442
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions _test/namespace_includes.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
23 changes: 19 additions & 4 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.
Expand Down

0 comments on commit c69c442

Please sign in to comment.