Oppaitime'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.0KB

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