Sort.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace dokuwiki\Utf8;
  3. use dokuwiki\Logger;
  4. /**
  5. * DokuWiki sort functions
  6. *
  7. * When "intl" extension is available, all sorts are done using a collator.
  8. * Otherwise, primitive PHP functions are called.
  9. *
  10. * The collator is created using the locale given in $conf['lang'].
  11. * It always uses case insensitive "natural" ordering in its collation.
  12. * The fallback solution uses the primitive PHP functions that return almost the same results
  13. * when the input is text with only [A-Za-z0-9] characters.
  14. *
  15. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  16. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  17. * @author Andreas Gohr <andi@splitbrain.org>
  18. */
  19. class Sort
  20. {
  21. /** @var \Collator[] language specific collators, usually only one */
  22. protected static $collators = [];
  23. /** @var bool should the intl extension be used if available? For testing only */
  24. protected static $useIntl = true;
  25. /**
  26. * Initialization of a collator using $conf['lang'] as the locale.
  27. * The initialization is done only once.
  28. * The collation takes "natural ordering" into account, that is, "page 2" is before "page 10".
  29. *
  30. * @return \Collator Returns a configured collator or null if the collator cannot be created.
  31. *
  32. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  33. */
  34. protected static function getCollator()
  35. {
  36. global $conf;
  37. $lc = $conf['lang'];
  38. // check if intl extension is available
  39. if (!self::$useIntl || !class_exists('\Collator')) {
  40. return null;
  41. }
  42. // load collator if not available yet
  43. if (!isset(self::$collators[$lc])) {
  44. $collator = \Collator::create($lc);
  45. if (!isset($collator)) return null; // check needed as stated in the docs
  46. $collator->setAttribute(\Collator::NUMERIC_COLLATION, \Collator::ON);
  47. Logger::getInstance(Logger::LOG_DEBUG)->log(
  48. 'Collator created with locale "' . $lc . '": numeric collation on, ' .
  49. 'valid locale "' . $collator->getLocale(\Locale::VALID_LOCALE) . '", ' .
  50. 'actual locale "' . $collator->getLocale(\Locale::ACTUAL_LOCALE) . '"',
  51. null, __FILE__, __LINE__
  52. );
  53. self::$collators[$lc] = $collator;
  54. }
  55. return self::$collators[$lc];
  56. }
  57. /**
  58. * Enable or disable the use of the "intl" extension collator.
  59. * This is used for testing and should not be used in normal code.
  60. *
  61. * @param bool $use
  62. *
  63. * @author Andreas Gohr <andi@splitbrain.org>
  64. */
  65. public static function useIntl($use = true)
  66. {
  67. self::$useIntl = $use;
  68. }
  69. /**
  70. * Drop-in replacement for strcmp(), strcasecmp(), strnatcmp() and strnatcasecmp().
  71. * It uses a collator-based comparison, or strnatcasecmp() as a fallback.
  72. *
  73. * @param string $str1 The first string.
  74. * @param string $str2 The second string.
  75. * @return int Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than $str2, and 0 if they are equal.
  76. *
  77. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  78. */
  79. public static function strcmp($str1, $str2)
  80. {
  81. $collator = self::getCollator();
  82. if (isset($collator)) {
  83. return $collator->compare($str1, $str2);
  84. } else {
  85. return strnatcasecmp($str1, $str2);
  86. }
  87. }
  88. /**
  89. * Drop-in replacement for sort().
  90. * It uses a collator-based sort, or sort() with flags SORT_NATURAL and SORT_FLAG_CASE as a fallback.
  91. *
  92. * @param array $array The input array.
  93. * @return bool Returns true on success or false on failure.
  94. *
  95. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  96. */
  97. public static function sort(&$array)
  98. {
  99. $collator = self::getCollator();
  100. if (isset($collator)) {
  101. return $collator->sort($array);
  102. } else {
  103. return sort($array, SORT_NATURAL | SORT_FLAG_CASE);
  104. }
  105. }
  106. /**
  107. * Drop-in replacement for ksort().
  108. * It uses a collator-based sort, or ksort() with flags SORT_NATURAL and SORT_FLAG_CASE as a fallback.
  109. *
  110. * @param array $array The input array.
  111. * @return bool Returns true on success or false on failure.
  112. *
  113. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  114. */
  115. public static function ksort(&$array)
  116. {
  117. $collator = self::getCollator();
  118. if (isset($collator)) {
  119. return uksort($array, array($collator, 'compare'));
  120. } else {
  121. return ksort($array, SORT_NATURAL | SORT_FLAG_CASE);
  122. }
  123. }
  124. /**
  125. * Drop-in replacement for asort(), natsort() and natcasesort().
  126. * It uses a collator-based sort, or asort() with flags SORT_NATURAL and SORT_FLAG_CASE as a fallback.
  127. *
  128. * @param array $array The input array.
  129. * @return bool Returns true on success or false on failure.
  130. *
  131. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  132. */
  133. public static function asort(&$array)
  134. {
  135. $collator = self::getCollator();
  136. if (isset($collator)) {
  137. return $collator->asort($array);
  138. } else {
  139. return asort($array, SORT_NATURAL | SORT_FLAG_CASE);
  140. }
  141. }
  142. /**
  143. * Drop-in replacement for asort(), natsort() and natcasesort() when the parameter is an array of filenames.
  144. * Filenames may not be equal to page names, depending on the setting in $conf['fnencode'],
  145. * so the correct behavior is to sort page names and reflect this sorting in the filename array.
  146. *
  147. * @param array $array The input array.
  148. * @return bool Returns true on success or false on failure.
  149. *
  150. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  151. * @author Andreas Gohr <andi@splitbrain.org>
  152. */
  153. public static function asortFN(&$array)
  154. {
  155. $collator = self::getCollator();
  156. return uasort($array, function ($fn1, $fn2) use ($collator) {
  157. if (isset($collator)) {
  158. return $collator->compare(utf8_decodeFN($fn1), utf8_decodeFN($fn2));
  159. } else {
  160. return strnatcasecmp(utf8_decodeFN($fn1), utf8_decodeFN($fn2));
  161. }
  162. });
  163. }
  164. }