/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; /// Set own core attributes $this->does_generate = ACTION_NONE; //$this->does_generate = ACTION_GENERATE_HTML; /// These are always here global $CFG, $XMLDB; /// Do the job, setting $result as needed /// Get the dir containing the file $dirpath = required_param('dir', PARAM_PATH); $dirpath = $CFG->dirroot . $dirpath; /// Get the correct dir if (!empty($XMLDB->dbdirs)) { $dbdir =& $XMLDB->dbdirs[$dirpath]; if ($dbdir) { /// Set some defaults $dbdir->xml_exists = false; $dbdir->xml_writeable = false; $dbdir->xml_loaded = false; ///Only if the directory exists if (!$dbdir->path_exists) { return false; } $xmldb_file = new xmldb_file($dbdir->path . '/install.xml'); ///Set the XML DTD and schema $xmldb_file->setDTD($CFG->dirroot . '/lib/xmldb/xmldb.dtd'); $xmldb_file->setSchema($CFG->dirroot . '/lib/xmldb/xmldb.xsd'); /// Set dbdir as necessary if ($xmldb_file->fileExists()) { $dbdir->xml_exists = true; } if ($xmldb_file->fileWriteable()) { $dbdir->xml_writeable = true; } /// Load the XML contents to structure $loaded = $xmldb_file->loadXMLStructure(); if ($loaded && $xmldb_file->isLoaded()) { $dbdir->xml_loaded = true; $dbdir->filemtime = filemtime($dbdir->path . '/install.xml'); } $dbdir->xml_file = $xmldb_file; } else { $this->errormsg = 'Wrong directory (' . $dirpath . ')'; $result = false; } } else { $this->errormsg = 'XMLDB structure not found'; $result = false; } /// Launch postaction if exists if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } return $result; }
/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; /// Set own core attributes $this->does_generate = ACTION_NONE; //$this->does_generate = ACTION_GENERATE_HTML; /// These are always here global $CFG, $XMLDB; /// Do the job, setting $result as needed /// Iterate over $XMLDB->dbdirs, loading their XML data to memory if ($XMLDB->dbdirs) { $dbdirs =& $XMLDB->dbdirs; foreach ($dbdirs as $dbdir) { /// Set some defaults $dbdir->xml_exists = false; $dbdir->xml_writeable = false; $dbdir->xml_loaded = false; ///Only if the directory exists if (!$dbdir->path_exists) { continue; } $xmldb_file = new xmldb_file($dbdir->path . '/install.xml'); /// Set dbdir as necessary if ($xmldb_file->fileExists()) { $dbdir->xml_exists = true; } if ($xmldb_file->fileWriteable()) { $dbdir->xml_writeable = true; } /// Load the XML contents to structure $loaded = $xmldb_file->loadXMLStructure(); if ($loaded && $xmldb_file->isLoaded()) { $dbdir->xml_loaded = true; } $dbdir->xml_file = $xmldb_file; } } return $result; }
private function recreate_table($component, $tablename) { global $DB; unset($DB->donesetup); $manager = $DB->get_manager(); $filename = get_component_directory($component) . "/db/install.xml"; $xmldbfile = new xmldb_file($filename); if (!$xmldbfile->fileExists()) { throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist'); } $loaded = $xmldbfile->loadXMLStructure(); if (!$loaded || !$xmldbfile->isLoaded()) { // Show info about the error if we can find it. if ($structure =& $xmldbfile->getStructure()) { if ($errors = $structure->getAllErrors()) { throw new ddl_exception('ddlxmlfileerror', null, 'Errors found in XMLDB file: ' . implode(', ', $errors)); } } throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??'); } $structure = $xmldbfile->getStructure(); $table = $structure->getTable($tablename); $manager->create_table($table); $DB->donesetup = true; }
/** * Create temporary tables to speed up log generation */ function stats_temp_table_create() { global $CFG, $DB; $dbman = $DB->get_manager(); // We are going to use database_manager services stats_temp_table_drop(); $xmlfile = $CFG->dirroot . '/lib/db/install.xml'; $tables = array(); // Allows for the additional xml files to be used (if necessary) $files = array($xmlfile => array('stats_daily' => array('temp_stats_daily'), 'stats_user_daily' => array('temp_stats_user_daily'), 'temp_enroled_template' => array('temp_enroled'), 'temp_log_template' => array('temp_log1', 'temp_log2'))); foreach ($files as $file => $contents) { $xmldb_file = new xmldb_file($file); if (!$xmldb_file->fileExists()) { throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist'); } $loaded = $xmldb_file->loadXMLStructure(); if (!$loaded || !$xmldb_file->isLoaded()) { throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??'); } $xmldb_structure = $xmldb_file->getStructure(); foreach ($contents as $template => $names) { $table = $xmldb_structure->getTable($template); if (is_null($table)) { throw new ddl_exception('ddlunknowntable', null, 'The table ' . $name . ' is not defined in the file ' . $xmlfile); } $table->setNext(null); $table->setPrevious(null); foreach ($names as $name) { $named = clone $table; $named->setName($name); $tables[$name] = $named; } } } try { foreach ($tables as $table) { $dbman->create_temp_table($table); } } catch (Exception $e) { mtrace('Temporary table creation failed: ' . $e->getMessage()); return false; } return true; }
/** * Invoke method, every class will have its own * returns true/false on completion, setting both * errormsg and output as necessary */ function invoke() { parent::invoke(); $result = true; /// Set own core attributes $this->does_generate = ACTION_GENERATE_HTML; /// These are always here global $CFG, $XMLDB, $DB, $OUTPUT; /// And we nedd some ddl suff $dbman = $DB->get_manager(); /// Here we'll acummulate all the wrong fields found $problemsfound = array(); /// Do the job, setting $result as needed /// Get the confirmed to decide what to do $confirmed = optional_param('confirmed', false, PARAM_BOOL); /// If not confirmed, show confirmation box if (!$confirmed) { $o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">'; $o .= ' <tr><td class="generalboxcontent">'; $o .= ' <p class="centerpara">' . $this->str[$this->introstr] . '</p>'; $o .= ' <table class="boxaligncenter" cellpadding="20"><tr><td>'; $o .= ' <div class="singlebutton">'; $o .= ' <form action="index.php?action=' . $this->title . '&confirmed=yes&sesskey=' . sesskey() . '" method="post"><fieldset class="invisiblefieldset">'; $o .= ' <input type="submit" value="' . $this->str['yes'] . '" /></fieldset></form></div>'; $o .= ' </td><td>'; $o .= ' <div class="singlebutton">'; $o .= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">'; $o .= ' <input type="submit" value="' . $this->str['no'] . '" /></fieldset></form></div>'; $o .= ' </td></tr>'; $o .= ' </table>'; $o .= ' </td></tr>'; $o .= '</table>'; $this->output = $o; } else { /// The back to edit table button $b = ' <p class="centerpara buttons">'; $b .= '<a href="index.php">[' . $this->str['back'] . ']</a>'; $b .= '</p>'; /// Iterate over $XMLDB->dbdirs, loading their XML data to memory if ($XMLDB->dbdirs) { $dbdirs =& $XMLDB->dbdirs; $o = '<ul>'; foreach ($dbdirs as $dbdir) { /// Only if the directory exists if (!$dbdir->path_exists) { continue; } /// Load the XML file $xmldb_file = new xmldb_file($dbdir->path . '/install.xml'); /// Only if the file exists if (!$xmldb_file->fileExists()) { continue; } /// Load the XML contents to structure $loaded = $xmldb_file->loadXMLStructure(); if (!$loaded || !$xmldb_file->isLoaded()) { echo $OUTPUT->notification('Errors found in XMLDB file: ' . $dbdir->path . '/install.xml'); continue; } /// Arriving here, everything is ok, get the XMLDB structure $structure = $xmldb_file->getStructure(); $o .= ' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml'); /// Getting tables if ($xmldb_tables = $structure->getTables()) { $o .= ' <ul>'; /// Foreach table, process its fields foreach ($xmldb_tables as $xmldb_table) { /// Skip table if not exists if (!$dbman->table_exists($xmldb_table)) { continue; } /// Fetch metadata from physical DB. All the columns info. if (!($metacolumns = $DB->get_columns($xmldb_table->getName()))) { //// Skip table if no metacolumns is available for it continue; } /// Table processing starts here $o .= ' <li>' . $xmldb_table->getName(); /// Do the specific check. list($output, $newproblems) = $this->check_table($xmldb_table, $metacolumns); $o .= $output; $problemsfound = array_merge($problemsfound, $newproblems); $o .= ' </li>'; /// Give the script some more time (resetting to current if exists) if ($currenttl = @ini_get('max_execution_time')) { @ini_set('max_execution_time', $currenttl); } } $o .= ' </ul>'; } $o .= ' </li>'; } $o .= '</ul>'; } /// Create a report of the problems found. $r = $this->display_results($problemsfound); /// Combine the various bits of output. $this->output = $b . $r . $o; } /// Launch postaction if exists (leave this here!) if ($this->getPostAction() && $result) { return $this->launch($this->getPostAction()); } /// Return ok if arrived here return $result; }
/** * Given one "real" tablename, create one temp table suitable for be used in backup/restore operations */ public static function create_temptable_from_real_table($backupid, $realtablename, $temptablename) { global $CFG, $DB; $dbman = $DB->get_manager(); // We are going to use database_manager services // As far as xmldb objects use a lot of circular references (prev and next) and we aren't destroying // them at all, that causes one memory leak of about 3M per backup execution, not problematic for // individual backups but critical for automated (multiple) ones. // So we are statically caching the xmldb_table definition here to produce the leak "only" once static $xmldb_tables = array(); // Not cached, get it if (!isset($xmldb_tables[$realtablename])) { // Note: For now we are going to load the realtablename from core lib/db/install.xml // that way, any change in the "template" will be applied here automatically. If this causes // too much slow, we can always forget about the template and keep maintained the xmldb_table // structure inline - manually - here. // TODO: Right now, loading the whole lib/db/install.xml is "eating" 10M, we should // change our way here in order to decrease that memory usage $templatetablename = $realtablename; $targettablename = $temptablename; $xmlfile = $CFG->dirroot . '/lib/db/install.xml'; $xmldb_file = new xmldb_file($xmlfile); if (!$xmldb_file->fileExists()) { throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist'); } $loaded = $xmldb_file->loadXMLStructure(); if (!$loaded || !$xmldb_file->isLoaded()) { throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??'); } $xmldb_structure = $xmldb_file->getStructure(); $xmldb_table = $xmldb_structure->getTable($templatetablename); if (is_null($xmldb_table)) { throw new ddl_exception('ddlunknowntable', null, 'The table ' . $templatetablename . ' is not defined in file ' . $xmlfile); } // Clean prev & next, we are alone $xmldb_table->setNext(null); $xmldb_table->setPrevious(null); // Rename $xmldb_table->setName($targettablename); // Cache it $xmldb_tables[$realtablename] = $xmldb_table; } // Arrived here, we have the table always in static cache, get it $xmldb_table = $xmldb_tables[$realtablename]; // Set default backupid (not needed but this enforce any missing backupid). That's hackery in action! $xmldb_table->getField('backupid')->setDefault($backupid); $dbman->create_temp_table($xmldb_table); // And create it }
/** * Load an install.xml file, checking that it exists, and that the structure is OK. * @param string $file the full path to the XMLDB file. * @return xmldb_file the loaded file. */ private function load_xmldb_file($file) { $xmldb_file = new xmldb_file($file); if (!$xmldb_file->fileExists()) { throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist'); } $loaded = $xmldb_file->loadXMLStructure(); if (!$loaded || !$xmldb_file->isLoaded()) { // Show info about the error if we can find it if ($structure = $xmldb_file->getStructure()) { if ($errors = $structure->getAllErrors()) { throw new ddl_exception('ddlxmlfileerror', null, 'Errors found in XMLDB file: ' . implode(', ', $errors)); } } throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??'); } return $xmldb_file; }