3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This contains functions and classes that will be used by scripts in wiki module
21 * @package mod-wiki-2.0
22 * @copyrigth 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
23 * @copyrigth 2009 Universitat Politecnica de Catalunya http://www.upc.edu
25 * @author Jordi Piguillem
27 * @author David Jimenez
29 * @author Daniel Serrano
30 * @author Kenneth Riba
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 require_once($CFG->dirroot . '/mod/wiki/lib.php');
36 require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
37 require_once($CFG->libdir . '/filelib.php');
39 define('WIKI_REFRESH_CACHE_TIME', 30); // @TODO: To be deleted.
40 define('FORMAT_CREOLE', '37');
41 define('FORMAT_NWIKI', '38');
42 define('NO_VALID_RATE', '-999');
43 define('IMPROVEMENT', '+');
47 define('LOCK_TIMEOUT', 30);
51 * @param int $wikiid the instance id of wiki
53 function wiki_get_wiki($wikiid) {
56 return $DB->get_record('wiki', array('id' => $wikiid));
60 * Get sub wiki instances with same wiki id
63 function wiki_get_subwikis($wikiid) {
65 return $DB->get_records('wiki_subwikis', array('wikiid' => $wikiid));
69 * Get a sub wiki instance by wiki id and group id
74 function wiki_get_subwiki_by_group($wikiid, $groupid, $userid = 0) {
76 return $DB->get_record('wiki_subwikis', array('wikiid' => $wikiid, 'groupid' => $groupid, 'userid' => $userid));
80 * Get a sub wiki instace by instance id
81 * @param int $subwikiid
84 function wiki_get_subwiki($subwikiid) {
86 return $DB->get_record('wiki_subwikis', array('id' => $subwikiid));
91 * Add a new sub wiki instance
94 * @return int $insertid
96 function wiki_add_subwiki($wikiid, $groupid, $userid = 0) {
99 $record = new StdClass();
100 $record->wikiid = $wikiid;
101 $record->groupid = $groupid;
102 $record->userid = $userid;
104 $insertid = $DB->insert_record('wiki_subwikis', $record);
109 * Get a wiki instance by pageid
113 function wiki_get_wiki_from_pageid($pageid) {
117 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
119 p.subwikiid = s.id AND
122 return $DB->get_record_sql($sql, array($pageid));
126 * Get a wiki page by pageid
130 function wiki_get_page($pageid) {
132 return $DB->get_record('wiki_pages', array('id' => $pageid));
136 * Get latest version of wiki page
140 function wiki_get_current_version($pageid) {
143 // @TODO: Fix this query
147 ORDER BY version DESC";
148 return array_pop($DB->get_records_sql($sql, array($pageid), 0, 1));
153 * Alias of wiki_get_current_version
154 * @TODO, does the exactly same thing as wiki_get_current_version, should be removed
158 function wiki_get_last_version($pageid) {
159 return wiki_get_current_version($pageid);
165 * @param string $section
167 function wiki_get_section_page($page, $section) {
169 $version = wiki_get_current_version($page->id);
170 return wiki_parser_proxy::get_section($version->content, $version->contentformat, $section);
174 * Get a wiki page by page title
175 * @param int $swid, sub wiki id
176 * @param string $title
179 function wiki_get_page_by_title($swid, $title) {
181 return $DB->get_record('wiki_pages', array('subwikiid' => $swid, 'title' => $title));
185 * Get a version record by record id
186 * @param int $versionid, the version id
189 function wiki_get_version($versionid) {
191 return $DB->get_record('wiki_versions', array('id' => $versionid));
195 * Get first page of wiki instace
196 * @param int $subwikiid
197 * @param int $module, wiki instance object
199 function wiki_get_first_page($subwikid, $module = null) {
203 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
206 w.firstpagetitle = p.title AND
208 return $DB->get_record_sql($sql, array($subwikid));
211 function wiki_save_section($wikipage, $sectiontitle, $sectioncontent, $userid) {
213 $wiki = wiki_get_wiki_from_pageid($wikipage->id);
214 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
215 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
217 if (has_capability('mod/wiki:editpage', $context)) {
218 $version = wiki_get_current_version($wikipage->id);
219 $content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $sectiontitle, true);
221 $newcontent = $content[0] . $sectioncontent . $content[2];
223 return wiki_save_page($wikipage, $newcontent, $userid);
231 * @param object $wikipage
232 * @param string $newcontent
235 function wiki_save_page($wikipage, $newcontent, $userid) {
238 $wiki = wiki_get_wiki_from_pageid($wikipage->id);
239 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
240 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
242 if (has_capability('mod/wiki:editpage', $context)) {
243 $version = wiki_get_current_version($wikipage->id);
245 $version->content = $newcontent;
246 $version->userid = $userid;
248 $version->timecreated = time();
249 $versionid = $DB->insert_record('wiki_versions', $version);
251 $wikipage->timemodified = $version->timecreated;
252 $wikipage->userid = $userid;
253 $return = wiki_refresh_cachedcontent($wikipage, $newcontent);
261 function wiki_refresh_cachedcontent($page, $newcontent = null) {
264 $version = wiki_get_current_version($page->id);
265 if (empty($version)) {
268 if (!isset($newcontent)) {
269 $newcontent = $version->content;
272 $options = array('swid' => $page->subwikiid, 'pageid' => $page->id);
273 $parseroutput = wiki_parse_content($version->contentformat, $newcontent, $options);
274 $page->cachedcontent = $parseroutput['toc'] . $parseroutput['parsed_text'];
275 $page->timerendered = time();
276 $DB->update_record('wiki_pages', $page);
278 wiki_refresh_page_links($page, $parseroutput['link_count']);
280 return array('page' => $page, 'sections' => $parseroutput['repeated_sections'], 'version' => $version->version);
285 function wiki_restore_page($wikipage, $newcontent, $userid) {
286 $return = wiki_save_page($wikipage, $newcontent, $userid);
287 return $return['page'];
290 function wiki_refresh_page_links($page, $links) {
293 $DB->delete_records('wiki_links', array('frompageid' => $page->id));
294 foreach ($links as $linkname => $linkinfo) {
296 $newlink = new stdClass();
297 $newlink->subwikiid = $page->subwikiid;
298 $newlink->frompageid = $page->id;
300 if ($linkinfo['new']) {
301 $newlink->tomissingpage = $linkname;
303 $newlink->topageid = $linkinfo['pageid'];
307 $DB->insert_record('wiki_links', $newlink);
308 } catch (dml_exception $e) {
309 debugging($e->getMessage());
316 * Create a new wiki page, if the page exists, return existing pageid
318 * @param string $title
319 * @param string $format
322 function wiki_create_page($swid, $title, $format, $userid) {
324 $subwiki = wiki_get_subwiki($swid);
325 $cm = get_coursemodule_from_instance('wiki', $subwiki->wikiid);
326 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
327 require_capability('mod/wiki:editpage', $context);
329 if ($page = wiki_get_page_by_title($swid, $title)) {
333 // Creating a new empty version
334 $version = new stdClass();
335 $version->content = '';
336 $version->contentformat = $format;
337 $version->version = 0;
338 $version->timecreated = time();
339 $version->userid = $userid;
342 $versionid = $DB->insert_record('wiki_versions', $version);
344 // Createing a new empty page
345 $page = new stdClass();
346 $page->subwikiid = $swid;
347 $page->title = $title;
348 $page->cachedcontent = '';
349 $page->timecreated = $version->timecreated;
350 $page->timemodified = $version->timecreated;
351 $page->timerendered = $version->timecreated;
352 $page->userid = $userid;
353 $page->pageviews = 0;
356 $pageid = $DB->insert_record('wiki_pages', $page);
358 // Setting the pageid
359 $version->id = $versionid;
360 $version->pageid = $pageid;
361 $DB->update_record('wiki_versions', $version);
363 wiki_make_cache_expire($page->title);
367 function wiki_make_cache_expire($pagename) {
370 $sql = "UPDATE {wiki_pages}
372 WHERE id IN ( SELECT l.frompageid
374 WHERE l.tomissingpage = ?
376 $DB->execute ($sql, array($pagename));
380 * Get a specific version of page
382 * @param int $version
384 function wiki_get_wiki_page_version($pageid, $version) {
386 return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'version' => $version));
392 * @param int $limitfrom
393 * @param int $limitnum
395 function wiki_get_wiki_page_versions($pageid, $limitfrom, $limitnum) {
397 return $DB->get_records('wiki_versions', array('pageid' => $pageid), 'version DESC', '*', $limitfrom, $limitnum);
401 * Count the number of page version
404 function wiki_count_wiki_page_versions($pageid) {
406 return $DB->count_records('wiki_versions', array('pageid' => $pageid));
410 * Get linked from page
413 function wiki_get_linked_to_pages($pageid) {
415 return $DB->get_records('wiki_links', array('frompageid' => $pageid));
419 * Get linked from page
422 function wiki_get_linked_from_pages($pageid) {
424 return $DB->get_records('wiki_links', array('topageid' => $pageid));
428 * Get pages which user have been edited
432 function wiki_get_contributions($swid, $userid) {
436 FROM {wiki_versions} v, {wiki_pages} p
437 WHERE p.subwikiid = ? AND
441 return $DB->get_records_sql($sql, array($swid, $userid));
445 * Get missing or empty pages in wiki
446 * @param int $swid sub wiki id
448 function wiki_get_missing_or_empty_pages($swid) {
451 $sql = "SELECT DISTINCT p.title, p.id, p.subwikiid
452 FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
453 WHERE s.wikiid = w.id and
455 w.firstpagetitle != p.title and
458 FROM {wiki_versions} v
459 WHERE v.pageid = p.id)
461 SELECT DISTINCT l.tomissingpage as title, 0 as id, l.subwikiid
463 WHERE l.subwikiid = ? and
466 return $DB->get_records_sql($sql, array($swid, $swid, $swid));
470 * Get pages list in wiki
471 * @param int $swid sub wiki id
473 function wiki_get_page_list($swid) {
475 $records = $DB->get_records('wiki_pages', array('subwikiid' => $swid), 'title ASC');
480 * Return a list of orphaned wikis for one specific subwiki
482 * @param int $swid sub wiki id
484 function wiki_get_orphaned_pages($swid) {
487 $sql = "SELECT p.id, p.title
488 FROM {wiki_pages} p, {wiki} w , {wiki_subwikis} s
489 WHERE p.subwikiid = ?
492 AND p.title != w.firstpagetitle
493 AND p.id NOT IN (SELECT topageid FROM {wiki_links} WHERE subwikiid = ?);";
495 return $DB->get_records_sql($sql, array($swid, $swid, $swid));
500 * @param int $swid sub wiki id
501 * @param string $search
503 function wiki_search_title($swid, $search) {
506 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND title LIKE ?", array($swid, '%'.$search.'%'));
510 * Search wiki content
511 * @param int $swid sub wiki id
512 * @param string $search
514 function wiki_search_content($swid, $search) {
517 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND cachedcontent LIKE ?", array($swid, '%'.$search.'%'));
521 * Search wiki title and content
522 * @param int $swid sub wiki id
523 * @param string $search
525 function wiki_search_all($swid, $search) {
528 return $DB->get_records_select('wiki_pages', "subwikiid = ? AND (cachedcontent LIKE ? OR title LIKE ?)", array($swid, '%'.$search.'%', '%'.$search.'%'));
534 function wiki_get_user_info($userid) {
536 return $DB->get_record('user', array('id' => $userid));
540 * Increase page view nubmer
541 * @param int $page, database record
543 function wiki_increment_pageviews($page) {
547 $DB->update_record('wiki_pages', $page);
550 //----------------------------------------------------------
551 //----------------------------------------------------------
554 * Text format supported by wiki module
556 function wiki_get_formats() {
557 return array('html', 'creole', 'nwiki');
561 * Parses a string with the wiki markup language in $markup.
563 * @return Array or false when something wrong has happened.
565 * Returned array contains the following fields:
566 * 'parsed_text' => String. Contains the parsed wiki content.
567 * 'unparsed_text' => String. Constains the original wiki content.
568 * 'link_count' => Array of array('destination' => ..., 'new' => "is new?"). Contains the internal wiki links found in the wiki content.
569 * 'deleted_sections' => the list of deleted sections.
572 * @author Josep Arús Pous
574 function wiki_parse_content($markup, $pagecontent, $options = array()) {
577 $subwiki = wiki_get_subwiki($options['swid']);
578 $cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid);
579 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
581 $parser_options = array('link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', 'link_callback_args' => array('swid' => $options['swid']), 'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table', 'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path', 'real_path_callback_args' => array('context' => $context, 'component' => 'mod_wiki', 'filearea' => 'attachments', 'pageid' => $options['pageid']), 'pageid' => $options['pageid'], 'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']), 'printable' => (isset($options['printable']) && $options['printable']));
583 return wiki_parser_proxy::parse($pagecontent, $markup, $parser_options);
587 * This function is the parser callback to parse wiki links.
589 * It returns the necesary information to print a link.
591 * NOTE: Empty pages and non-existent pages must be print in red color.
593 * @param link name of a page
598 * @TODO Doc return and options
600 function wiki_parser_link($link, $options = null) {
603 if (is_object($link)) {
604 $parsedlink = array('content' => $link->title, 'url' => $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $link->id, 'new' => false, 'link_info' => array('link' => $link->title, 'pageid' => $link->id, 'new' => false));
606 $version = wiki_get_current_version($link->id);
607 if ($version->version == 0) {
608 $parsedlink['new'] = true;
612 $swid = $options['swid'];
614 if ($page = wiki_get_page_by_title($swid, $link)) {
615 $parsedlink = array('content' => $link, 'url' => $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $page->id, 'new' => false, 'link_info' => array('link' => $link, 'pageid' => $page->id, 'new' => false));
617 $version = wiki_get_current_version($page->id);
618 if ($version->version == 0) {
619 $parsedlink['new'] = true;
625 return array('content' => $link, 'url' => $CFG->wwwroot . '/mod/wiki/create.php?swid=' . $swid . '&title=' . urlencode($link) . '&action=new', 'new' => true, 'link_info' => array('link' => $link, 'new' => true, 'pageid' => 0));
631 * Returns the table fully parsed (HTML)
633 * @return HTML for the table $table
634 * @author Josep Arús Pous
637 function wiki_parser_table($table) {
640 $htmltable = new html_table();
642 $headers = $table[0];
643 $htmltable->head = array();
644 foreach ($headers as $h) {
645 $htmltable->head[] = $h[1];
649 $htmltable->data = array();
650 foreach ($table as $row) {
652 foreach ($row as $r) {
655 $htmltable->data[] = $row_data;
658 return html_writer::table($htmltable);
662 * Returns an absolute path link, unless there is no such link.
664 * @param string url Link's URL
665 * @param stdClass context filearea params
666 * @param string filearea
667 * @param int fileareaid
669 * @return File full path
672 function wiki_parser_real_path($url, $context, $filearea, $fileareaid) {
675 if (preg_match("/^(?:http|ftp)s?\:\/\//", $url)) {
678 return "{$CFG->wwwroot}/pluginfile.php/{$context->id}/$filearea/$fileareaid/$url";
683 * Returns the token used by a wiki language to represent a given tag or "object" (bold -> **)
685 * @return A string when it has only one token at the beginning (f. ex. lists). An array composed by 2 strings when it has 2 tokens, one at the beginning and one at the end (f. ex. italics). Returns false otherwise.
686 * @author Josep Arús Pous
688 function wiki_parser_get_token($markup, $name) {
690 return wiki_parser_proxy::get_token($name, $markup);
694 * Checks if current user can view a subwiki
698 function wiki_user_can_view($subwiki) {
701 $wiki = wiki_get_wiki($subwiki->wikiid);
702 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
703 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
705 // Working depending on activity groupmode
706 switch (groups_get_activity_groupmode($cm)) {
709 if ($wiki->wikimode == 'collaborative') {
710 // Collaborative Mode:
711 // There is one wiki for all the class.
713 // Only view capbility needed
714 return has_capability('mod/wiki:viewpage', $context);
715 } else if ($wiki->wikimode == 'individual') {
717 // Each person owns a wiki.
718 if ($subwiki->userid == $USER->id) {
719 // Only the owner of the wiki can view it
720 return has_capability('mod/wiki:viewpage', $context);
721 } else { // User has special capabilities
723 // mod/wiki:viewpage capability
725 // mod/wiki:managewiki capability
726 $view = has_capability('mod/wiki:viewpage', $context);
727 $manage = has_capability('mod/wiki:managewiki', $context);
729 return $view && $manage;
736 // Collaborative and Individual Mode
738 // Collaborative Mode:
739 // There is one wiki per group.
741 // Each person owns a wiki.
742 if ($wiki->wikimode == 'collaborative' || $wiki->wikimode == 'individual') {
743 // Only members of subwiki group could view that wiki
744 if ($subwiki->groupid == groups_get_activity_group($cm)) {
745 // Only view capability needed
746 return has_capability('mod/wiki:viewpage', $context);
748 } else { // User is not part of that group
750 // mod/wiki:managewiki capability
752 // moodle/site:accessallgroups capability
754 // mod/wiki:viewpage capability
755 $view = has_capability('mod/wiki:viewpage', $context);
756 $manage = has_capability('mod/wiki:managewiki', $context);
757 $access = has_capability('moodle/site:accessallgroups', $context);
758 return ($manage || $access) && $view;
765 // Collaborative and Individual Mode
767 // Collaborative Mode:
768 // There is one wiki per group.
770 // Each person owns a wiki.
771 if ($wiki->wikimode == 'collaborative' || $wiki->wikimode == 'individual') {
772 // Everybody can read all wikis
774 // Only view capability needed
775 return has_capability('mod/wiki:viewpage', $context);
786 * Checks if current user can edit a subwiki
790 function wiki_user_can_edit($subwiki) {
793 $wiki = wiki_get_wiki($subwiki->wikiid);
794 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
795 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
797 // Working depending on activity groupmode
798 switch (groups_get_activity_groupmode($cm)) {
801 if ($wiki->wikimode == 'collaborative') {
802 // Collaborative Mode:
803 // There is a wiki for all the class.
805 // Only edit capbility needed
806 return has_capability('mod/wiki:editpage', $context);
807 } else if ($wiki->wikimode == 'individual') {
809 // There is a wiki per user
811 // Only the owner of that wiki can edit it
812 if ($subwiki->userid == $USER->id) {
813 return has_capability('mod/wiki:editpage', $context);
814 } else { // Current user is not the owner of that wiki.
817 // mod/wiki:editpage capability
819 // mod/wiki:managewiki capability
820 $edit = has_capability('mod/wiki:editpage', $context);
821 $manage = has_capability('mod/wiki:managewiki', $context);
823 return $edit && $manage;
830 if ($wiki->wikimode == 'collaborative') {
831 // Collaborative Mode:
832 // There is one wiki per group.
834 // Only members of subwiki group could edit that wiki
835 if ($subwiki->groupid == groups_get_activity_group($cm)) {
836 // Only edit capability needed
837 return has_capability('mod/wiki:editpage', $context);
838 } else { // User is not part of that group
840 // mod/wiki:managewiki capability
842 // moodle/site:accessallgroups capability
844 // mod/wiki:editpage capability
845 $manage = has_capability('mod/wiki:managewiki', $context);
846 $access = has_capability('moodle/site:accessallgroups', $context);
847 $edit = has_capability('mod/wiki:editpage', $context);
848 return $manage && $access && $edit;
850 } else if ($wiki->wikimode == 'individual') {
852 // Each person owns a wiki.
854 // Only the owner of that wiki can edit it
855 if ($subwiki->userid == $USER->id) {
856 return has_capability('mod/wiki:editpage', $context);
857 } else { // Current user is not the owner of that wiki.
859 // mod/wiki:managewiki capability
861 // moodle/site:accessallgroups capability
863 // mod/wiki:editpage capability
864 $manage = has_capability('mod/wiki:managewiki', $context);
865 $access = has_capability('moodle/site:accessallgroups', $context);
866 $edit = has_capability('mod/wiki:editpage', $context);
867 return $manage && $access && $edit;
874 if ($wiki->wikimode == 'collaborative') {
875 // Collaborative Mode:
876 // There is one wiki per group.
878 // Only members of subwiki group could edit that wiki
879 if ($subwiki->groupid == groups_get_activity_group($cm)) {
880 // Only edit capability needed
881 return has_capability('mod/wiki:editpage', $context);
882 } else { // User is not part of that group
884 // mod/wiki:managewiki capability
886 // mod/wiki:editpage capability
887 $manage = has_capability('mod/wiki:managewiki', $context);
888 $edit = has_capability('mod/wiki:editpage', $context);
889 return $manage && $edit;
891 } else if ($wiki->wikimode == 'individual') {
893 // Each person owns a wiki.
895 // Only the owner of that wiki can edit it
896 if ($subwiki->userid == $USER->id) {
897 return has_capability('mod/wiki:editpage', $context);
898 } else { // Current user is not the owner of that wiki.
900 // mod/wiki:managewiki capability
902 // mod/wiki:editpage capability
903 $manage = has_capability('mod/wiki:managewiki', $context);
904 $edit = has_capability('mod/wiki:editpage', $context);
905 return $manage && $edit;
921 * Checks if a page-section is locked.
923 * @return true if the combination of section and page is locked, FALSE otherwise.
925 function wiki_is_page_section_locked($pageid, $userid, $section = null) {
928 $sql = "pageid = ? AND lockedat > ? AND userid != ?";
929 $params = array($pageid, time(), $userid);
931 if (!empty($section)) {
932 $sql .= " AND (sectionname = ? OR sectionname IS null)";
933 $params[] = $section;
936 return $DB->record_exists_select('wiki_locks', $sql, $params);
940 * Inserts or updates a wiki_locks record.
942 function wiki_set_lock($pageid, $userid, $section = null, $insert = false) {
945 if (wiki_is_page_section_locked($pageid, $userid, $section)) {
949 $params = array('pageid' => $pageid, 'userid' => $userid, 'sectionname' => $section);
951 $lock = $DB->get_record('wiki_locks', $params);
954 $DB->update_record('wiki_locks', array('id' => $lock->id, 'lockedat' => time() + LOCK_TIMEOUT));
955 } else if ($insert) {
956 $DB->insert_record('wiki_locks', array('pageid' => $pageid, 'sectionname' => $section, 'userid' => $userid, 'lockedat' => time() + 30));
963 * Deletes wiki_locks that are not in use. (F.Ex. after submitting the changes). If no userid is present, it deletes ALL the wiki_locks of a specific page.
965 function wiki_delete_locks($pageid, $userid = null, $section = null, $delete_from_db = true, $delete_section_and_page = false) {
968 $params = array('pageid' => $pageid);
970 if (!empty($userid)) {
971 $params['userid'] = $userid;
974 if (!empty($section)) {
975 $params['sectionname'] = $section;
978 if ($delete_from_db) {
979 $DB->delete_records('wiki_locks', $params);
980 if ($delete_section_and_page && !empty($section)) {
981 $params['sectionname'] = null;
982 $DB->delete_records('wiki_locks', $params);
985 $DB->set_field('wiki_locks', 'lockedat', time(), $params);
990 * Deletes wiki_locks that expired 1 hour ago.
992 function wiki_delete_old_locks() {
995 $DB->delete_records_select('wiki_locks', "lockedat < ?", array(time() - 3600));
1003 * Uploads files to permanent disk space.
1005 * @param int draftitemid Draft space ID
1006 * @param int contextid
1008 * @return array of files that have not been inserted.
1011 function wiki_process_attachments($draftitemid, $deleteuploads, $contextid, $filearea, $itemid, $options = null) {
1014 if (empty($options)) {
1015 $options = page_wiki_edit::$attachmentoptions;
1020 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
1021 $fs = get_file_storage();
1023 $oldfiles = $fs->get_area_files($contextid, 'mod_wiki', 'attachments', $itemid, 'id');
1025 foreach ($oldfiles as $file) {
1026 if (in_array($file->get_pathnamehash(), $deleteuploads)) {
1031 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id');
1032 $oldfiles = $fs->get_area_files($contextid, 'mod_wiki', 'attachments', $itemid, 'id');
1034 $file_record = array('contextid' => $contextid, 'component' => 'mod_wiki', 'filearea' => 'attachments', 'itemid' => $itemid);
1035 //more or less a merge...
1036 $newhashes = array();
1037 foreach ($draftfiles as $file) {
1038 $newhash = sha1("/$contextid/mod_wiki/attachments/$itemid" . $file->get_filepath() . $file->get_filename());
1039 $newhashes[$newhash] = $file;
1043 foreach ($oldfiles as $file) {
1044 $oldhash = $file->get_pathnamehash();
1045 if (!$file->is_directory() && isset($newhashes[$oldhash])) {
1046 //repeated file: ERROR!!!
1047 unset($newhashes[$oldhash]);
1051 if (!$file->is_directory()) {
1056 foreach ($newhashes as $file) {
1057 if ($file->get_filepath() !== '/' or $file->is_directory()) {
1061 if ($options['maxfiles'] and $options['maxfiles'] <= $filecount) {
1065 if (!$file->is_directory()) {
1067 $fs->create_file_from_storedfile($file_record, $file);
1071 //delete all draft files
1072 $fs->delete_area_files($usercontext->id, 'user', 'draft', $draftitemid);
1077 function wiki_get_comment($commentid){
1079 return $DB->get_record('comments', array('id' => $commentid));
1083 * Returns all comments by context and pageid
1085 * @param $context. Current context
1086 * @param $pageid. Current pageid
1088 function wiki_get_comments($contextid, $pageid) {
1091 return $DB->get_records('comments', array('contextid' => $contextid, 'itemid' => $pageid, 'commentarea' => 'wiki_page'));
1095 * Add comments ro database
1097 * @param object $context. Current context
1098 * @param int $pageid. Current pageid
1099 * @param string $content. Content of the comment
1100 * @param string editor. Version of editor we are using.
1102 function wiki_add_comment($context, $pageid, $content, $editor) {
1104 require_once($CFG->dirroot . '/comment/lib.php');
1106 list($context, $course, $cm) = get_context_info_array($context->id);
1107 $cmt = new stdclass();
1108 $cmt->context = $context;
1109 $cmt->itemid = $pageid;
1110 $cmt->area = 'wiki_page';
1111 $cmt->course = $course;
1112 $cmt->component = 'mod_wiki';
1114 $manager = new comment($cmt);
1116 if ($editor == 'creole') {
1117 $manager->add($content, FORMAT_CREOLE);
1118 } else if ($editor == 'html') {
1119 $manager->add($content, FORMAT_HTML);
1120 } else if ($editor == 'nwiki') {
1121 $manager->add($content, FORMAT_NWIKI);
1127 * Delete comments from database
1129 * @param $idcomment. Id of comment which will be deleted
1130 * @param $context. Current context
1131 * @param $pageid. Current pageid
1133 function wiki_delete_comment($idcomment, $context, $pageid) {
1135 require_once($CFG->dirroot . '/comment/lib.php');
1137 list($context, $course, $cm) = get_context_info_array($context->id);
1138 $cmt = new stdClass();
1139 $cmt->context = $context;
1140 $cmt->itemid = $pageid;
1141 $cmt->area = 'wiki_page';
1142 $cmt->course = $course;
1143 $cmt->component = 'mod_wiki';
1145 $manager = new comment($cmt);
1146 $manager->delete($idcomment);
1151 * Delete al comments from wiki
1154 function wiki_delete_comments_wiki() {
1158 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1160 $table = 'comments';
1161 $select = 'contextid = ?';
1163 $DB->delete_records_select($table, $select, array($context->id));
1167 function wiki_add_progress($pageid, $oldversionid, $versionid, $progress) {
1169 for ($v = $oldversionid + 1; $v <= $versionid; $v++) {
1170 $user = wiki_get_wiki_page_id($pageid, $v);
1172 $DB->insert_record('wiki_progress', array('userid' => $user->userid, 'pageid' => $pageid, 'versionid' => $v, 'progress' => $progress));
1176 function wiki_get_wiki_page_id($pageid, $id) {
1178 return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'id' => $id));
1181 function wiki_print_page_content($page, $context, $subwikiid) {
1182 global $OUTPUT, $CFG;
1184 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
1185 $content = wiki_refresh_cachedcontent($page);
1186 $page = $content['page'];
1189 if (isset($content)) {
1191 foreach ($content['sections'] as $s) {
1192 $box .= '<p>' . get_string('repeatedsection', 'wiki', $s) . '</p>';
1196 echo $OUTPUT->box($box);
1199 $html = file_rewrite_pluginfile_urls($page->cachedcontent, 'pluginfile.php', $context->id, 'mod_wiki', 'attachments', $subwikiid);
1200 $html = format_text($html, FORMAT_MOODLE, array('overflowdiv'=>true));
1201 echo $OUTPUT->box($html);
1203 if (!empty($CFG->usetags)) {
1204 $tags = tag_get_tags_array('wiki_pages', $page->id);
1205 echo $OUTPUT->container_start('wiki-tags');
1206 echo '<span class="wiki-tags-title">'.get_string('tags').': </span>';
1208 foreach ($tags as $tagid=>$tag) {
1209 $url = new moodle_url('/tag/index.php', array('tag'=>$tag));
1210 $links[] = html_writer::link($url, $tag, array('title'=>get_string('tagtitle', 'wiki', $tag)));
1212 echo join($links, ", ");
1213 echo $OUTPUT->container_end();
1216 wiki_increment_pageviews($page);
1220 * This function trims any given text and returns it with some dots at the end
1222 * @param string $text
1223 * @param string $limit
1227 function wiki_trim_string($text, $limit = 25) {
1229 if (strlen($text) > $limit) {
1230 $text = substr($text, 0, $limit) . '...';
1237 * Prints default edit form fields and buttons
1239 * @param string $format Edit form format (html, creole...)
1240 * @param integer $version Version number. A negative number means no versioning.
1243 function wiki_print_edit_form_default_fields($format, $pageid, $version = -1, $upload = false, $deleteuploads = array()) {
1244 global $CFG, $PAGE, $OUTPUT;
1246 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
1248 if ($version >= 0) {
1249 echo '<input type="hidden" name="version" value="' . $version . '" />';
1252 echo '<input type="hidden" name="format" value="' . $format . '"/>';
1255 require_once($CFG->dirroot . '/lib/form/filemanager.php');
1257 $filemanager = new MoodleQuickForm_filemanager('attachments', get_string('wikiattachments', 'wiki'), array('id' => 'attachments'), array('subdirs' => false, 'maxfiles' => 99, 'maxbytes' => $CFG->maxbytes));
1259 $value = file_get_submitted_draft_itemid('attachments');
1260 if (!empty($value) && !$upload) {
1261 $filemanager->setValue($value);
1264 echo "<fieldset class=\"wiki-upload-section clearfix\"><legend class=\"ftoggler\">" . get_string("uploadtitle", 'wiki') . "</legend>";
1266 echo $OUTPUT->container_start('mdl-align wiki-form-center aaaaa');
1267 print $filemanager->toHtml();
1268 echo $OUTPUT->container_end();
1271 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1273 echo $OUTPUT->container_start('mdl-align wiki-form-center wiki-upload-table');
1274 wiki_print_upload_table($context, 'wiki_upload', $pageid, $deleteuploads);
1275 echo $OUTPUT->container_end();
1279 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('save', 'wiki') . '"/>';
1280 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('upload', 'wiki') . '"/>';
1281 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('preview') . '"/>';
1282 echo '<input class="wiki_button" type="submit" name="editoption" value="' . get_string('cancel') . '" />';
1286 * Prints a table with the files attached to a wiki page
1287 * @param object $context
1288 * @param string $filearea
1289 * @param int $fileitemid
1290 * @param array deleteuploads
1292 function wiki_print_upload_table($context, $filearea, $fileitemid, $deleteuploads = array()) {
1293 global $CFG, $OUTPUT;
1295 $htmltable = new html_table();
1297 $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki'));
1299 $fs = get_file_storage();
1300 $files = $fs->get_area_files($context->id, 'mod_wiki', $filearea, $fileitemid); //TODO: this is weird (skodak)
1302 foreach ($files as $file) {
1303 if (!$file->is_directory()) {
1304 $checkbox = '<input type="checkbox" name="deleteupload[]", value="' . $file->get_pathnamehash() . '"';
1306 if (in_array($file->get_pathnamehash(), $deleteuploads)) {
1307 $checkbox .= ' checked="checked"';
1312 $htmltable->data[] = array($checkbox, '<a href="' . file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $context->id . '/wiki_upload/' . $fileitemid . '/' . $file->get_filename()) . '">' . $file->get_filename() . '</a>', "");
1316 print '<h3 class="upload-table-title">' . get_string('uploadfiletitle', 'wiki') . "</h3>";
1317 print html_writer::table($htmltable);
1321 * Generate wiki's page tree
1323 * @param $page. A wiki page object
1324 * @param $node. Starting navigation_node
1325 * @param $keys. An array to store keys
1326 * @return an array with all tree nodes
1328 function wiki_build_tree($page, $node, &$keys) {
1331 $icon = new pix_icon('f/odt', '');
1332 $pages = wiki_get_linked_pages($page->id);
1333 foreach ($pages as $p) {
1334 $key = $page->id . ':' . $p->id;
1335 if (in_array($key, $keys)) {
1338 array_push($keys, $key);
1339 $l = wiki_parser_link($p);
1340 $link = new moodle_url('/mod/wiki/view.php', array('pageid' => $p->id));
1341 $nodeaux = $node->add($p->title, $link, null, null, null, $icon);
1343 $nodeaux->add_class('wiki_newentry');
1345 wiki_build_tree($p, $nodeaux, $keys);
1352 * Get linked pages from page
1353 * @param int $pageid
1355 function wiki_get_linked_pages($pageid) {
1358 $sql = "SELECT p.id, p.title
1360 JOIN {wiki_links} l ON l.topageid = p.id
1361 WHERE l.frompageid = ?
1362 ORDER BY p.title ASC";
1363 return $DB->get_records_sql($sql, array($pageid));
1367 * Get updated pages from wiki
1368 * @param int $pageid
1370 function wiki_get_updated_pages_by_subwiki($swid) {
1375 WHERE subwikiid = ? AND timemodified > ?
1376 ORDER BY timemodified DESC";
1377 return $DB->get_records_sql($sql, array($swid, $USER->lastlogin));