public function execute()
 {
     if (!$this->execute_condition()) {
         // Check any condition to execute this
         return;
     }
     $fullpath = $this->task->get_taskbasepath();
     // We MUST have one fullpath here, else, error
     if (empty($fullpath)) {
         throw new restore_step_exception('restore_structure_step_undefined_fullpath');
     }
     // Append the filename to the fullpath
     $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
     // And it MUST exist
     if (!file_exists($fullpath)) {
         // Shouldn't happen ever, but...
         throw new restore_step_exception('missing_moodle_backup_xml_file', $fullpath);
     }
     // Get restore_path elements array adapting and preparing it for processing
     $structure = $this->define_structure();
     if (!is_array($structure)) {
         throw new restore_step_exception('restore_step_structure_not_array', $this->get_name());
     }
     $this->prepare_pathelements($structure);
     // Create parser and processor
     $xmlparser = new progressive_parser();
     $xmlparser->set_file($fullpath);
     $xmlprocessor = new restore_structure_parser_processor($this->task->get_courseid(), $this);
     $this->contentprocessor = $xmlprocessor;
     // Save the reference to the contentprocessor
     // as far as we are going to need it out
     // from parser (blame serialized data!)
     $xmlparser->set_processor($xmlprocessor);
     // Add pathelements to processor
     foreach ($this->pathelements as $element) {
         $xmlprocessor->add_path($element->get_path(), $element->is_grouped());
     }
     // And process it, dispatch to target methods in step will start automatically
     $xmlparser->process();
     // Have finished, launch the after_execute method of all the processing objects
     $this->launch_after_execute_methods();
 }
 /**
  * Load and format all the needed information from moodle_backup.xml
  *
  * This function loads and process all the moodle_backup.xml
  * information, composing a big information structure that will
  * be the used by the plan builder in order to generate the
  * appropiate tasks / steps / settings
  */
 public static function get_backup_information($tempdir)
 {
     global $CFG;
     $info = new stdclass();
     // Final information goes here
     $moodlefile = $CFG->tempdir . '/backup/' . $tempdir . '/moodle_backup.xml';
     if (!file_exists($moodlefile)) {
         // Shouldn't happen ever, but...
         throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile);
     }
     // Load the entire file to in-memory array
     $xmlparser = new progressive_parser();
     $xmlparser->set_file($moodlefile);
     $xmlprocessor = new restore_moodlexml_parser_processor();
     $xmlparser->set_processor($xmlprocessor);
     $xmlparser->process();
     $infoarr = $xmlprocessor->get_all_chunks();
     if (count($infoarr) !== 1) {
         // Shouldn't happen ever, but...
         throw new backup_helper_exception('problem_parsing_moodle_backup_xml_file');
     }
     $infoarr = $infoarr[0]['tags'];
     // for commodity
     // Let's build info
     $info->moodle_version = $infoarr['moodle_version'];
     $info->moodle_release = $infoarr['moodle_release'];
     $info->backup_version = $infoarr['backup_version'];
     $info->backup_release = $infoarr['backup_release'];
     $info->backup_date = $infoarr['backup_date'];
     $info->mnet_remoteusers = $infoarr['mnet_remoteusers'];
     $info->original_wwwroot = $infoarr['original_wwwroot'];
     $info->original_site_identifier_hash = $infoarr['original_site_identifier_hash'];
     $info->original_course_id = $infoarr['original_course_id'];
     $info->original_course_fullname = $infoarr['original_course_fullname'];
     $info->original_course_shortname = $infoarr['original_course_shortname'];
     $info->original_course_startdate = $infoarr['original_course_startdate'];
     $info->original_course_contextid = $infoarr['original_course_contextid'];
     $info->original_system_contextid = $infoarr['original_system_contextid'];
     // Moodle backup file don't have this option before 2.3
     if (!empty($infoarr['include_file_references_to_external_content'])) {
         $info->include_file_references_to_external_content = 1;
     } else {
         $info->include_file_references_to_external_content = 0;
     }
     $info->type = $infoarr['details']['detail'][0]['type'];
     $info->format = $infoarr['details']['detail'][0]['format'];
     $info->mode = $infoarr['details']['detail'][0]['mode'];
     // Build the role mappings custom object
     $rolemappings = new stdclass();
     $rolemappings->modified = false;
     $rolemappings->mappings = array();
     $info->role_mappings = $rolemappings;
     // Some initially empty containers
     $info->sections = array();
     $info->activities = array();
     // Now the contents
     $contentsarr = $infoarr['contents'];
     if (isset($contentsarr['course']) && isset($contentsarr['course'][0])) {
         $info->course = new stdclass();
         $info->course = (object) $contentsarr['course'][0];
         $info->course->settings = array();
     }
     if (isset($contentsarr['sections']) && isset($contentsarr['sections']['section'])) {
         $sectionarr = $contentsarr['sections']['section'];
         foreach ($sectionarr as $section) {
             $section = (object) $section;
             $section->settings = array();
             $sections[basename($section->directory)] = $section;
         }
         $info->sections = $sections;
     }
     if (isset($contentsarr['activities']) && isset($contentsarr['activities']['activity'])) {
         $activityarr = $contentsarr['activities']['activity'];
         foreach ($activityarr as $activity) {
             $activity = (object) $activity;
             $activity->settings = array();
             $activities[basename($activity->directory)] = $activity;
         }
         $info->activities = $activities;
     }
     $info->root_settings = array();
     // For root settings
     // Now the settings, putting each one under its owner
     $settingsarr = $infoarr['settings']['setting'];
     foreach ($settingsarr as $setting) {
         switch ($setting['level']) {
             case 'root':
                 $info->root_settings[$setting['name']] = $setting['value'];
                 break;
             case 'course':
                 $info->course->settings[$setting['name']] = $setting['value'];
                 break;
             case 'section':
                 $info->sections[$setting['section']]->settings[$setting['name']] = $setting['value'];
                 break;
             case 'activity':
                 $info->activities[$setting['activity']]->settings[$setting['name']] = $setting['value'];
                 break;
             default:
                 // Shouldn't happen
                 throw new backup_helper_exception('wrong_setting_level_moodle_backup_xml_file', $setting['level']);
         }
     }
     return $info;
 }
