BioTorrents.de’s version of Gazelle
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.

wiki.class.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types = 1);
  3. class Wiki
  4. {
  5. /**
  6. * Normalize an alias
  7. * @param string $str
  8. * @return string
  9. */
  10. public static function normalize_alias($str)
  11. {
  12. return trim(substr(preg_replace('/[^a-z0-9]/', '', strtolower(htmlentities($str))), 0, 50));
  13. }
  14. /**
  15. * Get all aliases in an associative array of Alias => ArticleID
  16. * @return array
  17. */
  18. public static function get_aliases()
  19. {
  20. $Aliases = G::$Cache->get_value('wiki_aliases');
  21. if (!$Aliases) {
  22. $QueryID = G::$DB->get_query_id();
  23. G::$DB->query("
  24. SELECT Alias, ArticleID
  25. FROM wiki_aliases");
  26. $Aliases = G::$DB->to_pair('Alias', 'ArticleID');
  27. G::$DB->set_query_id($QueryID);
  28. G::$Cache->cache_value('wiki_aliases', $Aliases, 3600 * 24 * 14); // 2 weeks
  29. }
  30. return $Aliases;
  31. }
  32. /**
  33. * Flush the alias cache. Call this whenever you touch the wiki_aliases table.
  34. */
  35. public static function flush_aliases()
  36. {
  37. G::$Cache->delete_value('wiki_aliases');
  38. }
  39. /**
  40. * Get the ArticleID corresponding to an alias
  41. * @param string $Alias
  42. * @return int
  43. */
  44. public static function alias_to_id($Alias)
  45. {
  46. $Aliases = self::get_aliases();
  47. $Alias = self::normalize_alias($Alias);
  48. if (!isset($Aliases[$Alias])) {
  49. return false;
  50. } else {
  51. return (int)$Aliases[$Alias];
  52. }
  53. }
  54. /**
  55. * Get an article; returns false on error if $Error = false
  56. * @param int $ArticleID
  57. * @param bool $Error
  58. * @return array|bool
  59. */
  60. public static function get_article($ArticleID, $Error = true)
  61. {
  62. $Contents = G::$Cache->get_value('wiki_article_'.$ArticleID);
  63. if (!$Contents) {
  64. $QueryID = G::$DB->get_query_id();
  65. G::$DB->query("
  66. SELECT
  67. w.Revision,
  68. w.Title,
  69. w.Body,
  70. w.MinClassRead,
  71. w.MinClassEdit,
  72. w.Date,
  73. w.Author,
  74. u.Username,
  75. GROUP_CONCAT(a.Alias),
  76. GROUP_CONCAT(a.UserID)
  77. FROM wiki_articles AS w
  78. LEFT JOIN wiki_aliases AS a ON w.ID=a.ArticleID
  79. LEFT JOIN users_main AS u ON u.ID=w.Author
  80. WHERE w.ID='$ArticleID'
  81. GROUP BY w.ID");
  82. if (!G::$DB->has_results()) {
  83. if ($Error) {
  84. error(404);
  85. } else {
  86. return false;
  87. }
  88. }
  89. $Contents = G::$DB->to_array();
  90. G::$DB->set_query_id($QueryID);
  91. G::$Cache->cache_value('wiki_article_'.$ArticleID, $Contents, 3600 * 24 * 14); // 2 weeks
  92. }
  93. return $Contents;
  94. }
  95. /**
  96. * Flush an article's cache. Call this whenever you edited a wiki article or its aliases.
  97. * @param int $ArticleID
  98. */
  99. public static function flush_article($ArticleID)
  100. {
  101. G::$Cache->delete_value('wiki_article_'.$ArticleID);
  102. }
  103. }