You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

181 lines
6.2 KiB

  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,
  52. __FILE__,
  53. __LINE__
  54. );
  55. self::$collators[$lc] = $collator;
  56. }
  57. return self::$collators[$lc];
  58. }
  59. /**
  60. * Enable or disable the use of the "intl" extension collator.
  61. * This is used for testing and should not be used in normal code.
  62. *
  63. * @param bool $use
  64. *
  65. * @author Andreas Gohr <andi@splitbrain.org>
  66. */
  67. public static function useIntl($use = true)
  68. {
  69. self::$useIntl = $use;
  70. }
  71. /**
  72. * Drop-in replacement for strcmp(), strcasecmp(), strnatcmp() and strnatcasecmp().
  73. * It uses a collator-based comparison, or strnatcasecmp() as a fallback.
  74. *
  75. * @param string $str1 The first string.
  76. * @param string $str2 The second string.
  77. * @return int Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than $str2, and 0 if they are equal.
  78. *
  79. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  80. */
  81. public static function strcmp($str1, $str2)
  82. {
  83. $collator = self::getCollator();
  84. if (isset($collator)) {
  85. return $collator->compare($str1, $str2);
  86. } else {
  87. return strnatcasecmp($str1, $str2);
  88. }
  89. }
  90. /**
  91. * Drop-in replacement for sort().
  92. * It uses a collator-based sort, or sort() with flags SORT_NATURAL and SORT_FLAG_CASE as a fallback.
  93. *
  94. * @param array $array The input array.
  95. * @return bool Returns true on success or false on failure.
  96. *
  97. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  98. */
  99. public static function sort(&$array)
  100. {
  101. $collator = self::getCollator();
  102. if (isset($collator)) {
  103. return $collator->sort($array);
  104. } else {
  105. return sort($array, SORT_NATURAL | SORT_FLAG_CASE);
  106. }
  107. }
  108. /**
  109. * Drop-in replacement for ksort().
  110. * It uses a collator-based sort, or ksort() with flags SORT_NATURAL and SORT_FLAG_CASE as a fallback.
  111. *
  112. * @param array $array The input array.
  113. * @return bool Returns true on success or false on failure.
  114. *
  115. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  116. */
  117. public static function ksort(&$array)
  118. {
  119. $collator = self::getCollator();
  120. if (isset($collator)) {
  121. return uksort($array, [$collator, 'compare']);
  122. } else {
  123. return ksort($array, SORT_NATURAL | SORT_FLAG_CASE);
  124. }
  125. }
  126. /**
  127. * Drop-in replacement for asort(), natsort() and natcasesort().
  128. * It uses a collator-based sort, or asort() with flags SORT_NATURAL and SORT_FLAG_CASE as a fallback.
  129. *
  130. * @param array $array The input array.
  131. * @return bool Returns true on success or false on failure.
  132. *
  133. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  134. */
  135. public static function asort(&$array)
  136. {
  137. $collator = self::getCollator();
  138. if (isset($collator)) {
  139. return $collator->asort($array);
  140. } else {
  141. return asort($array, SORT_NATURAL | SORT_FLAG_CASE);
  142. }
  143. }
  144. /**
  145. * Drop-in replacement for asort(), natsort() and natcasesort() when the parameter is an array of filenames.
  146. * Filenames may not be equal to page names, depending on the setting in $conf['fnencode'],
  147. * so the correct behavior is to sort page names and reflect this sorting in the filename array.
  148. *
  149. * @param array $array The input array.
  150. * @return bool Returns true on success or false on failure.
  151. *
  152. * @author Moisés Braga Ribeiro <moisesbr@gmail.com>
  153. * @author Andreas Gohr <andi@splitbrain.org>
  154. */
  155. public static function asortFN(&$array)
  156. {
  157. $collator = self::getCollator();
  158. return uasort($array, function ($fn1, $fn2) use ($collator) {
  159. if (isset($collator)) {
  160. return $collator->compare(utf8_decodeFN($fn1), utf8_decodeFN($fn2));
  161. } else {
  162. return strnatcasecmp(utf8_decodeFN($fn1), utf8_decodeFN($fn2));
  163. }
  164. });
  165. }
  166. }