/** * AMOS upgrade scripts * * @package local_amos * @copyright 2010 David Mudrak <*****@*****.**> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ function xmldb_local_amos_upgrade($oldversion) { global $CFG, $DB, $OUTPUT; $dbman = $DB->get_manager(); $result = true; if ($oldversion < 2010090103) { $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/local/amos/db/install.xml', 'amos_stashes'); upgrade_plugin_savepoint(true, 2010090103, 'local', 'amos'); } if ($oldversion < 2010090107) { $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/local/amos/db/install.xml', 'amos_hidden_requests'); upgrade_plugin_savepoint(true, 2010090107, 'local', 'amos'); } if ($oldversion < 2010110400) { $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/local/amos/db/install.xml', 'amos_greylist'); upgrade_plugin_savepoint(true, 2010110400, 'local', 'amos'); } if ($oldversion < 2011010600) { $dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/local/amos/db/install.xml', 'amos_contributions'); upgrade_plugin_savepoint(true, 2011010600, 'local', 'amos'); } if ($oldversion < 2011011000) { require_once dirname(dirname(__FILE__)) . '/mlanglib.php'; // convert legacy stashes that were pull-requested $stashids = $DB->get_records('amos_stashes', array('pullrequest' => 1), 'timemodified ASC', 'id'); foreach ($stashids as $stashrecord) { $stash = mlang_stash::instance_from_id($stashrecord->id); // split the stashed components into separate packages by their language $stage = new mlang_stage(); $langstages = array(); // (string)langcode => (mlang_stage) $stash->apply($stage); foreach ($stage->get_iterator() as $component) { $lang = $component->lang; if (!isset($langstages[$lang])) { $langstages[$lang] = new mlang_stage(); } $langstages[$lang]->add($component); } $stage->clear(); unset($stage); // create new contribution record for every language and attach a new stash to it foreach ($langstages as $lang => $stage) { if (!$stage->has_component()) { // this should not happen, but... continue; } $copy = new mlang_stage(); foreach ($stage->get_iterator() as $component) { $copy->add($component); } $copy->rebase(); if ($copy->has_component()) { $tostatus = 0; // new } else { $tostatus = 30; // nothing left after rebase - consider it accepted } $langstash = mlang_stash::instance_from_stage($stage, 0, $stash->name); $langstash->message = $stash->message; $langstash->push(); $contribution = new stdClass(); $contribution->authorid = $stash->ownerid; $contribution->lang = $lang; $contribution->assignee = null; $contribution->subject = $stash->name; $contribution->message = $stash->message; $contribution->stashid = $langstash->id; $contribution->status = $tostatus; $contribution->timecreated = $stash->timemodified; $contribution->timemodified = null; $contribution->id = $DB->insert_record('amos_contributions', $contribution); // add a comment there $comment = new stdClass(); $comment->contextid = SITEID; $comment->commentarea = 'amos_contribution'; $comment->itemid = $contribution->id; $comment->content = 'This contribution was automatically created during the conversion of legacy pull-requested stashes.'; $comment->format = 0; $comment->userid = 2; $comment->timecreated = time(); $DB->insert_record('comments', $comment); } $stash->drop(); } upgrade_plugin_savepoint(true, 2011011000, 'local', 'amos'); } if ($oldversion < 2011011001) { $table = new xmldb_table('amos_stashes'); $field = new xmldb_field('shared'); if ($dbman->field_exists($table, $field)) { $dbman->drop_field($table, $field); } $field = new xmldb_field('pullrequest'); if ($dbman->field_exists($table, $field)) { $dbman->drop_field($table, $field); } $table = new xmldb_table('amos_hidden_requests'); if ($dbman->table_exists($table)) { $dbman->drop_table($table); } upgrade_plugin_savepoint(true, 2011011001, 'local', 'amos'); } return $result; }
$emailsubject = get_string('emailcontributionsubject', 'local_amos'); $emailbody = get_string('emailcontributionbody', 'local_amos', $a); foreach ($maintainers as $maintainer) { email_to_user($maintainer, $amosbot, $emailsubject, $emailbody); } } // stash has been submited so it is dropped $stash->drop(); redirect(new moodle_url('/local/amos/contrib.php')); } $output = $PAGE->get_renderer('local_amos'); /// Output starts here echo $output->header(); if ($submit) { require_sesskey(); $stash = mlang_stash::instance_from_id($submit); if ($stash->ownerid != $USER->id) { print_error('stashaccessdenied', 'local_amos'); die; } echo $output->heading_with_help(get_string('submitting', 'local_amos'), 'submitting', 'local_amos'); $stashinfo = local_amos_stash::instance_from_mlang_stash($stash, $USER); if (empty($stashinfo->strings)) { echo $output->heading(get_string('stagestringsnone', 'local_amos')); echo $output->footer(); die; } echo $output->render($stashinfo); $submitform->set_data(array('name' => s($stash->name), 'stashid' => $stash->id)); $submitform->display(); echo $output->footer();
if ($importform->save_file('importfile', $pathname)) { if (substr($filenameorig, -4) === '.php') { $name = mlang_component::name_from_filename($filenameorig); $version = mlang_version::by_code($data->version); $component = new mlang_component($name, $data->language, $version); $parser = mlang_parser_factory::get_parser('php'); try { $parser->parse(file_get_contents($pathname), $component); } catch (mlang_parser_exception $e) { notice($e->getMessage(), new moodle_url('/local/amos/stage.php')); } $encomponent = mlang_component::from_snapshot($component->name, 'en', $version); $component->intersect($encomponent); if (!$component->has_string()) { notice(get_string('nostringtoimport', 'local_amos'), new moodle_url('/local/amos/stage.php')); } $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $stage->add($component, true); $stage->store(); mlang_stash::autosave($stage); } else { notice(get_string('nostringtoimport', 'local_amos'), new moodle_url('/local/amos/stage.php')); } } else { notice(get_string('nofiletoimport', 'local_amos'), new moodle_url('/local/amos/stage.php')); } } if (!isset($stage) or !$stage->has_component()) { notice(get_string('nostringtoimport', 'local_amos'), new moodle_url('/local/amos/stage.php')); } redirect(new moodle_url('/local/amos/stage.php'));
/** * Factory method using an instance if {@link mlang_stash} as a data source * * @param mlang_stash $stash * @param stdClass $owner owner user data * @return local_amos_stash new instance */ public static function instance_from_mlang_stash(mlang_stash $stash, stdClass $owner) { if ($stash->ownerid != $owner->id) { throw new coding_exception('Stash owner mismatch'); } $new = new local_amos_stash(); $new->id = $stash->id; $new->name = $stash->name; $new->timecreated = $stash->timecreated; $stage = new mlang_stage(); $stash->apply($stage); list($new->strings, $new->languages, $new->components) = mlang_stage::analyze($stage); $stage->clear(); unset($stage); $new->components = explode('/', trim($new->components, '/')); $new->languages = explode('/', trim($new->languages, '/')); $new->owner = $owner; if ($stash->hash === 'xxxxautosaveuser' . $new->owner->id) { $new->isautosave = true; } else { $new->isautosave = false; } return $new; }
/** * Updates the AUTOSAVE stash of the given persistent stage * * For every user, AMOS keeps a single instance of mlang_stash to store the most recent * stage state. This behaves as a backup of the staged strings before they are committed. * * @param mlang_persistent_stage $stage */ public static function autosave(mlang_persistent_stage $stage) { global $DB; $instance = new mlang_stash($stage->userid); $instance->name = 'AUTOSAVE'; $instance->set_stage($stage); $instance->hash = 'xxxxautosaveuser' . $stage->userid; if ($id = $DB->get_field('amos_stashes', 'id', array('hash' => $instance->hash, 'ownerid' => $stage->userid))) { $instance->id = $id; } $instance->push(); }
$stage->store(); redirect($PAGE->url); } if (!empty($unstageall)) { require_sesskey(); $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $stage->clear(); $stage->store(); unset($SESSION->local_amos->presetcommitmessage); unset($SESSION->local_amos->stagedcontribution); redirect($PAGE->url); } if (!empty($submit)) { require_sesskey(); $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $stash = mlang_stash::instance_from_stage($stage, $stage->userid); $stash->push(); redirect(new moodle_url('/local/amos/stash.php', array('sesskey' => sesskey(), 'submit' => $stash->id))); } if (!empty($propagate)) { require_sesskey(); $stage = mlang_persistent_stage::instance_for_user($USER->id, sesskey()); $ver = optional_param('ver', array(), PARAM_INT); $num = null; if (!empty($ver) and is_array($ver)) { $versions = array(); foreach ($ver as $versioncode) { $versions[] = mlang_version::by_code($versioncode); } if ($versions) { $num = $stage->propagate($versions);
$maintainerof = false; } $contribution = $DB->get_record('amos_contributions', array('id' => $id), '*', MUST_EXIST); if ($contribution->authorid !== $USER->id) { require_capability('local/amos:commit', get_system_context()); if ($maintainerof !== 'all') { if (!in_array($contribution->lang, $maintainerof)) { print_error('contributionaccessdenied', 'local_amos'); } } $author = $DB->get_record('user', array('id' => $contribution->authorid)); } else { $author = $USER; } // get the contributed components and rebase them to see what would happen $stash = mlang_stash::instance_from_id($contribution->stashid); $stage = new mlang_stage(); $stash->apply($stage); list($origstrings, $origlanguages, $origcomponents) = mlang_stage::analyze($stage); $stage->rebase(); list($rebasedstrings, $rebasedlanguages, $rebasedcomponents) = mlang_stage::analyze($stage); if ($stage->has_component()) { } else { // nothing left after rebase } $contribinfo = new local_amos_contribution($contribution, $author); $contribinfo->language = implode(', ', array_filter(array_map('trim', explode('/', $origlanguages)))); $contribinfo->components = implode(', ', array_filter(array_map('trim', explode('/', $origcomponents)))); $contribinfo->strings = $origstrings; $contribinfo->stringsreb = $rebasedstrings; echo $output->render($contribinfo);