diff --git a/backup/backuplib.php b/backup/backuplib.php index 3d267f08e87..5efee1ba92b 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -957,8 +957,16 @@ if(empty($blocks[$instance->blockid]->name)) { continue; } - //Begin Block + //Give the block a chance to process any links in configdata. + if (!isset($blocks[$instance->blockid]->blockobject)) { + $blocks[$instance->blockid]->blockobject = block_instance($blocks[$instance->blockid]->name); + } + $config = unserialize(base64_decode($instance->configdata)); + $blocks[$instance->blockid]->blockobject->backup_encode_absolute_links_in_config($config); + $instance->configdata = base64_encode(serialize($config)); + + //Begin Block fwrite ($bf,start_tag('BLOCK',3,true)); fwrite ($bf,full_tag('ID', 4, false,$instance->id)); fwrite ($bf,full_tag('NAME',4,false,$blocks[$instance->blockid]->name)); diff --git a/backup/restorelib.php b/backup/restorelib.php index 7b69462faa8..f8759ddb790 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -90,8 +90,29 @@ } } - // TODO: process all html text also in blocks too - + // Process all html text also in blocks too + if (!defined('RESTORE_SILENTLY')) { + echo '
  • ' . get_string ('from') . ' ' . get_string('blocks'); + } + if (!empty($restore->blockinstanceids)) { + $blocks = blocks_get_record(); + $instances = get_records_list('block_instance', 'id', implode(',', $restore->blockinstanceids), '', 'id,blockid,configdata'); + foreach ($instances as $instance) { + if (!isset($blocks[$instance->blockid]->blockobject)) { + $blocks[$instance->blockid]->blockobject = block_instance($blocks[$instance->blockid]->name); + } + $config = unserialize(base64_decode($instance->configdata)); + if ($blocks[$instance->blockid]->blockobject->restore_decode_absolute_links_in_config($config)) { +echo '

    Updating config for block ', $instance->id, '.

    '; + $instance->configdata = base64_encode(serialize($config)); + $status = $status && update_record('block_instance', $instance); + } + } + } + if (!defined('RESTORE_SILENTLY')) { + echo '
  • '; + } + // Restore links in questions. require_once("$CFG->dirroot/question/restorelib.php"); if (!defined('RESTORE_SILENTLY')) { @@ -759,6 +780,11 @@ function restore_create_block_instances($restore,$xml_file) { $status = true; + + // Tracks which blocks we create during the restore. + // This is used in restore_decode_content_links. + $restore->blockinstanceids = array(); + //Check it exists if (!file_exists($xml_file)) { $status = false; @@ -851,6 +877,7 @@ if (!empty($instance->id)) { // this will only be set if we come from 1.7 and above backups backup_putid ($restore->backup_unique_code,"block_instance",$instance->id,$newid); } + $restore->blockinstanceids[] = $newid; } else { $status = false; break; diff --git a/blocks/html/block_html.php b/blocks/html/block_html.php index ff539610cbf..e6665e01bbf 100755 --- a/blocks/html/block_html.php +++ b/blocks/html/block_html.php @@ -35,5 +35,16 @@ class block_html extends block_base { return $this->content; } + + function backup_encode_absolute_links_in_config(&$config) { + $config->text = backup_encode_absolute_links($config->text); + } + + function restore_decode_absolute_links_in_config(&$config) { + debugging("In block_html::restore_decode_absolute_links_in_config"); // DONOTCOMMIT + $oldtext = $config->text; + $config->text = restore_decode_absolute_links($oldtext); + return $config->text != $oldtext; + } } ?> diff --git a/blocks/moodleblock.class.php b/blocks/moodleblock.class.php index e8598cec1ad..c0af39efe45 100644 --- a/blocks/moodleblock.class.php +++ b/blocks/moodleblock.class.php @@ -125,6 +125,28 @@ class block_base { function after_restore($restore) { } + /** + * Will be called before an instance of this block is backed up, so that any links in + * any links in any HTML fields on config can be encoded. For example, for the HTML block + * we need to do $config->text = backup_encode_absolute_links($config->text);. + * + * @param object $config the config field for an insance of this class. + */ + function backup_encode_absolute_links_in_config(&$config) { + } + + /** + * Undo the effect of backup_encode_absolute_links_in_config. For exmaple, in the + * HTML block we need to do $config->text = restore_decode_absolute_links($config->text); + * + * @param object $config the config field for an insance of this class. + * @return boolean return true if something has changed, so the database can be updated, + * false if not, for efficiency reasons. + */ + function restore_decode_absolute_links_in_config(&$config) { + return false; + } + /** * Returns the block name, as present in the class name, * the database, the block directory, etc etc.