NaturalSort.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author AW-UC <git@a-wesemann.de>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC;
  28. class NaturalSort {
  29. private static $instance;
  30. private $collator;
  31. private $cache = array();
  32. /**
  33. * Instantiate a new \OC\NaturalSort instance.
  34. * @param object $injectedCollator
  35. */
  36. public function __construct($injectedCollator = null) {
  37. // inject an instance of \Collator('en_US') to force using the php5-intl Collator
  38. // or inject an instance of \OC\NaturalSort_DefaultCollator to force using Owncloud's default collator
  39. if (isset($injectedCollator)) {
  40. $this->collator = $injectedCollator;
  41. \OCP\Util::writeLog('core', 'forced use of '.get_class($injectedCollator), \OCP\Util::DEBUG);
  42. }
  43. }
  44. /**
  45. * Split the given string in chunks of numbers and strings
  46. * @param string $t string
  47. * @return array of strings and number chunks
  48. */
  49. private function naturalSortChunkify($t) {
  50. // Adapted and ported to PHP from
  51. // http://my.opera.com/GreyWyvern/blog/show.dml/1671288
  52. if (isset($this->cache[$t])) {
  53. return $this->cache[$t];
  54. }
  55. $tz = array();
  56. $x = 0;
  57. $y = -1;
  58. $n = null;
  59. while (isset($t[$x])) {
  60. $c = $t[$x];
  61. // only include the dot in strings
  62. $m = ((!$n && $c === '.') || ($c >= '0' && $c <= '9'));
  63. if ($m !== $n) {
  64. // next chunk
  65. $y++;
  66. $tz[$y] = '';
  67. $n = $m;
  68. }
  69. $tz[$y] .= $c;
  70. $x++;
  71. }
  72. $this->cache[$t] = $tz;
  73. return $tz;
  74. }
  75. /**
  76. * Returns the string collator
  77. * @return \Collator string collator
  78. */
  79. private function getCollator() {
  80. if (!isset($this->collator)) {
  81. // looks like the default is en_US_POSIX which yields wrong sorting with
  82. // German umlauts, so using en_US instead
  83. if (class_exists('Collator')) {
  84. $this->collator = new \Collator('en_US');
  85. }
  86. else {
  87. $this->collator = new \OC\NaturalSort_DefaultCollator();
  88. }
  89. }
  90. return $this->collator;
  91. }
  92. /**
  93. * Compare two strings to provide a natural sort
  94. * @param string $a first string to compare
  95. * @param string $b second string to compare
  96. * @return int -1 if $b comes before $a, 1 if $a comes before $b
  97. * or 0 if the strings are identical
  98. */
  99. public function compare($a, $b) {
  100. // Needed because PHP doesn't sort correctly when numbers are enclosed in
  101. // parenthesis, even with NUMERIC_COLLATION enabled.
  102. // For example it gave ["test (2).txt", "test.txt"]
  103. // instead of ["test.txt", "test (2).txt"]
  104. $aa = self::naturalSortChunkify($a);
  105. $bb = self::naturalSortChunkify($b);
  106. for ($x = 0; isset($aa[$x]) && isset($bb[$x]); $x++) {
  107. $aChunk = $aa[$x];
  108. $bChunk = $bb[$x];
  109. if ($aChunk !== $bChunk) {
  110. // test first character (character comparison, not number comparison)
  111. if ($aChunk[0] >= '0' && $aChunk[0] <= '9' && $bChunk[0] >= '0' && $bChunk[0] <= '9') {
  112. $aNum = (int)$aChunk;
  113. $bNum = (int)$bChunk;
  114. return $aNum - $bNum;
  115. }
  116. return self::getCollator()->compare($aChunk, $bChunk);
  117. }
  118. }
  119. return count($aa) - count($bb);
  120. }
  121. /**
  122. * Returns a singleton
  123. * @return \OC\NaturalSort instance
  124. */
  125. public static function getInstance() {
  126. if (!isset(self::$instance)) {
  127. self::$instance = new \OC\NaturalSort();
  128. }
  129. return self::$instance;
  130. }
  131. }