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 backup_step_exception('backup_structure_step_undefined_fullpath'); } // Append the filename to the fullpath $fullpath = rtrim($fullpath, '/') . '/' . $this->filename; // Create output, transformer, writer, processor $xo = new file_xml_output($fullpath); $xt = null; if (class_exists('backup_xml_transformer')) { $xt = new backup_xml_transformer($this->get_courseid()); $this->contenttransformer = $xt; // Save the reference to the transformer // as far as we are going to need it out // from xml_writer (blame serialized data!) } $xw = new xml_writer($xo, $xt); $pr = new backup_structure_processor($xw); // Set processor variables from settings foreach ($this->get_settings() as $setting) { $pr->set_var($setting->get_name(), $setting->get_value()); } // Add backupid as one more var for processor $pr->set_var(backup::VAR_BACKUPID, $this->get_backupid()); // Get structure definition $structure = $this->define_structure(); if (!$structure instanceof backup_nested_element) { throw new backup_step_exception('backup_structure_step_wrong_structure'); } // Start writer $xw->start(); // Process structure definition $structure->process($pr); // Get the results from the nested elements $results = $structure->get_results(); // Get the log messages to append to the log $logs = $structure->get_logs(); foreach ($logs as $log) { $this->log($log->message, $log->level, $log->a, $log->depth, $log->display); } // Close everything $xw->stop(); // Destroy the structure. It helps PHP 5.2 memory a lot! $structure->destroy(); return $results; }
/** * Opens the XML writer - after calling, one is free to use $xmlwriter * * @param string $filename XML file name to write into * @return void */ protected function open_xml_writer($filename) { if (!is_null($this->xmlfilename) and $filename !== $this->xmlfilename) { throw new moodle1_convert_exception('xml_writer_already_opened_for_other_file', $this->xmlfilename); } if (!$this->xmlwriter instanceof xml_writer) { $this->xmlfilename = $filename; $fullpath = $this->converter->get_workdir_path() . '/' . $this->xmlfilename; $directory = pathinfo($fullpath, PATHINFO_DIRNAME); if (!check_dir_exists($directory)) { throw new moodle1_convert_exception('unable_create_target_directory', $directory); } $this->xmlwriter = new xml_writer(new file_xml_output($fullpath), new moodle1_xml_transformer()); $this->xmlwriter->start(); } }
/** * Generate the mpr contents (xml files) in the temporal directory. * * @param array $runids list of runids to be generated. * @param string $tmpdir filesystem fullpath of tmp generation. * @return boolean the mpr contents have been generated (true) or no (false). */ function profiling_export_generate(array $runids, $tmpdir) { global $CFG, $DB; // Calculate the header information to be sent to moodle_profiling_runs.xml. $release = $CFG->release; $version = $CFG->version; $dbtype = $CFG->dbtype; $githash = phpunit_util::get_git_hash(); $date = time(); // Create the xml output and writer for the main file. $mainxo = new file_xml_output($tmpdir . '/moodle_profiling_runs.xml'); $mainxw = new xml_writer($mainxo); // Output begins. $mainxw->start(); $mainxw->begin_tag('moodle_profiling_runs'); // Send header information. $mainxw->begin_tag('info'); $mainxw->full_tag('release', $release); $mainxw->full_tag('version', $version); $mainxw->full_tag('dbtype', $dbtype); if ($githash) { $mainxw->full_tag('githash', $githash); } $mainxw->full_tag('date', $date); $mainxw->end_tag('info'); // Send information about runs. $mainxw->begin_tag('runs'); foreach ($runids as $runid) { // Get the run information from DB. $run = $DB->get_record('profiling', array('runid' => $runid), '*', MUST_EXIST); $attributes = array('id' => $run->id, 'ref' => $run->runid . '.xml'); $mainxw->full_tag('run', null, $attributes); // Create the individual run file. $runxo = new file_xml_output($tmpdir . '/' . $attributes['ref']); $runxw = new xml_writer($runxo); $runxw->start(); $runxw->begin_tag('moodle_profiling_run'); $runxw->full_tag('id', $run->id); $runxw->full_tag('runid', $run->runid); $runxw->full_tag('url', $run->url); $runxw->full_tag('runreference', $run->runreference); $runxw->full_tag('runcomment', $run->runcomment); $runxw->full_tag('timecreated', $run->timecreated); $runxw->full_tag('totalexecutiontime', $run->totalexecutiontime); $runxw->full_tag('totalcputime', $run->totalcputime); $runxw->full_tag('totalcalls', $run->totalcalls); $runxw->full_tag('totalmemory', $run->totalmemory); $runxw->full_tag('data', $run->data); $runxw->end_tag('moodle_profiling_run'); $runxw->stop(); } $mainxw->end_tag('runs'); $mainxw->end_tag('moodle_profiling_runs'); $mainxw->stop(); return true; }
$sitecontext = get_context_instance(CONTEXT_SYSTEM); // Init file storage object $fs = get_file_storage(); // Init empty array of roles to export $role_ids = array(); // File information $fileinfo = array('userid' => $USER->id, 'contextid' => $sitecontext->id, 'component' => 'report_rolesmigration', 'filearea' => 'backup', 'itemid' => time(), 'filepath' => '/admin/report/rolesmigration/temp/', 'filename' => 'rolesexport.xml'); // Delete file if it already exists if ($file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename'])) { $file->delete(); } // Init XML oupt and writer objects $xml_output = new memory_xml_output(); $xml = new xml_writer($xml_output); // Start the XML construction process $xml->start(); // Open the top level XML element $xml->begin_tag('MOODLE_ROLES_MIGRATION'); // General site and migration data $xml->begin_tag('INFO'); $xml->full_tag('NAME', 'rolesmigration'); $xml->full_tag('MOODLE_VERSION', $CFG->version); $xml->full_tag('MOODLE_RELEASE', $CFG->release); $xml->full_tag('BACKUP_VERSION', $CFG->backup_version); $xml->full_tag('BACKUP_RELEASE', $CFG->backup_release); $xml->full_tag('DATE', time()); $xml->full_tag('ORIGINAL_WWWROOT', $CFG->wwwroot); $xml->full_tag('ORIGINAL_SITE_IDENTIFIER_HASH', md5(get_site_identifier())); $xml->end_tag('INFO'); // The roles tag contains all data for selected Roles on export screen $xml->begin_tag('ROLES');
/** * Backup structures tests (construction, definition and execution) */ function test_backup_structure_construct() { global $DB; $backupid = 'Testing Backup ID'; // Official backupid for these tests // Create all the elements that will conform the tree $forum = new backup_nested_element('forum', array('id'), array('type', 'name', 'intro', 'introformat', 'assessed', 'assesstimestart', 'assesstimefinish', 'scale', 'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype', 'rsstype', 'rssarticles', 'timemodified', 'warnafter', 'blockafter', new backup_final_element('blockperiod'), new mock_skip_final_element('completiondiscussions'), new mock_modify_final_element('completionreplies'), new mock_final_element_interceptor('completionposts'))); $discussions = new backup_nested_element('discussions'); $discussion = new backup_nested_element('discussion', array('id'), array('forum', 'name', 'firstpost', 'userid', 'groupid', 'assessed', 'timemodified', 'usermodified', 'timestart', 'timeend')); $posts = new backup_nested_element('posts'); $post = new backup_nested_element('post', array('id'), array('discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message', 'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow')); $ratings = new backup_nested_element('ratings'); $rating = new backup_nested_element('rating', array('id'), array('userid', 'itemid', 'time', 'post_rating')); $reads = new backup_nested_element('readposts'); $read = new backup_nested_element('read', array('id'), array('userid', 'discussionid', 'postid', 'firstread', 'lastread')); $inventeds = new backup_nested_element('invented_elements', array('reason', 'version')); $invented = new backup_nested_element('invented', null, array('one', 'two', 'three')); $one = $invented->get_final_element('one'); $one->add_attributes(array('attr1', 'attr2')); // Build the tree $forum->add_child($discussions); $discussions->add_child($discussion); $discussion->add_child($posts); $posts->add_child($post); $post->add_child($ratings); $ratings->add_child($rating); $forum->add_child($reads); $reads->add_child($read); $forum->add_child($inventeds); $inventeds->add_child($invented); // Let's add 1 optigroup with 4 elements $alternative1 = new backup_optigroup_element('alternative1', array('name', 'value'), '../../id', 1); $alternative2 = new backup_optigroup_element('alternative2', array('name', 'value'), backup::VAR_PARENTID, 2); $alternative3 = new backup_optigroup_element('alternative3', array('name', 'value'), '/forum/discussions/discussion/posts/post/id', 3); $alternative4 = new backup_optigroup_element('alternative4', array('forumtype', 'forumname')); // Alternative without conditions // Create the optigroup, adding one element $optigroup = new backup_optigroup('alternatives', $alternative1, false); // Add second opti element $optigroup->add_child($alternative2); // Add optigroup to post element $post->add_optigroup($optigroup); // Add third opti element, on purpose after the add_optigroup() line above to check param evaluation works ok $optigroup->add_child($alternative3); // Add 4th opti element (the one without conditions, so will be present always) $optigroup->add_child($alternative4); /// Create some new nested elements, both named 'dupetest1', and add them to alternative1 and alternative2 /// (not problem as far as the optigroup in not unique) $dupetest1 = new backup_nested_element('dupetest1', null, array('field1', 'field2')); $dupetest2 = new backup_nested_element('dupetest2', null, array('field1', 'field2')); $dupetest3 = new backup_nested_element('dupetest3', null, array('field1', 'field2')); $dupetest4 = new backup_nested_element('dupetest1', null, array('field1', 'field2')); $dupetest1->add_child($dupetest3); $dupetest2->add_child($dupetest4); $alternative1->add_child($dupetest1); $alternative2->add_child($dupetest2); // Define sources $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID)); $discussion->set_source_sql('SELECT * FROM {forum_discussions} WHERE forum = ?', array('/forum/id')); $post->set_source_table('forum_posts', array('discussion' => '/forum/discussions/discussion/id')); $rating->set_source_sql('SELECT * FROM {rating} WHERE itemid = ?', array(backup::VAR_PARENTID)); $read->set_source_table('forum_read', array('id' => '../../id')); $inventeds->set_source_array(array((object) array('reason' => 'I love Moodle', 'version' => '1.0'), (object) array('reason' => 'I love Moodle', 'version' => '2.0'))); // 2 object array $invented->set_source_array(array((object) array('one' => 1, 'two' => 2, 'three' => 3), (object) array('one' => 11, 'two' => 22, 'three' => 33))); // 2 object array // Set optigroup_element sources $alternative1->set_source_array(array((object) array('name' => 'alternative1', 'value' => 1))); // 1 object array // Skip alternative2 source definition on purpose (will be tested) // $alternative2->set_source_array(array((object)array('name' => 'alternative2', 'value' => 2))); // 1 object array $alternative3->set_source_array(array((object) array('name' => 'alternative3', 'value' => 3))); // 1 object array // Alternative 4 source is the forum type and name, so we'll get that in ALL posts (no conditions) that // have not another alternative (post4 in our testing data in the only not matching any other alternative) $alternative4->set_source_sql('SELECT type AS forumtype, name AS forumname FROM {forum} WHERE id = ?', array('/forum/id')); // Set children of optigroup_element source $dupetest1->set_source_array(array((object) array('field1' => '1', 'field2' => 1))); // 1 object array $dupetest2->set_source_array(array((object) array('field1' => '2', 'field2' => 2))); // 1 object array $dupetest3->set_source_array(array((object) array('field1' => '3', 'field2' => 3))); // 1 object array $dupetest4->set_source_array(array((object) array('field1' => '4', 'field2' => 4))); // 1 object array // Define some aliases $rating->set_source_alias('rating', 'post_rating'); // Map the 'rating' value from DB to 'post_rating' final element // Mark to detect files of type 'forum_intro' in forum (and not item id) $forum->annotate_files('mod_forum', 'intro', null); // Mark to detect file of type 'forum_post' and 'forum_attachment' in post (with itemid being post->id) $post->annotate_files('mod_forum', 'post', 'id'); $post->annotate_files('mod_forum', 'attachment', 'id'); // Mark various elements to be annotated $discussion->annotate_ids('user1', 'userid'); $post->annotate_ids('forum_post', 'id'); $rating->annotate_ids('user2', 'userid'); $rating->annotate_ids('forum_post', 'itemid'); // Create the backup_ids_temp table backup_controller_dbops::create_backup_ids_temp_table($backupid); // Instantiate in memory xml output $xo = new memory_xml_output(); // Instantiate xml_writer and start it $xw = new xml_writer($xo); $xw->start(); // Instantiate the backup processor $processor = new backup_structure_processor($xw); // Set some variables $processor->set_var(backup::VAR_ACTIVITYID, $this->forumid); $processor->set_var(backup::VAR_BACKUPID, $backupid); $processor->set_var(backup::VAR_CONTEXTID, $this->contextid); // Process the backup structure with the backup processor $forum->process($processor); // Stop the xml_writer $xw->stop(); // Check various counters $this->assertEquals($forum->get_counter(), $DB->count_records('forum')); $this->assertEquals($discussion->get_counter(), $DB->count_records('forum_discussions')); $this->assertEquals($rating->get_counter(), $DB->count_records('rating')); $this->assertEquals($read->get_counter(), $DB->count_records('forum_read')); $this->assertEquals($inventeds->get_counter(), 2); // Array // Perform some validations with the generated XML $dom = new DomDocument(); $dom->loadXML($xo->get_allcontents()); $xpath = new DOMXPath($dom); // Some more counters $query = '/forum/discussions/discussion/posts/post'; $posts = $xpath->query($query); $this->assertEquals($posts->length, $DB->count_records('forum_posts')); $query = '/forum/invented_elements/invented'; $inventeds = $xpath->query($query); $this->assertEquals($inventeds->length, 2 * 2); // Check ratings information against DB $ratings = $dom->getElementsByTagName('rating'); $this->assertEquals($ratings->length, $DB->count_records('rating')); foreach ($ratings as $rating) { $ratarr = array(); $ratarr['id'] = $rating->getAttribute('id'); foreach ($rating->childNodes as $node) { if ($node->nodeType != XML_TEXT_NODE) { $ratarr[$node->nodeName] = $node->nodeValue; } } $this->assertEquals($ratarr['userid'], $DB->get_field('rating', 'userid', array('id' => $ratarr['id']))); $this->assertEquals($ratarr['itemid'], $DB->get_field('rating', 'itemid', array('id' => $ratarr['id']))); $this->assertEquals($ratarr['post_rating'], $DB->get_field('rating', 'rating', array('id' => $ratarr['id']))); } // Check forum has "blockeperiod" with value 0 (was declared by object instead of name) $query = '/forum[blockperiod="0"]'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check forum is missing "completiondiscussions" (as we are using mock_skip_final_element) $query = '/forum/completiondiscussions'; $result = $xpath->query($query); $this->assertEquals($result->length, 0); // Check forum has "completionreplies" with value "original was 0, now changed" (because of mock_modify_final_element) $query = '/forum[completionreplies="original was 0, now changed"]'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check forum has "completionposts" with value "intercepted!" (because of mock_final_element_interceptor) $query = '/forum[completionposts="intercepted!"]'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check there isn't any alternative2 tag, as far as it hasn't source defined $query = '//alternative2'; $result = $xpath->query($query); $this->assertEquals($result->length, 0); // Check there are 4 "field1" elements $query = '/forum/discussions/discussion/posts/post//field1'; $result = $xpath->query($query); $this->assertEquals($result->length, 4); // Check first post has one name element with value "alternative1" $query = '/forum/discussions/discussion/posts/post[@id="1"][name="alternative1"]'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check there are two "dupetest1" elements $query = '/forum/discussions/discussion/posts/post//dupetest1'; $result = $xpath->query($query); $this->assertEquals($result->length, 2); // Check second post has one name element with value "dupetest2" $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check element "dupetest2" of second post has one field1 element with value "2" $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2[field1="2"]'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check forth post has no name element $query = '/forum/discussions/discussion/posts/post[@id="4"]/name'; $result = $xpath->query($query); $this->assertEquals($result->length, 0); // Check 1st, 2nd and 3rd posts have no forumtype element $query = '/forum/discussions/discussion/posts/post[@id="1"]/forumtype'; $result = $xpath->query($query); $this->assertEquals($result->length, 0); $query = '/forum/discussions/discussion/posts/post[@id="2"]/forumtype'; $result = $xpath->query($query); $this->assertEquals($result->length, 0); $query = '/forum/discussions/discussion/posts/post[@id="3"]/forumtype'; $result = $xpath->query($query); $this->assertEquals($result->length, 0); // Check 4th post has one forumtype element with value "general" // (because it doesn't matches alternatives 1, 2, 3, then alternative 4, // the one without conditions is being applied) $query = '/forum/discussions/discussion/posts/post[@id="4"][forumtype="general"]'; $result = $xpath->query($query); $this->assertEquals($result->length, 1); // Check annotations information against DB // Count records in original tables $c_postsid = $DB->count_records_sql('SELECT COUNT(DISTINCT id) FROM {forum_posts}'); $c_dissuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {forum_discussions}'); $c_ratuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {rating}'); // Count records in backup_ids_table $f_forumpost = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'forum_post')); $f_user1 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user1')); $f_user2 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user2')); $c_notbackupid = $DB->count_records_select('backup_ids_temp', 'backupid != ?', array($backupid)); // Peform tests by comparing counts $this->assertEquals($c_notbackupid, 0); // there isn't any record with incorrect backupid $this->assertEquals($c_postsid, $f_forumpost); // All posts have been registered $this->assertEquals($c_dissuserid, $f_user1); // All users coming from discussions have been registered $this->assertEquals($c_ratuserid, $f_user2); // All users coming from ratings have been registered // Check file annotations against DB $fannotations = $DB->get_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'file')); $ffiles = $DB->get_records('files', array('contextid' => $this->contextid)); $this->assertEquals(count($fannotations), count($ffiles)); // Same number of recs in both (all files have been annotated) foreach ($fannotations as $annotation) { // Check ids annotated $this->assertTrue($DB->record_exists('files', array('id' => $annotation->itemid))); } // Drop the backup_ids_temp table backup_controller_dbops::drop_backup_ids_temp_table('testingid'); }