]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-12066 - Links in the HTML block are not recoded on backup and restore.
authortjhunt <tjhunt>
Wed, 14 Nov 2007 17:03:32 +0000 (17:03 +0000)
committertjhunt <tjhunt>
Wed, 14 Nov 2007 17:03:32 +0000 (17:03 +0000)
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.

backup/backuplib.php
backup/restorelib.php
blocks/html/block_html.php
blocks/moodleblock.class.php

index 2b9aadc20e25551987c01f7efe62305d67ce2b49..99f0697d74c6c0d1b704f2568ed9680d5b63b441 100644 (file)
                         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));
index 7b69462faa80b2bfaec4acb38996c385018863a6..f8759ddb790980d01a570722a5d9096e9ed8b42b 100644 (file)
             }
         }
 
-        // TODO: process all html text also in blocks too
-
+        // Process all html text also in blocks too
+        if (!defined('RESTORE_SILENTLY')) {
+            echo '<li>' . 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 '<p>Updating config for block ', $instance->id, '.</p>';
+                    $instance->configdata = base64_encode(serialize($config));
+                    $status = $status && update_record('block_instance', $instance);
+                }
+            }
+        }
+        if (!defined('RESTORE_SILENTLY')) {
+            echo '</li>';
+        }
+        
         // Restore links in questions.
         require_once("$CFG->dirroot/question/restorelib.php");
         if (!defined('RESTORE_SILENTLY')) {
     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;
                         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;
index ff539610cbf49a9ceae4bd2ea2ae5b933599adf4..e6665e01bbf9562214cc4cb6b9eff7dab6a4b738 100755 (executable)
@@ -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;
+    }
 }
 ?>
index e8598cec1adb792dbcc2bfbab501bf307bde7d03..c0af39efe45aab6e6465e256171a0d349e9698fc 100644 (file)
@@ -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.