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 = 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
);
122 $entry->userid
= $user->new_id
;
124 //We have to recode the groupid field
125 $group = restore_group_getid($restore, $entry->groupid
);
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);
134 if (($i+
1) %
50 == 0) {
135 if (!defined('RESTORE_SILENTLY')) {
137 if (($i+
1) %
1000 == 0) {
145 //We have the newid, update backup_ids
146 backup_putid($restore->backup_unique_code
,"wiki_entries",$oldid,$newid);
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);
160 //This function restores the wiki_pages
161 function wiki_pages_restore_mods($old_entry_id,$new_entry_id,$info,$restore) {
167 //Get the comments array
168 $pages = $info['#']['PAGES']['0']['#']['PAGE'];
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
);
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);
205 if (($i+
1) %
50 == 0) {
206 if (!defined('RESTORE_SILENTLY')) {
208 if (($i+
1) %
1000 == 0) {
215 //We have the newid, update backup_ids
216 backup_putid($restore->backup_unique_code
,"wiki_pages",$oldid,$newid);
224 function wiki_restore_files ($oldwikiid, $newwikiid, $oldentryid, $newentryid, $restore) {
234 //First, we check to "course_id" exists and create is as necessary
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
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
254 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
255 "/moddata/wiki/".$oldwikiid."/".$oldentryid;
257 if (is_dir($temp_path)) {
262 //If todo, we create the neccesary dirs in course moddata/wiki
263 if ($status and $todo) {
265 $this_wiki_path = $wiki_path."/".$newwikiid;
266 $status = check_dir_exists($this_wiki_path,true);
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);
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) {
286 //Link to the list of wikis
288 $searchstring='/\$@(WIKIINDEX)\*([0-9]+)@\$/';
290 preg_match_all($searchstring,$content,$foundset);
291 //If found, then we are going to look for its new id (in backup tables)
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
303 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/wiki/index.php?id='.$rec->new_id
,$result);
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]+)@\$/';
315 preg_match_all($searchstring,$result,$foundset);
316 //If found, then we are going to look for its new id (in backup tables)
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
328 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/wiki/view.php?id='.$rec->new_id
,$result);
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);
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) {
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,
352 WHERE w.course = $restore->course_id AND
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) {
359 $content = $page->definition
;
360 $result = restore_decode_content_links_worker($content,$restore);
361 if ($result != $content) {
363 $page->content
= addslashes($result);
364 $status = update_record("wiki_pages",$page);
366 if (!defined('RESTORE_SILENTLY')) {
367 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
372 if (($i+
1) %
5 == 0) {
373 if (!defined('RESTORE_SILENTLY')) {
375 if (($i+
1) %
100 == 0) {
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) {
393 $content = $wiki->summary
;
394 $result = restore_decode_content_links_worker($content,$restore);
395 if ($result != $content) {
397 $wiki->summary
= addslashes($result);
398 $status = update_record("wiki",$wiki);
400 if (!defined('RESTORE_SILENTLY')) {
401 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
406 if (($i+
1) %
5 == 0) {
407 if (!defined('RESTORE_SILENTLY')) {
409 if (($i+
1) %
100 == 0) {