1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
<?php //$Id: restorelib.php,v 1.23 2006/04/11 16:19:17 stronk7 Exp $
//This php script contains all the stuff to backup/restore
//resource mods
//This is the "graphical" structure of the resource mod:
//
// resource
// (CL,pk->id,files)
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// CL->course level info
// UL->user level info
// files->table may have files)
//
//-----------------------------------------------------------
//This function executes all the restore procedure about this mod
function resource_restore_mods($mod,$restore) {
global $CFG;
$status = true;
//Get record from backup_ids
$data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the RESOURCE record structure
$resource->course = $restore->course_id;
$resource->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$resource->type = $info['MOD']['#']['TYPE']['0']['#'];
$resource->reference = backup_todb($info['MOD']['#']['REFERENCE']['0']['#']);
$resource->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
$resource->alltext = backup_todb($info['MOD']['#']['ALLTEXT']['0']['#']);
$resource->popup = backup_todb($info['MOD']['#']['POPUP']['0']['#']);
$resource->options = backup_todb($info['MOD']['#']['OPTIONS']['0']['#']);
$resource->timemodified = $info['MOD']['#']['TIMEMODIFIED']['0']['#'];
//To mantain compatibility, in 1.4 the type and alltext meaning has changed and
//two new fields have arrived (popup and options). We have to modify somethigs
//if the popup field isn't present in the backup file to be upwards compatible.
if (! isset($info['MOD']['#']['POPUP']['0']['#'])) { //It's a pre-14 backup file
//Move alltext to popup in 3 and 5 resource types
if ($resource->type == 3 || $resource->type == 5) {
$resource->popup = $resource->alltext;
$resource->alltext = '';
}
//Move reference to alltext for references
if ($resource->type == 1) {
$resource->popup = '';
$resource->alltext = $resource->reference;
$resource->reference = '';
}
//Reencode the type field to its new values and fill the options field as needed
//Array 1-9 of new types
$types = array ('','html','file','file','text','file',
'html','file','text','directory');
//Array 1-9 of corresponding options
$options = array ('','','frame','','0','',
'','','3','');
//Make the conversion
$oldtype = $resource->type;
$resource->type = $types[$oldtype];
$resource->options = $options[$oldtype];
}
if ($resource->type == 'reference') { // Obsolete type of resource
$resource->type == 'html';
$resource->alltext = $resource->reference;
$resource->reference = '';
}
//The structure is equal to the db, so insert the resource
$newid = insert_record ("resource",$resource);
//Do some output
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("modulename","resource")." \"".format_string(stripslashes($resource->name),true)."\"</li>";
}
backup_flush(300);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,$mod->modtype,
$mod->id, $newid);
// restore any associated files...
if ($resource->type == 'file' || $resource->type == 'directory' || $resource->type == 'ims') {
resource_restore_files($mod->id,$newid,$resource,$restore);
}
} else {
$status = false;
}
} else {
$status = false;
}
return $status;
}
//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//resource_decode_content_links_caller() function in each module
//in the restore process
function resource_decode_content_links ($content,$restore) {
global $CFG;
$result = $content;
//Link to the list of resources
$searchstring='/\$@(RESOURCEINDEX)\*([0-9]+)@\$/';
//We look for it
preg_match_all($searchstring,$content,$foundset);
//If found, then we are going to look for its new id (in backup tables)
if ($foundset[0]) {
//print_object($foundset); //Debug
//Iterate over foundset[2]. They are the old_ids
foreach($foundset[2] as $old_id) {
//We get the needed variables here (course id)
$rec = backup_getid($restore->backup_unique_code,"course",$old_id);
//Personalize the searchstring
$searchstring='/\$@(RESOURCEINDEX)\*('.$old_id.')@\$/';
//If it is a link to this course, update the link to its new location
if($rec->new_id) {
//Now replace it
$result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resource/index.php?id='.$rec->new_id,$result);
} else {
//It's a foreign link so leave it as original
$result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resource/index.php?id='.$old_id,$result);
}
}
}
//Link to resource view by moduleid
$searchstring='/\$@(RESOURCEVIEWBYID)\*([0-9]+)@\$/';
//We look for it
preg_match_all($searchstring,$result,$foundset);
//If found, then we are going to look for its new id (in backup tables)
if ($foundset[0]) {
//print_object($foundset); //Debug
//Iterate over foundset[2]. They are the old_ids
foreach($foundset[2] as $old_id) {
//We get the needed variables here (course_modules id)
$rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
//Personalize the searchstring
$searchstring='/\$@(RESOURCEVIEWBYID)\*('.$old_id.')@\$/';
//If it is a link to this course, update the link to its new location
if($rec->new_id) {
//Now replace it
$result= preg_replace($searchstring,$CFG->wwwroot.'/mod/resource/view.php?id='.$rec->new_id,$result);
} else {
//It's a foreign link so leave it as original
$result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/resource/view.php?id='.$old_id,$result);
}
}
}
return $result;
}
//This function makes all the necessary calls to xxxx_decode_content_links()
//function in each module, passing them the desired contents to be decoded
//from backup format to destination site/course in order to mantain inter-activities
//working in the backup/restore process. It's called from restore_decode_content_links()
//function in restore process
function resource_decode_content_links_caller($restore) {
global $CFG;
$status = true;
if ($resources = get_records_sql ("SELECT r.id, r.alltext, r.summary, r.reference
FROM {$CFG->prefix}resource r
WHERE r.course = $restore->course_id")) {
$i = 0; //Counter to send some output to the browser to avoid timeouts
foreach ($resources as $resource) {
//Increment counter
$i++;
$content1 = $resource->alltext;
$content2 = $resource->summary;
$content3 = $resource->reference;
$result1 = restore_decode_content_links_worker($content1,$restore);
$result2 = restore_decode_content_links_worker($content2,$restore);
$result3 = restore_decode_content_links_worker($content3,$restore);
if ($result1 != $content1 || $result2 != $content2 || $result3 != $content3) {
//Update record
$resource->alltext = addslashes($result1);
$resource->summary = addslashes($result2);
$resource->reference = addslashes($result3);
$status = update_record("resource",$resource);
if ($CFG->debug>7) {
if (!defined('RESTORE_SILENTLY')) {
echo '<br /><hr />'.s($content1).'<br />changed to<br />'.s($result1).'<hr /><br />';
echo '<br /><hr />'.s($content2).'<br />changed to<br />'.s($result2).'<hr /><br />';
echo '<br /><hr />'.s($content3).'<br />changed to<br />'.s($result3).'<hr /><br />';
}
}
}
//Do some output
if (($i+1) % 5 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 100 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
return $status;
}
//This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
//some texts in the module
function resource_restore_wiki2markdown ($restore) {
global $CFG;
$status = true;
//Convert resource->alltext
if ($records = get_records_sql ("SELECT r.id, r.alltext, r.options
FROM {$CFG->prefix}resource r,
{$CFG->prefix}backup_ids b
WHERE r.course = $restore->course_id AND
options = ".FORMAT_WIKI. " AND
b.backup_code = $restore->backup_unique_code AND
b.table_name = 'resource' AND
b.new_id = r.id")) {
foreach ($records as $record) {
//Rebuild wiki links
$record->alltext = restore_decode_wiki_content($record->alltext, $restore);
//Convert to Markdown
$wtm = new WikiToMarkdown();
$record->alltext = $wtm->convert($record->alltext, $restore->course_id);
$record->options = FORMAT_MARKDOWN;
$status = update_record('resource', addslashes_object($record));
//Do some output
$i++;
if (($i+1) % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
return $status;
}
//This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.
function resource_restore_logs($restore,$log) {
$status = false;
//Depending of the action, we recode different things
switch ($log->action) {
case "add":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "update":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "view":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "view all":
$log->url = "index.php?id=".$log->course;
$status = true;
break;
default:
if (!defined('RESTORE_SILENTLY')) {
echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
}
break;
}
if ($status) {
$status = $log;
}
return $status;
}
function resource_restore_files($oldid,$newid,$resource,$restore) {
global $CFG;
$status = true;
$status = check_dir_exists($CFG->dataroot."/".$restore->course_id,true);
// we need to do anything referenced by $resource->reference and anything in moddata/resource/instance
// do referenced files/dirs first.
$temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.'/course_files/'.$resource->reference;
if (file_exists($temp_path)) { // ok, it was backed up, restore it.
$new_path = $CFG->dataroot.'/'.$restore->course_id.'/'.$resource->reference;
// if this is somewhere deeply nested we need to do all the structure stuff first.....
$bits = explode('/',$resource->reference);
$newbit = '';
for ($i = 0; $i< count($bits)-1; $i++) {
$newbit .= $bits[$i].'/';
$status = $status && check_dir_exists($CFG->dataroot.'/'.$restore->course_id.'/'.$newbit,true);
}
$status = $status && backup_copy_file($temp_path,$new_path);
}
// and now for moddata.
$temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
"/moddata/resource/".$oldid;
if (file_exists($temp_path)) { // there's something to back up, restore it.
$new_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
$status = $status && check_dir_exists($new_path,true);
$new_path .= '/resource';
$status = $status && check_dir_exists($new_path,true);
$new_path .= '/'.$newid;
$status = $status && backup_copy_file($temp_path,$new_path);
}
return $status;
}
?>
|