Wiki module, copied from contrib/wiki.
[moodle.git] / mod / wiki / lib.php
CommitLineData
39fcb981 1<?PHP // $Id$
2
3/// Library of functions and constants for module wiki
4/// (replace wiki with the name of your module and delete this line)
5
6
7$wiki_CONSTANT = 7; /// for example
8$site = get_site();
9$WIKI_TYPES = array ('teacher' => $site->teacher,
10 'group' => get_string('groups',"wiki"),
11 'student' => $site->student );
12define("EWIKI_ESCAPE_AT", 0); # For the algebraic filter
13
14function 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.
19
20 $wiki->timemodified = time();
21
22 # May have to add extra stuff in here #
23
24 /// If specified, decode $wiki->initialcontent as the initial page name.
25 /// Make sure its in wiki CamelHumpForm.
26 if (!empty($wiki->initialcontent)) {
27 $wiki->pagename = '';
28 }
29 /// If specified, use $wiki->pagename as the initial page name.
30 /// Make sure its in wiki CamelHumpForm.
31 else if (!empty($wiki->pagename)) {
32 $wiki->pagename = wiki_wiki_name($wiki->pagename);
33 }
34 /// Use the wiki name for the initial page name.
35 else {
36 $wiki->pagename = wiki_wiki_name($wiki->name);
37 }
38 return insert_record("wiki", $wiki);
39}
40
41
42function wiki_update_instance($wiki) {
43/// Given an object containing all the necessary data,
44/// (defined by the form in mod.html) this function
45/// will update an existing instance with new data.
46
47 /// If specified, decode $wiki->initialcontent as the initial page name.
48 /// Make sure its in wiki CamelHumpForm.
49 if (!empty($wiki->initialcontent)) {
50 $wiki->pagename = '';
51 }
52 /// If specified, use $wiki->pagename as the initial page name.
53 /// Make sure its in wiki CamelHumpForm.
54 else if (!empty($wiki->pagename)) {
55 $wiki->pagename = wiki_wiki_name($wiki->pagename);
56 }
57 /// Use the wiki name for the initial page name.
58 else {
59 $wiki->pagename = wiki_wiki_name($wiki->name);
60 }
61
62 $wiki->timemodified = time();
63 $wiki->id = $wiki->instance;
64 return update_record("wiki", $wiki);
65}
66
67/// Delete all Directories recursively
68function wiki_rmdir($basedir) {
69 $handle = @opendir($basedir);
70 if($handle) {
71 while (false!==($folder = readdir($handle))) {
72 if($folder != "." && $folder != ".." && $Folder != "CVS") {
73 wiki_rmdir("$basedir/$folder"); // recursive
74 }
75 }
76 closedir($handle);
77 }
78 @rmdir($basedir);
79}
80
81function wiki_delete_instance($id) {
82/// Given an ID of an instance of this module,
83/// this function will permanently delete the instance
84/// and any data that depends on it.
85 global $CFG;
86
87 if (! $wiki = get_record("wiki", "id", $id)) {
88 return false;
89 }
90
91 $result = true;
92
93 #Delete Files
94### Should probably check regardless of this setting in case its been changed...
95 if($wiki->ewikiacceptbinary) {
96 if ($basedir = $CFG->dataroot."/".$wiki->course."/".$CFG->moddata."/wiki/$id") {
97 if ($files = get_directory_list($basedir)) {
98 foreach ($files as $file) {
99 #if ($file != $exception) {
100 unlink("$basedir/$file");
101 notify("Existing file '$file' has been deleted!");
102 #}
103 }
104 }
105 #if (!$exception) { // Delete directory as well, if empty
106 wiki_rmdir("$basedir");
107 #}
108 }
109 }
110
111 # Delete any dependent records here #
112 if (! delete_records("wiki", "id", $wiki->id)) {
113 $result = false;
114 }
115
116 /// Delete all wiki_entries and wiki_pages.
117 if (($wiki_entries = wiki_get_entries($wiki)) !== false) {
118 foreach ($wiki_entries as $wiki_entry) {
119 if (! delete_records("wiki_pages", "wiki", "$wiki_entry->id")) {
120 $result = false;
121 }
122 if (! delete_records("wiki_entries", "id", "$wiki_entry->id")) {
123 $result = false;
124 }
125 }
126 }
127
128 return $result;
129}
130
131function wiki_user_outline($course, $user, $mod, $wiki) {
132/// Return a small object with summary information about what a
133/// user has done with a given particular instance of this module
134/// Used for user activity reports.
135/// $return->time = the time they did it
136/// $return->info = a short text description
137
138 return $return;
139}
140
141function wiki_user_complete($course, $user, $mod, $wiki) {
142/// Print a detailed representation of what a user has done with
143/// a given particular instance of this module, for user activity reports.
144
145 return true;
146}
147
148function wiki_print_recent_activity($course, $isteacher, $timestart) {
149/// Given a course and a time, this module should find recent activity
150/// that has occurred in wiki activities and print it out.
151/// Return true if there was output, or false is there was none.
152
153 global $CFG;
154
155 return false; // True if anything was printed, otherwise false
156}
157
158function wiki_cron () {
159/// Function to be run periodically according to the moodle cron
160/// This function searches for things that need to be done, such
161/// as sending out mail, toggling flags etc ...
162
163 global $CFG;
164
165 return true;
166}
167
168function wiki_grades($wikiid) {
169/// Must return an array of grades for a given instance of this module,
170/// indexed by user. It also returns a maximum allowed grade.
171
172 $return->grades = NULL;
173 $return->maxgrade = NULL;
174
175 return $return;
176}
177
178function wiki_get_participants($wikiid) {
179//Must return an array of user records (all data) who are participants
180//for a given instance of wiki. Must include every user involved
181//in the instance, independient of his role (student, teacher, admin...)
182//See other modules as example.
183
184 return false;
185}
186
187//////////////////////////////////////////////////////////////////////////////////////
188/// Any other wiki functions go here. Each of them must have a name that
189/// starts with wiki_
190
191function wiki_wiki_name($wikiname) {
192/// Return the passed in string in Wiki name format.
193/// Remove any leading and trailing whitespace, capitalize all the words
194/// and then remove any internal whitespace.
195
196 if (wiki_is_wiki_name($wikiname)) {
197 return $wikiname;
198 }
199 else {
200 /// Create uppercase words and remove whitespace.
201 $wikiname = preg_replace("/(\w+)\s/", "$1", ucwords(trim($wikiname)));
202
203 /// Check again - there may only be one word.
204 if (wiki_is_wiki_name($wikiname)) {
205 return $wikiname;
206 }
207 /// If there is only one word, append default wiki name to it.
208 else {
209 return $wikiname.get_string('wikidefaultpagename', 'wiki');
210 }
211 }
212}
213
214function wiki_is_wiki_name($wikiname) {
215/// Check for correct wikiname syntax and return true or false.
216
217 /// If there are spaces between the words, incorrect format.
218 if (preg_match_all('/\w+/', $wikiname, $out) > 1) {
219 return false;
220 }
221 /// If there isn't more than one group of uppercase letters separated by
222 /// lowercase letters or '_', incorrect format.
223 else if (preg_match_all('/[A-Z]+[a-z_]+/', $wikiname, $out) > 1) {
224 return true;
225 }
226 else {
227 return false;
228 }
229}
230
231function wiki_page_name(&$wiki) {
232/// Determines the wiki's page name and returns it.
233 if (!empty($wiki->initialcontent)) {
234 $ppos = strrpos($wiki->initialcontent, '/');
235 if ($ppos === false) {
236 $pagename = $wiki->initialcontent;
237 }
238 else {
239 $pagename = substr($wiki->initialcontent, $ppos+1);
240 }
241 }
242 else if (!empty($wiki->pagename)) {
243 $pagename = $wiki->pagename;
244 }
245 else {
246 $pagename = wiki_wiki_name($wiki->name);
247 }
248 return $pagename;
249}
250
251function wiki_content_dir(&$wiki) {
252/// Determines the wiki's default content directory (if there is one).
253 global $CFG;
254
255 if (!empty($wiki->initialcontent)) {
256 $ppos = strrpos($wiki->initialcontent, '/');
257 if ($ppos === false) {
258 $subdir = '';
259 }
260 else {
261 $subdir = substr($wiki->initialcontent, 0, $ppos+1);
262 }
263 $contentdir = $CFG->dataroot.'/'.$wiki->course.'/'.$subdir;
264 }
265 else {
266 $contentdir = false;
267 }
268 return $contentdir;
269}
270
271function wiki_has_entries(&$wiki) {
272/// Returns true if wiki already has wiki entries; otherwise false.
273
274 return record_exists('wiki_entries', 'wikiid', $wiki->id);
275}
276
277function wiki_get_entries(&$wiki, $byindex=NULL) {
278/// Returns an array with all wiki entries indexed by entry id; false if there are none.
279/// If the optional $byindex is specified, returns the entries indexed by that field.
280/// Valid values for $byindex are 'student', 'group'.
281
282 if ($byindex == 'student') {
283 return get_records('wiki_entries', 'wikiid', $wiki->id, '',
284 'userid,id,wikiid,course,groupid,pagename,timemodified');
285 }
286 else if ($byindex == 'group') {
287 return get_records('wiki_entries', 'wikiid', $wiki->id, '',
288 'groupid,id,wikiid,course,userid,pagename,timemodified');
289 }
290 else {
291 return get_records('wiki_entries', 'wikiid', $wiki->id);
292 }
293}
294
295function wiki_get_entry(&$wiki, &$course, $userid=0, $groupid=0) {
296/// Returns the wiki entry according to the wiki type.
297/// Optionally, will return wiki entry for $userid student wiki, or
298/// $groupid group or teacher wiki.
299 global $USER;
300
301 switch ($wiki->wtype) {
302 case 'student':
303 /// If a specific user was requested, return it, if allowed.
304 if ($userid and wiki_user_can_access_student_wiki($wiki, $userid, $course)) {
305 $wentry = wiki_get_student_entry($wiki, $userid);
306 }
307
308 /// If there is no entry for this user, check if this user is a teacher.
309 else if (!$wentry = wiki_get_student_entry($wiki, $USER->id)) {
310/* if (isteacher($course->id, $USER->id)) {
311 /// If this user is a teacher, return the first entry.
312 if ($wentries = wiki_get_entries($wiki)) {
313 $wentry = current($wentries);
314 }
315 }*/
316 }
317 break;
318
319 case 'group':
320 /// If there is a groupmode, get the user's group id.
321 $groupmode = groupmode($course, $wiki);
322
323 /// If a specific group was requested, return it, if allowed.
324 if ($groupid and wiki_user_can_access_group_wiki($wiki, $groupid, $course)) {
325 $wentry = wiki_get_group_entry($wiki, $groupid);
326 }
327 else if ($groupmode) {
328 /// If there is no entry for this user, check if this user is a teacher.
329 if (!$wentry = wiki_get_group_entry($wiki, mygroupid($course->id))) {
330/* if (isteacher($course->id, $USER->id)) {
331 /// If this user is a teacher, return the first entry.
332 if ($wentries = wiki_get_entries($wiki)) {
333 $wentry = current($wentries);
334 }
335 } */
336 }
337 }
338 /// If mode is 'nogroups', then groupid is zero.
339 else {
340 $wentry = wiki_get_group_entry($wiki, 0);
341 }
342 break;
343
344 case 'teacher':
345 /// If there is a groupmode, get the user's group id.
346 if (groupmode($course, $wiki)) {
347 $groupid = $groupid ? $groupid : mygroupid($course->id);
348 }
349
350 /// If a specific group was requested, return it, if allowed.
351 if (wiki_user_can_access_teacher_wiki($wiki, $groupid, $course)) {
352 $wentry = wiki_get_teacher_entry($wiki, $groupid);
353 }
354 break;
355 }
356 return $wentry;
357}
358
359function wiki_get_teacher_entry(&$wiki, $groupid=0) {
360/// Returns the wiki entry for the wiki teacher type.
361 return get_record('wiki_entries', 'wikiid', $wiki->id, 'course', $wiki->course, 'groupid', $groupid);
362}
363
364function wiki_get_group_entry(&$wiki, $groupid=null) {
365/// Returns the wiki entry for the given group.
366 return get_record('wiki_entries', 'wikiid', $wiki->id, 'groupid', $groupid);
367}
368
369function wiki_get_student_entry(&$wiki, $userid=null) {
370/// Returns the wiki entry for the given student.
371 global $USER;
372
373 if (is_null($userid)) {
374 $userid = $USER->id;
375 }
376 return get_record('wiki_entries', 'wikiid', $wiki->id, 'userid', $userid);
377}
378
379function wiki_get_other_wikis(&$wiki, &$user, &$course, $currentid=0) {
380 /// Returns a list of other wikis to display, depending on the type, group and user.
381 /// Returns the key containing the currently selected entry as well.
382
383 global $CFG, $ME, $id;
384
385 $wikis = false;
386
387 $groupmode = groupmode($course, $wiki);
388 $mygroupid = mygroupid($course->id);
389 $isteacher = isteacher($course->id, $user->id);
390 $isteacheredit = isteacheredit($course->id, $user->id);
391 $site = get_site();
392
393 switch ($wiki->wtype) {
394
395 case 'student':
396 /// Get all the existing entries for this wiki.
397 $wiki_entries = wiki_get_entries($wiki, 'student');
398 if ($isteacher and ($site->id != $course->id)) {
399
400 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all student
401 /// wikis, regardless of creation.
402 if (($site->id != $course->id) and ($isteacheredit or ($groupmode == NOGROUPS))) {
403
404 if ($students = get_course_students($course->id)) {
405 /// Default pagename is dependent on the wiki settings.
406 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
407
408 foreach ($students as $student) {
409
410 /// If this student already has an entry, use its pagename.
411 if ($wiki_entries[$student->id]) {
412 $pagename = $wiki_entries[$student->id]->pagename;
413 }
414 else {
415 $pagename = $defpagename;
416 }
417
418 $key = $ME.'?id='.$id.'&userid='.$student->id.'&wikipage='.$pagename;
419 $wikis[$key] = fullname($student).':'.$pagename;
420 }
421 }
422 }
423 else if ($groupmode == SEPARATEGROUPS) {
424 if ($students = get_group_students($mygroupid)) {
425 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
426 foreach ($students as $student) {
427
428 /// If this student already has an entry, use its pagename.
429 if ($wiki_entries[$student->id]) {
430 $pagename = $wiki_entries[$student->id]->pagename;
431 }
432 else {
433 $pagename = $defpagename;
434 }
435
436 $key = $ME.'?id='.$id.'&userid='.$student->id.'&wikipage='.$pagename;
437 $wikis[$key] = fullname($student).':'.$pagename;
438 }
439 }
440 }
441 else if ($groupmode == VISIBLEGROUPS) {
442 /// Get all students in your group.
443 if ($students = get_group_students($mygroupid)) {
444 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
445 foreach ($students as $student) {
446 /// If this student already has an entry, use its pagename.
447 if ($wiki_entries[$student->id]) {
448 $pagename = $wiki_entries[$student->id]->pagename;
449 }
450 else {
451 $pagename = $defpagename;
452 }
453 $key = $ME.'?id='.$id.'&userid='.$student->id.'&wikipage='.$pagename;
454 $wikis[$key] = fullname($student).':'.$pagename;
455 }
456 }
457 /// Get all student wikis created, regardless of group.
458 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
459 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'user u '
460 .' WHERE w.wikiid = '.$wiki->id.' AND u.id = w.userid '
461 .' ORDER BY w.id';
462 $wiki_entries = get_records_sql($sql);
463 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
464 foreach ($wiki_entries as $wiki_entry) {
465 $key = $ME.'?id='.$id.'&userid='.$wiki_entry->userid.'&wikipage='.$wiki_entry->pagename;
466 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
467 if ($currentid == $wiki_entry->id) {
468 $wikis['selected'] = $key;
469 }
470 }
471 }
472 }
473 else {
474 /// A user can see other student wikis if they are a member of the same
475 /// group (for separate groups) or there are visible groups, or if this is
476 /// a site-level wiki, and they are an administrator.
477 if (($groupmode == VISIBLEGROUPS) or
478 (($site->id == $course->id) and isadmin())) {
479 $viewall = true;
480 }
481 else if ($groupmode == SEPARATEGROUPS) {
482 $viewall = mygroupid($course->id);
483 }
484 else {
485 $viewall = false;
486 }
487
488 if ($viewall !== false) {
489 $sql = 'SELECT w.id, w.userid, w.pagename, u.firstname, u.lastname '
490 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'user u '
491 .' WHERE w.wikiid = '.$wiki->id.' AND u.id = w.userid '
492 .' ORDER BY w.id';
493 $wiki_entries = get_records_sql($sql);
494 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
495 foreach ($wiki_entries as $wiki_entry) {
496 if (($viewall === true) or ismember($viewall, $wiki_entry->userid)) {
497 $key = $ME.'?id='.$id.'&userid='.$wiki_entry->userid.'&wikipage='.$wiki_entry->pagename;
498 $wikis[$key] = fullname($wiki_entry).':'.$wiki_entry->pagename;
499 if ($currentid == $wiki_entry->id) {
500 $wikis['selected'] = $key;
501 }
502 }
503 }
504 }
505 }
506 break;
507
508 case 'group':
509 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all group
510 /// wikis, regardless of creation.
511
512 /// Get all the existing entries for this wiki.
513 $wiki_entries = wiki_get_entries($wiki, 'group');
514
515 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
516 if ($groups = get_groups($course->id)) {
517 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
518 foreach ($groups as $group) {
519
520 /// If this group already has an entry, use its pagename.
521 if (isset($wiki_entries[$group->id])) {
522 $pagename = $wiki_entries[$group->id]->pagename;
523 }
524 else {
525 $pagename = $defpagename;
526 }
527
528 $key = $ME.'?id='.$id.'&groupid='.$group->id.'&wikipage='.$pagename;
529 $wikis[$key] = $group->name.':'.$pagename;
530 }
531 }
532 }
533 /// A user can see other group wikis if there are visible groups.
534 else if ($groupmode == VISIBLEGROUPS) {
535 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
536 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
537 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
538 .' ORDER BY w.groupid';
539 $wiki_entries = get_records_sql($sql);
540 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
541 foreach ($wiki_entries as $wiki_entry) {
542 $key = $ME.'?id='.$id.'&groupid='.$wiki_entry->groupid.'&wikipage='.$wiki_entry->pagename;
543 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
544 if ($currentid == $wiki_entry->id) {
545 $wikis['selected'] = $key;
546 }
547 }
548 }
549 break;
550
551 case 'teacher':
552 if ($isteacher) {
553 /// If the user is an editing teacher, or a non-editing teacher not assigned to a group, show all
554 /// teacher wikis, regardless of creation.
555 if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
556 if ($groups = get_groups($course->id)) {
557 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
558
559 foreach ($groups as $group) {
560 /// If this group already has an entry, use its pagename.
561 if ($wiki_entries[$group->id]) {
562 $pagename = $wiki_entries[$group->id]->pagename;
563 }
564 else {
565 $pagename = $defpagename;
566 }
567
568 $key = $ME.'?id='.$id.'&groupid='.$group->id.'&wikipage='.$pagename;
569 $wikis[$key] = $group->name.':'.$pagename;
570 }
571 }
572 }
573 /// A teacher can see all other group teacher wikis.
574 else if ($groupmode) {
575 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
576 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
577 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
578 .' ORDER BY w.groupid';
579 $wiki_entries = get_records_sql($sql);
580 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
581 foreach ($wiki_entries as $wiki_entry) {
582 $key = $ME.'?id='.$id.'&groupid='.$wiki_entry->groupid.'&wikipage='.$wiki_entry->pagename;
583 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
584 if ($currentid == $wiki_entry->id) {
585 $wikis['selected'] = $key;
586 }
587 }
588 }
589 }
590 else {
591 /// A user can see other teacher wikis if they are a teacher, a member of the same
592 /// group (for separate groups) or there are visible groups.
593 if ($groupmode == VISIBLEGROUPS) {
594 $viewall = true;
595 }
596 else if ($groupmode == SEPARATEGROUPS) {
597 $viewall = $mygroupid;
598 }
599 else {
600 $viewall = false;
601 }
602 if ($viewall !== false) {
603 $sql = 'SELECT w.id, w.groupid, w.pagename, g.name as gname '
604 .' FROM '.$CFG->prefix.'wiki_entries w, '.$CFG->prefix.'groups g '
605 .' WHERE w.wikiid = '.$wiki->id.' AND g.id = w.groupid '
606 .' ORDER BY w.groupid';
607 $wiki_entries = get_records_sql($sql);
608 $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
609 foreach ($wiki_entries as $wiki_entry) {
610 if (($viewall === true) or $viewall == $wiki_entry->groupid) {
611 $key = $ME.'?id='.$id.'&groupid='.$wiki_entry->groupid.'&wikipage='.$wiki_entry->pagename;
612 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
613 if ($currentid == $wiki_entry->id) {
614 $wikis['selected'] = $key;
615 }
616 }
617 }
618 }
619 }
620 break;
621 }
622
623 return $wikis;
624}
625
626function wiki_add_entry(&$wiki, &$course, $userid=0, $groupid=0) {
627/// Adds a new wiki entry of the specified type, unless already entered.
628/// No checking is done here. It is assumed that the caller has the correct
629/// privileges to add this entry.
630
631 global $USER;
632
633 /// If this wiki already has a wiki_type entry, return false.
634 if (wiki_get_entry($wiki, $course, $userid, $groupid) !== false) {
635 return false;
636 }
637
638 switch ($wiki->wtype) {
639
640 case 'student':
641 $wiki_entry->wikiid = $wiki->id;
642 $wiki_entry->userid = $userid ? $userid : $USER->id;
643 $wiki_entry->pagename = wiki_page_name($wiki);
644 $wiki_entry->timemodified = time();
645 break;
646
647 case 'group':
648 /// Get the groupmode. It's been added to the wiki object.
649 $groupmode = groupmode($course, $wiki);
650
651 /// If there is a groupmode, get the group id.
652 if ($groupmode) {
653 $groupid = $groupid ? $groupid : mygroupid($course->id);
654 }
655 /// If mode is 'nogroups', then groupid is zero.
656 else {
657 $groupid = 0;
658 }
659 $wiki_entry->wikiid = $wiki->id;
660 $wiki_entry->groupid = $groupid;
661 $wiki_entry->pagename = wiki_page_name($wiki);
662 $wiki_entry->timemodified = time();
663 break;
664
665 case 'teacher':
666 /// Get the groupmode. It's been added to the wiki object.
667 $groupmode = groupmode($course, $wiki);
668
669 /// If there is a groupmode, get the user's group id.
670 if ($groupmode and $groupid == 0) {
671 $groupid = mygroupid($course->id);
672 }
673
674 $wiki_entry->wikiid = $wiki->id;
675 $wiki_entry->course = $wiki->course;
676 $wiki_entry->groupid = $groupid;
677 $wiki_entry->pagename = wiki_page_name($wiki);
678 $wiki_entry->timemodified = time();
679 break;
680 }
681 return insert_record("wiki_entries", $wiki_entry, true);
682}
683
684function wiki_can_add_entry(&$wiki, &$user, &$course, $userid=0, $groupid=0) {
685/// Returns true or false if the user can add a wiki entry for this wiki.
686
687 /// Get the groupmode. It's been added to the wiki object.
688 $groupmode = groupmode($course, $wiki);
689 $mygroupid = mygroupid($course->id);
690 $site = get_site();
691
692 switch ($wiki->wtype) {
693
694 case 'student':
695/// A student can create their own wiki, if they are a member of that course.
696/// A user can create their own wiki at the site level.
697 if ($userid == 0) {
698 return (isstudent($course->id, $user->id) or
699 (($site->id == $course->id) and !empty($user) and !isguest()));
700 }
701/// An editing teacher can create any student wiki, or
702/// a non-editing teacher, if not assigned to a group can create any student wiki, or if assigned to a group can
703/// create any student wiki in their group.
704 else {
705 return ((($userid == $user->id) and isstudent($course->id, $user->id)) or isteacheredit($course->id) or
706 (isteacher($course->id) and (!$groupmode or $mygroupid == 0 or (ismember($mygroupid, $userid)))));
707 }
708 break;
709
710 case 'group':
711 /// If mode is 'nogroups', then all participants can add wikis.
712 if (!$groupmode) {
713 return (isstudent($course->id, $user->id) or isteacher($course->id, $user->id) or
714 (($site->id == $course->id) and !empty($user) and !isguest()));
715 }
716 /// If not requesting a group, must be a member of a group.
717 else if ($groupid == 0) {
718 return ($mygroupid != 0);
719 }
720 /// If requesting a group, must be an editing teacher, a non-editing teacher with no assigned group,
721 /// or a non-editing teacher requesting their group.
722 else {
723 return (isteacheredit($course->id) or
724 (isteacher($course->id) and ($mygroupid == 0 or $mygroupid == $groupid)));
725 }
726 break;
727
728 case 'teacher':
729 /// If mode is 'nogroups', then all teachers can add wikis.
730 if (!$groupmode) {
731 return (isteacher($course->id, $user->id) or (($site->id == $course->id) and isadmin()));
732 }
733 /// If not requesting a group, must be a member of a group.
734 else if ($groupid == 0) {
735 return ($mygroupid != 0 and isteacher($course->id));
736 }
737 /// If there is a group mode, non-editing teachers with an assigned group, can only create wikis
738 /// in their group. Non-editing teachers with no assigned group and editing teachers can create any wiki.
739 else {
740 return (isteacheredit($course->id) or
741 (isteacher($course->id) and ($mygroupid == 0 or $mygroupid == $groupid)));
742 }
743 break;
744 }
745
746 return false;
747}
748
749function wiki_can_edit_entry(&$wiki_entry, &$wiki, &$user, &$course) {
750/// Returns true or false if the user can edit this wiki entry.
751
752 $can_edit = false;
753 $groupmode = groupmode($course, $wiki);
754 $mygroupid = mygroupid($course->id);
755 $site = get_site();
756
757 /// Editing teacher's and admins can edit all wikis, non-editing teachers can edit wikis in their groups,
758 /// or all wikis if group mode is 'no groups' or they don't belong to a group.
759 if (isadmin() or isteacheredit($course->id, $user->id) or
760 ((!$groupmode or $mygroupid == 0) and isteacher($course->id, $user->id))) {
761 $can_edit = true;
762 }
763 else {
764 switch ($wiki->wtype) {
765
766 /// Only a teacher or the owner of a student wiki can edit it.
767 case 'student':
768 $can_edit = (($user->id == $wiki_entry->userid) or
769 ($groupmode and isteacher($course->id, $user->id) and
770 ismember($mygroupid, $wiki_entry->userid)));
771 break;
772
773 case 'group':
774 /// If there is a groupmode, determine the user's group status.
775 if ($groupmode) {
776 /// If the user is a member of the wiki group, they can edit the wiki.
777 $can_edit = ismember($wiki_entry->groupid, $user->id);
778 }
779 /// If mode is 'nogroups', then all participants can edit the wiki.
780 else {
781 $can_edit = (isstudent($course->id, $user->id) or isteacher($course->id, $user->id) or
782 (($site->id == $course->id) and !empty($user) and !isguest()));
783 }
784 break;
785
786 case 'teacher':
787 /// If there is a groupmode, determine the user's group status.
788 if ($groupmode) {
789 /// If the user is a member of the wiki group, they can edit the wiki.
790 $can_edit = (isteacher($course->id, $user->id) and ismember($wiki_entry->groupid, $user->id));
791 }
792 else {
793 $can_edit = (isteacher($course->id, $user->id) or (($site->id == $course->id) and isadmin()));
794 }
795 break;
796 }
797 }
798
799 return $can_edit;
800}
801
802function wiki_user_can_access_student_wiki(&$wiki, $userid, &$course) {
803 global $USER;
804
805 /// Get the groupmode. It's been added to the wiki object.
806 $groupmode = groupmode($course, $wiki);
807 $usersgroup = mygroupid($course->id);
808 $isteacher = isteacher($course->id, $USER->id);
809
810 /// If this user is allowed to access this wiki then return TRUE.
811 /// *** THIS COULD BE A PROBLEM, IF STUDENTS COULD EVER BE PART OF MORE THAN ONE GROUP ***
812 /// A user can access a student wiki, if:
813 /// - it is their wiki,
814 /// - group mode is VISIBLEGROUPS,
815 /// - group mode is SEPARATEGROUPS, and the user is a member of the requested user's group,
816 /// - they are an editing teacher or administrator,
817 /// - they are a non-editing teacher not assigned to a specific group,
818 /// - they are a non-editing teacher and group mode is NOGROUPS.
819 /// - they are an administrator (mostly for site-level wikis).
820 if (($userid and ($USER->id == $userid)) or ($groupmode == VISIBLEGROUPS) or
821 (($groupmode == SEPARATEGROUPS) and ismember($usersgroup, $userid)) or
822 (isteacheredit($course->id, $USER->id)) or
823 (isteacher($course->id, $USER->id) and (!$usersgroup or ($groupmode == NOGROUPS))) or
824 (isadmin())) {
825 $can_access = true;
826 }
827 else {
828 $can_access = false;
829 }
830 return $can_access;
831}
832
833function wiki_user_can_access_group_wiki(&$wiki, $groupid, &$course) {
834 global $USER;
835
836 /// Get the groupmode. It's been added to the wiki object.
837 $groupmode = groupmode($course, $wiki);
838 $usersgroup = mygroupid($course->id);
839 $isteacher = isteacher($course->id, $USER->id);
840
841 /// A user can access a group wiki, if:
842 /// - group mode is NOGROUPS,
843 /// - group mode is VISIBLEGROUPS,
844 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
845 /// - they are an editing teacher or administrator,
846 /// - they are a non-editing teacher not assigned to a specific group.
847 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
848 (($groupmode == SEPARATEGROUPS) and ($usersgroup == $groupid)) or
849 (isteacheredit($course->id, $USER->id)) or
850 (isteacher($course->id, $USER->id) and !$usersgroup)) {
851 $can_access = true;
852 }
853 else {
854 $can_access = false;
855 }
856 return $can_access;
857}
858
859function wiki_user_can_access_teacher_wiki(&$wiki, $groupid, &$course) {
860 global $USER;
861
862 /// Get the groupmode. It's been added to the wiki object.
863 $groupmode = groupmode($course, $wiki);
864
865 /// A user can access a teacher wiki, if:
866 /// - group mode is NOGROUPS,
867 /// - group mode is VISIBLEGROUPS,
868 /// - group mode is SEPARATEGROUPS, and they are a member of the requested group,
869 /// - they are a teacher or administrator,
870 if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
871 (($groupmode == SEPARATEGROUPS) and (mygroupid($course->id) == $groupid)) or
872 (isteacher($course->id, $USER->id))){
873 $can_access = true;
874 }
875 else {
876 $can_access = false;
877 }
878 return $can_access;
879}
880
881function wiki_get_owner(&$wiki_entry) {
882 if ($wiki_entry->userid > 0) {
883 $user = get_record('user', 'id', $wiki_entry->userid);
884 $owner = fullname($user);
885 }
886 else if ($wiki_entry->groupid > 0) {
887 $group = get_record('groups', 'id', $wiki_entry->groupid);
888 $owner = $group->name;
889 }
890 else if ($wiki_entry->course > 0) {
891 $course = get_record('course', 'id', $wiki_entry->course);
892 $owner = $course->shortname;
893 }
894 else {
895 $owner = '- unknown -';
896 }
897 return $owner;
898}
899
900function wiki_print_search_form($cmid, $search="", $userid, $groupid, $return=false) {
901 global $CFG;
902 # TODO: Add Group and User !!!
903 $output = "<form name=\"search\" action=\"$CFG->wwwroot/mod/wiki/view.php\">";
904 $output .= "<font size=\"-1\">".get_string("search").': ';
905 $output .= "<input name=\"id\" type=\"hidden\" value=\"$cmid\">";
906 $output = $output.($groupid?"<input name=\"groupid\" type=\"hidden\" value=\"$groupid\">":"");
907 $output = $output.($userid?"<input name=\"userid\" type=\"hidden\" value=\"$userid\">":"");
908 $output .= "<input name=\"q\" type=\"text\" size=\"20\" value=\"$search\">".' ';
909 $output .= "<input value=\"".get_string("searchwiki", "wiki")."\" type=\"submit\">";
910 $output .= "</font>";
911 $output .= "<input name=\"wikipage\" type=\"hidden\" value=\"SearchPages\">";
912 $output .= "</form>";
913
914 if ($return) {
915 return $output;
916 }
917 echo $output;
918}
919
920function wiki_print_wikilinks_block($cmid, $binary=false, $return=false) {
921/// Prints a lin-list of special wiki-pages
922 global $CFG, $ewiki_title;
923
924 $links=array();
925
926 $links["PageIndex"]=get_string("pageindex", "wiki");
927 $links["NewestPages"]=get_string("newestpages", "wiki");
928 $links["MostVisitedPages"]=get_string("mostvisitedpages", "wiki");
929 $links["MostOftenChangedPages"]=get_string("mostoftenchangedpages", "wiki");
930 $links["UpdatedPages"]=get_string("updatedpages", "wiki");
931 $links["OrphanedPages"]=get_string("orphanedpages", "wiki");
932 $links["WantedPages"]=get_string("wantedpages", "wiki");
933 if($binary) {
934 $links["FileDownload"]=get_string("filedownload", "wiki");
935 }
936 popup_form(EWIKI_SCRIPT, $links, "wikilinks", "", get_string("choosewikilinks", "wiki"), "", "", $return);
937}
938
939function wiki_print_page_actions($cmid, $specialpages, $wikipage, $action, $binary=false, $canedit=true) {
940/// Displays actions which can be performed on the page
941
942 $page=array();
943
944 // Edit this Page
945 if (in_array($action, array("edit", "links", "info", "attachments"))) {
946 $page["view/$wikipage"]=get_string("viewpage","wiki");
947 }
948 if ($canedit && !in_array($wikipage, $specialpages) && $action != "edit") {
949 $page["edit/$wikipage"]=get_string("editthispage","wiki");
950 }
951 if ($action != "links") {
952 $page["links/$wikipage"]=get_string("backlinks","wiki");
953 }
954 if ($canedit && !in_array($wikipage, $specialpages) && $action!="info") {
955 $page["info/$wikipage"]=get_string("pageinfo","wiki");
956 }
957 if($canedit && $binary && !in_array($wikipage, $specialpages) && $action != "attachments") {
958 $page["attachments/$wikipage"]=get_string("attachments","wiki");
959 }
960
961 popup_form(EWIKI_SCRIPT, $page, "wikiactions", "", get_string("action", "wiki"), "", "", false);
962}
963
964function wiki_print_administration_actions($cmid, $userid, $groupid, $wikipage, $noeditor) {
965/// Displays actions which can be performed on the page
966 global $ME;
967
968
969 /// Create the URL
970 $ewscript = 'admin.php?id='.$cmid;
971 if (isset($userid)) $ewscript .= '&userid='.$userid;
972 if (isset($groupid)) $ewscript .= '&groupid='.$groupid;
973 if (isset($wikipage)) $ewscript .= '&wikipage='.$wikipage;
974 $ewscript.="&action=";
975
976
977 $action=array(
978 "removepages" => get_string("removepages", "wiki"),
979 "strippages" => get_string("strippages", "wiki"),
980 "setpageflags" => get_string("setpageflags", "wiki"),
981 "revertpages" => get_string("revertpages", "wiki"),
982 );
983 // We cannot do certain things if html is used !
984 if($noeditor) {
985 $action["checklinks"]=get_string("checklinks", "wiki");
986 }
987 popup_form($ewscript, $action, "wikiadministration", "", get_string("chooseadministration", "wiki"), "", "", false);
988}
989
990function wiki_admin_get_flagarray() {
991 $ret = array(
992 EWIKI_DB_F_TEXT => get_string("flagtxt","wiki"),
993 EWIKI_DB_F_BINARY => get_string("flagbin","wiki"),
994 EWIKI_DB_F_DISABLED => get_string("flagoff","wiki"),
995 EWIKI_DB_F_HTML => get_string("flaghtm","wiki"),
996 EWIKI_DB_F_READONLY => get_string("flagro","wiki"),
997 EWIKI_DB_F_WRITEABLE => get_string("flagwr","wiki"),
998 );
999
1000 return $ret;
1001}
1002
1003///////// Ewiki Administration. Mostly taken from the ewiki/tools folder and changed
1004function wiki_admin_setpageflags_list($pageflagstatus) {
1005 $FD = wiki_admin_get_flagarray();
1006 $table->head = array(get_string("pagename","wiki"), get_string("flags","wiki"));
1007 if($pageflagstatus) {
1008 $table->head[]=get_string("status","wiki");
1009 }
1010
1011 $result = ewiki_database("GETALL", array("version", "flags"));
1012 while ($row = $result->get()) {
1013 $id = $row["id"];
1014 $data = ewiki_database("GET", $row);
1015
1016 $cell_pagename="";
1017 $cell_flags="";
1018 if ($data["flags"] & EWIKI_DB_F_TEXT) {
1019 $cell_pagename .= '<A HREF="' . EWIKI_SCRIPT . $id . '">';
1020 } else {
1021 $cell_pagename .= '<A HREF="' . EWIKI_SCRIPT_BINARY . $id . '">';
1022 }
1023 $cell_pagename .= htmlentities($id) . '</A> / '.get_string("version","wiki").": ".$row["version"];
1024
1025 foreach ($FD as $n=>$str) {
1026 $cell_flags .='<INPUT TYPE="checkbox" NAME="flags['. rawurlencode($id)
1027 . '][' . $n . ']" VALUE="1" '
1028 . (($data["flags"] & $n) ? "CHECKED" : "")
1029 . '>'.$str. ' ';
1030 }
1031 if($pageflagstatus) {
1032 $table->data[]=array($cell_pagename, $cell_flags, $pageflagstatus[$id]);
1033 } else {
1034 $table->data[]=array($cell_pagename, $cell_flags);
1035 }
1036 }
1037 return $table;
1038}
1039
1040function wiki_admin_setpageflags($pageflags) {
1041 $FD = wiki_admin_get_flagarray();
1042
1043 $status=array();
1044 if($pageflags) {
1045 foreach($pageflags as $page=>$fa) {
1046
1047 $page = rawurldecode($page);
1048
1049 $flags = 0;
1050 $fstr = "";
1051 foreach($fa as $num=>$isset) {
1052 if ($isset) {
1053 $flags += $num;
1054 $fstr .= ($fstr?",":""). $FD[$num];
1055 }
1056 }
1057
1058 #$status[$page] .= "{$flags}=[{$fstr}]";
1059
1060 $data = ewiki_database("GET", array("id" => $page));
1061
1062 if ($data["flags"] != $flags) {
1063 $data["flags"] = $flags;
1064 $data["author"] = "ewiki-tools, " . ewiki_author();
1065 $data["version"]++;
1066 ewiki_database("WRITE", $data);
1067 $status[$page] = "<b>".get_string("flagsset","wiki")."</b> ".$status[$page];
1068 }
1069 }
1070 }
1071 return $status;
1072}
1073
1074
1075function wiki_admin_remove_list($listall="") {
1076 /// Table header
1077 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("errororreason","wiki"));
1078
1079 /// Get all pages
1080 $result = ewiki_database("GETALL", array("version"));
1081 $selected = array();
1082
1083 /// User wants to see all pages
1084 if ($listall) {
1085 while ($row = $result->get()) {
1086 $selected[$row["id"]] = get_string("listall","wiki")."<br>";
1087 }
1088 }
1089 while ($page = $result->get()) {
1090 $id = $page["id"];
1091 $page = ewiki_database("GET", array("id"=>$id));
1092 $flags = $page["flags"];
1093 #print "$id ".strlen(trim(($page["content"])))."<br>";
1094
1095 if (!strlen(trim(($page["content"]))) && !($flags & EWIKI_DB_F_BINARY)) {
1096 @$selected[$id] .= get_string("emptypage","wiki")."<br>";
1097 }
1098
1099 // Check for orphaned pages
1100 $result2 = ewiki_database("SEARCH", array("content" => $id));
1101 $orphanedpage=true;
1102 if ($result2 && $result2->count()) {
1103 while ($row = $result2->get()) {
1104 $checkcontent = ewiki_database("GET", array("id"=>$row["id"]));
1105 $checkcontent = strtolower($checkcontent["content"]);
1106
1107 if(strpos($checkcontent, strtolower($id)) !== false) {
1108 $orphanedpage=false;
1109 }
1110
1111 #echo "rc({$row['id']})==>($id): $check2 <br>";
1112 }
1113 }
1114
1115 /// Some more reasons for Deletion...
1116 if ($orphanedpage && $id!=EWIKI_PAGE_INDEX &&!($flags & EWIKI_DB_F_BINARY)) {
1117 @$selected[$id] .= get_string("orphanedpage","wiki")."<br>";
1118 }
1119
1120 if ($flags & EWIKI_DB_F_DISABLED) {
1121 @$selected[$id] .= get_string("disabledpage","wiki")."<br>";
1122 }
1123
1124 if (($flags & 3) == 3) {
1125 @$selected[$id] .= get_string("errorbinandtxt","wiki")."<br>";
1126 }
1127
1128 if (!($flags & 3)) {
1129 @$selected[$id] .= get_string("errornotype","wiki")."<br>";
1130 }
1131
1132 if ($flags & EWIKI_DB_F_HTML) {
1133 @$selected[$id] .= get_string("errorhtml","wiki")."<br>";
1134 }
1135
1136 if (($flags & EWIKI_DB_F_READONLY) && !($flags & EWIKI_DB_F_BINARY)) {
1137 @$selected[$id] .= get_string("readonly","wiki")."<br>";
1138 }
1139
1140 if (($flags & EWIKI_DB_F_READONLY) && ($flags & EWIKI_DB_F_WRITEABLE)) {
1141 @$selected[$id] .= get_string("errorroandwr","wiki")."<br>";
1142 }
1143
1144 if (strlen($page["content"]) >= 65536) {
1145 @$selected[$id] .= get_string("errorsize","wiki")."<br>";
1146 }
1147
1148 if (strpos($page["refs"], "\n".get_string("deletemewikiword","wiki")."\n")!==false) {
1149 @$selected[$id] .= get_string("deletemewikiwordfound","wiki",get_string("deletemewikiword","wiki"))."<br>";
1150 }
1151 }
1152
1153 foreach ($selected as $id => $reason) {
1154 $table_checkbox='<INPUT TYPE="checkbox" VALUE="'.rawurlencode($id).'" NAME="pagestodelete[]">';
1155
1156 #-- link & id
1157 if (strpos($id, EWIKI_IDF_INTERNAL) === false) {
1158 $table_page='<A HREF="' . ewiki_script("", $id) . '">';
1159 } else {
1160 $table_page='<A HREF="' . ewiki_script_binary("", $id) . '">';
1161 }
1162 $table_page .= htmlentities($id) . '</A>';
1163
1164 #-- print reason
1165 $table_reason=$reason;
1166
1167 $table->data[]=array($table_checkbox, $table_page, $table_reason);
1168 }
1169
1170 return $table;
1171}
1172
1173/// This function actually removes the pages
1174function wiki_admin_remove($pagestodelete, $course, $wiki, $userid, $groupid) {
1175 $ret="";
1176 foreach ($pagestodelete as $id) {
1177
1178 $id = rawurldecode($id);
1179
1180 $data = ewiki_database("GET", array("id"=>$id));
1181 for ($version=1; $version<=$data["version"]; $version++) {
1182 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1183 if($data["flags"] & EWIKI_DB_F_BINARY) {
1184 $filepath=moodle_binary_get_path($id, $data["meta"], $course, $wiki, $userid, $groupid);
1185 @unlink("$filepath");
1186 }
1187 }
1188
1189 }
1190 return $ret;
1191}
1192
1193function wiki_admin_strip_list($pagestostrip="",$version="",$err="") {
1194 /// Table header
1195 $table->head = array("&nbsp;", get_string("pagename","wiki"), get_string("deleteversions","wiki"));
1196
1197 $vc=ewiki_database("COUNTVERSIONS", array());
1198 $result = ewiki_database("GETALL",array());
1199 $i=0;
1200 while ($row = $result->get()) {
1201 $id = $row["id"];
1202 if($vc[$id]>1) {
1203 $error="";
1204 if($err[$id]) {
1205 $error=" ".join(", ",$err[$id]);
1206 }
1207 $checked="";
1208 if($pagestostrip=="" || $pagestostrip[$i]) {
1209 $checked=" CHECKED";
1210 }
1211 if($version=="") {
1212 $versiondefault="1-".($row["version"]-1);
1213 } else {
1214 $versiondefault=$version[$i];
1215 }
1216 $table->data[]=array('<input type="checkbox" value="'.rawurlencode($id).'" name="pagestostrip['.$i.']" '.$checked.'>',
1217 '<A HREF="'.EWIKI_SCRIPT.$id.'">'.htmlentities($id).'</A> / '.get_string("version","wiki").": ".$row["version"],
1218 '<input name="version['.$i.']" value="'.$versiondefault.'" size="7">'.$error);
1219
1220 }
1221 $i++;
1222 }
1223 return $table;
1224}
1225
1226function wiki_admin_strip_versions($pagestostrip, $version, &$err) {
1227 $ret=array();
1228 foreach ($pagestostrip as $key => $id_ue) {
1229
1230 $id = rawurldecode($id_ue);
1231 if (preg_match('/^(\d+)[-\s._:]+(\d+)$/', trim($version[$key]), $uu)) {
1232 $versA = $uu[1];
1233 $versZ = $uu[2];
1234
1235 // Let the last Version in the database
1236 $checkdata = ewiki_database("GET", array("id" => $id_ue));
1237 if($versZ>=$checkdata["version"]) {
1238 $err[$id][] = get_string("versionrangetoobig","wiki");
1239 } else {
1240 if($versA<=$versZ) {
1241 for ($v=$versA; $v<=$versZ; $v++) {
1242 $ret[$id][]=$v;
1243 }
1244 } else {
1245 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1246 }
1247 }
1248 }
1249 else {
1250 $err[$id][]=get_string("wrongversionrange","wiki",$version[$key]);
1251 }
1252 }
1253 return $ret;
1254}
1255
1256function wiki_admin_strip($pagestostrip) {
1257 /// Purges old page-versions
1258 foreach($pagestostrip as $id => $versions) {
1259 foreach($versions as $version) {
1260 ewiki_database("DELETE", array("id"=>$id, "version"=>$version));
1261 }
1262 }
1263}
1264
1265function wiki_admin_checklinks_list() {
1266 $ret=array();
1267 $result = ewiki_database("GETALL",array());
1268 while ($row = $result->get()) {
1269 if(!($row["flags"] & EWIKI_DB_F_BINARY)) {
1270 $index=htmlentities($row["id"]);
1271 $ret[$index] = $row["id"];
1272 }
1273 }
1274 return $ret;
1275}
1276
1277function wiki_admin_checklinks($pagetocheck) {
1278 /// Checks http:// Links
1279 $ret="";
1280 if($pagetocheck) {
1281 $get = ewiki_database("GET", array("id" => $pagetocheck));
1282 $content = $get["content"];
1283
1284 preg_match_all('_(http://[^\s"\'<>#,;]+[^\s"\'<>#,;.])_', $content, $links);
1285 $badlinks = array();
1286 if(!$links[1]) {
1287 $ret = get_string("nolinksfound","wiki")."<br><br>";
1288 } else {
1289 foreach ($links[1] as $href) {
1290 #print "[ $href ]";
1291 #$d = @implode("", @file($href));
1292 $d="";
1293 if($checkfd = @fopen($href, 'r')) {
1294 fclose($checkfd);
1295 $d="OK";
1296 }
1297 if (empty($d) || !strlen(trim($d)) || stristr("not found", $d) || stristr("error 404", $d)) {
1298 $ret.="[".get_string("linkdead","wiki")."] $href <br>\n";
1299 $badlinks[] = $href;
1300 } else {
1301 $ret.="[".get_string("linkok","wiki")."] $href <br>\n";
1302 }
1303 }
1304 }
1305
1306 /// Remove old Notices
1307