Skip to content

Commit

Permalink
Improved automatic relinking in backup and restore. Credits go to sko…
Browse files Browse the repository at this point in the history
…dak.

Complete refactoring of the system that was really awful (my fault!).
Now everything is in its place and working like a charm, making things really
easier to be implemented and amplied. Bug 3678
(http://moodle.org/bugs/bug.php?op=show&bugid=3678)
(http://moodle.org/mod/forum/discuss.php?d=26530)
  • Loading branch information
stronk7 committed Jul 4, 2005
1 parent 6cd4abd commit 69731e2
Show file tree
Hide file tree
Showing 22 changed files with 1,281 additions and 357 deletions.
21 changes: 17 additions & 4 deletions backup/restorelib.php
Expand Up @@ -63,9 +63,6 @@ function restore_refresh_events($restore) {
//from backup format to destination site/course in order to mantain inter-activities //from backup format to destination site/course in order to mantain inter-activities
//working in the backup/restore process //working in the backup/restore process
function restore_decode_content_links($restore) { function restore_decode_content_links($restore) {

global $CFG;

$status = true; $status = true;


echo "<ul>"; echo "<ul>";
Expand All @@ -75,15 +72,31 @@ function restore_decode_content_links($restore) {
//Check if the xxxx_decode_content_links_caller exists //Check if the xxxx_decode_content_links_caller exists
$function_name = $name."_decode_content_links_caller"; $function_name = $name."_decode_content_links_caller";
if (function_exists($function_name)) { if (function_exists($function_name)) {
echo "<li>".get_string ("to")." ".get_string("modulenameplural",$name); echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
$status = $function_name($restore); $status = $function_name($restore);
echo '</li>'; echo '</li>';
} }
} }
} }
echo "</ul>"; echo "</ul>";

// TODO: process all html text also in blocks too

return $status; return $status;
} }

//This function is called from all xxxx_decode_content_links_caller(),
//its task is to ask all modules (maybe other linkable objects) to restore
//links to them.
function restore_decode_content_links_worker($content,$restore) {
foreach($restore->mods as $name => $info) {
$function_name = $name."_decode_content_links";
if (function_exists($function_name)) {
$content = $function_name($content,$restore);
}
}
return $content;
}


//This function converts all the wiki texts in the restored course //This function converts all the wiki texts in the restored course
//to the Markdown format. Used only for backup files prior 2005041100. //to the Markdown format. Used only for backup files prior 2005041100.
Expand Down
14 changes: 14 additions & 0 deletions mod/assignment/backuplib.php
Expand Up @@ -156,10 +156,24 @@ function assignment_check_backup_mods($course,$user_data=false,$backup_unique_co
return $info; return $info;
} }


//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function assignment_encode_content_links ($content,$preferences) {


global $CFG;

$base = preg_quote($CFG->wwwroot,"/");


//Link to the list of assignments
$buscar="/(".$base."\/mod\/assignment\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@ASSIGNMENTINDEX*$2@$',$content);


//Link to assignment view by moduleid
$buscar="/(".$base."\/mod\/assignment\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@ASSIGNMENTVIEWBYID*$2@$',$result);


return $result;
}


// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE


Expand Down
103 changes: 103 additions & 0 deletions mod/assignment/restorelib.php
Expand Up @@ -232,6 +232,109 @@ function assignment_restore_files ($oldassid, $newassid, $olduserid, $newuserid,
return $status; return $status;
} }


//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//assignment_decode_content_links_caller() function in each module
//in the restore process
function assignment_decode_content_links ($content,$restore) {

global $CFG;

$result = $content;

//Link to the list of assignments

$searchstring='/\$@(ASSIGNMENTINDEX)\*([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='/\$@(ASSIGNMENTINDEX)\*('.$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/assignment/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/assignment/index.php?id='.$old_id,$result);
}
}
}

//Link to assignment view by moduleid

