MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / wiki / restorelib.php
bloba696be04ac11ff239f2c2de733f9deea9cac65c0
1 <?PHP //$Id$
2 //This php script contains all the stuff to backup/restore
3 //wiki mods
5 //This is the "graphical" structure of the wiki mod:
6 //
7 // wiki
8 // (CL,pk->id)
9 //
10 // wiki_entries
11 // (pk->id, fk->wikiid)
13 // wiki_pages
14 // (pk->pagename,version,wiki, fk->wiki)
16 // Meaning: pk->primary key field of the table
17 // fk->foreign key to link with parent
18 // nt->nested field (recursive data)
19 // CL->course level info
20 // UL->user level info
21 // files->table may have files)
23 //-----------------------------------------------------------
25 function wiki_restore_mods($mod,$restore) {
27 global $CFG;
29 $status = true;
31 //Get record from backup_ids
32 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
34 if ($data) {
35 //Now get completed xmlized object
36 $info = $data->info;
37 //if necessary, write to restorelog and adjust date/time fields
38 if ($restore->course_startdateoffset) {
39 restore_log_date_changes('Wiki', $restore, $info['MOD']['#'], array('TIMEMODIFIED'));
41 //traverse_xmlize($info); //Debug
42 //print_object ($GLOBALS['traverse_array']); //Debug
43 //$GLOBALS['traverse_array']=""; //Debug
45 //Now, build the wiki record structure
46 $wiki->course = $restore->course_id;
47 $wiki->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
48 $wiki->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
49 $wiki->pagename = backup_todb($info['MOD']['#']['PAGENAME']['0']['#']);
50 $wiki->wtype = backup_todb($info['MOD']['#']['WTYPE']['0']['#']);
51 $wiki->ewikiprinttitle = backup_todb($info['MOD']['#']['EWIKIPRINTTITLE']['0']['#']);
52 $wiki->htmlmode = backup_todb($info['MOD']['#']['HTMLMODE']['0']['#']);
53 $wiki->ewikiacceptbinary = backup_todb($info['MOD']['#']['EWIKIACCEPTBINARY']['0']['#']);
54 $wiki->disablecamelcase = backup_todb($info['MOD']['#']['DISABLECAMELCASE']['0']['#']);
55 $wiki->setpageflags = backup_todb($info['MOD']['#']['SETPAGEFLAGS']['0']['#']);
56 $wiki->strippages = backup_todb($info['MOD']['#']['STRIPPAGES']['0']['#']);
57 $wiki->removepages = backup_todb($info['MOD']['#']['REMOVEPAGES']['0']['#']);
58 $wiki->revertchanges = backup_todb($info['MOD']['#']['REVERTCHANGES']['0']['#']);
59 $wiki->initialcontent = backup_todb($info['MOD']['#']['INITIALCONTENT']['0']['#']);
60 $wiki->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
62 //The structure is equal to the db, so insert the wiki
63 $newid = insert_record ("wiki",$wiki);
65 //Do some output
66 if (!defined('RESTORE_SILENTLY')) {
67 echo "<li>".get_string("modulename","wiki")." \"".format_string(stripslashes($wiki->name),true)."\"</li>";
69 backup_flush(300);
71 if ($newid) {
72 //We have the newid, update backup_ids
73 backup_putid($restore->backup_unique_code,$mod->modtype,
74 $mod->id, $newid);
75 //Now check if want to restore user data and do it.
76 if (restore_userdata_selected($restore,'wiki',$mod->id)) {
77 //Restore wiki_entries
78 $status = wiki_entries_restore_mods($mod->id,$newid,$info,$restore);
80 } else {
81 $status = false;
83 } else {
84 $status = false;
87 return $status;
90 //This function restores the wiki_entries
91 function wiki_entries_restore_mods($old_wiki_id,$new_wiki_id,$info,$restore) {
93 global $CFG;
95 $status = true;
97 //Get the entries array
98 $entries = $info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'];
100 //Iterate over entries
101 for($i = 0; $i < sizeof($entries); $i++) {
102 $ent_info = $entries[$i];
103 //traverse_xmlize($ent_info); //Debug
104 //print_object ($GLOBALS['traverse_array']); //Debug
105 //$GLOBALS['traverse_array']=""; //Debug
107 //We'll need this later!!
108 $oldid = backup_todb($ent_info['#']['ID']['0']['#']);
110 //Now, build the wiki_ENTRIES record structure
111 $entry->wikiid = $new_wiki_id;
112 $entry->course = $restore->course_id;
113 $entry->userid = backup_todb($ent_info['#']['USERID']['0']['#']);
114 $entry->groupid = backup_todb($ent_info['#']['GROUPID']['0']['#']);
115 $entry->pagename = backup_todb($ent_info['#']['PAGENAME']['0']['#']);
116 $entry->timemodified = backup_todb($ent_info['#']['TIMEMODIFIED']['0']['#']);
117 $entry->timemodified += $restore->course_startdateoffset;
118 //We have to recode the userid field
119 $user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
120 if ($user) {
121 $entry->userid = $user->new_id;
123 //We have to recode the groupid field
124 $group = backup_getid($restore->backup_unique_code, 'groups', $entry->groupid);
125 if ($group) {
126 $entry->groupid = $group->new_id;
129 //The structure is equal to the db, so insert the wiki_entries
130 $newid = insert_record ("wiki_entries",$entry);
132 //Do some output
133 if (($i+1) % 50 == 0) {
134 if (!defined('RESTORE_SILENTLY')) {
135 echo ".";
136 if (($i+1) % 1000 == 0) {
137 echo "<br />";
140 backup_flush(300);
143 if ($newid) {
144 //We have the newid, update backup_ids
145 backup_putid($restore->backup_unique_code,"wiki_entries",$oldid,$newid);
147 //Restore wiki_pages
148 $status = wiki_pages_restore_mods($oldid,$newid,$ent_info,$restore);
150 //Now copy moddata associated files
151 $status = wiki_restore_files ($old_wiki_id, $new_wiki_id, $oldid, $newid, $restore);
152 } else {
153 $status = false;
156 return $status;
159 //This function restores the wiki_pages
160 function wiki_pages_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
162 global $CFG;
164 $status = true;
166 //Get the comments array
167 $pages = $info['#']['PAGES']['0']['#']['PAGE'];
169 //Iterate over pages
170 for($i = 0; $i < sizeof($pages); $i++) {
171 $pag_info = $pages[$i];
172 //traverse_xmlize($pag_info); //Debug
173 //print_object ($GLOBALS['traverse_array']); //Debug
174 //$GLOBALS['traverse_array']=""; //Debug
176 //We'll need this later!!
177 $oldid = backup_todb($pag_info['#']['ID']['0']['#']);
179 //Now, build the wiki_page record structure
180 $page->wiki = $new_entry_id;
181 $page->pagename = backup_todb($pag_info['#']['PAGENAME']['0']['#']);
182 $page->version = backup_todb($pag_info['#']['VERSION']['0']['#']);
183 $page->flags = backup_todb($pag_info['#']['FLAGS']['0']['#']);
184 $page->content = backup_todb($pag_info['#']['CONTENT']['0']['#']);
185 $page->author = backup_todb($pag_info['#']['AUTHOR']['0']['#']);
186 $page->userid = backup_todb($pag_info['#']['USERID']['0']['#']);
187 $page->created = backup_todb($pag_info['#']['CREATED']['0']['#']);
188 $page->created += $restore->course_startdateoffset;
189 $page->lastmodified = backup_todb($pag_info['#']['LASTMODIFIED']['0']['#']);
190 $page->lastmodified += $restore->course_startdateoffset;
191 $page->refs = str_replace("$@LINEFEED@$","\n",backup_todb($pag_info['#']['REFS']['0']['#']));
192 $page->meta = backup_todb($pag_info['#']['META']['0']['#']);
193 $page->hits = backup_todb($pag_info['#']['HITS']['0']['#']);
195 //We have to recode the userid field
196 $user = backup_getid($restore->backup_unique_code,"user",$page->userid);
197 if ($user) {
198 $page->userid = $user->new_id;
200 //The structure is equal to the db, so insert the wiki_pages
201 $newid = insert_record ("wiki_pages",$page);
203 //Do some output
204 if (($i+1) % 50 == 0) {
205 if (!defined('RESTORE_SILENTLY')) {
206 echo ".";
207 if (($i+1) % 1000 == 0) {
208 echo "<br />";
211 backup_flush(300);
213 if ($newid) {
214 //We have the newid, update backup_ids
215 backup_putid($restore->backup_unique_code,"wiki_pages",$oldid,$newid);
216 } else {
217 $status = false;
220 return $status;
223 function wiki_restore_files ($oldwikiid, $newwikiid, $oldentryid, $newentryid, $restore) {
225 global $CFG;
227 $status = true;
228 $todo = false;
229 $moddata_path = "";
230 $forum_path = "";
231 $temp_path = "";
233 //First, we check to "course_id" exists and create is as necessary
234 //in CFG->dataroot
235 $dest_dir = $CFG->dataroot."/".$restore->course_id;
236 $status = check_dir_exists($dest_dir,true);
238 //First, locate course's moddata directory
239 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
241 //Check it exists and create it
242 $status = check_dir_exists($moddata_path,true);
244 //Now, locate wiki directory
245 if ($status) {
246 $wiki_path = $moddata_path."/wiki";
247 //Check it exists and create it
248 $status = check_dir_exists($wiki_path,true);
251 //Now locate the temp dir we are restoring from
252 if ($status) {
253 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
254 "/moddata/wiki/".$oldwikiid."/".$oldentryid;
255 //Check it exists
256 if (is_dir($temp_path)) {
257 $todo = true;
261 //If todo, we create the neccesary dirs in course moddata/wiki
262 if ($status and $todo) {
263 //First this wiki id
264 $this_wiki_path = $wiki_path."/".$newwikiid;
265 $status = check_dir_exists($this_wiki_path,true);
266 //Now this entry id
267 $entry_wiki_path = $this_wiki_path."/".$newentryid;
268 //And now, copy temp_path to entry_wiki_path
269 $status = backup_copy_file($temp_path, $entry_wiki_path);
272 return $status;
275 //Return a content decoded to support interactivities linking. Every module
276 //should have its own. They are called automatically from
277 //wiki_decode_content_links_caller() function in each module
278 //in the restore process
279 function wiki_decode_content_links ($content,$restore) {
281 global $CFG;
283 $result = $content;
285 //Link to the list of wikis
287 $searchstring='/\$@(WIKIINDEX)\*([0-9]+)@\$/';
288 //We look for it
289 preg_match_all($searchstring,$content,$foundset);
290 //If found, then we are going to look for its new id (in backup tables)
291 if ($foundset[0]) {
292 //print_object($foundset); //Debug
293 //Iterate over foundset[2]. They are the old_ids
294 foreach($foundset[2] as $old_id) {
295 //We get the needed variables here (course id)
296 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
297 //Personalize the searchstring
298 $searchstring='/\$@(WIKIINDEX)\*('.$old_id.')@\$/';
299 //If it is a link to this course, update the link to its new location
300 if($rec->new_id) {
301 //Now replace it
302 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/wiki/index.php?id='.$rec->new_id,$result);
303 } else {
304 //It's a foreign link so leave it as original
305 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/wiki/index.php?id='.$old_id,$result);
310 //Link to wiki view by moduleid
312 $searchstring='/\$@(WIKIVIEWBYID)\*([0-9]+)@\$/';
313 //We look for it
314 preg_match_all($searchstring,$result,$foundset);
315 //If found, then we are going to look for its new id (in backup tables)
316 if ($foundset[0]) {
317 //print_object($foundset); //Debug
318 //Iterate over foundset[2]. They are the old_ids
319 foreach($foundset[2] as $old_id) {
320 //We get the needed variables here (course_modules id)
321 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
322 //Personalize the searchstring
323 $searchstring='/\$@(WIKIVIEWBYID)\*('.$old_id.')@\$/';
324 //If it is a link to this course, update the link to its new location
325 if($rec->new_id) {
326 //Now replace it
327 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/wiki/view.php?id='.$rec->new_id,$result);
328 } else {
329 //It's a foreign link so leave it as original
330 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/wiki/view.php?id='.$old_id,$result);
335 return $result;
338 //This function makes all the necessary calls to xxxx_decode_content_links()
339 //function in each module, passing them the desired contents to be decoded
340 //from backup format to destination site/course in order to mantain inter-activities
341 //working in the backup/restore process. It's called from restore_decode_content_links()
342 //function in restore process
343 function wiki_decode_content_links_caller($restore) {
344 global $CFG;
345 $status = true;
347 //Process every wiki PAGE in the course
348 if ($pages = get_records_sql ("SELECT p.id, p.content
349 FROM {$CFG->prefix}wiki_pages p,
350 {$CFG->prefix}wiki w
351 WHERE w.course = $restore->course_id AND
352 p.wiki = w.id")) {
353 //Iterate over each post->message
354 $i = 0; //Counter to send some output to the browser to avoid timeouts
355 foreach ($pages as $page) {
356 //Increment counter
357 $i++;
358 $content = $page->definition;
359 $result = restore_decode_content_links_worker($content,$restore);
360 if ($result != $content) {
361 //Update record
362 $page->content = addslashes($result);
363 $status = update_record("wiki_pages",$page);
364 if (debugging()) {
365 if (!defined('RESTORE_SILENTLY')) {
366 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
370 //Do some output
371 if (($i+1) % 5 == 0) {
372 if (!defined('RESTORE_SILENTLY')) {
373 echo ".";
374 if (($i+1) % 100 == 0) {
375 echo "<br />";
378 backup_flush(300);
383 //Process every wiki (summary) in the course
384 if ($wikis = get_records_sql ("SELECT w.id, w.summary
385 FROM {$CFG->prefix}wiki w
386 WHERE w.course = $restore->course_id")) {
387 //Iterate over each wiki->summary
388 $i = 0; //Counter to send some output to the browser to avoid timeouts
389 foreach ($wikis as $wiki) {
390 //Increment counter
391 $i++;
392 $content = $wiki->summary;
393 $result = restore_decode_content_links_worker($content,$restore);
394 if ($result != $content) {
395 //Update record
396 $wiki->summary = addslashes($result);
397 $status = update_record("wiki",$wiki);
398 if (debugging()) {
399 if (!defined('RESTORE_SILENTLY')) {
400 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
404 //Do some output
405 if (($i+1) % 5 == 0) {
406 if (!defined('RESTORE_SILENTLY')) {
407 echo ".";
408 if (($i+1) % 100 == 0) {
409 echo "<br />";
412 backup_flush(300);
417 return $status;