2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the wiki mod:
11 // (pk->id, fk->wikiid)
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) {
31 //Get record from backup_ids
32 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
35 //Now get completed xmlized object
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);
66 if (!defined('RESTORE_SILENTLY')) {
67 echo "<li>".get_string("modulename","wiki")." \"".format_string(stripslashes($wiki->name
),true)."\"</li>";
72 //We have the newid, update backup_ids
73 backup_putid($restore->backup_unique_code
,$mod->modtype
,
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);
90 //This function restores the wiki_entries
91 function wiki_entries_restore_mods($old_wiki_id,$new_wiki_id,$info,$restore) {
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
);
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
);
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);
133 if (($i+
1) %
50 == 0) {
134 if (!defined('RESTORE_SILENTLY')) {
136 if (($i+
1) %
1000 == 0) {
144 //We have the newid, update backup_ids
145 backup_putid($restore->backup_unique_code
,"wiki_entries",$oldid,$newid);
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);
159 //This function restores the wiki_pages
160 function wiki_pages_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
166 //Get the comments array
167 $pages = $info['#']['PAGES']['0']['#']['PAGE'];
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
);
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);
204 if (($i+
1) %
50 == 0) {
205 if (!defined('RESTORE_SILENTLY')) {
207 if (($i+
1) %
1000 == 0) {
214 //We have the newid, update backup_ids
215 backup_putid($restore->backup_unique_code
,"wiki_pages",$oldid,$newid);
223 function wiki_restore_files ($oldwikiid, $newwikiid, $oldentryid, $newentryid, $restore) {
233 //First, we check to "course_id" exists and create is as necessary
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
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
253 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
254 "/moddata/wiki/".$oldwikiid."/".$oldentryid;
256 if (is_dir($temp_path)) {
261 //If todo, we create the neccesary dirs in course moddata/wiki
262 if ($status and $todo) {
264 $this_wiki_path = $wiki_path."/".$newwikiid;
265 $status = check_dir_exists($this_wiki_path,true);
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);
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) {
285 //Link to the list of wikis
287 $searchstring='/\$@(WIKIINDEX)\*([0-9]+)@\$/';
289 preg_match_all($searchstring,$content,$foundset);
290 //If found, then we are going to look for its new id (in backup tables)
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
302 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/wiki/index.php?id='.$rec->new_id
,$result);
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]+)@\$/';
314 preg_match_all($searchstring,$result,$foundset);
315 //If found, then we are going to look for its new id (in backup tables)
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
327 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/wiki/view.php?id='.$rec->new_id
,$result);
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);
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) {
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,
351 WHERE w.course = $restore->course_id AND
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) {
358 $content = $page->definition
;
359 $result = restore_decode_content_links_worker($content,$restore);
360 if ($result != $content) {
362 $page->content
= addslashes($result);
363 $status = update_record("wiki_pages",$page);
365 if (!defined('RESTORE_SILENTLY')) {
366 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
371 if (($i+
1) %
5 == 0) {
372 if (!defined('RESTORE_SILENTLY')) {
374 if (($i+
1) %
100 == 0) {
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) {
392 $content = $wiki->summary
;
393 $result = restore_decode_content_links_worker($content,$restore);
394 if ($result != $content) {
396 $wiki->summary
= addslashes($result);
397 $status = update_record("wiki",$wiki);
399 if (!defined('RESTORE_SILENTLY')) {
400 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
405 if (($i+
1) %
5 == 0) {
406 if (!defined('RESTORE_SILENTLY')) {
408 if (($i+
1) %
100 == 0) {