PageResolver.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace dokuwiki\File;
  3. /**
  4. * Creates an absolute page ID from a relative one
  5. */
  6. class PageResolver extends Resolver
  7. {
  8. /**
  9. * Resolves a given ID to be absolute
  10. *
  11. * This handles all kinds of relative shortcuts, startpages and autoplurals
  12. * @inheritDoc
  13. */
  14. public function resolveId($id, $rev = '', $isDateAt = false)
  15. {
  16. global $conf;
  17. $id = (string) $id;
  18. // pages may have a hash attached, we separate it on resolving
  19. if (strpos($id, '#') !== false) {
  20. list($id, $hash) = sexplode('#', $id, 2);
  21. $hash = cleanID($hash);
  22. } else {
  23. $hash = '';
  24. }
  25. if ($id !== '') {
  26. $id = parent::resolveId($id, $rev, $isDateAt);
  27. $id = $this->resolveStartPage($id, $rev, $isDateAt);
  28. if ($conf['autoplural']) {
  29. $id = $this->resolveAutoPlural($id, $rev, $isDateAt);
  30. }
  31. } else {
  32. $id = $this->contextID;
  33. }
  34. $id = cleanID($id); // FIXME always? or support parameter
  35. // readd hash if any
  36. if ($hash !== '') $id .= "#$hash";
  37. return $id;
  38. }
  39. /**
  40. * IDs ending in :
  41. *
  42. * @param string $id
  43. * @param string|int|false $rev
  44. * @param bool $isDateAt
  45. * @return string
  46. */
  47. protected function resolveStartPage($id, $rev, $isDateAt)
  48. {
  49. global $conf;
  50. if ($id[-1] !== ':') return $id;
  51. if (page_exists($id . $conf['start'], $rev, true, $isDateAt)) {
  52. // start page inside namespace
  53. return $id . $conf['start'];
  54. } elseif (page_exists($id . noNS(cleanID($id)), $rev, true, $isDateAt)) {
  55. // page named like the NS inside the NS
  56. return $id . noNS(cleanID($id));
  57. } elseif (page_exists(substr($id, 0, -1), $rev, true, $isDateAt)) {
  58. // page named like the NS outside the NS
  59. return substr($id, 0, -1);
  60. }
  61. // fall back to default start page
  62. return $id . $conf['start'];
  63. }
  64. /**
  65. * Try alternative plural/singular form
  66. *
  67. * @param string $id
  68. * @param int $rev
  69. * @param bool $isDateAt
  70. * @return string
  71. */
  72. protected function resolveAutoPlural($id, $rev, $isDateAt)
  73. {
  74. if (page_exists($id, $rev, $isDateAt)) return $id;
  75. if ($id[-1] === 's') {
  76. $try = substr($id, 0, -1);
  77. } else {
  78. $try = $id . 's';
  79. }
  80. if (page_exists($try, $rev, true, $isDateAt)) {
  81. return $try;
  82. }
  83. return $id;
  84. }
  85. }