$mergeform = new local_amos_merge_form(null, local_amos_merge_options()); if ($data = $mergeform->get_data()) { $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $sourceversion = mlang_version::by_code($data->sourceversion); $targetversion = mlang_version::by_code($data->targetversion); if (is_null($sourceversion) or is_null($targetversion)) { notice('Invalid version selected', new moodle_url('/local/amos/stage.php')); } $tree = mlang_tools::components_tree(array('branch' => $sourceversion->code, 'lang' => $data->language)); $sourcecomponentnames = array_keys(reset(reset($tree))); unset($tree); foreach ($sourcecomponentnames as $sourcecomponentname) { // get a snapshot of both components and merge source into target $sourcecomponent = mlang_component::from_snapshot($sourcecomponentname, $data->language, $sourceversion); $targetcomponent = mlang_component::from_snapshot($sourcecomponent->name, $sourcecomponent->lang, $targetversion); mlang_tools::merge($sourcecomponent, $targetcomponent); $sourcecomponent->clear(); // keep just strings that are defined in english $englishcomponent = mlang_component::from_snapshot($sourcecomponent->name, 'en', $targetversion); $targetcomponent->intersect($englishcomponent); $englishcomponent->clear(); // stage the target $stage->add($targetcomponent); $targetcomponent->clear(); } // prune the stage so that only committable strings are staged $allowed = mlang_tools::list_allowed_languages($USER->id); $stage->prune($allowed); // keep just really modified (that is new in this case) strings $stage->rebase(); // and store the persistant stage
public function test_merge_strings_from_another_component() { // prepare two components with some strings $component19 = new mlang_component('test', 'xx', mlang_version::by_branch('MOODLE_19_STABLE')); $component19->add_string(new mlang_string('first', 'First $a')); $component19->add_string(new mlang_string('second', 'Second \\"string\\"')); $component19->add_string(new mlang_string('third', 'Third')); $component19->add_string(new mlang_string('fifth', 'Fifth \\"string\\"')); $component20 = new mlang_component('test', 'xx', mlang_version::by_branch('MOODLE_20_STABLE')); $component20->add_string(new mlang_string('second', '*deleted*', null, true)); $component20->add_string(new mlang_string('third', 'Third already merged')); $component20->add_string(new mlang_string('fourth', 'Fourth only in component20')); // merge component19 into component20 mlang_tools::merge($component19, $component20); // check the results $this->assertEqual(4, $component19->get_number_of_strings()); $this->assertEqual(5, $component20->get_number_of_strings()); $this->assertEqual('First {$a}', $component20->get_string('first')->text); $this->assertEqual('*deleted*', $component20->get_string('second')->text); $this->assertTrue($component20->get_string('second')->deleted); $this->assertEqual('Third already merged', $component20->get_string('third')->text); $this->assertEqual('Fourth only in component20', $component20->get_string('fourth')->text); $this->assertFalse($component19->has_string('fourth')); $this->assertEqual('Fifth "string"', $component20->get_string('fifth')->text); // clear source component and make sure that strings are still in the new one $component19->clear(); unset($component19); $this->assertEqual(5, $component20->get_number_of_strings()); $this->assertEqual('First {$a}', $component20->get_string('first')->text); }