/** * Checks if PHP version is high enough to run Mibew. * * @return boolean True if PHP version is suitable and false otherwise. */ protected function checkPhpVersion() { $current_version = $this->getPhpVersionId(); if ($current_version < self::MIN_PHP_VERSION) { $this->errors[] = getlocal("PHP version is {0}, but Mibew works with {1} and later versions.", array(Utils::formatVersionId($current_version), Utils::formatVersionId(self::MIN_PHP_VERSION))); return false; } return true; }
/** * Tries to update a plugin. * * @param string $plugin_name Name of the plugin to update. * @return boolean Indicates if the plugin has been updated or not. */ public function update($plugin_name) { $plugin = new PluginInfo($plugin_name); if (!$plugin->needsUpdate()) { // There is no need to update the plugin return true; } if (!$plugin->canBeUpdated()) { // The plugin cannot be updated. return false; } try { // Perform incremental updates $updates = MaintenanceUtils::getUpdates($plugin->getClass()); foreach ($updates as $version => $method) { // Skip updates to lower versions. if (version_compare($version, $plugin->getInstalledVersion()) <= 0) { continue; } // Skip updates to versions that greater then the current plugin // version. if (version_compare($version, $plugin->getVersion()) > 0) { break; } // Run the update if (!$method()) { // By some reasons we cannot update to the next version. // Stop the process here. return false; } // Store new version number in the database. With this info // we can rerun the updating process if one of pending // updates fails. $plugin->getState()->version = $version; $plugin->getState()->save(); } } catch (\Exception $e) { // Something went wrong trigger_error(sprintf('Update of "%s" plugin failed: %s', $plugin->getName(), $e->getMessage()), E_USER_WARNING); return false; } // All updates are done. Make sure the state of the plugin contains // current plugin version. $plugin->getState()->version = $plugin->getVersion(); $plugin->getState()->save(); return true; }
/** * Performs all database updates needed for 2.1.0. * * @return boolean True if the updates have been applied successfully and * false otherwise. */ protected function update20100() { $db = $this->getDatabase(); if (!$db) { return false; } try { // Alter locale table. $db->query('ALTER TABLE {locale} ADD COLUMN name varchar(128) NOT NULL DEFAULT "" AFTER code'); $db->query('ALTER TABLE {locale} ADD COLUMN rtl tinyint NOT NULL DEFAULT 0'); $db->query('ALTER TABLE {locale} ADD COLUMN time_locale varchar(128) NOT NULL DEFAULT "en_US"'); $db->query('ALTER TABLE {locale} ADD COLUMN date_format text'); $db->query('ALTER TABLE {locale} ADD UNIQUE KEY code (code)'); // Create a table for available updates. $db->query('CREATE TABLE {availableupdate} ( ' . 'id INT NOT NULL auto_increment PRIMARY KEY, ' . 'target varchar(255) NOT NULL, ' . 'version varchar(255) NOT NULL, ' . 'url text, ' . 'description text, ' . 'UNIQUE KEY target (target) ' . ') charset utf8 ENGINE=InnoDb'); // Generate Unique ID of Mibew instance. $db->query('INSERT INTO {config} (vckey, vcvalue) VALUES (:key, :value)', array(':key' => '_instance_id', ':value' => Utils::generateInstanceId())); } catch (\Exception $e) { $this->errors[] = getlocal('Cannot update tables: {0}', $e->getMessage()); return false; } try { // Store configs for available locales in the database. $locales = $db->query('SELECT localeid as id, code from {locale}', null, array('return_rows' => Database::RETURN_ALL_ROWS)); $locales_info = get_locales(); foreach ($locales as $row) { $id = $row['id']; $code = $row['code']; $info = (isset($locales_info[$code]) ? $locales_info[$code] : array()) + array('name' => $code, 'rtl' => false, 'time_locale' => 'en_US', 'date_format' => array('full' => '%d %B %Y, %H:%M', 'date' => '%d %B %Y', 'time' => '%H:%M')); $db->query('UPDATE {locale} SET ' . 'name = :name, rtl = :rtl, time_locale = :time_locale, ' . 'date_format = :date_format ' . 'WHERE localeid = :id', array(':id' => $id, ':name' => $info['name'], ':rtl' => $info['rtl'] ? 1 : 0, ':time_locale' => $info['time_locale'], ':date_format' => serialize($info['date_format']))); } } catch (\Exception $e) { $this->errors[] = getlocal('Cannot update content: {0}', $e->getMessage()); return false; } return true; }
/** * Performs all actions that are needed to update database structure. * * @return boolean True if the system updated successfully and false * otherwise. */ public function run() { $current_version = $this->getDatabaseVersion(); if (!preg_match("/^([0-9]{1,2}\.){2}[0-9]{1,2}(-(alpha|beta|rc)\.[0-9]+)?$/", $current_version)) { $this->errors[] = getlocal( 'The current version ({0}) is unknown or wrong formated', array($current_version) ); return false; } if (version_compare($current_version, MIBEW_VERSION) == 0) { $this->log[] = getlocal('The database is already up to date'); return true; } if (version_compare($current_version, self::MIN_VERSION) < 0) { $this->errors[] = getlocal( 'You can update the system only from {0} and later versions. The current version is {1}', array( self::MIN_VERSION, $current_version ) ); return false; } try { // Perform incremental updates foreach (Utils::getUpdates($this) as $version => $method) { if (version_compare($version, $current_version) <= 0) { // Skip updates to lower versions. continue; } // Run the update if (!$method()) { $this->errors[] = getlocal('Cannot update to {0}', array($version)); return false; } // Store new version number in the database. With this info // we can rerun the updating process if one of pending // updates fails. if (!$this->setDatabaseVersion($version)) { return false; } else { $this->log[] = getlocal('Updated to {0}', array($version)); } $current_version = $version; } } catch (\Exception $e) { // Something went wrong $this->errors[] = getlocal( 'Update failed: {0}', array($e->getMessage()) ); return false; } // Use the version from the PHP's constant as the current database // version. if ($this->setDatabaseVersion(MIBEW_VERSION)) { return false; } // Clean up the cache $this->cache->flush(); return true; }