--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-helper
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Helper class in charge of providing the contents to be processed by restore_decode_rules
+ *
+ * This class is in charge of looking (in DB) for the contents needing to be
+ * processed by the declared restore_decode_rules. Basically it iterates over
+ * one recordset (optimised by joining them with backup_ids records), retrieving
+ * them from DB, delegating process to the restore_plan and storing results back
+ * to DB.
+ *
+ * Implements one visitor-like pattern so the decode_processor will visit it
+ * to get all the contents processed by its defined rules
+ *
+ * TODO: Complete phpdocs
+ */
+class restore_decode_content implements processable {
+
+ protected $tablename; // Name, without prefix, of the table we are going to retrieve contents
+ protected $fields; // Array of fields we are going to decode in that table (usually 1)
+ protected $mapping; // Mapping (itemname) in backup_ids used to determine target ids (defaults to $tablename)
+
+ protected $restoreid; // Unique id of the restore operation we are running
+ protected $iterator; // The iterator for this content (usually one recordset)
+
+ public function __construct($tablename, $fields, $mapping = null) {
+ // TODO: check table exists
+ // TODO: check fields exist
+ $this->tablename = $tablename;
+ $this->fields = !is_array($fields) ? array($fields) : $fields; // Accept string/array
+ $this->mapping = is_null($mapping) ? $tablename : $mapping; // Default to tableanme
+ $this->restoreid = 0;
+ }
+
+ public function set_restoreid($restoreid) {
+ $this->restoreid = $restoreid;
+ }
+
+ public function process($processor) {
+ if (!$processor instanceof restore_decode_processor) { // No correct processor, throw exception
+ throw new restore_decode_content_exception('incorrect_restore_decode_processor', get_class($processor));
+ }
+ if (!$this->restoreid) { // Check restoreid is set
+ throw new restore_decode_rule_exception('decode_content_restoreid_not_set');
+ }
+
+ // Get the iterator of contents
+ $it = $this->get_iterator();
+ foreach ($it as $itrow) { // Iterate over rows
+ $itrowarr = (array)$itrow; // Array-ize for clean access
+ $rowchanged = false; // To track changes in the row
+ foreach ($this->fields as $field) { // Iterate for each field
+ $content = $this->preprocess_field($itrowarr[$field]); // Apply potential pre-transformations
+ if ($result = $processor->decode_content($content)) {
+ $itrowarr[$field] = $this->postprocess_field($result); // Apply potential post-transformations
+ $rowchanged = true;
+ }
+ }
+ if ($rowchanged) { // Change detected, perform update in the row
+ $this->update_iterator_row($itrowarr);
+ }
+ }
+ $it->close(); // Always close the iterator at the end
+ }
+
+// Protected API starts here
+
+ protected function get_iterator() {
+ global $DB;
+
+ // Build the SQL dynamically here
+ $fieldslist = 't.' . implode(', t.', $this->fields);
+ $sql = "SELECT t.id, $fieldslist
+ FROM {" . $this->tablename . "} t
+ JOIN {backup_ids_temp} b ON b.newitemid = t.id
+ WHERE b.backupid = ?
+ AND b.itemname = ?";
+ $params = array($this->restoreid, $this->mapping);
+ return ($DB->get_recordset_sql($sql, $params));
+ }
+
+ protected function update_iterator_row($row) {
+ global $DB;
+ $DB->update_record($this->tablename, $row);
+ }
+
+ protected function preprocess_field($field) {
+ return $field;
+ }
+
+ protected function postprocess_field($field) {
+ return $field;
+ }
+}
+
+/*
+ * Exception class used by all the @restore_decode_content stuff
+ */
+class restore_decode_content_exception extends backup_exception {
+
+ public function __construct($errorcode, $a=NULL, $debuginfo=null) {
+ return parent::__construct($errorcode, $a, $debuginfo);
+ }
+}
--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-helper
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Helper class that will perform all the necessary decoding tasks on restore
+ *
+ * This class will register all the restore_decode_content and
+ * restore_decode_rule instances defined by the restore tasks
+ * in order to perform the complete decoding of links in the
+ * final task of the restore_plan execution.
+ *
+ * By visiting each content provider will apply all the defined rules
+ *
+ * TODO: Complete phpdocs
+ */
+class restore_decode_processor {
+
+ protected $contents; // Array of restore_decode_content providers
+ protected $rules; // Array of restore_decode_rule workers
+ protected $restoreid; // The unique restoreid we are executing
+ protected $sourcewwwroot; // The original wwwroot of the backup file
+ protected $targetwwwroot; // The targer wwwroot of the restore operation
+
+ public function __construct($restoreid, $sourcewwwroot, $targetwwwroot) {
+ $this->restoreid = $restoreid;
+ $this->sourcewwwroot = $sourcewwwroot;
+ $this->targetwwwroot = $targetwwwroot;
+
+ $this->contents = array();
+ $this->rules = array();
+ }
+
+ public function add_content($content) {
+ if (!$content instanceof restore_decode_content) {
+ throw new restore_decode_processor_exception('incorrect_restore_decode_content', get_class($content));
+ }
+ $content->set_restoreid($this->restoreid);
+ $this->contents[] = $content;
+ }
+
+ public function add_rule($rule) {
+ if (!$rule instanceof restore_decode_rule) {
+ throw new restore_decode_processor_exception('incorrect_restore_decode_rule', get_class($content));
+ }
+ $rule->set_restoreid($this->restoreid);
+ $rule->set_wwwroots($this->sourcewwwroot, $this->targetwwwroot);
+ $this->rules[] = $rule;
+ }
+
+ /**
+ * Visit all the restore_decode_content providers
+ * that will cause decode_content() to be called
+ * for each content
+ */
+ public function execute() {
+ // Iterate over all contents, visiting them
+ foreach ($this->contents as $content) {
+ $content->process($this);
+ }
+ }
+
+ /**
+ * Receive content from restore_decode_content objects
+ * and apply all the restore_decode_rules to them
+ */
+ public function decode_content($content) {
+ if (!$content = $this->precheck_content($content)) { // Perform some prechecks
+ return false;
+ }
+ // Iterate over all rules, chaining results
+ foreach ($this->rules as $rule) {
+ $content = $rule->decode($content);
+ }
+ return $content;
+ }
+
+ /**
+ * Adds all the course/section/activity/block contents and rules
+ */
+ public static function register_link_decoders($processor) {
+ $tasks = array(); // To get the list of tasks having decoders
+
+ // Add the course task
+ $tasks[] = 'restore_course_task';
+
+ // Add the section task
+ $tasks[] = 'restore_section_task';
+
+ // Add the module tasks
+ $mods = get_plugin_list('mod');
+ foreach ($mods as $mod => $moddir) {
+ if (class_exists('restore_' . $mod . '_activity_task')) {
+ $tasks[] = 'restore_' . $mod . '_activity_task';
+ }
+ }
+
+ // Add the default block task
+ $tasks[] = 'restore_default_block_task';
+
+ // Add the custom block tasks
+ $blocks = get_plugin_list('block');
+ foreach ($blocks as $block => $blockdir) {
+ if (class_exists('restore_' . $block . '_block_task')) {
+ $tasks[] = 'restore_' . $block . '_block_task';
+ }
+ }
+
+ // Add the course format ones
+ // TODO: Same than blocks, need to know how courseformats are going to handle restore
+
+ // Add local encodes
+ // TODO: Any interest? 1.9 never had that.
+
+ // We have all the tasks registered, let's iterate over them, getting
+ // contents and rules and adding them to the processor
+ foreach ($tasks as $classname) {
+ // Get restore_decode_content array and add to processor
+ $contents = call_user_func(array($classname, 'define_decode_contents'));
+ if (!is_array($contents)) {
+ throw new restore_decode_processor_exception('define_decode_contents_not_array', $classname);
+ }
+ foreach ($contents as $content) {
+ $processor->add_content($content);
+ }
+ // Get restore_decode_rule array and add to processor
+ $rules = call_user_func(array($classname, 'define_decode_rules'));
+ if (!is_array($rules)) {
+ throw new restore_decode_processor_exception('define_decode_rules_not_array', $classname);
+ }
+ foreach ($rules as $rule) {
+ $processor->add_rule($rule);
+ }
+ }
+ }
+
+// Protected API starts here
+
+ /**
+ * Perform some general checks in content. Returning false rules processing is skipped
+ */
+ protected function precheck_content($content) {
+ // TODO: look for $@ in content
+ return $content;
+ }
+}
+
+/*
+ * Exception class used by all the @restore_decode_content stuff
+ */
+class restore_decode_processor_exception extends backup_exception {
+
+ public function __construct($errorcode, $a=NULL, $debuginfo=null) {
+ return parent::__construct($errorcode, $a, $debuginfo);
+ }
+}
--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-helper
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Helper class used to decode links back to their original form
+ *
+ * This class allows each restore task to specify the changes that
+ * will be applied to any encoded (by backup) link to revert it back
+ * to its original form, recoding any parameter as needed.
+ *
+ * TODO: Complete phpdocs
+ */
+class restore_decode_rule {
+
+ protected $linkname; // How the link has been encoded in backup (CHOICEVIEWBYID, COURSEVIEWBYID...)
+ protected $urltemplate; // How the original URL looks like, with dollar placeholders
+ protected $mappings; // Which backup_ids mappings do we need to apply for replacing the placeholders
+ protected $restoreid; // The unique restoreid we are executing
+ protected $sourcewwwroot; // The original wwwroot of the backup file
+ protected $targetwwwroot; // The targer wwwroot of the restore operation
+
+ protected $cregexp; // Calculated regular expresion we'll be looking for matches
+
+ public function __construct($linkname, $urltemplate, $mappings) {
+ // Validate all the params are ok
+ $this->mappings = $this->validate_params($linkname, $urltemplate, $mappings);
+ $this->linkname = $linkname;
+ $this->urltemplate = $urltemplate;
+ $this->restoreid = 0;
+ $this->sourcewwwroot = '';
+ $this->targetwwwroot = ''; // yes, uses to be $CFG->wwwroot, and? ;-)
+ $this->cregexp = $this->get_calculated_regexp();
+ }
+
+ public function set_restoreid($restoreid) {
+ $this->restoreid = $restoreid;
+ }
+
+ public function set_wwwroots($sourcewwwroot, $targetwwwroot) {
+ $this->sourcewwwroot = $sourcewwwroot;
+ $this->targetwwwroot = $targetwwwroot;
+ }
+
+ public function decode($content) {
+ if (preg_match_all($this->cregexp, $content, $matches) === 0) { // 0 matches, nothing to change
+ return $content;
+ }
+ // Have found matches, iterate over them
+ foreach ($matches[0] as $key => $tosearch) {
+ $mappingsok = true; // To detect if any mapping has failed
+ $placeholdersarr = array(); // The placeholders to be replaced
+ $mappingssourcearr = array(); // To store the original mappings values
+ $mappingstargetarr = array(); // To store the target mappings values
+ $toreplace = $this->urltemplate;// The template used to build the replacement
+ foreach ($this->mappings as $mappingkey => $mappingsource) {
+ $source = $matches[$mappingkey][$key]; // get source
+ $mappingssourcearr[$mappingkey] = $source; // set source arr
+ $mappingstargetarr[$mappingkey] = 0; // apply default mapping
+ $placeholdersarr[$mappingkey] = '$'.$mappingkey;// set the placeholders arr
+ if (!$mappingsok) { // already missing some mapping, continue
+ continue;
+ }
+ if (!$target = $this->get_mapping($mappingsource, $source)) {// mapping not found, mark and continue
+ $mappingsok = false;
+ continue;
+ }
+ $mappingstargetarr[$mappingkey] = $target; // store found mapping
+ }
+ $toreplace = $this->apply_modifications($toreplace, $mappingsok); // Apply other changes before replacement
+ if (!$mappingsok) { // Some mapping has failed, apply original values to the template
+ $toreplace = str_replace($placeholdersarr, $mappingssourcearr, $toreplace);
+
+ } else { // All mappings found, apply target values to the template
+ $toreplace = str_replace($placeholdersarr, $mappingstargetarr, $toreplace);
+ }
+ // Finally, perform the replacement in original content
+ $content = str_replace($tosearch, $toreplace, $content);
+ }
+ return $content; // return the decoded content, pointing to original or target values
+ }
+
+// Protected API starts here
+
+ /**
+ * Looks for mapping values in backup_ids table, simple wrapper over get_backup_ids_record
+ */
+ protected function get_mapping($itemname, $itemid) {
+ // Check restoreid is set
+ if (!$this->restoreid) {
+ throw new restore_decode_rule_exception('decode_rule_restoreid_not_set');
+ }
+ if (!$found = restore_dbops::get_backup_ids_record($this->restoreid, $itemname, $itemid)) {
+ return false;
+ }
+ return $found->newitemid;
+ }
+
+ /**
+ * Apply other modifications, based in the result of $mappingsok before placeholder replacements
+ *
+ * Right now, simply prefix with the proper wwwroot (source/target)
+ */
+ protected function apply_modifications($toreplace, $mappingsok) {
+ // Check wwwroots are set
+ if (!$this->targetwwwroot || !$this->sourcewwwroot) {
+ throw new restore_decode_rule_exception('decode_rule_wwwroots_not_set');
+ }
+ return ($mappingsok ? $this->targetwwwroot : $this->sourcewwwroot) . $toreplace;
+ }
+
+ /**
+ * Perform all the validations and checks on the rule attributes
+ */
+ protected function validate_params($linkname, $urltemplate, $mappings) {
+ // Check linkname is A-Z0-9
+ if (empty($linkname) || preg_match('/[^A-Z0-9]/', $linkname)) {
+ throw new restore_decode_rule_exception('decode_rule_incorrect_name', $linkname);
+ }
+ // Look urltemplate starts by /
+ if (empty($urltemplate) || substr($urltemplate, 0, 1) != '/') {
+ throw new restore_decode_rule_exception('decode_rule_incorrect_urltemplate', $urltemplate);
+ }
+ if (!is_array($mappings)) {
+ $mappings = array($mappings);
+ }
+ // Look for placeholders in template
+ $countph = preg_match_all('/(\$\d+)/', $urltemplate, $matches);
+ $countma = count($mappings);
+ // Check mappings number matches placeholders
+ if ($countph != $countma) {
+ $msg = new stdclass();
+ $a->placeholders = $countph;
+ $a->mappings = $countma;
+ throw new restore_decode_rule_exception('decode_rule_mappings_incorrect_count', $a);
+ }
+ // Verify they are consecutive (starting on 1)
+ $smatches = str_replace('$', '', $matches[1]);
+ sort($smatches, SORT_NUMERIC);
+ if (reset($smatches) != 1 || end($smatches) != $countma) {
+ throw new restore_decode_rule_exception('decode_rule_nonconsecutive_placeholders', implode(', ', $smatches));
+ }
+ // No dupes in placeholders
+ if (count($smatches) != count(array_unique($smatches))) {
+ throw new restore_decode_rule_exception('decode_rule_duplicate_placeholders', implode(', ', $smatches));
+ }
+
+ // Return one array of placeholders as keys and mappings as values
+ return array_combine($smatches, $mappings);
+ }
+
+ /**
+ * based on rule definition, build the regular expression to execute on decode
+ */
+ protected function get_calculated_regexp() {
+ $regexp = '/\$@' . $this->linkname;
+ foreach ($this->mappings as $key => $value) {
+ $regexp .= '\*(\d+)';
+ }
+ $regexp .= '@\$/';
+ return $regexp;
+ }
+}
+
+/*
+ * Exception class used by all the @restore_decode_rule stuff
+ */
+class restore_decode_rule_exception extends backup_exception {
+
+ public function __construct($errorcode, $a=NULL, $debuginfo=null) {
+ return parent::__construct($errorcode, $a, $debuginfo);
+ }
+}
--- /dev/null
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-tests
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// Prevent direct access to this file
+if (!defined('MOODLE_INTERNAL')) {
+ die('Direct access to this script is forbidden.');
+}
+
+// Include all the needed stuff
+require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
+
+/*
+ * restore_decode tests (both rule and content)
+ */
+class restore_decode_test extends UnitTestCase {
+
+ public static $includecoverage = array(
+ 'backup/util/helper/restore_decode_rule.class.php',
+ 'backup/util/helper/restore_decode_content.class.php'
+ );
+
+ /*
+ * test restore_decode_rule class
+ */
+ function test_restore_decode_rule() {
+
+ // Test various incorrect constructors
+ try {
+ $dr = new restore_decode_rule('28 HJH', '/index.php', array());
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_incorrect_name');
+ $this->assertEqual($e->a, '28 HJH');
+ }
+
+ try {
+ $dr = new restore_decode_rule('HJHJhH', '/index.php', array());
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_incorrect_name');
+ $this->assertEqual($e->a, 'HJHJhH');
+ }
+
+ try {
+ $dr = new restore_decode_rule('', '/index.php', array());
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_incorrect_name');
+ $this->assertEqual($e->a, '');
+ }
+
+ try {
+ $dr = new restore_decode_rule('TESTRULE', 'index.php', array());
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_incorrect_urltemplate');
+ $this->assertEqual($e->a, 'index.php');
+ }
+
+ try {
+ $dr = new restore_decode_rule('TESTRULE', '', array());
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_incorrect_urltemplate');
+ $this->assertEqual($e->a, '');
+ }
+
+ try {
+ $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$2$3', array('test1', 'test2'));
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_mappings_incorrect_count');
+ $this->assertEqual($e->a->placeholders, 3);
+ $this->assertEqual($e->a->mappings, 2);
+ }
+
+ try {
+ $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$5&c=$4$1', array('test1', 'test2', 'test3'));
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
+ $this->assertEqual($e->a, '1, 4, 5');
+ }
+
+ try {
+ $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$0&c=$3$2', array('test1', 'test2', 'test3'));
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
+ $this->assertEqual($e->a, '0, 2, 3');
+ }
+
+ try {
+ $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$3$3', array('test1', 'test2', 'test3'));
+ $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
+ } catch (exception $e) {
+ $this->assertTrue($e instanceof restore_decode_rule_exception);
+ $this->assertEqual($e->errorcode, 'decode_rule_duplicate_placeholders');
+ $this->assertEqual($e->a, '1, 3, 3');
+ }
+
+ // Provide some example content and test the regexp is calculated ok
+ $content = '$@TESTRULE*22*33*44@$';
+ $linkname = 'TESTRULE';
+ $urltemplate= '/course/view.php?id=$1&c=$3$2';
+ $mappings = array('test1', 'test2', 'test3');
+ $result = '1/course/view.php?id=44&c=8866';
+ $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
+ $this->assertEqual($dr->decode($content), $result);
+
+ $content = '$@TESTRULE*22*33*44@$ñ$@TESTRULE*22*33*44@$';
+ $linkname = 'TESTRULE';
+ $urltemplate= '/course/view.php?id=$1&c=$3$2';
+ $mappings = array('test1', 'test2', 'test3');
+ $result = '1/course/view.php?id=44&c=8866ñ1/course/view.php?id=44&c=8866';
+ $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
+ $this->assertEqual($dr->decode($content), $result);
+
+ $content = 'ñ$@TESTRULE*22*0*44@$ñ$@TESTRULE*22*33*44@$ñ';
+ $linkname = 'TESTRULE';
+ $urltemplate= '/course/view.php?id=$1&c=$3$2';
+ $mappings = array('test1', 'test2', 'test3');
+ $result = 'ñ0/course/view.php?id=22&c=440ñ1/course/view.php?id=44&c=8866ñ';
+ $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
+ $this->assertEqual($dr->decode($content), $result);
+ }
+
+ /*
+ * test restore_decode_content class
+ */
+ function test_restore_decode_content() {
+ // TODO: restore_decode_content tests
+ }
+
+ /*
+ * test restore_decode_processor class
+ */
+ function test_restore_decode_processor() {
+ // TODO: restore_decode_processor tests
+ }
+}
+
+/**
+ * Mockup restore_decode_rule for testing purposes
+ */
+class mock_restore_decode_rule extends restore_decode_rule {
+
+ /**
+ * Originally protected, make it public
+ */
+ public function get_calculated_regexp() {
+ return parent::get_calculated_regexp();
+ }
+
+ /**
+ * Simply map each itemid by its double
+ */
+ protected function get_mapping($itemname, $itemid) {
+ return $itemid * 2;
+ }
+
+ /**
+ * Simply prefix with '0' non-mapped results and with '1' mapped ones
+ */
+ protected function apply_modifications($toreplace, $mappingsok) {
+ return ($mappingsok ? '1' : '0') . $toreplace;
+ }
+}
require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
require_once($CFG->dirroot . '/backup/util/interfaces/loggable.class.php');
require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php');
+require_once($CFG->dirroot . '/backup/util/interfaces/processable.class.php');
require_once($CFG->dirroot . '/backup/backup.class.php');
require_once($CFG->dirroot . '/backup/util/structure/restore_path_element.class.php');
require_once($CFG->dirroot . '/backup/util/helper/backup_file_manager.class.php');
require_once($CFG->dirroot . '/backup/util/helper/restore_users_parser_processor.class.php');
require_once($CFG->dirroot . '/backup/util/helper/restore_roles_parser_processor.class.php');
require_once($CFG->dirroot . '/backup/util/helper/restore_structure_parser_processor.class.php');
+require_once($CFG->dirroot . '/backup/util/helper/restore_decode_rule.class.php');
+require_once($CFG->dirroot . '/backup/util/helper/restore_decode_content.class.php');
+require_once($CFG->dirroot . '/backup/util/helper/restore_decode_processor.class.php');
require_once($CFG->dirroot . '/backup/util/xml/parser/progressive_parser.class.php');
require_once($CFG->dirroot . '/backup/util/output/output_controller.class.php');
require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php');