Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / mod / wiki / view.php
blob1bb79715af0c845e99c83ad39b3fce54a28a5fac
1 <?php // $Id$
2 /// Extended by Michael Schneider
3 /// This page prints a particular instance of wiki
5 global $CFG,$USER;
7 require_once("../../config.php");
8 require_once("lib.php");
9 #require_once("$CFG->dirroot/course/lib.php"); // For side-blocks
10 require_once($CFG->libdir . '/ajax/ajaxlib.php');
11 require_js(array('yui_yahoo', 'yui_event', 'yui_connection'));
13 $ewiki_action = optional_param('ewiki_action', '', PARAM_ALPHA); // Action on Wiki-Page
14 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
15 $wid = optional_param('wid', 0, PARAM_INT); // Wiki ID
16 $page = optional_param('page', false); // Wiki Page Name
17 $q = optional_param('q',"", PARAM_PATH); // Search Context
18 $userid = optional_param('userid', 0, PARAM_INT); // User wiki.
19 $groupid = optional_param('groupid', 0, PARAM_INT); // Group wiki.
20 $canceledit = optional_param('canceledit','', PARAM_ALPHA); // Editing has been cancelled
21 $cacheme = optional_param('allowcache', 1, PARAM_INT); // Set this to 0 to try and disable page caching.
23 // Only want to add edit log entries if we have made some changes ie submitted a form
24 $editsave = optional_param('thankyou', '');
26 if($page) {
27 // Split page command into action and page
28 $actions = explode('/', $page,2);
29 if(count($actions)==2) {
30 $pagename=$actions[1];
31 } else {
32 $pagename=$actions[0];
34 } else {
35 $actions=array('');
36 $pagename='';
39 if ($id) {
40 if (! $cm = get_coursemodule_from_id('wiki', $id)) {
41 error("Course Module ID was incorrect");
44 if (! $course = get_record("course", "id", $cm->course)) {
45 error("Course is misconfigured");
48 if (! $wiki = get_record("wiki", "id", $cm->instance)) {
49 error("Course module is incorrect");
52 } else {
53 if (! $wiki = get_record("wiki", "id", $wid)) {
54 error("Course module is incorrect");
56 if (! $course = get_record("course", "id", $wiki->course)) {
57 error("Course is misconfigured");
59 if (! $cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
60 error("Course Module ID was incorrect");
64 require_course_login($course, true, $cm);
66 /// Add the course module info to the wiki object, for easy access.
67 $wiki->groupmode = $cm->groupmode;
68 $wiki->groupingid = $cm->groupingid;
69 $wiki->groupmembersonly = $cm->groupmembersonly;
70 $wiki->cmid = $cm->id;
72 /// Default format:
73 $moodle_format=FORMAT_MOODLE;
75 /// Globally disable CamelCase, if the option is selected for this wiki.
76 $moodle_disable_camel_case = ($wiki->disablecamelcase == 1);
78 if (($wiki_entry = wiki_get_default_entry($wiki, $course, $userid, $groupid))) {
79 // OK, now we know the entry ID, we can do lock etc.
81 // If true, we are 'really' on an editing page, not just on edit/something
82 $reallyedit=$actions[0]=='edit' && !$canceledit && !$editsave;
84 // Remove lock when we go to another wiki page (such as the cancel page)
85 if(!$reallyedit) {
86 wiki_release_lock($wiki_entry->id,$pagename);
87 } else if(array_key_exists('content',$_POST)) {
88 // Do not allow blank content because it causes problems (the wiki decides
89 // the page should automatically go into edit mode, but Moodle doesn't realise
90 // this and filters out the JS)
91 if($_POST['content']=='') {
92 $_POST['content']="\n";
93 $_REQUEST['content']="\n";
96 // We must have the edit lock in order to be permitted to save
97 list($ok,$lock)=wiki_obtain_lock($wiki_entry->id,$pagename);
98 if(!$ok) {
99 $strsavenolock=get_string('savenolock','wiki');
100 error($strsavenolock,$CFG->wwwroot.'/mod/wiki/view.php?id='.$cm->id.'&page=view/'.urlencode($pagename));
104 /// ################# EWIKI Part ###########################
105 /// The wiki_entry->pagename is set to the specified value of the wiki,
106 /// or the default value in the 'lang' file if the specified value was empty.
107 define("EWIKI_PAGE_INDEX",$wiki_entry->pagename);
109 /// If the page has a ' in it, it may have slashes added to it. Remove them if it does.
110 $page = ($page === false) ? stripslashes(EWIKI_PAGE_INDEX) : stripslashes($page);
112 /// # Prevent ewiki getting id as PageID...
113 unset($_REQUEST["id"]);
114 unset($_GET["id"]);
115 unset($_POST["id"]);
116 unset($_POST["id"]);
117 unset($_SERVER["QUERY_STRING"]);
118 if (isset($HTTP_GET_VARS)) {
119 unset($HTTP_GET_VARS["id"]);
121 if (isset($HTTP_POST_VARS)) {
122 unset($HTTP_POST_VARS["id"]);
124 global $ewiki_title;
126 /// #-- predefine some of the configuration constants
129 /// EWIKI_NAME is defined in ewikimoodlelibs, so that also admin.php can use this
130 #define("EWIKI_NAME", $wiki_entry->pagename);
132 /// Search Hilighting
133 if($ewiki_title=="SearchPages") {
134 $qArgument="&amp;q=".urlencode($q);
137 /// Build the ewsiki script constant
138 /// ewbase will also be needed by EWIKI_SCRIPT_BINARY
139 $ewbase = 'view.php?id='.$cm->id;
140 if (isset($userid) && $userid!=0) $ewbase .= '&amp;userid='.$userid;
141 if (isset($groupid) && $groupid!=0) $ewbase .= '&amp;groupid='.$groupid;
142 $ewscript = $ewbase.'&amp;page=';
143 define("EWIKI_SCRIPT", $ewscript);
144 define("EWIKI_SCRIPT_URL", $ewscript);
146 /// # Settings for this specific Wiki
147 define("EWIKI_PRINT_TITLE", $wiki->ewikiprinttitle);
149 define("EWIKI_INIT_PAGES", wiki_content_dir($wiki));
151 /// # Moodle always addslashes to everything so we are going to strip them always
152 /// # to allow wiki itself to add them again. It's a triple add-strip-add but
153 /// # was the only way to solve the problem without modifying how the rest of
154 /// # the module works.
155 include($CFG->dirroot."/mod/wiki/ewiki/fragments/strip_wonderful_slashes.php");
157 if (ini_get("register_globals")) {
158 # include($CFG->dirroot."/mod/wiki/ewiki/fragments/strike_register_globals.php");
161 # Database Handler
162 include_once($CFG->dirroot."/mod/wiki/ewikimoodlelib.php");
163 # Plugins
164 //include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/email_protect.php");
165 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/patchsaving.php");
166 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/notify.php");
167 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/feature/imgresize_gd.php");
168 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_highlight.php");
169 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/f_fixhtml.php");
170 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/wikinews.php");
171 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/sitemap.php");
172 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_wikidump.php");
173 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/aview/backlinks.php");
174 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/markup/css.php");
175 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/markup/footnotes.php");
176 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/diff.php");
177 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/page/pageindex.php");
178 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/page/orphanedpages.php");
179 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/wantedpages.php");
181 # Binary Handling
182 if($wiki->ewikiacceptbinary) {
183 define("EWIKI_UPLOAD_MAXSIZE", get_max_upload_file_size());
184 define("EWIKI_SCRIPT_BINARY", $ewbase."&binary=");
185 define("EWIKI_ALLOW_BINARY",1);
186 define("EWIKI_IMAGE_CACHING",1);
187 #define("EWIKI_AUTOVIEW",1);
188 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/lib/mime_magic.php");
189 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/aview/downloads.php");
190 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/downloads.php");
191 #include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/db/binary_store.php");
192 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_binary_store.php");
193 } else {
194 define("EWIKI_SCRIPT_BINARY", 0);
195 define("EWIKI_ALLOW_BINARY",0);
198 # The mighty Wiki itself
199 include_once($CFG->dirroot."/mod/wiki/ewiki/ewiki.php");
201 if($canceledit) {
202 if ($delim = strpos($page, EWIKI_ACTION_SEP_CHAR)) {
203 @$page = substr($page, $delim + 1);
204 } else {
205 @$page="";
208 # Language-stuff: eWiki gets language from Browser. Lets correct it. Empty arrayelements do no harm
209 $ewiki_t["languages"]=array(current_language(), $course->lang, $CFG->lang,"en","c");
211 # Check Access Rights
212 $canedit = wiki_can_edit_entry($wiki_entry, $wiki, $USER, $course);
213 if (!$canedit) {
214 # Protected Mode
215 unset($ewiki_plugins["action"]["edit"]);
216 unset($ewiki_plugins["action"]["info"]);
219 # HTML Handling
220 $ewiki_use_editor=0;
221 if($wiki->htmlmode == 0) {
222 # No HTML
223 $ewiki_config["htmlentities"]=array(); // HTML is managed by moodle
224 $moodle_format=FORMAT_TEXT;
226 if($wiki->htmlmode == 1) {
227 # Safe HTML
228 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_rescue_html.php");
229 $moodle_format=FORMAT_HTML;
231 if($wiki->htmlmode == 2) {
232 # HTML Only
233 $moodle_format=FORMAT_HTML;
234 $ewiki_use_editor=1;
235 $ewiki_config["htmlentities"]=array(); // HTML is allowed
236 $ewiki_config["wiki_link_regex"] = "\007 [!~]?(
237 \#?\[[^<>\[\]\n]+\] |
238 \^[-".EWIKI_CHARS_U.EWIKI_CHARS_L."]{3,} |
239 \b([\w]{3,}:)*([".EWIKI_CHARS_U."]+[".EWIKI_CHARS_L."]+){2,}\#?[\w\d]* |
240 \w[-_.+\w]+@(\w[-_\w]+[.])+\w{2,} ) \007x";
243 global $ewiki_author, $USER;
244 $ewiki_author=fullname($USER);
245 $content=ewiki_page($page);
246 $content2='';
248 /// ################# EWIKI Part ###########################
250 else {
251 $content = '';
252 $content2 = '<div class="boxaligncenter">'.get_string('nowikicreated', 'wiki').'</div>';
256 # Group wiki, ...: No page and no ewiki_title
257 if(!isset($ewiki_title)) {
258 $ewiki_title="";
262 /// Moodle Log
263 if ($editsave != NULL) { /// We've submitted an edit and have been redirected back here
264 add_to_log($course->id, "wiki", 'edit',
265 addslashes("view.php?id=$cm->id&amp;groupid=$groupid&amp;userid=$userid&amp;page=$ewiki_title"),
266 format_string($wiki->name,true).": ".$ewiki_title, $cm->id, $userid);
267 } else if ($ewiki_action != 'edit') {
268 add_to_log($course->id, "wiki", $ewiki_action,
269 addslashes("view.php?id=$cm->id&amp;groupid=$groupid&amp;userid=$userid&amp;page=$ewiki_title"),
270 format_string($wiki->name,true).": ".$ewiki_title, $cm->id, $userid);
274 /// Print the page header
276 $strwikis = get_string("modulenameplural", "wiki");
277 $strwiki = get_string("modulename", "wiki");
279 $navlinks = array();
280 /// Add page name if not main page
281 if ($ewiki_title != $wiki->name) {
282 $navlinks[] = array('name' => format_string($ewiki_title), 'link' => '', 'type' => 'title');
285 $navigation = build_navigation($navlinks, $cm);
286 print_header_simple($ewiki_title?$ewiki_title:format_string($wiki->name), "", $navigation,
287 "", "", $cacheme, update_module_button($cm->id, $course->id, $strwiki),
288 navmenu($course, $cm));
291 /// Print Page
292 echo ' <div id="wikiPageActions">
294 /// The top row contains links to other wikis, if applicable.
295 if ($wiki_entry && $wiki_list = wiki_get_other_wikis($wiki, $USER, $course, $wiki_entry->id)) {
296 //echo "wiki list ";print_r($wiki_list);
297 $selected="";
299 if (isset($wiki_list['selected'])) {
300 $selected = $wiki_list['selected'];
301 unset($wiki_list['selected']);
303 echo '<tr><td colspan="2">';
305 echo '<form id="otherwikis" action="'.$CFG->wwwroot.'/mod/wiki/view.php">';
306 echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>';
307 echo '<td class="sideblockheading">&nbsp;'
308 .$WIKI_TYPES[$wiki->wtype].' '
309 .get_string('modulename', 'wiki')." ".get_string('for',"wiki")." "
310 .wiki_get_owner($wiki_entry).':</td>';
312 echo '<td class="sideblockheading">'
313 .get_string('otherwikis', 'wiki').':&nbsp;&nbsp;';
314 $script = 'self.location=getElementById(\'otherwikis\').wikiselect.options[getElementById(\'otherwikis\').wikiselect.selectedIndex].value';
315 choose_from_menu($wiki_list, "wikiselect", $selected, "choose", $script);
316 echo '</td>';
317 echo '</tr></table>';
318 echo '</form>';
320 echo '</td>';
321 echo '</tr>';
324 if ($wiki_entry) {
325 $specialpages=array("WikiExport", "SiteMap", "SearchPages", "PageIndex","NewestPages","MostVisitedPages","MostOftenChangedPages","UpdatedPages","FileDownload","FileUpload","OrphanedPages","WantedPages");
326 /// Page Actions
327 echo '<table border="0" width="100%">';
328 echo '<tr>';
330 /// Searchform
331 echo '<td class="wikisearchform">';
332 wiki_print_search_form($cm->id, $q, $userid, $groupid, false);
333 echo '</td>';
335 /// Internal Wikilinks
336 echo '<td class="wikilinksblock">';
337 wiki_print_wikilinks_block($cm->id, $wiki->ewikiacceptbinary);
338 echo '</td>';
340 /// Administrative Links
341 if($canedit) {
342 echo '<td class="wikiadminactions">';
343 wiki_print_administration_actions($wiki, $cm->id, $userid, $groupid, $ewiki_title, $wiki->htmlmode!=2, $course);
344 echo '</td>';
347 /// Formatting Rules
348 echo '<td class="howtowiki">';
349 helpbutton('howtowiki', get_string('howtowiki', 'wiki'), 'wiki');
350 echo '</td>';
352 echo '</tr></table>';
355 echo '</div>
356 <div id="wiki-view" class="mwiki">
359 if($wiki_entry && $ewiki_title==$wiki_entry->pagename && !empty($wiki->summary)) {
360 if (trim(strip_tags($wiki->summary))) {
361 print_box(format_text($wiki->summary, FORMAT_MOODLE), 'generalbox', 'intro');
365 // The wiki Contents
367 if (!empty($canedit)) { /// Print tabs with commands for this page
368 $tabs = array('view', 'edit','links','info');
369 if ($wiki->ewikiacceptbinary) {
370 $tabs[] = 'attachments';
373 $tabrows = array();
374 $row = array();
375 $currenttab = '';
376 foreach ($tabs as $tab) {
377 $tabname = get_string("tab$tab", 'wiki');
378 $row[] = new tabobject($tabname, $ewbase.'&amp;page='.$tab.'/'.s($ewiki_id), $tabname);
379 if ($ewiki_action == "$tab" or in_array($page, $specialpages)) {
380 $currenttab = $tabname;
383 $tabrows[] = $row;
385 print_tabs($tabrows, $currenttab);
388 /// Insert a link to force page refresh if new content isn't showing.
390 // build new URL + query string
391 $queries = preg_split('/[?&]/', me());
392 $nqueries = count($queries);
393 $me = $queries[0] . '?';
394 for($i=1; $i < $nqueries; $i++)
396 if( !strstr($queries[$i], 'allowcache') )
397 $me .= $queries[$i] . '&amp;';
399 $me .= 'allowcache=0';
401 // Insert the link
402 $linkdesc = get_string('reloadlinkdescription', 'wiki');
403 $linktext = get_string('reloadlinktext', 'wiki');
404 echo "<div class='wikilinkright'><a href='$me' title='$linkdesc'><input type='button' value='$linktext' /></a></div>";
406 print_simple_box_start('center', '100%', '', '20');
408 /// Don't filter any pages containing wiki actions (except view). A wiki page containing
409 /// actions will have the form [action]/[pagename]. If the action is 'view' or the '/'
410 /// isn't there (so the action defaults to 'view'), filter it.
411 /// If the page does not yet exist, the display will default to 'edit'.
412 if((count($actions) < 2 || $actions[0] == "view") && $wiki_entry &&
413 record_exists('wiki_pages', 'pagename', addslashes($page), 'wiki', $wiki_entry->id)) {
414 print(format_text($content, $moodle_format));
415 } else if($actions[0]=='edit' && $reallyedit) {
416 // Check the page isn't locked before printing out standard wiki content. (Locking
417 // is implemented as a wrapper over the existing wiki.)
418 list($gotlock,$lock)=wiki_obtain_lock($wiki_entry->id,$pagename);
419 if(!$gotlock) {
420 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
421 $canoverridelock = has_capability('mod/wiki:overridelock', $modcontext);
423 $a=new stdClass;
424 $a->since=userdate($lock->lockedsince);
425 $a->seen=userdate($lock->lockedseen);
426 $user=get_record('user','id',$lock->lockedby);
427 $a->name=fullname($user,
428 has_capability('moodle/site:viewfullnames', $modcontext));
430 print_string('pagelocked','wiki',$a);
432 if($canoverridelock) {
433 $pageesc=htmlspecialchars($page);
434 $stroverrideinfo=get_string('overrideinfo','wiki');
435 $stroverridebutton=get_string('overridebutton','wiki');
436 $sesskey=sesskey();
437 print "
438 <form id='overridelock' method='post' action='overridelock.php'>
439 <div>
440 <input type='hidden' name='sesskey' value='$sesskey' />
441 <input type='hidden' name='id' value='$cm->id' />
442 <input type='hidden' name='page' value='$pageesc' />
443 $stroverrideinfo
444 <input type='submit' value='$stroverridebutton' />
445 </div>
446 </form>
449 } else {
450 if (ajaxenabled()) {
451 // OK, the page is now locked to us. Put in the AJAX for keeping the lock
452 $strlockcancelled=addslashes(get_string('lockcancelled','wiki'));
453 $strnojslockwarning=get_string('nojslockwarning','wiki');
454 $intervalms=WIKI_LOCK_RECONFIRM*1000;
455 print "
456 <script type='text/javascript'>
457 var intervalID;
458 function handleResponse(o) {
459 if(o.responseText=='cancel') {
460 document.forms['ewiki'].elements['preview'].disabled=true;
461 document.forms['ewiki'].elements['save'].disabled=true;
462 clearInterval(intervalID);
463 alert('$strlockcancelled');
466 function handleFailure(o) {
467 // Ignore for now
469 intervalID=setInterval(function() {
470 YAHOO.util.Connect.asyncRequest('POST','confirmlock.php',
471 {success:handleResponse,failure:handleFailure},'lockid={$lock->id}');
472 },$intervalms);
473 </script>
474 <noscript><p>
475 $strnojslockwarning
476 </p></noscript>
479 // Print editor etc
480 print $content;
482 } else {
483 print $content;
485 print $content2;
486 print_simple_box_end();
487 echo "<br />";
489 /// Finish the page
490 echo '
491 </div>
494 print_footer($course);