Source for file mvblog_upgrade.php
Documentation is available at mvblog_upgrade.php
* MvBlog -- An open source no-nosense blogtool
* Copyright (C) 2005-2007, Michiel van Baak
* Michiel van Baak <mvanbaak@users.sourceforge.net>
* See http://dev.mvblog.org for more information on MvBlog.
* That page also provides Bugtrackers, Filereleases etc.
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
* @author Michiel van Baak
* @copyright 2005-2007 Michiel van Baak
* Start the autoloader, so we never have to include anything
require_once("mvblog_autoloader.php");
$mvblog_AutoLoader->registerPath($pathInfo["dirname"], "%s.php", mvblog_AutoLoader::OPT_LOWERCASE);
// Register the AutoLoader object as the autoloader.
global $mvblog_AutoLoader;
$mvblog_AutoLoader->autoload($className);
/* Start heavy error reporting if we're on a dev site */
/* Read the configuration file */
* Uprade mvblog to the latest version
parent::__construct("plugins/", 0, 1);
* Find out what patchfiles we need to commit.
* @param string $mode Can be svn or release and is used to pick the filenameformat.
* @return array Patchfiles that should be applied to the database
/* get current database version */
$current_version =
$this->settings["dbversion"];
/* read the db patchfiles into an array */
while (false !==
($v =
readdir($dh))) {
if (!is_dir($v) &&
$v !=
"README") {
if ($mode ==
"release") {
* Better addslashes for SQL queries.
* Taken from phpMyAdmin and modified by me.
* Create a database backup before running the pathes against it so we can rollback when something goes wrong
$backuplocation =
"backups/";
$dbname =
$this->db->database_name;
die("Backupdir not available. Please create a directory 'backups' and give the webserver user write permissions on it.");
switch ($this->db->phptype) {
$src =
sprintf("db/%s.db", $dbname);
$dst =
sprintf("backups/%s-%s.db", $dbname, date("YmdHi"));
die("unknown database type. Please report on http://dev.mvblog.org");
/* _mysql_backup_db {{{ */
* @param string $database The database name to backup
* @param string $backuplocation The directory to store the backup in
* @return bool true on success, false on failure
//variable to hold the database
$dbdata =
"-- MvBlog upgrade backup dump\n";
$dbdata .=
sprintf("-- Host: %s\n", $this->db->dsn["hostspec"]);
$dbdata .=
sprintf("-- Database: %s\n", $database);
$dbdata .=
sprintf("-- Dump started on %s\n\n", date("Y-m-d H:i:s"));
$dbdata .=
"/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
$dbdata .=
"/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
$dbdata .=
"/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
$dbdata .=
"/*!40101 SET NAMES utf8 */;\n";
$dbdata .=
"/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n";
$dbdata .=
"/*!40103 SET TIME_ZONE='+00:00' */;\n";
$dbdata .=
"/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n";
$dbdata .=
"/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n";
$dbdata .=
"/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n";
$dbdata .=
"/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n";
// get the tables in the database
$res =
$this->db->query($sql);
while ($row =
$res->fetchRow()) {
$dbdata .=
sprintf("--\n-- Table structure for table '%s'\n--\n\n", $table);
$dbdata .=
sprintf("DROP TABLE IF EXISTS %s;\n", $table);
$q =
sprintf("SHOW CREATE TABLE %s", $table);
$r =
$this->db->query($q);
while ($tabledata =
$r->fetchRow()) {
$dbdata .=
$tabledata[1].
";\n\n";
$dbdata .=
sprintf("--\n-- Dumping data for table '%s'\n--\n\n", $table);
$dbdata .=
sprintf("LOCK TABLES `%s` WRITE;\n", $table);
$dbdata .=
sprintf("/*!40000 ALTER TABLE `%s` DISABLE KEYS */;\n", $table);
$q =
sprintf("DESCRIBE %s", $table);
$r =
$this->db->query($q);
while ($ts =
$r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
// find fields that dont need ' as data enclosure
foreach ($tablestruct as $struct) {
if (strpos($struct["type"], "bigint") ===
0 ||
strpos($struct["type"], "int") ===
0 ||
strpos($struct["type"], "mediumint") ===
0 ||
strpos($struct["type"], "smallint") ===
0 ||
strpos($struct["type"], "tinyint") ===
0 ||
strpos($struct["type"], "timestamp") ===
0)
$q =
sprintf("SELECT * FROM %s", $table);
$r =
$this->db->query($q);
// \x08\\x09, not required
$search =
array("\x00", "\x0a", "\x0d", "\x1a");
$replace =
array('\0', '\n', '\r', '\Z');
while ($tabledata =
$r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
$dbdata .=
sprintf("INSERT INTO `%s` VALUES (", $table);
foreach ($tabledata as $k=>
$v) {
$dbdata .=
sprintf("/*!40000 ALTER TABLE `%s` ENABLE KEYS */;\n", $table);
$dbdata .=
"UNLOCK TABLES;\n\n";
$dbdata .=
"/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n\n";
$dbdata .=
"/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n";
$dbdata .=
"/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n";
$dbdata .=
"/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n";
$dbdata .=
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n";
$dbdata .=
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n";
$dbdata .=
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n";
$dbdata .=
"/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n\n";
$dbdata .=
sprintf("-- Dump completed on %s", date("Y-m-d H:i:s"));
$backupfile =
sprintf($backuplocation.
"%s-%s.mysql", $database, date("YmdHi"));
/* _psql_backup_db {{{ */
* Create postgresql backup. Code and ideas borrowed from phpPgAdmin.
* @param string $database The database name to backup
* @param string $backuplocation The directory to store the backup in
* @return bool true on success, false on failure
// set enviroment vars needed by pg_dump
$server_info["username"] =
$this->db->dsn["username"];
$server_info["password"] =
$this->db->dsn["password"];
$server_info["host"] =
$this->db->dsn["hostspec"];
$server_info["port"] =
$this->db->dsn["port"];
putenv('PGPASSWORD=' .
$server_info['password']);
putenv('PGUSER=' .
$server_info['username']);
$hostname =
$server_info['host'];
if ($hostname !==
null &&
$hostname !=
'') {
putenv('PGHOST=' .
$hostname);
$port =
$server_info['port'];
if ($port !==
null &&
$port !=
'') {
$backupfile =
sprintf($backuplocation.
"%s-%s.psql", $database, date("YmdHi"));
$cmd .=
sprintf("-f %s %s", $backupfile, $database);
$output =
system($cmd, $retval);
/* set_latest_version {{{ */
* Update/create dbversion setting in the database
* @param int $version The latest patchlevel we applied to the database
if (array_key_exists("dbversion", $this->settings) &&
$this->settings["dbversion"]) {
$sql =
sprintf("UPDATE settings SET settingvalue = '%s' WHERE settingname = 'dbversion'", $version);
$sql =
sprintf("INSERT INTO settings (settingname, settingvalue) VALUES ('dbversion', '%s')", $version);
$res =
$this->db->exec($sql);
* Apply a specific database patch
* @param int $patch The patch to apply
$patchfile =
sprintf("upgrades/%s/%s", $this->db->phptype, $patch);
require_once($patchfile);
foreach ($sql as $query) {
$res =
$this->db->exec($query);
Documentation generated on Fri, 28 Dec 2007 13:17:41 +0100 by phpDocumentor 1.4.1