PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/search/documents/wiki_document.php

https://github.com/nadavkav/Moodle-RTL--Shenkar-Translation-Team-
PHP | 280 lines | 130 code | 33 blank | 117 comment | 21 complexity | 906d8de513aa76f9c63271007be11287 MD5 | raw file
  1. <?php
  2. /**
  3. * Global Search Engine for Moodle
  4. *
  5. * @package search
  6. * @category core
  7. * @subpackage document_wrappers
  8. * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
  9. * @contributor Tatsuva Shirai 20090530
  10. * @date 2008/03/31
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  12. *
  13. * document handling for wiki activity module
  14. * This file contains the mapping between a wiki page and it's indexable counterpart,
  15. * e.g. searchdocument->title = wikipage->pagename
  16. *
  17. * Functions for iterating and retrieving the necessary records are now also included
  18. * in this file, rather than mod/wiki/lib.php
  19. */
  20. /**
  21. * includes and requires
  22. */
  23. require_once("$CFG->dirroot/search/documents/document.php");
  24. require_once("$CFG->dirroot/mod/wiki/lib.php");
  25. /**
  26. * All the $doc->___ fields are required by the base document class!
  27. * Each and every module that requires search functionality must correctly
  28. * map their internal fields to the five $doc fields (id, title, author, contents
  29. * and url). Any module specific data can be added to the $data object, which is
  30. * serialised into a binary field in the index.
  31. */
  32. class WikiSearchDocument extends SearchDocument {
  33. public function __construct(&$page, $wiki_id, $course_id, $group_id, $user_id, $context_id) {
  34. // generic information; required
  35. $doc->docid = $page['id'];
  36. $doc->documenttype = SEARCH_TYPE_WIKI;
  37. $doc->itemtype = 'standard';
  38. $doc->contextid = $context_id;
  39. $doc->title = $page['pagename'];
  40. $doc->date = $page['lastmodified'];
  41. //remove '(ip.ip.ip.ip)' from wiki author field
  42. $doc->author = preg_replace('/\(.*?\)/', '', $page['author']);
  43. $doc->contents = $page['content'];
  44. $doc->url = wiki_make_link($wiki_id, $page['pagename'], $page['version']);
  45. // module specific information; optional
  46. $data->version = $page['version'];
  47. $data->wiki = $wiki_id;
  48. // construct the parent class
  49. parent::__construct($doc, $data, $course_id, $group_id, $user_id, 'mod/'.SEARCH_TYPE_WIKI);
  50. }
  51. }
  52. /**
  53. * converts a page name to cope Wiki constraints. Transforms spaces in plus.
  54. * @param str the name to convert
  55. * @return the converted name
  56. */
  57. function wiki_name_convert($str) {
  58. return str_replace(' ', '+', $str);
  59. }
  60. /**
  61. * constructs a valid link to a wiki content
  62. * @param int $wikiId
  63. * @param string $title
  64. * @param int $version
  65. * @uses CFG
  66. */
  67. function wiki_make_link($wikiId, $title, $version) {
  68. global $CFG;
  69. return $CFG->wwwroot.'/mod/wiki/view.php?wid='.$wikiId.'&amp;page='.wiki_name_convert($title).'&amp;version='.$version;
  70. } //wiki_make_link
  71. /**
  72. * rescued and converted from ewikimoodlelib.php
  73. * retrieves latest version of a page
  74. * @param object $entry the wiki object as a reference
  75. * @param string $pagename the name of the page known by the wiki engine
  76. * @param int $version
  77. */
  78. function wiki_get_latest_page(&$entry, $pagename, $version = 0) {
  79. $pagename = "'".addslashes($pagename)."'";
  80. if ($version > 0 and is_int($version)) {
  81. $version = "AND (version=$version)";
  82. } else {
  83. $version = '';
  84. }
  85. $select = "(pagename=$pagename) AND wiki=".$entry->id." $version ";
  86. $sort = 'version DESC';
  87. //change this to recordset_select, as per http://docs.moodle.org/en/Datalib_Notes
  88. if ($result_arr = get_records_select('wiki_pages', $select, $sort, '*', 0, 1)) {
  89. foreach ($result_arr as $obj) {
  90. $result_obj = $obj;
  91. }
  92. }
  93. if (isset($result_obj)) {
  94. $result_obj->meta = @unserialize($result_obj->meta);
  95. return $result_obj;
  96. } else {
  97. return false;
  98. }
  99. }
  100. /**
  101. * fetches all pages, including old versions
  102. * @param object $entry the wiki object as a reference
  103. * @return an array of record objects that represents pages of this wiki object
  104. */
  105. function wiki_get_pages(&$entry) {
  106. return get_records('wiki_pages', 'wiki', $entry->id);
  107. }
  108. /**
  109. * fetches all the latest versions of all the pages
  110. * @param object $entry
  111. */
  112. function wiki_get_latest_pages(&$entry) {
  113. //== (My)SQL for this
  114. /* select * from wiki_pages
  115. inner join
  116. (select wiki_pages.pagename, max(wiki_pages.version) as ver
  117. from wiki_pages group by pagename) as a
  118. on ((wiki_pages.version = a.ver) and
  119. (wiki_pages.pagename like a.pagename)) */
  120. $pages = array();
  121. //http://moodle.org/bugs/bug.php?op=show&bugid=5877&pos=0
  122. if ($ids = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
  123. if ($pagesets = get_records('wiki_pages', 'wiki', $entry->id, '', 'distinct pagename')) {
  124. foreach ($pagesets as $aPageset) {
  125. $pages[] = wiki_get_latest_page($entry, $aPageset->pagename);
  126. }
  127. } else {
  128. return false;
  129. }
  130. }
  131. return $pages;
  132. }
  133. /**
  134. * part of search engine API
  135. *
  136. */
  137. function wiki_iterator() {
  138. $wikis = get_records('wiki');
  139. return $wikis;
  140. }
  141. /**
  142. * part of search engine API
  143. * @param wiki a wiki instance
  144. * @return an array of searchable deocuments
  145. */
  146. function wiki_get_content_for_index(&$wiki) {
  147. $documents = array();
  148. $entries = wiki_get_entries($wiki);
  149. if ($entries){
  150. $coursemodule = get_field('modules', 'id', 'name', 'wiki');
  151. $cm = get_record('course_modules', 'course', $wiki->course, 'module', $coursemodule, 'instance', $wiki->id);
  152. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  153. foreach($entries as $entry) {
  154. //all pages
  155. //$pages = wiki_get_pages($entry);
  156. //latest pages
  157. $pages = wiki_get_latest_pages($entry);
  158. if (is_array($pages)) {
  159. foreach($pages as $page) {
  160. if (strlen($page->content) > 0) {
  161. $documents[] = new WikiSearchDocument(get_object_vars($page), $entry->wikiid, $entry->course, $entry->groupid, $page->userid, $context->id);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. return $documents;
  168. }
  169. /**
  170. * returns a single wiki search document based on a wiki_entry id
  171. * @param id the id of the wiki
  172. * @param itemtype the type of information (standard)
  173. * @return a searchable document
  174. */
  175. function wiki_single_document($id, $itemtype) {
  176. $page = get_record('wiki_pages', 'id', $id);
  177. $entry = get_record('wiki_entries', 'id', $page->wiki);
  178. $coursemodule = get_field('modules', 'id', 'name', 'wiki');
  179. $cm = get_record('course_modules', 'course', $entry->course, 'module', $coursemodule, 'instance', $entry->wikiid);
  180. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  181. return new WikiSearchDocument(get_object_vars($page), $entry->wikiid, $entry->course, $entry->groupid, $page->userid, $context->id);
  182. }
  183. /**
  184. * dummy delete function that packs id with itemtype.
  185. * this was here for a reason, but I can't remember it at the moment.
  186. *
  187. */
  188. function wiki_delete($info, $itemtype) {
  189. $object->id = $info;
  190. $object->itemtype = $itemtype;
  191. return $object;
  192. }
  193. //returns the var names needed to build a sql query for addition/deletions
  194. function wiki_db_names() {
  195. //[primary id], [table name], [time created field name], [time modified field name]
  196. return array(array('id', 'wiki_pages', 'created', 'lastmodified', 'standard'));
  197. }
  198. /**
  199. * this function handles the access policy to contents indexed as searchable documents. If this
  200. * function does not exist, the search engine assumes access is allowed.
  201. * When this point is reached, we already know that :
  202. * - user is legitimate in the surrounding context
  203. * - user may be guest and guest access is allowed to the module
  204. * - the function may perform local checks within the module information logic
  205. * @param path the access path to the module script code
  206. * @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
  207. * @param this_id the item id within the information class denoted by itemtype. In wikies, this id
  208. * points out the indexed wiki page.
  209. * @param user the user record denoting the user who searches
  210. * @param group_id the current group used by the user when searching
  211. * @uses CFG
  212. * @return true if access is allowed, false elsewhere
  213. */
  214. function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id){
  215. global $CFG;
  216. // get the wiki object and all related stuff
  217. $page = get_record('wiki_pages', 'id', $this_id);
  218. $wiki = get_record('wiki', 'id', $page->wiki);
  219. $course = get_record('course', 'id', $wiki->course);
  220. $context = get_record('context', 'id', $context_id);
  221. $cm = get_record('course_modules', 'id', $context->instanceid);
  222. if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete
  223. if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
  224. if (!empty($CFG->search_access_debug)) echo "search reject : hidden wiki ";
  225. return false;
  226. }
  227. //group consistency check : checks the following situations about groups
  228. // trap if user is not same group and groups are separated
  229. $current_group = get_current_group($course->id);
  230. if ((groupmode($course) == SEPARATEGROUPS) && $group_id != $current_group && !has_capability('moodle/site:accessallgroups', $context)) {
  231. if (!empty($CFG->search_access_debug)) echo "search reject : separated group owner wiki ";
  232. return false;
  233. }
  234. return true;
  235. }
  236. /**
  237. * this call back is called when displaying the link for some last post processing
  238. *
  239. */
  240. function wiki_link_post_processing($title){
  241. global $CFG;
  242. if ($CFG->block_search_utf8dir){
  243. return mb_convert_encoding($title, 'UTF-8', 'auto');
  244. }
  245. return mb_convert_encoding($title, 'auto', 'UTF-8');
  246. }
  247. ?>