Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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