From: tjhunt Date: Wed, 14 Nov 2007 17:03:32 +0000 (+0000) Subject: MDL-12066 - Links in the HTML block are not recoded on backup and restore. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=43457dc87cacf5c83d5c7eec2a1f88d0feeba59d;p=moodle.git MDL-12066 - Links in the HTML block are not recoded on backup and restore. This is messy because the links are hidden in the configdata column, which is serialized and base64encoded. So we have to untangle that, then ask the block whether there are any bits of $config that need to be fixed, then re-encode it before backup up. And reverse that on restore. It needs to remain base64 encoded in the backup file, so that the file format is backwards compatible. I disucssed this with Eloy before doing it. Merged from MOODLE_19_STABLE. --- diff --git a/backup/backuplib.php b/backup/backuplib.php index 2b9aadc20e..99f0697d74 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 7b69462faa..f8759ddb79 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 ff539610cb..e6665e01bb 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 e8598cec1a..c0af39efe4 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.