$searchstring='/\$@(ASSIGNMENTVIEWBYID)\*([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='/\$@(ASSIGNMENTVIEWBYID)\*('.$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/assignment/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/assignment/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 assignment_decode_content_links_caller($restore) {
global $CFG;
$status = true;

if ($assignments = get_records_sql ("SELECT a.id, a.description
FROM {$CFG->prefix}assignment a
WHERE a.course = $restore->course_id")) {
//Iterate over each assignment->description
$i = 0; //Counter to send some output to the browser to avoid timeouts
foreach ($assignments as $assignment) {
//Increment counter
$i++;
$content = $assignment->description;
$result = restore_decode_content_links_worker($content,$restore);
if ($result != $content) {
//Update record
$assignment->description = addslashes($result);
$status = update_record("assignment",$assignment);
if ($CFG->debug>7) {
echo "<br /><hr />".$content."<br />changed to<br />".$result."<hr /><br />";
}
}
//Do some output
if (($i+1) % 5 == 0) {
echo ".";
if (($i+1) % 100 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
}
return $status;
}

//This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
//some texts in the module //some texts in the module
function assignment_restore_wiki2markdown ($restore) { function assignment_restore_wiki2markdown ($restore) {
Expand Down
14 changes: 14 additions & 0 deletions mod/chat/backuplib.php
Expand Up @@ -109,10 +109,24 @@ function chat_check_backup_mods($course,$user_data=false,$backup_unique_code) {
return $info; return $info;
} }


//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function chat_encode_content_links ($content,$preferences) {


global $CFG;

$base = preg_quote($CFG->wwwroot,"/");


//Link to the list of chats
$buscar="/(".$base."\/mod\/chat\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHATINDEX*$2@$',$content);


//Link to chat view by moduleid
$buscar="/(".$base."\/mod\/chat\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHATVIEWBYID*$2@$',$result);


return $result;
}


// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE


Expand Down
104 changes: 104 additions & 0 deletions mod/chat/restorelib.php
Expand Up @@ -130,6 +130,110 @@ function chat_messages_restore_mods($old_chat_id, $new_chat_id,$info,$restore) {
return $status; return $status;
} }


//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//chat_decode_content_links_caller() function in each module
//in the restore process
function chat_decode_content_links ($content,$restore) {

global $CFG;

$result = $content;

//Link to the list of chats

$searchstring='/\$@(CHATINDEX)\*([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='/\$@(CHATINDEX)\*('.$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/chat/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/chat/index.php?id='.$old_id,$result);
}
}
}

//Link to chat view by moduleid

$searchstring='/\$@(CHATVIEWBYID)\*([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='/\$@(CHATVIEWBYID)\*('.$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/chat/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/chat/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 chat_decode_content_links_caller($restore) {
global $CFG;
$status = true;

if ($chats = get_records_sql ("SELECT c.id, c.intro
FROM {$CFG->prefix}chat c
WHERE c.course = $restore->course_id")) {
//Iterate over each chat->intro
$i = 0; //Counter to send some output to the browser to avoid timeouts
foreach ($chats as $chat) {
//Increment counter
$i++;
$content = $chat->intro;
$result = restore_decode_content_links_worker($content,$restore);
if ($result != $content) {
//Update record
$chat->intro = addslashes($result);
$status = update_record("chat",$chat);
if ($CFG->debug>7) {
echo "<br /><hr />".$content."<br />changed to</br>".$result."<hr /><br />";
}
}
//Do some output
if (($i+1) % 5 == 0) {
echo ".";
if (($i+1) % 100 == 0) {
echo "<br />";
}
backup_flush(300);
}
}
}

return $status;
}

//This function returns a log record with all the necessay transformations //This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log. //done. It's used by restore_log_module() to restore modules log.
function chat_restore_logs($restore,$log) { function chat_restore_logs($restore,$log) {
Expand Down
17 changes: 17 additions & 0 deletions mod/choice/backuplib.php
Expand Up @@ -151,7 +151,24 @@ function choice_check_backup_mods($course,$user_data=false,$backup_unique_code)
return $info; return $info;
} }


//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function choice_encode_content_links ($content,$preferences) {


global $CFG;

$base = preg_quote($CFG->wwwroot,"/");

//Link to the list of choices
$buscar="/(".$base."\/mod\/choice\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHOICEINDEX*$2@$',$content);

//Link to choice view by moduleid
$buscar="/(".$base."\/mod\/choice\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHOICEVIEWBYID*$2@$',$result);

return $result;
}


// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE


Expand Down

0 comments on commit 69731e2

Please sign in to comment.