MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / admin.php
blobfbd7431be749aadfd04c4852b41542cb2f79b5e2
1 <?PHP // $Id$
2 /// Extended by Michael Schneider
4 require_once("../../config.php");
5 require_once("lib.php");
7 $id = optional_param('id', '', PARAM_INT); // Course Module ID, or
8 $a = optional_param('a', '', PARAM_INT); // wiki ID
9 $page = optional_param('page', false, PARAM_CLEAN); // Pagename
10 $confirm = optional_param('confirm', '', PARAM_RAW);
11 $action = optional_param('action', '', PARAM_ACTION); // Admin Action
12 $userid = optional_param('userid', 0, PARAM_INT); // User wiki.
13 $groupid = optional_param('groupid', 0, PARAM_INT); // Group wiki.
15 if ($id) {
16 if (! $cm = get_coursemodule_from_id('wiki', $id)) {
17 error("Course Module ID was incorrect");
20 if (! $course = get_record("course", "id", $cm->course)) {
21 error("Course is misconfigured");
24 if (! $wiki = get_record("wiki", "id", $cm->instance)) {
25 error("Course module is incorrect");
28 } else {
29 if (! $wiki = get_record("wiki", "id", $a)) {
30 error("Course module is incorrect");
32 if (! $course = get_record("course", "id", $wiki->course)) {
33 error("Course is misconfigured");
35 if (! $cm = get_coursemodule_from_instance("wiki", $wiki->id, $course->id)) {
36 error("Course Module ID was incorrect");
40 require_login($course->id, false, $cm);
41 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
42 require_capability('mod/wiki:manage', $context);
44 /// Build the ewsiki script constant
45 $ewbase = 'view.php?id='.$id;
46 if (isset($userid) && $userid!=0) $ewbase .= '&amp;userid='.$userid;
47 if (isset($groupid) && $groupid!=0) $ewbase .= '&amp;groupid='.$groupid;
48 $ewscript = $ewbase.'&amp;page=';
49 define("EWIKI_SCRIPT", $ewscript);
50 if($wiki->ewikiacceptbinary) {
51 define("EWIKI_UPLOAD_MAXSIZE", get_max_upload_file_size());
52 define("EWIKI_SCRIPT_BINARY", $ewbase."&binary=");
56 /// Add the course module 'groupmode' to the wiki object, for easy access.
57 $wiki->groupmode = $cm->groupmode;
59 /// Is an Action given ?
60 if(!$action) {
61 error(get_string("noadministrationaction","wiki"));
64 /// Correct Action ?
65 if(!in_array($action, array("setpageflags", "removepages", "strippages", "checklinks", "revertpages"))) {
66 error("Unknown action '$action'","wiki");
70 /// May the User administrate it ?
71 if (($wiki_entry = wiki_get_entry($wiki, $course, $userid, $groupid)) === false || wiki_can_edit_entry($wiki_entry, $wiki, $USER, $course) === false) {
72 error(get_string("notadministratewiki","wiki"));
75 $canedit = wiki_can_edit_entry($wiki_entry, $wiki, $USER, $course);
76 # Check for dangerous events (hacking) !
77 if(in_array($action,array("removepages","strippages","revertpages"))) {
78 if(!($wiki->wtype=="student" || ($wiki->wtype=="group" and $canedit) || wiki_is_teacher($wiki))) {
79 add_to_log($course->id, "wiki", "hack", "", $wiki->name.": Tried to trick admin.php with action=$action.");
80 error("Hack attack detected !");
84 # Database and Binary Handler
85 include_once($CFG->dirroot."/mod/wiki/ewikimoodlelib.php");
86 include_once($CFG->dirroot."/mod/wiki/ewiki/plugins/moodle/moodle_binary_store.php");
88 /// The wiki_entry->pagename is set to the specified value of the wiki,
89 /// or the default value in the 'lang' file if the specified value was empty.
90 define("EWIKI_PAGE_INDEX",$wiki_entry->pagename);
91 # The mighty Wiki itself
92 include_once($CFG->dirroot."/mod/wiki/ewiki/ewiki.php");
94 $strwikis = get_string("modulenameplural", "wiki");
95 $strwiki = get_string("modulename", "wiki");
97 /// Validate Form
98 if ($form = data_submitted()) {
99 switch($action) {
100 case "revertpages":
101 if(!$form->deleteversions || 0 > $form->deleteversions || $form->deleteversions > 1000) {
102 $focus="form.deleteversions";
103 $err->deleteversions=get_string("deleteversionserror","wiki");
105 if(!$form->changesfield || 0 > $form->changesfield || $form->changesfield > 100000) {
106 $focus="form.changesfield";
107 $err->changesfield=get_string("changesfielderror","wiki");
109 if($form->authorfieldpattern=="") {
110 $focus="form.authorfieldpattern";
111 $err->authorfieldpattern=get_string("authorfieldpatternerror","wiki");
113 break;
114 default: break;
117 $navlinks = array();
118 $navlinks[] = array('name' => $strwikis, 'link' => "index.php?id=$course->id", 'type' => 'activity');
119 $navlinks[] = array('name' => format_string($wiki->name,true), 'link' => "view.php?id=$moodleID", 'type' => 'activityinstace');
120 $navlinks[] = array('name' => get_string("administration","wiki"), 'link' => '', 'type' => 'title');
122 $navigation = build_navigation($navlinks);
124 print_header_simple("$wiki_entry->pagename", "", $navigation,
125 $focus, "", true, update_module_button($cm->id, $course->id, $strwiki),
126 navmenu($course, $cm));
129 ////////////////////////////////////////////////////////////
130 /// Check if the Form has been submitted and display confirmation
131 ////////////////////////////////////////////////////////////
132 if ($form = data_submitted()) {
133 /// Moodle Log
134 /// Get additional info
135 $addloginfo="";
136 switch($action) {
137 case "removepages":
138 $addloginfo=@join(", ", $form->pagestodelete);
139 break;
140 case "strippages":
141 $addloginfo=@join(", ", $form->pagestostrip);
142 break;
143 case "checklinks":
144 $addloginfo=$form->pagetocheck;
145 break;
146 case "setpageflags":
147 // No additional info
148 break;
149 case "revertpages":
150 // No additional info
151 break;
153 add_to_log($course->id, "wiki", $action, "admin.php?action=$action&amp;userid=$userid&amp;groupid=$groupid&amp;id=$id", $wiki->name.($addloginfo?": ".$addloginfo:""));
154 $link="admin.php?action=$action".($userid?"&amp;userid=".$userid:"").($groupid?"&amp;groupid=".$groupid:"")."&amp;id=$id&amp;page=$page";
155 switch($action) {
156 case "removepages":
157 if($form->proceed) {
158 if(!$confirm && $form->pagestodelete) {
159 notice_yesno(get_string("removepagecheck", "wiki")."<br />".join(", ", $form->pagestodelete),
160 $link."&amp;confirm=".urlencode(join(" ",$form->pagestodelete)), $link);
161 print_footer($course);
162 exit;
165 break;
166 case "strippages":
167 if($form->proceed) {
168 if(!$confirm && $form->pagestostrip) {
169 $err=array();
170 $strippages=wiki_admin_strip_versions($form->pagestostrip,$form->version, $err);
171 $confirm="";
172 foreach($strippages as $cnfid => $cnfver) {
173 $confirm.="&confirm[$cnfid]=".urlencode(join(" ",$cnfver));
175 if(count($err)==0) {
176 $pagestostrip=array();
177 foreach($form->pagestostrip as $pagetostrip) {
178 $pagestostrip[]=htmlspecialchars(urldecode($pagetostrip));
180 notice_yesno(get_string("strippagecheck", "wiki")."<br />".join(", ", $pagestostrip),
181 $link.$confirm, $link);
182 print_footer($course);
183 exit;
187 break;
188 case "checklinks":
189 if($form->proceed) {
190 if(!$confirm && $form->pagetocheck) {
191 $confirm="&amp;confirm=".$form->pagetocheck;
192 notice_yesno(get_string("checklinkscheck", "wiki").$form->pagetocheck,
193 $link.$confirm, $link);
194 print_footer($course);
195 exit;
198 break;
199 case "setpageflags":
200 // pageflagstatus is used in setpageflags.html
201 $pageflagstatus=wiki_admin_setpageflags($form->flags);
202 break;
203 case "revertpages":
204 if(!$err) {
205 if(!$confirm) {
206 $confirm="&confirm[changesfield]=".urlencode($form->changesfield).
207 "&confirm[authorfieldpattern]=".urlencode($form->authorfieldpattern).
208 "&confirm[howtooperate]=".urlencode($form->howtooperate).
209 "&confirm[deleteversions]=".urlencode($form->deleteversions);
210 $revertedpages=wiki_admin_revert("", $form->authorfieldpattern, $form->changesfield, $form->howtooperate, $form->deleteversions);
211 if($revertedpages) {
212 notice_yesno(get_string("revertpagescheck", "wiki")."<br />".$revertedpages,
213 $link.$confirm, $link);
214 print_footer($course);
215 exit;
216 } else {
217 $err->remark=get_string("nochangestorevert","wiki");
221 break;
222 default: error("No such Wiki-Admin action: $action");
223 break;
227 /// Actions which need a confirmation. If confirmed, do the action
228 $redirect="view.php?".($groupid?"&amp;groupid=".$groupid:"").($userid?"&amp;userid=".$userid:"")."&amp;id=$id&amp;page=$page";
229 if($confirm && !$err) {
230 switch($action) {
231 case "removepages":
232 $ret=wiki_admin_remove(split(" ",$confirm), $course, $wiki, $userid, $groupid);
233 if(!$ret) {
234 redirect($redirect, get_string("pagesremoved","wiki"), 1);
235 } else {
236 error($ret);
238 exit;
239 case "strippages":
240 $strippages=array();
241 foreach($confirm as $pageid => $versions) {
242 $strippages[$pageid]=split(" ",$versions);
244 $ret=wiki_admin_strip($strippages);
245 if(!$ret) {
246 redirect($redirect, get_string("pagesstripped","wiki"), 1);
247 } else {
248 error($ret);
250 exit;
251 case "checklinks":
252 $ret=wiki_admin_checklinks($confirm);
253 redirect($redirect, get_string("linkschecked","wiki")."<br />".$ret, 5);
254 exit;
255 case "revertpages":
256 $revertedpages=wiki_admin_revert(1, $confirm["authorfieldpattern"], $confirm["changesfield"], $confirm["howtooperate"], $confirm["deleteversions"]);
257 redirect($redirect, get_string("pagesreverted","wiki"), 1);
258 exit;
259 case "setpageflags":
260 # No confirmation needed
261 break;
262 default: error("No such action '$action' with confirmation");
267 /// The top row contains links to other wikis, if applicable.
268 if ($wiki_list = wiki_get_other_wikis($wiki, $USER, $course, $wiki_entry->id)) {
269 if (isset($wiki_list['selected'])) {
270 $selected = $wiki_list['selected'];
271 unset($wiki_list['selected']);
273 echo '<tr><td colspan="2">';
275 echo '<form id="otherwikis" action="'.$CFG->wwwroot.'/mod/wiki/admin.php">';
276 echo '<fieldset class="invisiblefieldset">';
277 echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr>';
278 echo '<td class="sideblockheading">&nbsp;'
279 .$WIKI_TYPES[$wiki->wtype].' '
280 .get_string('modulename', 'wiki').' for '
281 .wiki_get_owner($wiki_entry).':</td>';
283 echo '<td class="sideblockheading" align="right">'
284 .get_string('otherwikis', 'wiki').':&nbsp;&nbsp;';
285 $script = 'self.location=getElementById(\'otherwikis\').wikiselect.options[getElementById(\'otherwikis\').wikiselect.selectedIndex].value';
287 /// Add Admin-Action
288 reset($wiki_list);
289 $wiki_admin_list=array();
290 while(list($key,$val)=each($wiki_list)) {
291 $wiki_admin_list[$key."&amp;action=$action"]=$val;
293 choose_from_menu($wiki_admin_list, "wikiselect", $selected, "choose", $script);
294 echo '</td>';
295 echo '</tr></table>';
296 echo '</fieldset></form>';
298 echo '</td>';
299 echo '</tr>';
302 if ($wiki_entry) {
305 /// Page Actions
306 echo '<table border="0" width="100%">';
307 echo '<tr>';
308 # echo '<tr><td align="center">';
309 # $specialpages=array("SearchPages", "PageIndex","NewestPages","MostVisitedPages","MostOftenChangedPages","UpdatedPages","FileDownload","FileUpload","OrphanedPages","WantedPages");
310 # wiki_print_page_actions($cm->id, $specialpages, $ewiki_id, $ewiki_action, $wiki->ewikiacceptbinary, $canedit);
311 # echo '</td>';
313 /// Searchform
314 echo '<td align="center">';
315 wiki_print_search_form($cm->id, $q, $userid, $groupid, false);
316 echo '</td>';
318 /// Internal Wikilinks
320 /// TODO: DOES NOT WORK !!!!
321 echo '<td align="center">';
322 wiki_print_wikilinks_block($cm->id, $wiki->ewikiacceptbinary);
323 echo '</td>';
325 /// Administrative Links
326 echo '<td align="center">';
327 wiki_print_administration_actions($wiki, $cm->id, $userid, $groupid, $page, $wiki->htmlmode!=2, $course);
328 echo '</td>';
330 # if($wiki->htmlmode!=2) {
331 # echo '<td align="center">';
332 # helpbutton('formattingrules', get_string('formattingrules', 'wiki'), 'wiki');
333 # echo get_string("formattingrules","wiki");
334 # echo '</td>';
337 echo '</tr></table>';
340 // The wiki Contents
341 print_simple_box_start( 'center', '100%', '', '20');
342 // Do the Action
343 # "setpageflags", "removepages", "strippages", "checklinks", "revertpages"
344 print_heading_with_help(get_string($action,"wiki"), $action, "wiki");
345 include $action.".html";
346 print_simple_box_end();
348 /// Finish the page
349 print_footer($course);
350 exit;