Beispiel #3
0
 /**
  * Load the needed questions.xml file to backup_ids table for future reference
  */
 public static function load_categories_and_questions_to_tempids($restoreid, $questionsfile)
 {
     if (!file_exists($questionsfile)) {
         // Shouldn't happen ever, but...
         throw new backup_helper_exception('missing_questions_xml_file', $questionsfile);
     }
     // Let's parse, custom processor will do its work, sending info to DB
     $xmlparser = new progressive_parser();
     $xmlparser->set_file($questionsfile);
     $xmlprocessor = new restore_questions_parser_processor($restoreid);
     $xmlparser->set_processor($xmlprocessor);
     $xmlparser->process();
 }
Beispiel #4
0
 /**
  */
 function test_grouped_at_empty_node()
 {
     global $CFG;
     // Instantiate progressive_parser.
     $pp = new progressive_parser();
     // Instantiate grouped_parser_processor.
     $pr = new mock_grouped_parser_processor();
     $this->assertTrue($pr instanceof progressive_parser_processor);
     // Add interesting paths - moodle1 style.
     $pr->add_path('/test/MOODLE_BACKUP/COURSE/FORMATDATA', true);
     $pr->add_path('/test/MOODLE_BACKUP/COURSE/FORMATDATA/WEEKS/WEEK');
     $pr->add_path('/test/MOODLE_BACKUP/COURSE/EMPTYGROUPED', true);
     $pr->add_path('/test/MOODLE_BACKUP/COURSE/SECONDGROUPED', true);
     $pr->add_path('/test/MOODLE_BACKUP/COURSE/SECONDGROUPED/SUBS/SUB');
     // Add interesting paths - moodle2 style.
     $pr->add_path('/test/moodle2/grouped', true);
     $pr->add_path('/test/moodle2/grouped/subs/sub');
     $pr->add_path('/test/moodle2/groupedemptywithattr', true);
     $pr->add_path('/test/moodle2/groupednonemptywithattr', true);
     $pr->add_path('/test/moodle2/groupednonemptywithattr/subs/sub');
     // Assign processor to parser.
     $pp->set_processor($pr);
     // Set file from fixtures.
     $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/tests/fixtures/test6.xml');
     // Process the file.
     $pp->process();
     // Get all the simplified chunks and perform various validations.
     $chunks = $pr->get_chunks();
     $this->assertEquals(count($chunks), 6);
     // All grouped elements.
     // Check some random data.
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/FORMATDATA', $chunks[0]['path']);
     $this->assertEquals(2, $chunks[0]['tags']['WEEKS']['WEEK'][1]['SECTION']);
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/EMPTYGROUPED', $chunks[1]['path']);
     $this->assertEquals(array(), $chunks[1]['tags']);
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/SECONDGROUPED', $chunks[2]['path']);
     $this->assertEquals('Unit tests rock!', $chunks[2]['tags']['SUBS']['SUB'][0]['PROP']);
     $this->assertEquals('/test/moodle2/grouped', $chunks[3]['path']);
     $this->assertFalse(isset($chunks[3]['tags']['id']));
     // No final elements, this should be fixed one day.
     $this->assertEquals(34, $chunks[3]['tags']['subs']['sub'][0]['id']);
     // We have final element so this is parsed.
     $this->assertEquals('Oh yeah', $chunks[3]['tags']['subs']['sub'][0]['prop']);
     $this->assertEquals('/test/moodle2/groupednonemptywithattr', $chunks[4]['path']);
     $this->assertEquals(78, $chunks[4]['tags']['id']);
     // We have final element so this is parsed.
     $this->assertEquals('Go baby go', $chunks[4]['tags']['prop']);
     $this->assertEquals(89, $chunks[4]['tags']['subs']['sub'][0]['id']);
     $this->assertEquals('http://moodle.org', $chunks[4]['tags']['subs']['sub'][0]['prop']);
     $this->assertEquals('/test/moodle2/groupedemptywithattr', $chunks[5]['path']);
     $this->assertFalse(isset($chunks[5]['tags']['attr']));
     // No final elements, this should be fixed one day.
     // Now check start notifications.
     $snotifs = $pr->get_start_notifications();
     // Check we have received the correct number of notifications.
     $this->assertEquals(count($snotifs), 6);
     // Check the order of notifications (in order they appear in test6.xml).
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/FORMATDATA', $snotifs[0]);
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/EMPTYGROUPED', $snotifs[1]);
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/SECONDGROUPED', $snotifs[2]);
     $this->assertEquals('/test/moodle2/grouped', $snotifs[3]);
     $this->assertEquals('/test/moodle2/groupednonemptywithattr', $snotifs[4]);
     $this->assertEquals('/test/moodle2/groupedemptywithattr', $snotifs[5]);
     // Now check end notifications.
     $enotifs = $pr->get_end_notifications();
     // Check we have received the correct number of notifications.
     $this->assertEquals(count($enotifs), 6);
     // Check the order of notifications (in order they appear in test6.xml).
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/FORMATDATA', $enotifs[0]);
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/EMPTYGROUPED', $enotifs[1]);
     $this->assertEquals('/test/MOODLE_BACKUP/COURSE/SECONDGROUPED', $enotifs[2]);
     $this->assertEquals('/test/moodle2/grouped', $enotifs[3]);
     $this->assertEquals('/test/moodle2/groupednonemptywithattr', $enotifs[4]);
     $this->assertEquals('/test/moodle2/groupedemptywithattr', $enotifs[5]);
     // Now verify that the start/process/end order is correct.
     $allnotifs = $pr->get_all_notifications();
     $this->assertEquals(count($allnotifs), count($snotifs) + count($enotifs) + count($chunks));
     // Check integrity of the notifications.
     $errcount = $this->helper_check_notifications_order_integrity($allnotifs);
     $this->assertEquals(0, $errcount);
 }
 /**
  * test how the grouped processor and the order of start/process/end events happens
  * with one real fragment of one backup 1.9 file, where some problems
  * were found by David, hence we honor him in the name of the test ;-)
  */
 function test_grouped_david_backup19_file_fragment()
 {
     global $CFG;
     // Instantiate progressive_parser
     $pp = new progressive_parser();
     // Instantiate grouped_parser_processor
     $pr = new mock_grouped_parser_processor();
     // Add interesting paths
     $pr->add_path('/MOODLE_BACKUP/COURSE');
     $pr->add_path('/MOODLE_BACKUP/COURSE/SECTIONS/SECTION', true);
     $pr->add_path('/MOODLE_BACKUP/COURSE/SECTIONS/SECTION/MODS/MOD');
     $pr->add_path('/MOODLE_BACKUP/COURSE/SECTIONS/SECTION/MODS/MOD/ROLES_OVERRIDES');
     $this->assertTrue($pr instanceof progressive_parser_processor);
     // Assign processor to parser
     $pp->set_processor($pr);
     // Set file from fixtures
     $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test5.xml');
     // Process the file
     $pp->process();
     // Get all the simplified chunks and perform various validations
     $chunks = $pr->get_chunks();
     $this->assertEqual(count($chunks), 1);
     // Only 1, the SECTION one
     // Now check start notifications
     $snotifs = $pr->get_start_notifications();
     // Check we have received the correct number of notifications
     $this->assertEqual(count($snotifs), 2);
     // Check first and last notifications
     $this->assertEqual($snotifs[0], '/MOODLE_BACKUP/COURSE');
     $this->assertEqual($snotifs[1], '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION');
     // Now check end notifications
     $enotifs = $pr->get_end_notifications();
     // Check we have received the correct number of notifications
     $this->assertEqual(count($snotifs), 2);
     // End tags are dispatched for empties (ROLES_OVERRIDES)
     // Check first, and last notifications
     $this->assertEqual($enotifs[0], '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION');
     $this->assertEqual($enotifs[1], '/MOODLE_BACKUP/COURSE');
     // Check start and end notifications are balanced
     sort($snotifs);
     sort($enotifs);
     $this->assertEqual($snotifs, $enotifs);
     // Now verify that the start/process/end order is correct
     $allnotifs = $pr->get_all_notifications();
     $this->assertEqual(count($allnotifs), count($snotifs) + count($enotifs) + count($chunks));
     // The count
     // Check integrity of the notifications
     $errcount = $this->helper_check_notifications_order_integrity($allnotifs);
     $this->assertEqual($errcount, 0);
     // No errors found, plz
 }
