1
0

NaturalSort.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author AW-UC <git@a-wesemann.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Vincent Petry <vincent@nextcloud.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. use Psr\Log\LoggerInterface;
  29. class NaturalSort {
  30. private static $instance;
  31. private $collator;
  32. private $cache = [];
  33. /**
  34. * Instantiate a new \OC\NaturalSort instance.
  35. * @param object $injectedCollator
  36. */
  37. public function __construct($injectedCollator = null) {
  38. // inject an instance of \Collator('en_US') to force using the php5-intl Collator
  39. // or inject an instance of \OC\NaturalSort_DefaultCollator to force using Owncloud's default collator
  40. if (isset($injectedCollator)) {
  41. $this->collator = $injectedCollator;
  42. \OC::$server->get(LoggerInterface::class)->debug('forced use of '.get_class($injectedCollator));
  43. }
  44. }
  45. /**
  46. * Split the given string in chunks of numbers and strings
  47. * @param string $t string
  48. * @return array of strings and number chunks
  49. */
  50. private function naturalSortChunkify($t) {
  51. // Adapted and ported to PHP from
  52. // http://my.opera.com/GreyWyvern/blog/show.dml/1671288
  53. if (isset($this->cache[$t])) {
  54. return $this->cache[$t];
  55. }
  56. $tz = [];
  57. $x = 0;
  58. $y = -1;
  59. $n = null;
  60. while (isset($t[$x])) {
  61. $c = $t[$x];
  62. // only include the dot in strings
  63. $m = ((!$n && $c === '.') || ($c >= '0' && $c <= '9'));
  64. if ($m !== $n) {
  65. // next chunk
  66. $y++;
  67. $tz[$y] = '';
  68. $n = $m;
  69. }
  70. $tz[$y] .= $c;
  71. $x++;
  72. }
  73. $this->cache[$t] = $tz;
  74. return $tz;
  75. }
  76. /**
  77. * Returns the string collator
  78. * @return \Collator string collator
  79. */
  80. private function getCollator() {
  81. if (!isset($this->collator)) {
  82. // looks like the default is en_US_POSIX which yields wrong sorting with
  83. // German umlauts, so using en_US instead
  84. if (class_exists('Collator')) {
  85. $this->collator = new \Collator('en_US');
  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. }