Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / wiki / lib.php
blobaaa8203f0d17b612be20547039c40595ebbfb06e
1 <?php // $Id$
3 /// Library of functions and constants for module wiki
4 /// (replace wiki with the name of your module and delete this line)
7 $wiki_CONSTANT = 7; /// for example
8 $site = get_site();
9 $WIKI_TYPES = array ('teacher' => get_string('defaultcourseteacher'),
10 'group' => get_string('groups',"wiki"),
11 'student' => get_string('defaultcoursestudent') );
12 define("EWIKI_ESCAPE_AT", 0); # For the algebraic filter
14 function wiki_add_instance($wiki) {
15 /// Given an object containing all the necessary data,
16 /// (defined by the form in mod.html) this function
17 /// will create a new instance and return the id number
18 /// of the new instance.
20 $wiki->timemodified = time();
22 # May have to add extra stuff in here #
24 /// Determine the pagename for this wiki and save.
25 $wiki->pagename = wiki_page_name($wiki);
27 /// Check 'check boxes'. The variables won't be set at all of they were deselected.
28 $wiki->disablecamelcase = (isset($wiki->disablecamelcase)) ? 1 : 0;
29 $wiki->setpageflags = (isset($wiki->setpageflags)) ? 1 : 0;
30 $wiki->removepages = (isset($wiki->removepages)) ? 1 : 0;
31 $wiki->strippages = (isset($wiki->strippages)) ? 1 : 0;
32 $wiki->revertchanges = (isset($wiki->revertchanges)) ? 1 : 0;
34 return insert_record("wiki", $wiki);
38 function wiki_update_instance($wiki) {
39 /// Given an object containing all the necessary data,
40 /// (defined by the form in mod.html) this function
41 /// will update an existing instance with new data.
43 /// Determine the pagename for this wiki.
44 $wiki->pagename = wiki_page_name($wiki);
46 /// Check 'check boxes'. The variables won't be set at all of they were deselected.
47 $wiki->disablecamelcase = (isset($wiki->disablecamelcase)) ? 1 : 0;
48 $wiki->setpageflags = (isset($wiki->setpageflags)) ? 1 : 0;
49 $wiki->removepages = (isset($wiki->removepages)) ? 1 : 0;
50 $wiki->strippages = (isset($wiki->strippages)) ? 1 : 0;
51 $wiki->revertchanges = (isset($wiki->revertchanges)) ? 1 : 0;
53 $wiki->timemodified = time();
54 $wiki->id = $wiki->instance;
55 return update_record("wiki", $wiki);
58 /// Delete all Directories recursively
59 function wiki_rmdir($basedir) {
60 $handle = @opendir($basedir);
61 if($handle) {
62 while (false!==($folder = readdir($handle))) {
63 if($folder != "." && $folder != ".." && $Folder != "CVS") {
64 wiki_rmdir("$basedir/$folder"); // recursive
67 closedir($handle);
69 @rmdir($basedir);
72 function wiki_delete_instance($id) {
73 /// Given an ID of an instance of this module,
74 /// this function will permanently delete the instance
75 /// and any data that depends on it.
76 global $CFG;
78 if (! $wiki = get_record("wiki", "id", $id)) {
79 return false;
82 $result = true;
84 #Delete Files
85 ### Should probably check regardless of this setting in case its been changed...
86 if($wiki->ewikiacceptbinary) {
87 if ($basedir = $CFG->dataroot."/".$wiki->course."/".$CFG->moddata."/wiki/$id") {
88 if ($files = get_directory_list($basedir)) {
89 foreach ($files as $file) {
90 #if ($file != $exception) {
91 unlink("$basedir/$file");
92 notify("Existing file '$file' has been deleted!");
96 #if (!$exception) { // Delete directory as well, if empty
97 wiki_rmdir("$basedir");
102 # Delete any dependent records here #
103 if (! delete_records("wiki", "id", $wiki->id)) {
104 $result = false;
107 /// Delete all wiki_entries and wiki_pages.
108 if (($wiki_entries = wiki_get_entries($wiki)) !== false) {
109 foreach ($wiki_entries as $wiki_entry) {
110 if (! delete_records("wiki_pages", "wiki", "$wiki_entry->id")) {
111 $result = false;
113 if (! delete_records("wiki_entries", "id", "$wiki_entry->id")) {
114 $result = false;
119 return $result;
122 function wiki_user_outline($course, $user, $mod, $wiki) {
123 /// Return a small object with summary information about what a
124 /// user has done with a given particular instance of this module
125 /// Used for user activity reports.
126 /// $return->time = the time they did it
127 /// $return->info = a short text description
129 $return = NULL;
130 return $return;
133 function wiki_user_complete($course, $user, $mod, $wiki) {
134 /// Print a detailed representation of what a user has done with
135 /// a given particular instance of this module, for user activity reports.
137 return true;
140 function wiki_print_recent_activity($course, $isteacher, $timestart) {
141 /// Given a course and a time, this module should find recent activity
142 /// that has occurred in wiki activities and print it out.
143 /// Return true if there was output, or false is there was none.
145 global $CFG;
146 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
147 'course = \''.$course->id.'\' AND '.
148 'module = \'wiki\' AND '.
149 'action LIKE \'edit%\' ', 'time ASC')){
150 return false;
153 foreach ($logs as $log) {
154 //Create a temp valid module structure (course,id)
155 $tempmod->course = $log->course;
156 $tempmod->id = $log->cmid;
157 //Obtain the visible property from the instance
158 $modvisible = instance_is_visible($log->module,$tempmod);
160 //Only if the mod is visible
161 if ($modvisible) {
162 $wikis[$log->info] = wiki_log_info($log);
163 $wikis[$log->info]->pagename = $log->info;
164 $wikis[$log->info]->time = $log->time;
165 $wikis[$log->info]->url = str_replace('&', '&amp;', $log->url);
169 if ($wikis) {
170 $content = true;
171 print_headline(get_string('updatedwikipages', 'wiki').':', 3);
172 foreach ($wikis as $wiki) {
173 print_recent_activity_note($wiki->time, $wiki, $isteacher, $wiki->pagename,
174 $CFG->wwwroot.'/mod/wiki/'.$wiki->url);
177 return true; // True if anything was printed, otherwise false
180 function wiki_log_info($log) {
181 global $CFG;
182 return get_record_sql("SELECT u.firstname, u.lastname
183 FROM {$CFG->prefix}user u
184 WHERE u.id = '$log->userid'");
187 function wiki_cron () {
188 /// Function to be run periodically according to the moodle cron
189 /// This function searches for things that need to be done, such
190 /// as sending out mail, toggling flags etc ...
192 global $CFG;
194 return true;
197 function wiki_grades($wikiid) {
198 /// Must return an array of grades for a given instance of this module,
199 /// indexed by user. It also returns a maximum allowed grade.
201 return NULL;
204 function wiki_get_participants($wikiid) {
205 //Returns the users with data in one wiki
206 //(users with records in wiki_pages and wiki_entries)
208 global $CFG;
210 //Get users from wiki_pages
211 $st_pages = get_records_sql("SELECT DISTINCT u.id, u.id
212 FROM {$CFG->prefix}user u,
213 {$CFG->prefix}wiki_entries e,
214 {$CFG->prefix}wiki_pages p
215 WHERE e.wikiid = '$wikiid' and
216 p.wiki = e.id and
217 u.id = p.userid");
219 //Get users from wiki_entries
220 $st_entries = get_records_sql("SELECT DISTINCT u.id, u.id
221 FROM {$CFG->prefix}user u,
222 {$CFG->prefix}wiki_entries e
223 WHERE e.wikiid = '$wikiid' and
224 u.id = e.userid");
226 //Add entries to pages
227 if ($st_entries) {
228 foreach ($st_entries as $st_entry) {
229 $st_pages[$st_entry->id] = $st_entry;
233 return $st_pages;
237 //////////////////////////////////////////////////////////////////////////////////////
238 /// Any other wiki functions go here. Each of them must have a name that
239 /// starts with wiki_
241 function wiki_wiki_name($wikiname) {
242 /// Return the passed in string in Wiki name format.
243 /// Remove any leading and trailing whitespace, capitalize all the words
244 /// and then remove any internal whitespace.
246 if (wiki_is_wiki_name($wikiname)) {
247 return $wikiname;
249 else {
250 /// Create uppercase words and remove whitespace.
251 $wikiname = preg_replace("/(\w+)\s/", "$1", ucwords(trim($wikiname)));
253 /// Check again - there may only be one word.
254 if (wiki_is_wiki_name($wikiname)) {
255 return $wikiname;
257 /// If there is only one word, append default wiki name to it.
258 else {
259 return $wikiname.get_string('wikidefaultpagename', 'wiki');
264 function wiki_is_wiki_name($wikiname) {
265 /// Check for correct wikiname syntax and return true or false.
267 /// If there are spaces between the words, incorrect format.
268 if (preg_match_all('/\w+/', $wikiname, $out) > 1) {
269 return false;
271 /// If there isn't more than one group of uppercase letters separated by
272 /// lowercase letters or '_', incorrect format.
273 else if (preg_match_all('/[A-Z]+[a-z_]+/', $wikiname, $out) > 1) {
274 return true;
276 else {
277 return false;
281 function wiki_page_name(&$wiki) {
282 /// Determines the wiki's page name and returns it.
283 if (!empty($wiki->initialcontent)) {
284 $ppos = strrpos($wiki->initialcontent, '/');
285 if ($ppos === false) {
286 $pagename = $wiki->initialcontent;
288 else {
289 $pagename = substr($wiki->initialcontent, $ppos+1);
292 else if (!empty($wiki->pagename)) {
293 $pagename = $wiki->pagename;
295 else {
296 $pagename = $wiki->name;
298 return $pagename;
301 function wiki_content_dir(&$wiki) {
302 /// Determines the wiki's default content directory (if there is one).
303 global $CFG;
305 if (!empty($wiki->initialcontent)) {
306 $ppos = strrpos($wiki->initialcontent, '/');
307 if ($ppos === false) {
308 $subdir = '';
310 else {
311 $subdir = substr($wiki->initialcontent, 0, $ppos+1);
313 $contentdir = $CFG->dataroot.'/'.$wiki->course.'/'.$subdir;
315 else {
316 $contentdir = false;
318 return $contentdir;
321 function wiki_get_course_wikis($courseid, $wtype='*') {
322 /// Returns all wikis for the specified course and optionally of the specified type.
324 $select = 'course = '.$courseid;
325 if ($wtype != '*') {
326 $select .= ' AND wtype = \''.$wtype.'\'';
328 return get_records_select('wiki', $select, 'id');
331 function wiki_has_entries(&$wiki) {
332 /// Returns true if wiki already has wiki entries; otherwise false.
334 return record_exists('wiki_entries', 'wikiid', $wiki->id);
337 function wiki_get_entries(&$wiki, $byindex=NULL) {
338 /// Returns an array with all wiki entries indexed by entry id; false if there are none.
339 /// If the optional $byindex is specified, returns the entries indexed by that field.
340 /// Valid values for $byindex are 'student', 'group'.
342 if ($byindex == 'student') {
343 return get_records('wiki_entries', 'wikiid', $wiki->id, '',
344 'userid,id,wikiid,course,groupid,pagename,timemodified');
346 else if ($byindex == 'group') {
347 return get_records('wiki_entries', 'wikiid', $wiki->id, '',
348 'groupid,id,wikiid,course,userid,pagename,timemodified');
350 else {
351 return get_records('wiki_entries', 'wikiid', $wiki->id);
355 function wiki_get_default_entry(&$wiki, &$course, $userid=0, $groupid=0) {
356 /// Returns the wiki entry according to the wiki type.
357 /// Optionally, will return wiki entry for $userid student wiki, or
358 /// $groupid group or teacher wiki.
359 /// Creates one if it needs to and it can.
361 global $USER;
363 /// If the wiki entry doesn't exist, can this user create it?
364 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
366 if (wiki_can_add_entry($wiki, $USER, $course, $userid, $groupid)) {
367 wiki_add_entry($wiki, $course, $userid, $groupid);
368 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false) {
369 error("Could not add wiki entry.");
373 return $wiki_entry;
376 function wiki_get_entry(&$wiki, &$course, $userid=0, $groupid=0) {
377 /// Returns the wiki entry according to the wiki type.
378 /// Optionally, will return wiki entry for $userid student wiki, or
379 /// $groupid group or teacher wiki.
380 global $USER;
382 switch ($wiki->wtype) {
383 case 'student':
384 /// If a specific user was requested, return it, if allowed.
385 if ($userid and wiki_user_can_access_student_wiki($wiki, $userid, $course)) {
386 $wentry = wiki_get_student_entry($wiki, $userid);
389 /// If there is no entry for this user, check if this user is a teacher.
390 else if (!$wentry = wiki_get_student_entry($wiki, $USER->id)) {
391 /* if (isteacher($course->id, $USER->id)) {
392 /// If this user is a teacher, return the first entry.
393 if ($wentries = wiki_get_entries($wiki)) {
394 $wentry = current($wentries);
398 break;
400 case 'group':
401 /// If there is a groupmode, get the user's group id.
402 $groupmode = groupmode($course, $wiki);
404 /// If a specific group was requested, return it, if allowed.
405 if ($groupid and wiki_user_can_access_group_wiki($wiki, $groupid, $course)) {
406 $wentry = wiki_get_group_entry($wiki, $groupid);
408 else if ($groupmode) {
409 /// If there is no entry for this user, check if this user is a teacher.
410 if (!$wentry = wiki_get_group_entry($wiki, mygroupid($course->id))) {
411 /* if (isteacher($course->id, $USER->id)) {
412 /// If this user is a teacher, return the first entry.
413 if ($wentries = wiki_get_entries($wiki)) {
414 $wentry = current($wentries);
416 } */
419 /// If mode is 'nogroups', then groupid is zero.
420 else {
421 $wentry = wiki_get_group_entry($wiki, 0);
423 break;
425 case 'teacher':
426 /// If there is a groupmode, get the user's group id.
427 if (groupmode($course, $wiki)) {
428 $groupid = $groupid ? $groupid : mygroupid($course->id);
431 /// If a specific group was requested, return it, if allowed.
432 if (wiki_user_can_access_teacher_wiki($wiki, $groupid, $course)) {
433 $wentry = wiki_get_teacher_entry($wiki, $groupid);
435 break;
437 return $wentry;
440 function wiki_get_teacher_entry(&$wiki, $groupid=0) {
441 /// Returns the wiki entry for the wiki teacher type.
442 return get_record('wiki_entries', 'wikiid', $wiki->id, 'course', $wiki->course, 'groupid', $groupid);
445 function wiki_get_group_entry(&$wiki, $groupid=null) {
446 /// Returns the wiki entry for the given group.
447 return get_record('wiki_entries', 'wikiid', $wiki->id, 'groupid', $groupid);
450 function wiki_get_student_entry(&$wiki, $userid=null) {
451 /// Returns the wiki entry for the given student.
452 global $USER;
454 if (is_null($userid)) {
455 $userid = $USER->id;
457 return get_record('wiki_entries', 'wikiid', $wiki->id, 'userid', $userid);
460 function wiki_get_other_wikis(&$wiki, &$user, &$course, $currentid=0) {
461 /// Returns a list of other wikis to display, depending on the type, group and user.
462 /// Returns the key containing the currently selected entry as well.
464 global $CFG, $id;
466 $wikis = false;
468 $groupmode = groupmode($course, $wiki);
469 $mygroupid = mygroupid($course->id);
470 $isteacher = isteacher($course->id, $user->id);
471 $isteacheredit = isteacheredit($course->id, $user->id);
473 switch ($wiki->wtype) {
475 case 'student':
476 /// Get all the existing entries for this wiki.
477 $wiki_entries = wiki_get_entries($wiki, 'student');
478 if ($isteacher and (SITEID != $course->id)) {
480 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all student
481 /// wikis, regardless of creation.
482 if ((SITEID != $course->id) and ($isteacheredit or ($groupmode == NOGROUPS))) {
484 if ($students = get_course_students($course->id)) {
485 /// Default pagename is dependent on the wiki settings.
486 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
488 foreach ($students as $student) {
490 /// If this student already has an entry, use its pagename.
491 if ($wiki_entries[$student->id]) {
492 $pagename = $wiki_entries[$student->id]->pagename;
494 else {
495 $pagename = $defpagename;
498 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
499 $wikis[$key] = fullname($student).':'.$pagename;
503 else if ($groupmode == SEPARATEGROUPS) {
504 if ($students = get_group_students($mygroupid)) {
505 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
506 foreach ($students as $student) {
508 /// If this student already has an entry, use its pagename.
509 if ($wiki_entries[$student->id]) {
510 $pagename = $wiki_entries[$student->id]->pagename;
512 else {
513 $pagename = $defpagename;
516 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
517 $wikis[$key] = fullname($student).':'.$pagename;
521 else if ($groupmode == VISIBLEGROUPS) {
522 /// Get all students in your group.
523 if ($students = get_group_students($mygroupid)) {
524 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
525 foreach ($students as $student) {
526 /// If this student already has an entry, use its pagename.
527 if ($wiki_entries[$student->id]) {
528 $pagename = $wiki_entries[$student->id]->pagename;
530 else {
531 $pagename = $defpagename;
533 $key = 'view.php?id='.$id.'&userid='.$student->id.'&page='.$pagename;
534 $wikis[$key] = fullname($student).':'.$pagename;
537 /// Get all student wikis created, regardless of group.
538 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
539 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'user u '
540 .' WHERE w.wikiid = '.$wiki->id.' AND u.id = w.userid '
541 .' ORDER BY w.id';
542 $wiki_entries = get_records_sql($sql);
543 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
544 foreach ($wiki_entries as $wiki_entry) {
545 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
546 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
547 if ($currentid == $wiki_entry->id) {
548 $wikis['selected'] = $key;
553 else {
554 /// A user can see other student wikis if they are a member of the same
555 /// group (for separate groups) or there are visible groups, or if this is
556 /// a site-level wiki, and they are an administrator.
557 if (($groupmode == VISIBLEGROUPS) or
558 ((SITEID == $course->id) and isadmin())) {
559 $viewall = true;
561 else if ($groupmode == SEPARATEGROUPS) {
562 $viewall = mygroupid($course->id);
564 else {
565 $viewall = false;
568 if ($viewall !== false) {
569 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
570 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'user u '
571 .' WHERE w.wikiid = '.$wiki->id.' AND u.id = w.userid '
572 .' ORDER BY w.id';
573 $wiki_entries = get_records_sql($sql);
574 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
575 foreach ($wiki_entries as $wiki_entry) {
576 if (($viewall === true) or ismember($viewall, $wiki_entry->userid)) {
577 $key = 'view.php?id='.$id.'&userid='.$wiki_entry->userid.'&page='.$wiki_entry->pagename;
578 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
579 if ($currentid == $wiki_entry->id) {
580 $wikis['selected'] = $key;
586 break;
588 case 'group':
589 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all group
590 /// wikis, regardless of creation.
592 /// Get all the existing entries for this wiki.
593 $wiki_entries = wiki_get_entries($wiki, 'group');
595 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
596 if ($groups = get_groups($course->id)) {
597 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
598 foreach ($groups as $group) {
600 /// If this group already has an entry, use its pagename.
601 if (isset($wiki_entries[$group->id])) {
602 $pagename = $wiki_entries[$group->id]->pagename;
604 else {
605 $pagename = $defpagename;
608 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
609 $wikis[$key] = $group->name.':'.$pagename;
613 /// A user can see other group wikis if there are visible groups.
614 else if ($groupmode == VISIBLEGROUPS) {
615 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
616 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
617 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
618 .' ORDER BY w.groupid';
619 $wiki_entries = get_records_sql($sql);
620 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
621 foreach ($wiki_entries as $wiki_entry) {
622 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
623 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
624 if ($currentid == $wiki_entry->id) {
625 $wikis['selected'] = $key;
629 break;
631 case 'teacher':
632 if ($isteacher) {
633 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all
634 /// teacher wikis, regardless of creation.
635 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
636 if ($groups = get_groups($course->id)) {
637 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
639 foreach ($groups as $group) {
640 /// If this group already has an entry, use its pagename.
641 if ($wiki_entries[$group->id]) {
642 $pagename = $wiki_entries[$group->id]->pagename;
644 else {
645 $pagename = $defpagename;
648 $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
649 $wikis[$key] = $group->name.':'.$pagename;
653 /// A teacher can see all other group teacher wikis.
654 else if ($groupmode) {
655 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
656 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
657 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
658 .' ORDER BY w.groupid';
659 $wiki_entries = get_records_sql($sql);
660 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
661 foreach ($wiki_entries as $wiki_entry) {
662 $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
663 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
664 if ($currentid == $wiki_entry->id) {
665 $wikis['selected'] = $key;
670 else {
671 /// A user can see other teacher wikis if they are a teacher, a member of the same
672 /// group (for separate groups) or there are visible groups.
673 if ($groupmode == VISIBLEGROUPS) {
674 $viewall = true;
676 else if ($groupmode == SEPARATEGROUPS) {
677 $viewall = $mygroupid;
679 else {
680 $viewall = false;
682 if ($viewall !== false) {
683 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
684 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
685 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
686 .' ORDER BY w.groupid';
687 $wiki_entries = get_records_sql($sql);
688 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
689 foreach ($wiki_entries as $wiki_entry) {
690 if (($viewall === true) or $viewall == $wiki_entry->groupid) {
691 $key = 'view.php??id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
692 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
693 if ($currentid == $wiki_entry->id) {
694 $wikis['selected'] = $key;
700 break;
703 return $wikis;
706 function wiki_add_entry(&$wiki, &$course, $userid=0, $groupid=0) {
707 /// Adds a new wiki entry of the specified type, unless already entered.
708 /// No checking is done here. It is assumed that the caller has the correct
709 /// privileges to add this entry.
711 global $USER;
713 /// If this wiki already has a wiki_type entry, return false.
714 if (wiki_get_entry($wiki, $course, $userid, $groupid) !== false) {
715 return false;
718 switch ($wiki->wtype) {
720 case 'student':
721 $wiki_entry->wikiid = $wiki->id;
722 $wiki_entry->userid = $userid ? $userid : $USER->id;
723 $wiki_entry->pagename = wiki_page_name($wiki);
724 $wiki_entry->timemodified = time();
725 break;
727 case 'group':
728 /// Get the groupmode. It's been added to the wiki object.
729 $groupmode = groupmode($course, $wiki);
731 /// If there is a groupmode, get the group id.
732 if ($groupmode) {
733 $groupid = $groupid ? $groupid : mygroupid($course->id);
735 /// If mode is 'nogroups', then groupid is zero.
736 else {
737 $groupid = 0;
739 $wiki_entry->wikiid = $wiki->id;
740 $wiki_entry->groupid = $groupid;
741 $wiki_entry->pagename = wiki_page_name($wiki);
742 $wiki_entry->timemodified = time();
743 break;
745 case 'teacher':
746 /// Get the groupmode. It's been added to the wiki object.
747 $groupmode = groupmode($course, $wiki);
749 /// If there is a groupmode, get the user's group id.
750 if ($groupmode and $groupid == 0) {
751 $groupid = mygroupid($course->id);
754 $wiki_entry->wikiid = $wiki->id;
755 $wiki_entry->course = $wiki->course;
756 $wiki_entry->groupid = $groupid;
757 $wiki_entry->pagename = wiki_page_name($wiki);
758 $wiki_entry->timemodified = time();
759 break;
761 $wiki_entry->pagename = addslashes($wiki_entry->pagename);
763 return insert_record("wiki_entries", $wiki_entry, true);
766 function wiki_can_add_entry(&$wiki, &$user, &$course, $userid=0, $groupid=0) {
767 /// Returns true or false if the user can add a wiki entry for this wiki.
769 /// Get the groupmode. It's been added to the wiki object.
770 $groupmode = groupmode($course, $wiki);
771 $mygroupid = mygroupid($course->id);
773 switch ($wiki->wtype) {
775 case 'student':
776 /// A student can create their own wiki, if they are a member of that course.
777 /// A user can create their own wiki at the site level.
778 if ($userid == 0) {
779 return (isstudent($course->id, $user->id) or
780 ((SITEID == $course->id) and !empty($user) and !isguest()));
782 /// An editing teacher can create any student wiki, or
783 /// a non-editing teacher, if not assigned to a group can create any student wiki, or if assigned to a group can
784 /// create any student wiki in their group.
785 else {
786 return ((($userid == $user->id) and isstudent($course->id, $user->id)) or isteacheredit($course->id) or
787 (isteacher($course->id) and (!$groupmode or $mygroupid == 0 or (ismember($mygroupid, $userid)))));
789 break;
791 case 'group':
792 /// If mode is 'nogroups', then all participants can add wikis.
793 if (!$groupmode) {
794 return (isstudent($course->id, $user->id) or isteacher($course->id, $user->id) or
795 ((SITEID == $course->id) and !empty($user) and !isguest()));
797 /// If not requesting a group, must be a member of a group.
798 else if ($groupid == 0) {
799 return ($mygroupid != 0);
801 /// If requesting a group, must be an editing teacher, a non-editing teacher with no assigned group,
802 /// or a non-editing teacher requesting their group.
803 else {
804 return (isteacheredit($course->id) or
805 (isteacher($course->id) and ($mygroupid == 0 or $mygroupid == $groupid)));
807 break;
809 case 'teacher':
810 /// If mode is 'nogroups', then all teachers can add wikis.
811 if (!$groupmode) {
812 return (isteacher($course->id, $user->id) or ((SITEID == $course->id) and isadmin()));
814 /// If not requesting a group, must be a member of a group.
815 else if ($groupid == 0) {
816 return ($mygroupid != 0 and isteacher($course->id));
818 /// If there is a group mode, non-editing teachers with an assigned group, can only create wikis
819 /// in their group. Non-editing teachers with no assigned group and editing teachers can create any wiki.
820 else {
821 return (isteacheredit($course->id) or
822 (isteacher($course->id) and ($mygroupid == 0 or $mygroupid == $groupid)));
824 break;
827 return false;
830 function wiki_can_edit_entry(&$wiki_entry, &$wiki, &$user, &$course) {
831 /// Returns true or false if the user can edit this wiki entry.
833 $can_edit = false;
834 $groupmode = groupmode($course, $wiki);
835 $mygroupid = mygroupid($course->id);
837 /// Editing teacher's and admins can edit all wikis, non-editing teachers can edit wikis in their groups,
838 /// or all wikis if group mode is 'no groups' or they don't belong to a group.
839 if (isadmin($user->id) or isteacheredit($course->id, $user->id) or
840 ((!$groupmode or $mygroupid == 0) and isteacher($course->id, $user->id))) {
841 $can_edit = true;
843 else {
844 switch ($wiki->wtype) {
846 /// Only a teacher or the owner of a student wiki can edit it.
847 case 'student':
848 $can_edit = (($user->id == $wiki_entry->userid) or
849 ($groupmode and isteacher($course->id, $user->id) and
850 ismember($mygroupid, $wiki_entry->userid)));
851 break;
853 case 'group':
854 /// If there is a groupmode, determine the user's group status.
855 if ($groupmode) {
856 /// If the user is a member of the wiki group, they can edit the wiki.
857 $can_edit = ismember($wiki_entry->groupid, $user->id);
859 /// If mode is 'nogroups', then all participants can edit the wiki.
860 else {
861 $can_edit = (isstudent($course->id, $user->id) or isteacher($course->id, $user->id) or
862 ((SITEID == $course->id) and !empty($user) and !isguest()));
864 break;
866 case 'teacher':
867 /// If there is a groupmode, determine the user's group status.
868 if ($groupmode) {
869 /// If the user is a member of the wiki group, they can edit the wiki.
870 $can_edit = (isteacher($course->id, $user->id) and ismember($wiki_entry->groupid, $user->id));
872 else {
873 $can_edit = (isteacher($course->id, $user->id) or ((SITEID == $course->id) and isadmin()));
875 break;
879 return $can_edit;
882 function wiki_user_can_access_student_wiki(&$wiki, $userid, &$course) {
883 global $USER;
885 /// Get the groupmode. It's been added to the wiki object.
886 $groupmode = groupmode($course, $wiki);
887 $usersgroup = mygroupid($course->id);
888 $isteacher = isteacher($course->id, $USER->id);
890 /// If this user is allowed to access this wiki then return TRUE.
891 /// *** THIS COULD BE A PROBLEM, IF STUDENTS COULD EVER BE PART OF MORE THAN ONE GROUP ***
892 /// A user can access a student wiki, if:
893 /// - it is their wiki,
894 /// - group mode is VISIBLEGROUPS,
895 /// - group mode is SEPARATEGROUPS, and the user is a member of the requested user's group,
896 /// - they are an editing teacher or administrator,
897 /// - they are a non-editing teacher not assigned to a specific group,
898 /// - they are a non-editing teacher and group mode is NOGROUPS.
899 /// - they are an administrator (mostly for site-level wikis).
900 if (($userid and ($USER->id == $userid)) or ($groupmode == VISIBLEGROUPS) or
901 (($groupmode == SEPARATEGROUPS) and ismember($usersgroup, $userid)) or
902 (isteacheredit($course->id, $USER->id)) or
903 (isteacher($course->id, $USER->id) and (!$usersgroup or ($groupmode == NOGROUPS))) or
904 (isadmin())) {
905 $can_access = true;
907 else {
908 $can_access = false;
910 return $can_access;
913 function wiki_user_can_access_group_wiki(&$wiki, $groupid, &$course) {
914 global $USER;
916 /// Get the groupmode. It's been added to the wiki object.
917 $groupmode = groupmode($course, $wiki);
918 $usersgroup = mygroupid($course->id);
919 $isteacher = isteacher($course->id, $USER->id);
921 /// A user can access a group wiki, if:
922 /// - group mode is NOGROUPS,
923 /// - group mode is VISIBLEGROUPS,
924 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
925 /// - they are an editing teacher or administrator,
926 /// - they are a non-editing teacher not assigned to a specific group.
927 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
928 (($groupmode == SEPARATEGROUPS) and ($usersgroup == $groupid)) or
929 (isteacheredit($course->id, $USER->id)) or
930 (isteacher($course->id, $USER->id) and !$usersgroup)) {
931 $can_access = true;
933 else {
934 $can_access = false;
936 return $can_access;
939 function wiki_user_can_access_teacher_wiki(&$wiki, $groupid, &$course) {
940 global $USER;
942 /// Get the groupmode. It's been added to the wiki object.
943 $groupmode = groupmode($course, $wiki);
945 /// A user can access a teacher wiki, if:
946 /// - group mode is NOGROUPS,
947 /// - group mode is VISIBLEGROUPS,
948 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
949 /// - they are a teacher or administrator,
950 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
951 (($groupmode == SEPARATEGROUPS) and (mygroupid($course->id) == $groupid)) or
952 (isteacher($course->id, $USER->id))){
953 $can_access = true;
955 else {
956 $can_access = false;
958 return $can_access;
961 function wiki_get_owner(&$wiki_entry) {
962 if ($wiki_entry->userid > 0) {
963 $user = get_record('user', 'id', $wiki_entry->userid);
964 $owner = fullname($user);
966 else if ($wiki_entry->groupid > 0) {
967 $group = get_record('groups', 'id', $wiki_entry->groupid);
968 $owner = $group->name;
970 else if ($wiki_entry->course > 0) {
971 $course = get_record('course', 'id', $wiki_entry->course);
972 $owner = $course->shortname;
974 else {
975 $owner = '- '.get_string("ownerunknown","wiki").' -';
977 return $owner;
980 function wiki_print_search_form($cmid, $search="", $userid, $groupid, $return=false) {
981 global $CFG;
982 # TODO: Add Group and User !!!
983 $output = "<form name=\"search\" action=\"$CFG->wwwroot/mod/wiki/view.php\">";
984 $output .= "<font size=\"-1\">";
985 $output .= "<input value=\"".get_string("searchwiki", "wiki").":\" type=\"submit\" />";
986 $output .= "<input name=\"id\" type=\"hidden\" value=\"$cmid\" />";
987 $output = $output.($groupid?"<input name=\"groupid\" type=\"hidden\" value=\"$groupid\" />":"");
988 $output = $output.($userid?"<input name=\"userid\" type=\"hidden\" value=\"$userid\" />":"");
989 $output .= "<input name=\"q\" type=\"text\" size=\"20\" value=\"$search\" />".' ';
990 $output .= "</font>";
991 $output .= "<input name=\"page\" type=\"hidden\" value=\"SearchPages\" />";
992 $output .= "</form>";
994 if ($return) {
995 return $output;
997 echo $output;
1000 function wiki_print_wikilinks_block($cmid, $binary=false, $return=false) {
1001 /// Prints a link-list of special wiki-pages
1002 global $CFG, $ewiki_title;
1004 $links=array();
1006 $links["SiteMap"]=get_string("sitemap", "wiki");
1007 $links["PageIndex"]=get_string("pageindex", "wiki");
1008 $links["NewestPages"]=get_string("newestpages", "wiki");
1009 $links["MostVisitedPages"]=get_string("mostvisitedpages", "wiki");
1010 $links["MostOftenChangedPages"]=get_string("mostoftenchangedpages", "wiki");
1011 $links["UpdatedPages"]=get_string("updatedpages", "wiki");
1012 $links["OrphanedPages"]=get_string("orphanedpages", "wiki");
1013 $links["WantedPages"]=get_string("wantedpages", "wiki");
1014 $links["WikiExport"]=get_string("wikiexport", "wiki");
1015 if($binary) {
1016 $links["FileDownload"]=get_string("filedownload", "wiki");
1018 popup_form(EWIKI_SCRIPT, $links, "wikilinks", "", get_string("choosewikilinks", "wiki"), "", "", $return);
1021 function wiki_print_page_actions($cmid, $specialpages, $page, $action, $binary=false, $canedit=true) {
1022 /// Displays actions which can be performed on the page
1024 $page=array();
1026 // Edit this Page
1027 if (in_array($action, array("edit", "links", "info", "attachments"))) {
1028 $page["view/$page"]=get_string("viewpage","wiki");
1030 if ($canedit && !in_array($page, $specialpages) && $action != "edit") {
1031 $page["edit/$page"]=get_string("editthispage","wiki");
1033 if ($action != "links") {
1034 $page["links/$page"]=get_string("backlinks","wiki");
1036 if ($canedit && !in_array($page, $specialpages) && $action!="info") {
1037 $page["info/$page"]=get_string("pageinfo","wiki");
1039 if($canedit && $binary && !in_array($page, $specialpages) && $action != "attachments") {
1040 $page["attachments/$page"]=get_string("attachments","wiki");
1043 popup_form(EWIKI_SCRIPT, $page, "wikiactions", "", get_string("action", "wiki"), "", "", false);
1046 function wiki_print_administration_actions($wiki, $cmid, $userid, $groupid, $page, $noeditor, $course) {
1047 /// Displays actions which can be performed on the page
1049 /// Create the URL
1050 $ewscript = 'admin.php?id='.$cmid;
1051 if (isset($userid) && $userid!=0) $ewscript .= '&amp;userid='.$userid;
1052 if (isset($groupid) && $groupid!=0) $ewscript .= '&amp;groupid='.$groupid;
1053 if (isset($page)) $ewscript .= '&amp;page='.$page;
1054 $ewscript.="&amp;action=";
1057 /// Build that action array according to wiki flags.
1058 $action = array();
1059 $isteacher = isteacher($course->id);
1061 if ($wiki->setpageflags or $isteacher) {
1062 $action['setpageflags'] = get_string('setpageflags', 'wiki');
1064 if ($wiki->removepages or $isteacher) {
1065 $action['removepages'] = get_string('removepages', 'wiki');
1067 if ($wiki->strippages or $isteacher) {
1068 $action['strippages'] = get_string('strippages', 'wiki');
1070 if ($wiki->revertchanges or $isteacher) {
1071 $action['revertpages'] = get_string('revertpages', 'wiki');
1074 if($noeditor) {
1075 $action["checklinks"]=get_string("checklinks", "wiki");
1077 popup_form($ewscript, $action, "wikiadministration", "", get_string("chooseadministration", "wiki"), "", "", false);
1080 function wiki_admin_get_flagarray() {
1081 $ret = array(
1082 EWIKI_DB_F_TEXT => get_string("flagtxt","wiki"),
1083 EWIKI_DB_F_BINARY => get_string("flagbin","wiki"),
1084 EWIKI_DB_F_DISABLED => get_string("flagoff","wiki"),
1085 EWIKI_DB_F_HTML => get_string("flaghtm","wiki"),
1086 EWIKI_DB_F_READONLY => get_string("flagro","wiki"),
1087 EWIKI_DB_F_WRITEABLE => get_string("flagwr","wiki"),
1090 return $ret;
1093 ///////// Ewiki Administration. Mostly taken from the ewiki/tools folder and changed
1094 function wiki_admin_setpageflags_list($pageflagstatus) {
1095 $FD = wiki_admin_get_flagarray();
1096 $table->head = array(get_string("pagename","wiki"), get_string("flags","wiki"));
1097 if($pageflagstatus) {
1098 $table->head[]=get_string("status","wiki");
1101 $result = ewiki_database("GETALL", array("version", "flags"));
1102 while ($row = $result->get()) {
1103 $id = $row["id"];
1104 $data = ewiki_database("GET", $row);
1106 $cell_pagename="";
1107 $cell_flags="";
1108 if ($data["flags"] & EWIKI_DB_F_TEXT) {
1109 $cell_pagename .= '<A HREF="' . EWIKI_SCRIPT . $id . '">';
1110 } else {
1111 $cell_pagename .= '<A HREF="' . EWIKI_SCRIPT_BINARY . $id . '">';
1113 $cell_pagename .= htmlentities($id) . '</A> / '.get_string("version","wiki").": ".$row["version"];
1115 foreach ($FD as $n=>$str) {
1116 $cell_flags .='<INPUT TYPE="checkbox" NAME="flags['. rawurlencode($id)
1117 . '][' . $n . ']" VALUE="1" '
1118 . (($data["flags"] & $n) ? "CHECKED=\"checked\"" : "")
1119 . ' />'.$str. ' ';
1121 if($pageflagstatus) {
1122 $table->data[]=array($cell_pagename, $cell_flags, $pageflagstatus[$id]);
1123 } else {
1124 $table->data[]=array($cell_pagename, $cell_flags);
1127 return $table;
1130 function wiki_admin_setpageflags($pageflags) {
1131 $FD = wiki_admin_get_flagarray();
1133 $status=array();
1134 if($pageflags) {
1135 foreach($pageflags as $page=>$fa) {
1137 $page = rawurldecode($page);
1139 $flags = 0;
1140 $fstr = "";
1141 foreach($fa as $num=>$isset) {
1142 if ($isset) {
1143 $flags += $num;
1144 $fstr .= ($fstr?",":""). $FD[$num];
1148 #$status[$page] .= "{$flags}=[{$fstr}]";
1150 $data = ewiki_database("GET", array("id" => $page));
1152 if ($data["flags"] != $flags) {
1153 $data["flags"] = $flags;
1154 $data["author"] = "ewiki-tools, " . ewiki_author();
1155 $data["version"]++;
1156 ewiki_database("WRITE", $data);
1157 $status[$page] = "<b>".get_string("flagsset","wiki")."</b> ".$status[$page];
1161 return $status;
1165 function wiki_admin_remove_list($listall="") {
1166 /// Table header
1167 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("errororreason","wiki"));
1169 /// Get all pages
1170 $result = ewiki_database("GETALL", array("version"));
1171 $selected = array();
1173 /// User wants to see all pages
1174 if ($listall) {
1175 while ($row = $result->get()) {
1176 $selected[$row["id"]] = get_string("listall","wiki")."<br />";
1179 while ($page = $result->get()) {
1180 $id = $page["id"];
1181 $page = ewiki_database("GET", array("id"=>$id));
1182 $flags = $page["flags"];
1183 #print "$id ".strlen(trim(($page["content"])))."<br />";
1185 if (!strlen(trim(($page["content"]))) && !($flags & EWIKI_DB_F_BINARY)) {
1186 @$selected[$id] .= get_string("emptypage","wiki")."<br />";
1189 // Check for orphaned pages
1190 $result2 = ewiki_database("SEARCH", array("content" => $id));
1191 $orphanedpage=true;
1192 if ($result2 && $result2->count()) {
1193 while ($row = $result2->get()) {
1194 $checkcontent = ewiki_database("GET", array("id"=>$row["id"]));
1195 $checkcontent = strtolower($checkcontent["content"]);
1197 if(strpos($checkcontent, strtolower($id)) !== false) {
1198 $orphanedpage=false;
1201 #echo "rc({$row['id']})==>($id): $check2 <br />";
1205 /// Some more reasons for Deletion...
1206 if ($orphanedpage && $id!=EWIKI_PAGE_INDEX &&!($flags & EWIKI_DB_F_BINARY)) {
1207 @$selected[$id] .= get_string("orphanedpage","wiki")."<br />";
1210 if ($flags & EWIKI_DB_F_DISABLED) {
1211 @$selected[$id] .= get_string("disabledpage","wiki")."<br />";
1214 if (($flags & 3) == 3) {
1215 @$selected[$id] .= get_string("errorbinandtxt","wiki")."<br />";
1218 if (!($flags & 3)) {
1219 @$selected[$id] .= get_string("errornotype","wiki")."<br />";
1222 if ($flags & EWIKI_DB_F_HTML) {
1223 @$selected[$id] .= get_string("errorhtml","wiki")."<br />";
1226 if (($flags & EWIKI_DB_F_READONLY) && !($flags & EWIKI_DB_F_BINARY)) {
1227 @$selected[$id] .= get_string("readonly","wiki")."<br />";
1230 if (($flags & EWIKI_DB_F_READONLY) && ($flags & EWIKI_DB_F_WRITEABLE)) {
1231 @$selected[$id] .= get_string("errorroandwr","wiki")."<br />";
1234 if (strlen($page["content"]) >= 65536) {
1235 @$selected[$id] .= get_string("errorsize","wiki")."<br />";
1238 if (strpos($page["refs"], "\n".get_string("deletemewikiword","wiki")."\n")!==false) {
1239 @$selected[$id] .= get_string("deletemewikiwordfound","wiki",get_string("deletemewikiword","wiki"))."<br />";
1243 foreach ($selected as $id => $reason) {
1244 $table_checkbox='<INPUT TYPE="checkbox" VALUE="'.rawurlencode($id).'" NAME="pagestodelete[]" />';
1246 #-- link & id
1247 if (strpos($id, EWIKI_IDF_INTERNAL) === false) {
1248 $table_page='<A HREF="' . ewiki_script("", $id) . '">';
1249 } else {
1250 $table_page='<A HREF="' . ewiki_script_binary("", $id) . '">';
1252 $table_page .= htmlentities($id) . '</A>';
1254 #-- print reason
1255 $table_reason=$reason;
1257 $table->data[]=array($table_checkbox, $table_page, $table_reason);
1260 return $table;
1263 /// This function actually removes the pages
1264 function wiki_admin_remove($pagestodelete, $course, $wiki, $userid, $groupid) {
1265 $ret="";
1266 foreach ($pagestodelete as $id) {
1268 $id = rawurldecode($id);
1270 $data = ewiki_database("GET", array("id"=>$id));
1271 for ($version=1; $version<=$data["version"]; $version++) {
1272 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1273 if($data["flags"] & EWIKI_DB_F_BINARY) {
1274 $filepath=moodle_binary_get_path($id, $data["meta"], $course, $wiki, $userid, $groupid);
1275 @unlink("$filepath");
1280 return $ret;
1283 function wiki_admin_strip_list($pagestostrip="",$version="",$err="") {
1284 /// Table header
1285 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("deleteversions","wiki"));
1287 $vc=ewiki_database("COUNTVERSIONS", array());
1288 $result = ewiki_database("GETALL",array());
1289 $i=0;
1290 while ($row = $result->get()) {
1291 $id = $row["id"];
1292 if($vc[$id]>1) {
1293 $error="";
1294 if($err[$id]) {
1295 $error=" ".join(", ",$err[$id]);
1297 $checked="";
1298 if($pagestostrip=="" || $pagestostrip[$i]) {
1299 $checked=" CHECKED";
1301 if($version=="") {
1302 $versiondefault="1-".($row["version"]-1);
1303 } else {
1304 $versiondefault=$version[$i];
1306 $table->data[]=array('<input type="checkbox" value="'.rawurlencode($id).'" name="pagestostrip['.$i.']" '.$checked.' />',
1307 '<A HREF="'.EWIKI_SCRIPT.$id.'">'.htmlentities($id).'</A> / '.get_string("version","wiki").": ".$row["version"],
1308 '<input name="version['.$i.']" value="'.$versiondefault.'" size="7" />'.$error);
1311 $i++;
1313 return $table;
1316 function wiki_admin_strip_versions($pagestostrip, $version, &$err) {
1317 $ret=array();
1318 foreach ($pagestostrip as $key => $id_ue) {
1320 $id = rawurldecode($id_ue);
1321 if (preg_match('/^(\d+)[-\s._:]+(\d+)$/', trim($version[$key]), $uu)) {
1322 $versA = $uu[1];
1323 $versZ = $uu[2];
1325 // Let the last Version in the database
1326 $checkdata = ewiki_database("GET", array("id" => $id));
1327 if($versZ>=$checkdata["version"]) {
1328 $err[$id][] = get_string("versionrangetoobig","wiki");
1329 } else {
1330 if($versA<=$versZ) {
1331 for ($v=$versA; $v<=$versZ; $v++) {
1332 $ret[$id][]=$v;
1334 } else {
1335 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1339 else {
1340 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1343 return $ret;
1346 function wiki_admin_strip($pagestostrip) {
1347 /// Purges old page-versions
1348 foreach($pagestostrip as $id => $versions) {
1349 foreach($versions as $version) {
1350 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1355 function wiki_admin_checklinks_list() {
1356 $ret=array();
1357 $result = ewiki_database("GETALL",array());
1358 while ($row = $result->get()) {
1359 if(!($row["flags"] & EWIKI_DB_F_BINARY)) {
1360 $index=htmlentities($row["id"]);
1361 $ret[$index] = $row["id"];
1364 return $ret;
1367 function wiki_admin_checklinks($pagetocheck) {
1368 /// Checks http:// Links
1369 $ret="";
1370 if($pagetocheck) {
1371 $get = ewiki_database("GET", array("id" => $pagetocheck));
1372 $content = $get["content"];
1374 preg_match_all('_(http.?://[^\s"\'<>#,;]+[^\s"\'<>#,;.])_', $content, $links);
1375 $badlinks = array();
1376 if(!$links[1]) {
1377 $ret = get_string("nolinksfound","wiki")."<br /><br />";
1378 } else {
1379 foreach ($links[1] as $href) {
1380 #print "[ $href ]";
1381 #$d = @implode("", @file($href));
1382 $d="";
1383 if($checkfd = @fopen($href, 'r')) {
1384 fclose($checkfd);
1385 $d="OK";
1387 if (empty($d) || !strlen(trim($d)) || stristr("not found", $d) || stristr("error 404", $d)) {
1388 $ret.="[".get_string("linkdead","wiki")."] $href <br />\n";
1389 $badlinks[] = $href;
1390 } else {
1391 $ret.="[".get_string("linkok","wiki")."] $href <br />\n";
1396 /// Remove old Notices
1397 $content = eregi_replace(' µµ__~\['.get_string("offline","wiki").'\]__µµ ','', $content);
1399 #-- replace dead links
1400 foreach ($badlinks as $href) {
1401 $content = preg_replace("\377^(.*)($href)\377m", '$1 µµ__~['.get_string("offline","wiki").']__µµ $2', $content);
1404 #-- compare against db content
1405 if ($content != $get["content"]) {
1406 $get["content"] = $content;
1407 $get["version"]++;
1408 $get["author"] = ewiki_author("ewiki_checklinks");
1409 $get["lastmodified"] = time();
1411 ewiki_database("WRITE", $get);
1414 return $ret;
1417 function wiki_admin_revert($proceed, $authorfieldpattern, $changesfield, $howtooperate, $deleteversions) {
1418 $ret="";
1419 #-- params
1420 $m_time = $changesfield * 3600;
1421 $depth = $deleteversions - 1;
1422 $depth = ($depth>0?$depth:0);
1424 #-- walk through
1425 $result = ewiki_database("GETALL", array("id", "author", "lastmodified"));
1426 while ($row = $result->get()) {
1427 $id = $row["id"];
1428 #-- which versions to check
1429 $verZ = $row["version"];
1430 if ($howtooperate=="lastonly") {
1431 $verA = $verZ;
1433 else {
1434 $verA = $verZ-$depth;
1435 if ($verA <= 0) {
1436 $verA = 1;
1440 for ($ver=$verA; $ver<=$verZ; $ver++) {
1441 #-- load current $ver database entry
1442 if ($verA != $verZ) {
1443 $row = ewiki_database("GET", array("id"=>$id, "version"=>$ver));
1446 #-- match
1447 if (stristr($row["author"], $authorfieldpattern) && ($row["lastmodified"] + $m_time > time())) {
1448 $ret .= "$id (".get_string("versionstodelete","wiki").": ";
1449 #-- delete multiple versions
1450 if ($howtooperate=="allsince") {
1451 while ($ver<=$verZ) {
1452 $ret .= " $ver";
1453 if ($proceed) {
1454 ewiki_database("DELETE", array("id"=>$id, "version"=>$ver));
1456 $ver++;
1459 #-- or just the affected one
1460 else {
1461 $ret .= " $ver";
1462 if ($proceed) {
1463 ewiki_database("DELETE", $row);
1466 $ret .= ")<br />";
1467 break;
1469 } #-- for($ver)
1470 } #-- while($row)
1471 return $ret;