Beispiel #6
0
 function test_grouped_parser_results()
 {
     global $CFG;
     // Instantiate progressive_parser
     $pp = new progressive_parser();
     // Instantiate grouped_parser_processor
     $pr = new mock_grouped_parser_processor();
     // Add interesting paths
     $pr->add_path('/activity');
     $pr->add_path('/activity/glossary', true);
     $pr->add_path('/activity/glossary/entries/entry');
     $pr->add_path('/activity/glossary/entries/entry/aliases/alias');
     $pr->add_path('/activity/glossary/entries/entry/ratings/rating');
     $pr->add_path('/activity/glossary/categories/category');
     $pr->add_path('/activity/glossary/onetest');
     $pr->add_path('/activity/glossary/othertest');
     $this->assertTrue($pr instanceof progressive_parser_processor);
     // Assign processor to parser
     $pp->set_processor($pr);
     // Set file from fixtures
     $pp->set_file($CFG->dirroot . '/backup/util/xml/parser/simpletest/fixtures/test4.xml');
     // Process the file
     $pp->process();
     // Get processor debug info
     $debug = $pr->debug_info();
     $this->assertTrue(is_array($debug));
     $this->assertTrue(array_key_exists('chunks', $debug));
     // Check the number of chunks is correct for the file
     $this->assertEqual($debug['chunks'], 2);
     // Get all the simplified chunks and perform various validations
     $chunks = $pr->get_chunks();
     // Check we have received the correct number of chunks
     $this->assertEqual(count($chunks), 2);
     // chunk[0] (/activity) tests
     $this->assertEqual(count($chunks[0]), 3);
     $this->assertEqual($chunks[0]['path'], '/activity');
     $this->assertEqual($chunks[0]['level'], '2');
     $tags = $chunks[0]['tags'];
     $this->assertEqual(count($tags), 4);
     $this->assertEqual($tags['id'], 1);
     $this->assertEqual($tags['moduleid'], 5);
     $this->assertEqual($tags['modulename'], 'glossary');
     $this->assertEqual($tags['contextid'], 26);
     $this->assertEqual($chunks[0]['level'], '2');
     // chunk[1] (grouped /activity/glossary tests)
     $this->assertEqual(count($chunks[1]), 3);
     $this->assertEqual($chunks[1]['path'], '/activity/glossary');
     $this->assertEqual($chunks[1]['level'], '3');
     $tags = $chunks[1]['tags'];
     $this->assertEqual(count($tags), 27);
     $this->assertEqual($tags['id'], 1);
     $this->assertEqual($tags['intro'], '<p>One simple glossary to test backup &amp; restore. Here it\'s the standard image:</p>' . "\n" . '<p><img src="@@PLUGINFILE@@/88_31.png" alt="pwd by moodle" width="88" height="31" /></p>');
     $this->assertEqual($tags['timemodified'], 1275639747);
     $this->assertTrue(!isset($tags['categories']));
     $this->assertTrue(isset($tags['entries']));
     $this->assertTrue(isset($tags['onetest']));
     $this->assertTrue(isset($tags['othertest']));
     // Various tests under the entries
     $entries = $chunks[1]['tags']['entries']['entry'];
     $this->assertEqual(count($entries), 2);
     // First entry
     $entry1 = $entries[0];
     $this->assertEqual(count($entry1), 17);
     $this->assertEqual($entry1['id'], 1);
     $this->assertEqual($entry1['userid'], 2);
     $this->assertEqual($entry1['concept'], 'dog');
     $this->assertEqual($entry1['definition'], '<p>Traditional enemies of cats</p>');
     $this->assertTrue(isset($entry1['aliases']));
     $this->assertTrue(isset($entry1['ratings']));
     // aliases of first entry
     $aliases = $entry1['aliases']['alias'];
     $this->assertEqual(count($aliases), 1);
     // first alias
     $alias1 = $aliases[0];
     $this->assertEqual(count($alias1), 2);
     $this->assertEqual($alias1['id'], 1);
     $this->assertEqual($alias1['alias_text'], 'dogs');
     // ratings of first entry
     $ratings = $entry1['ratings']['rating'];
     $this->assertEqual(count($ratings), 1);
     // first rating
     $rating1 = $ratings[0];
     $this->assertEqual(count($rating1), 6);
     $this->assertEqual($rating1['id'], 2);
     $this->assertEqual($rating1['value'], 6);
     $this->assertEqual($rating1['timemodified'], '1275639797');
     // Second entry
     $entry2 = $entries[1];
     $this->assertEqual(count($entry2), 17);
     $this->assertEqual($entry2['id'], 2);
     $this->assertEqual($entry2['userid'], 2);
     $this->assertEqual($entry2['concept'], 'cat');
     $this->assertEqual($entry2['definition'], '<p>traditional enemies of dogs</p>');
     $this->assertTrue(isset($entry2['aliases']));
     $this->assertTrue(isset($entry2['ratings']));
     // aliases of first entry
     $aliases = $entry2['aliases']['alias'];
     $this->assertEqual(count($aliases), 2);
     // first alias
     $alias1 = $aliases[0];
     $this->assertEqual(count($alias1), 2);
     $this->assertEqual($alias1['id'], 2);
     $this->assertEqual($alias1['alias_text'], 'cats');
     // second alias
     $alias2 = $aliases[1];
     $this->assertEqual(count($alias2), 2);
     $this->assertEqual($alias2['id'], 3);
     $this->assertEqual($alias2['alias_text'], 'felines');
     // ratings of first entry
     $ratings = $entry2['ratings']['rating'];
     $this->assertEqual(count($ratings), 1);
     // first rating
     $rating1 = $ratings[0];
     $this->assertEqual(count($rating1), 6);
     $this->assertEqual($rating1['id'], 1);
     $this->assertEqual($rating1['value'], 5);
     $this->assertEqual($rating1['scaleid'], 10);
     // Onetest test (only 1 level nested)
     $onetest = $tags['onetest'];
     $this->assertEqual(count($onetest), 2);
     $this->assertEqual(count($onetest[0]), 2);
     $this->assertEqual($onetest[0]['name'], 1);
     $this->assertEqual($onetest[0]['value'], 1);
     $this->assertEqual(count($onetest[1]), 2);
     $this->assertEqual($onetest[1]['name'], 2);
     $this->assertEqual($onetest[1]['value'], 2);
     // Other test (0 level nested, only last one is retrieved)
     $othertest = $tags['othertest'];
     $this->assertEqual(count($othertest), 1);
     $this->assertEqual(count($othertest[0]), 2);
     $this->assertEqual($othertest[0]['name'], 4);
     $this->assertEqual($othertest[0]['value'], 5);
     // Now check start notifications
     $snotifs = $pr->get_start_notifications();
     // Check we have received the correct number of notifications
     $this->assertEqual(count($snotifs), 2);
     // Check first and last notifications
     $this->assertEqual($snotifs[0], '/activity');
     $this->assertEqual($snotifs[1], '/activity/glossary');
     // Now check end notifications
     $enotifs = $pr->get_end_notifications();
     // Check we have received the correct number of notifications
     $this->assertEqual(count($snotifs), 2);
     // Check first, and last notifications
     $this->assertEqual($enotifs[0], '/activity/glossary');
     $this->assertEqual($enotifs[1], '/activity');
     // Check start and end notifications are balanced
     sort($snotifs);
     sort($enotifs);
     $this->assertEqual($snotifs, $enotifs);
 }