Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / wiki / restorelib.php
blobc4b269e464002a94d089a2d3a84e8b1976957382
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 = new object();
112 $entry->wikiid = $new_wiki_id;
113 $entry->course = $restore->course_id;
114 $entry->userid = backup_todb($ent_info['#']['USERID']['0']['#']);
115 $entry->groupid = backup_todb($ent_info['#']['GROUPID']['0']['#']);
116 $entry->pagename = backup_todb($ent_info['#']['PAGENAME']['0']['#']);
117 $entry->timemodified = backup_todb($ent_info['#']['TIMEMODIFIED']['0']['#']);
118 $entry->timemodified += $restore->course_startdateoffset;
119 //We have to recode the userid field
120 $user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
121 if ($user) {
122 $entry->userid = $user->new_id;
124 //We have to recode the groupid field
125 $group = restore_group_getid($restore, $entry->groupid);
126 if ($group) {
127 $entry->groupid = $group->new_id;
130 //The structure is equal to the db, so insert the wiki_entries
131 $newid = insert_record ("wiki_entries",$entry);
133 //Do some output
134 if (($i+1) % 50 == 0) {
135 if (!defined('RESTORE_SILENTLY')) {
136 echo ".";
137 if (($i+1) % 1000 == 0) {
138 echo "<br />";
141 backup_flush(300);
144 if ($newid) {
145 //We have the newid, update backup_ids
146 backup_putid($restore->backup_unique_code,"wiki_entries",$oldid,$newid);
148 //Restore wiki_pages
149 $status = wiki_pages_restore_mods($oldid,$newid,$ent_info,$restore);
151 //Now copy moddata associated files
152 $status = wiki_restore_files ($old_wiki_id, $new_wiki_id, $oldid, $newid, $restore);
153 } else {
154 $status = false;
157 return $status;
160 //This function restores the wiki_pages
161 function wiki_pages_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
163 global $CFG;
165 $status = true;
167 //Get the comments array
168 $pages = $info['#']['PAGES']['0']['#']['PAGE'];
170 //Iterate over pages
171 for($i = 0; $i < sizeof($pages); $i++) {
172 $pag_info = $pages[$i];
173 //traverse_xmlize($pag_info); //Debug
174 //print_object ($GLOBALS['traverse_array']); //Debug
175 //$GLOBALS['traverse_array']=""; //Debug
177 //We'll need this later!!
178 $oldid = backup_todb($pag_info['#']['ID']['0']['#']);
180 //Now, build the wiki_page record structure
181 $page->wiki = $new_entry_id;
182 $page->pagename = backup_todb($pag_info['#']['PAGENAME']['0']['#']);
183 $page->version = backup_todb($pag_info['#']['VERSION']['0']['#']);
184 $page->flags = backup_todb($pag_info['#']['FLAGS']['0']['#']);
185 $page->content = backup_todb($pag_info['#']['CONTENT']['0']['#']);
186 $page->author = backup_todb($pag_info['#']['AUTHOR']['0']['#']);
187 $page->userid = backup_todb($pag_info['#']['USERID']['0']['#']);
188 $page->created = backup_todb($pag_info['#']['CREATED']['0']['#']);
189 $page->created += $restore->course_startdateoffset;
190 $page->lastmodified = backup_todb($pag_info['#']['LASTMODIFIED']['0']['#']);
191 $page->lastmodified += $restore->course_startdateoffset;
192 $page->refs = str_replace("$@LINEFEED@$","\n",backup_todb($pag_info['#']['REFS']['0']['#']));
193 $page->meta = backup_todb($pag_info['#']['META']['0']['#']);
194 $page->hits = backup_todb($pag_info['#']['HITS']['0']['#']);
196 //We have to recode the userid field
197 $user = backup_getid($restore->backup_unique_code,"user",$page->userid);
198 if ($user) {
199 $page->userid = $user->new_id;
201 //The structure is equal to the db, so insert the wiki_pages
202 $newid = insert_record ("wiki_pages",$page);
204 //Do some output
205 if (($i+1) % 50 == 0) {
206 if (!defined('RESTORE_SILENTLY')) {
207 echo ".";
208 if (($i+1) % 1000 == 0) {
209 echo "<br />";
212 backup_flush(300);
214 if ($newid) {
215 //We have the newid, update backup_ids
216 backup_putid($restore->backup_unique_code,"wiki_pages",$oldid,$newid);
217 } else {
218 $status = false;
221 return $status;
224 function wiki_restore_files ($oldwikiid, $newwikiid, $oldentryid, $newentryid, $restore) {
226 global $CFG;
228 $status = true;
229 $todo = false;
230 $moddata_path = "";
231 $forum_path = "";
232 $temp_path = "";
234 //First, we check to "course_id" exists and create is as necessary
235 //in CFG->dataroot
236 $dest_dir = $CFG->dataroot."/".$restore->course_id;
237 $status = check_dir_exists($dest_dir,true);
239 //First, locate course's moddata directory
240 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
242 //Check it exists and create it
243 $status = check_dir_exists($moddata_path,true);
245 //Now, locate wiki directory
246 if ($status) {
247 $wiki_path = $moddata_path."/wiki";
248 //Check it exists and create it
249 $status = check_dir_exists($wiki_path,true);
252 //Now locate the temp dir we are restoring from
253 if ($status) {
254 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
255 "/moddata/wiki/".$oldwikiid."/".$oldentryid;
256 //Check it exists
257 if (is_dir($temp_path)) {
258 $todo = true;
262 //If todo, we create the neccesary dirs in course moddata/wiki
263 if ($status and $todo) {
264 //First this wiki id
265 $this_wiki_path = $wiki_path."/".$newwikiid;
266 $status = check_dir_exists($this_wiki_path,true);
267 //Now this entry id
268 $entry_wiki_path = $this_wiki_path."/".$newentryid;
269 //And now, copy temp_path to entry_wiki_path
270 $status = backup_copy_file($temp_path, $entry_wiki_path);
273 return $status;
276 //Return a content decoded to support interactivities linking. Every module
277 //should have its own. They are called automatically from
278 //wiki_decode_content_links_caller() function in each module
279 //in the restore process
280 function wiki_decode_content_links ($content,$restore) {
282 global $CFG;
284 $result = $content;
286 //Link to the list of wikis
288 $searchstring='/\$@(WIKIINDEX)\*([0-9]+)@\$/';
289 //We look for it
290 preg_match_all($searchstring,$content,$foundset);
291 //If found, then we are going to look for its new id (in backup tables)
292 if ($foundset[0]) {
293 //print_object($foundset); //Debug
294 //Iterate over foundset[2]. They are the old_ids
295 foreach($foundset[2] as $old_id) {
296 //We get the needed variables here (course id)
297 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
298 //Personalize the searchstring
299 $searchstring='/\$@(WIKIINDEX)\*('.$old_id.')@\$/';
300 //If it is a link to this course, update the link to its new location
301 if($rec->new_id) {
302 //Now replace it
303 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/wiki/index.php?id='.$rec->new_id,$result);
304 } else {
305 //It's a foreign link so leave it as original
306 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/wiki/index.php?id='.$old_id,$result);
311 //Link to wiki view by moduleid
313 $searchstring='/\$@(WIKIVIEWBYID)\*([0-9]+)@\$/';
314 //We look for it
315 preg_match_all($searchstring,$result,$foundset);
316 //If found, then we are going to look for its new id (in backup tables)
317 if ($foundset[0]) {
318 //print_object($foundset); //Debug
319 //Iterate over foundset[2]. They are the old_ids
320 foreach($foundset[2] as $old_id) {
321 //We get the needed variables here (course_modules id)
322 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
323 //Personalize the searchstring
324 $searchstring='/\$@(WIKIVIEWBYID)\*('.$old_id.')@\$/';
325 //If it is a link to this course, update the link to its new location
326 if($rec->new_id) {
327 //Now replace it
328 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/wiki/view.php?id='.$rec->new_id,$result);
329 } else {
330 //It's a foreign link so leave it as original
331 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/wiki/view.php?id='.$old_id,$result);
336 return $result;
339 //This function makes all the necessary calls to xxxx_decode_content_links()
340 //function in each module, passing them the desired contents to be decoded
341 //from backup format to destination site/course in order to mantain inter-activities
342 //working in the backup/restore process. It's called from restore_decode_content_links()
343 //function in restore process
344 function wiki_decode_content_links_caller($restore) {
345 global $CFG;
346 $status = true;
348 //Process every wiki PAGE in the course
349 if ($pages = get_records_sql ("SELECT p.id, p.content
350 FROM {$CFG->prefix}wiki_pages p,
351 {$CFG->prefix}wiki w
352 WHERE w.course = $restore->course_id AND
353 p.wiki = w.id")) {
354 //Iterate over each post->message
355 $i = 0; //Counter to send some output to the browser to avoid timeouts
356 foreach ($pages as $page) {
357 //Increment counter
358 $i++;
359 $content = $page->definition;
360 $result = restore_decode_content_links_worker($content,$restore);
361 if ($result != $content) {
362 //Update record
363 $page->content = addslashes($result);
364 $status = update_record("wiki_pages",$page);
365 if (debugging()) {
366 if (!defined('RESTORE_SILENTLY')) {
367 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
371 //Do some output
372 if (($i+1) % 5 == 0) {
373 if (!defined('RESTORE_SILENTLY')) {
374 echo ".";
375 if (($i+1) % 100 == 0) {
376 echo "<br />";
379 backup_flush(300);
384 //Process every wiki (summary) in the course
385 if ($wikis = get_records_sql ("SELECT w.id, w.summary
386 FROM {$CFG->prefix}wiki w
387 WHERE w.course = $restore->course_id")) {
388 //Iterate over each wiki->summary
389 $i = 0; //Counter to send some output to the browser to avoid timeouts
390 foreach ($wikis as $wiki) {
391 //Increment counter
392 $i++;
393 $content = $wiki->summary;
394 $result = restore_decode_content_links_worker($content,$restore);
395 if ($result != $content) {
396 //Update record
397 $wiki->summary = addslashes($result);
398 $status = update_record("wiki",$wiki);
399 if (debugging()) {
400 if (!defined('RESTORE_SILENTLY')) {
401 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
405 //Do some output
406 if (($i+1) % 5 == 0) {
407 if (!defined('RESTORE_SILENTLY')) {
408 echo ".";
409 if (($i+1) % 100 == 0) {
410 echo "<br />";
413 backup_flush(300);
418 return $status;