2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the resource mod:
10 // Meaning: pk->primary key field of the table
11 // fk->foreign key to link with parent
12 // nt->nested field (recursive data)
13 // CL->course level info
14 // UL->user level info
15 // files->table may have files)
17 //-----------------------------------------------------------
19 //This function executes all the restore procedure about this mod
20 function resource_restore_mods($mod,$restore) {
26 //Get record from backup_ids
27 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
30 //Now get completed xmlized object
32 //traverse_xmlize($info); //Debug
33 //print_object ($GLOBALS['traverse_array']); //Debug
34 //$GLOBALS['traverse_array']=""; //Debug
36 //Now, build the RESOURCE record structure
37 $resource->course
= $restore->course_id
;
38 $resource->name
= backup_todb($info['MOD']['#']['NAME']['0']['#']);
39 $resource->type
= $info['MOD']['#']['TYPE']['0']['#'];
40 $resource->reference
= backup_todb($info['MOD']['#']['REFERENCE']['0']['#']);
41 $resource->summary
= backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
42 $resource->alltext
= backup_todb($info['MOD']['#']['ALLTEXT']['0']['#']);
43 $resource->popup
= backup_todb($info['MOD']['#']['POPUP']['0']['#']);
44 $resource->options
= backup_todb($info['MOD']['#']['OPTIONS']['0']['#']);
45 $resource->timemodified
= $info['MOD']['#']['TIMEMODIFIED']['0']['#'];
47 //To mantain compatibility, in 1.4 the type and alltext meaning has changed and
48 //two new fields have arrived (popup and options). We have to modify somethigs
49 //if the popup field isn't present in the backup file to be upwards compatible.
50 if (! isset($info['MOD']['#']['POPUP']['0']['#'])) { //It's a pre-14 backup file
51 //Move alltext to popup in 3 and 5 resource types
52 if ($resource->type
== 3 ||
$resource->type
== 5) {
53 $resource->popup
= $resource->alltext
;
54 $resource->alltext
= '';
56 //Move reference to alltext for references
57 if ($resource->type
== 1) {
58 $resource->popup
= '';
59 $resource->alltext
= $resource->reference
;
60 $resource->reference
= '';
62 //Reencode the type field to its new values and fill the options field as needed
63 //Array 1-9 of new types
64 $types = array ('','html','file','file','text','file',
65 'html','file','text','directory');
66 //Array 1-9 of corresponding options
67 $options = array ('','','frame','','0','',
70 $oldtype = $resource->type
;
71 $resource->type
= $types[$oldtype];
72 $resource->options
= $options[$oldtype];
75 if ($resource->type
== 'reference') { // Obsolete type of resource
76 $resource->type
== 'html';
77 $resource->alltext
= $resource->reference
;
78 $resource->reference
= '';
81 //The structure is equal to the db, so insert the resource
82 $newid = insert_record ("resource",$resource);
85 if (!defined('RESTORE_SILENTLY')) {
86 echo "<li>".get_string("modulename","resource")." \"".format_string(stripslashes($resource->name
),true)."\"</li>";
91 //We have the newid, update backup_ids
92 backup_putid($restore->backup_unique_code
,$mod->modtype
,
95 // restore any associated files...
96 if ($resource->type
== 'file' ||
$resource->type
== 'directory' ||
$resource->type
== 'ims') {
97 resource_restore_files($mod->id
,$newid,$resource,$restore);
110 //Return a content decoded to support interactivities linking. Every module
111 //should have its own. They are called automatically from
112 //resource_decode_content_links_caller() function in each module
113 //in the restore process
114 function resource_decode_content_links ($content,$restore) {
120 //Link to the list of resources
122 $searchstring='/\$@(RESOURCEINDEX)\*([0-9]+)@\$/';
124 preg_match_all($searchstring,$content,$foundset);
125 //If found, then we are going to look for its new id (in backup tables)
127 //print_object($foundset); //Debug
128 //Iterate over foundset[2]. They are the old_ids
129 foreach($foundset[2] as $old_id) {
130 //We get the needed variables here (course id)
131 $rec = backup_getid($restore->backup_unique_code
,"course",$old_id);
132 //Personalize the searchstring
133 $searchstring='/\$@(RESOURCEINDEX)\*('.$old_id.')@\$/';
134 //If it is a link to this course, update the link to its new location
135 if (!empty($rec->new_id
)) {
137 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/resource/index.php?id='.$rec->new_id
,$result);
139 //It's a foreign link so leave it as original
140 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/resource/index.php?id='.$old_id,$result);
145 //Link to resource view by moduleid
147 $searchstring='/\$@(RESOURCEVIEWBYID)\*([0-9]+)@\$/';
149 preg_match_all($searchstring,$result,$foundset);
150 //If found, then we are going to look for its new id (in backup tables)
152 //print_object($foundset); //Debug
153 //Iterate over foundset[2]. They are the old_ids
154 foreach($foundset[2] as $old_id) {
155 //We get the needed variables here (course_modules id)
156 $rec = backup_getid($restore->backup_unique_code
,"course_modules",$old_id);
157 //Personalize the searchstring
158 $searchstring='/\$@(RESOURCEVIEWBYID)\*('.$old_id.')@\$/';
159 //If it is a link to this course, update the link to its new location
160 if (!empty($rec->new_id
)) {
162 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/resource/view.php?id='.$rec->new_id
,$result);
164 //It's a foreign link so leave it as original
165 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/resource/view.php?id='.$old_id,$result);
170 //Link to resource view by resourceid
172 $searchstring='/\$@(RESOURCEVIEWBYR)\*([0-9]+)@\$/';
174 preg_match_all($searchstring,$result,$foundset);
175 //If found, then we are going to look for its new id (in backup tables)
177 //print_object($foundset); //Debug
178 //Iterate over foundset[2]. They are the old_ids
179 foreach($foundset[2] as $old_id) {
180 //We get the needed variables here (forum id)
181 $rec = backup_getid($restore->backup_unique_code
,"resource",$old_id);
182 //Personalize the searchstring
183 $searchstring='/\$@(RESOURCEVIEWBYR)\*('.$old_id.')@\$/';
184 //If it is a link to this course, update the link to its new location
187 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/resource/view.php?r='.$rec->new_id
,$result);
189 //It's a foreign link so leave it as original
190 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/resource/view.php?r='.$old_id,$result);
198 //This function makes all the necessary calls to xxxx_decode_content_links()
199 //function in each module, passing them the desired contents to be decoded
200 //from backup format to destination site/course in order to mantain inter-activities
201 //working in the backup/restore process. It's called from restore_decode_content_links()
202 //function in restore process
203 function resource_decode_content_links_caller($restore) {
207 if ($resources = get_records_sql ("SELECT r.id, r.alltext, r.summary, r.reference
208 FROM {$CFG->prefix}resource r
209 WHERE r.course = $restore->course_id")) {
211 $i = 0; //Counter to send some output to the browser to avoid timeouts
212 foreach ($resources as $resource) {
215 $content1 = $resource->alltext
;
216 $content2 = $resource->summary
;
217 $content3 = $resource->reference
;
218 $result1 = restore_decode_content_links_worker($content1,$restore);
219 $result2 = restore_decode_content_links_worker($content2,$restore);
220 $result3 = restore_decode_content_links_worker($content3,$restore);
222 if ($result1 != $content1 ||
$result2 != $content2 ||
$result3 != $content3) {
224 $resource->alltext
= addslashes($result1);
225 $resource->summary
= addslashes($result2);
226 $resource->reference
= addslashes($result3);
227 $status = update_record("resource",$resource);
229 if (!defined('RESTORE_SILENTLY')) {
230 echo '<br /><hr />'.s($content1).'<br />changed to<br />'.s($result1).'<hr /><br />';
231 echo '<br /><hr />'.s($content2).'<br />changed to<br />'.s($result2).'<hr /><br />';
232 echo '<br /><hr />'.s($content3).'<br />changed to<br />'.s($result3).'<hr /><br />';
237 if (($i+
1) %
5 == 0) {
238 if (!defined('RESTORE_SILENTLY')) {
240 if (($i+
1) %
100 == 0) {
251 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
252 //some texts in the module
253 function resource_restore_wiki2markdown ($restore) {
259 //Convert resource->alltext
260 if ($records = get_records_sql ("SELECT r.id, r.alltext, r.options
261 FROM {$CFG->prefix}resource r,
262 {$CFG->prefix}backup_ids b
263 WHERE r.course = $restore->course_id AND
264 options = ".FORMAT_WIKI
. " AND
265 b.backup_code = $restore->backup_unique_code AND
266 b.table_name = 'resource' AND
268 foreach ($records as $record) {
270 $record->alltext
= restore_decode_wiki_content($record->alltext
, $restore);
271 //Convert to Markdown
272 $wtm = new WikiToMarkdown();
273 $record->alltext
= $wtm->convert($record->alltext
, $restore->course_id
);
274 $record->options
= FORMAT_MARKDOWN
;
275 $status = update_record('resource', addslashes_object($record));
278 if (($i+
1) %
1 == 0) {
279 if (!defined('RESTORE_SILENTLY')) {
281 if (($i+
1) %
20 == 0) {
294 //This function returns a log record with all the necessay transformations
295 //done. It's used by restore_log_module() to restore modules log.
296 function resource_restore_logs($restore,$log) {
300 //Depending of the action, we recode different things
301 switch ($log->action
) {
304 //Get the new_id of the module (to recode the info field)
305 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
307 $log->url
= "view.php?id=".$log->cmid
;
308 $log->info
= $mod->new_id
;
315 //Get the new_id of the module (to recode the info field)
316 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
318 $log->url
= "view.php?id=".$log->cmid
;
319 $log->info
= $mod->new_id
;
326 //Get the new_id of the module (to recode the info field)
327 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
329 $log->url
= "view.php?id=".$log->cmid
;
330 $log->info
= $mod->new_id
;
336 $log->url
= "index.php?id=".$log->course
;
340 if (!defined('RESTORE_SILENTLY')) {
341 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug
352 function resource_restore_files($oldid,$newid,$resource,$restore) {
356 $status = check_dir_exists($CFG->dataroot
."/".$restore->course_id
,true);
358 // we need to do anything referenced by $resource->reference and anything in moddata/resource/instance
360 // do referenced files/dirs first.
361 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.'/course_files/'.$resource->reference
;
362 if (file_exists($temp_path)) { // ok, it was backed up, restore it.
363 $new_path = $CFG->dataroot
.'/'.$restore->course_id
.'/'.$resource->reference
;
365 // if this is somewhere deeply nested we need to do all the structure stuff first.....
366 $bits = explode('/',$resource->reference
);
368 for ($i = 0; $i< count($bits)-1; $i++
) {
369 $newbit .= $bits[$i].'/';
370 $status = $status && check_dir_exists($CFG->dataroot
.'/'.$restore->course_id
.'/'.$newbit,true);
372 $status = $status && backup_copy_file($temp_path,$new_path);
375 // and now for moddata.
376 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
377 "/moddata/resource/".$oldid;
378 if (file_exists($temp_path)) { // there's something to back up, restore it.
379 $new_path = $CFG->dataroot
."/".$restore->course_id
."/".$CFG->moddata
;
380 $status = $status && check_dir_exists($new_path,true);
381 $new_path .= '/resource';
382 $status = $status && check_dir_exists($new_path,true);
383 $new_path .= '/'.$newid;
384 $status = $status && backup_copy_file($temp_path,$new_path);