2006-02-26 16:31:11 +01:00
< ? php
/* Copyright ( C ) 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2010-03-28 01:26:01 +01:00
* Copyright ( C ) 2004 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2010 Regis Houssin < regis . houssin @ capnetworks . com >
2006-02-26 16:31:11 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2006-02-26 16:31:11 +01:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2014-01-15 19:12:55 +01:00
*
* Upgrade scripts can be ran from command line with syntax :
*
* cd htdocs / install
* php upgrade . php 3.4 . 0 3.5 . 0
* php upgrade2 . php 3.4 . 0 3.5 . 0
*
* Return code is 0 if OK , > 0 if error
2006-02-26 16:31:11 +01:00
*/
/**
2009-06-15 20:16:22 +02:00
* \file htdocs / install / upgrade . php
* \brief Run migration script
*/
2006-02-26 16:31:11 +01:00
2012-08-23 02:46:16 +02:00
include_once 'inc.php' ;
2009-09-11 17:23:42 +02:00
if ( ! file_exists ( $conffile ))
{
2011-09-29 22:21:57 +02:00
print 'Error: Dolibarr config file was not found. This may means that Dolibarr is not installed yet. Please call the page "/install/index.php" instead of "/install/upgrade.php").' ;
2009-09-11 17:23:42 +02:00
}
2012-08-22 23:11:24 +02:00
require_once $conffile ; if ( ! isset ( $dolibarr_main_db_type )) $dolibarr_main_db_type = 'mysql' ; // For backward compatibility
require_once $dolibarr_main_document_root . '/core/lib/admin.lib.php' ;
2008-01-12 19:00:48 +01:00
2006-02-26 16:31:11 +01:00
$grant_query = '' ;
$etape = 2 ;
$ok = 0 ;
2009-04-28 08:08:19 +02:00
// Cette page peut etre longue. On augmente le delai autorise.
2006-02-26 16:31:11 +01:00
// Ne fonctionne que si on est pas en safe_mode.
$err = error_reporting ();
error_reporting ( 0 );
2007-07-09 09:38:04 +02:00
@ set_time_limit ( 120 );
2006-02-26 16:31:11 +01:00
error_reporting ( $err );
2012-04-04 20:37:16 +02:00
$setuplang = GETPOST ( " selectlang " , '' , 3 ) ? GETPOST ( " selectlang " , '' , 3 ) : 'auto' ;
2005-10-30 02:07:00 +01:00
$langs -> setDefaultLang ( $setuplang );
2012-04-04 20:37:16 +02:00
$versionfrom = GETPOST ( " versionfrom " , '' , 3 ) ? GETPOST ( " versionfrom " , '' , 3 ) : ( empty ( $argv [ 1 ]) ? '' : $argv [ 1 ]);
$versionto = GETPOST ( " versionto " , '' , 3 ) ? GETPOST ( " versionto " , '' , 3 ) : ( empty ( $argv [ 2 ]) ? '' : $argv [ 2 ]);
2013-01-23 14:51:15 +01:00
$versionmodule = GETPOST ( " versionmodule " , '' , 3 ) ? GETPOST ( " versionmodule " , '' , 3 ) : ( empty ( $argv [ 3 ]) ? '' : $argv [ 3 ]);
2006-02-26 16:31:11 +01:00
$langs -> load ( " admin " );
2012-09-07 17:23:16 +02:00
$langs -> load ( " install " );
$langs -> load ( " errors " );
2006-02-26 16:31:11 +01:00
2006-08-06 01:55:10 +02:00
if ( $dolibarr_main_db_type == " mysql " ) $choix = 1 ;
if ( $dolibarr_main_db_type == " mysqli " ) $choix = 1 ;
if ( $dolibarr_main_db_type == " pgsql " ) $choix = 2 ;
2007-09-01 13:23:34 +02:00
if ( $dolibarr_main_db_type == " mssql " ) $choix = 3 ;
2006-08-06 01:55:10 +02:00
2006-02-26 16:31:11 +01:00
2007-05-10 20:58:52 +02:00
dolibarr_install_syslog ( " upgrade: Entering upgrade.php page " );
2007-10-04 19:07:15 +02:00
if ( ! is_object ( $conf )) dolibarr_install_syslog ( " upgrade2: conf file not initialized " , LOG_ERR );
2006-06-17 16:13:49 +02:00
2009-06-15 20:16:22 +02:00
/*
* View
*/
2012-04-04 20:37:16 +02:00
pHeader ( '' , " upgrade2 " , GETPOST ( 'action' ), 'versionfrom=' . $versionfrom . '&versionto=' . $versionto );
2006-02-26 16:31:11 +01:00
2009-06-15 20:16:22 +02:00
$actiondone = 0 ;
2007-10-04 19:07:15 +02:00
2012-04-04 20:37:16 +02:00
// Action to launch the migrate script
2010-11-11 00:54:59 +01:00
if ( ! GETPOST ( " action " ) || preg_match ( '/upgrade/i' , GETPOST ( 'action' )))
2006-02-26 16:31:11 +01:00
{
2011-09-29 22:21:57 +02:00
$actiondone = 1 ;
print '<h3>' . $langs -> trans ( " DatabaseMigration " ) . '</h3>' ;
if ( ! $versionfrom && ! $versionto )
{
2012-04-04 20:37:16 +02:00
print '<div class="error">Parameter versionfrom or versionto missing. Upgrade is launched from page install/index.php (like a first install) instead of install/upgrade.php</div>' ;
2011-09-29 22:21:57 +02:00
exit ;
}
print '<table cellspacing="0" cellpadding="1" border="0" width="100%">' ;
$error = 0 ;
// If password is encoded, we decode it
if ( preg_match ( '/crypted:/i' , $dolibarr_main_db_pass ) || ! empty ( $dolibarr_main_db_encrypted_pass ))
{
2012-08-22 23:11:24 +02:00
require_once $dolibarr_main_document_root . '/core/lib/security.lib.php' ;
2011-09-29 22:21:57 +02:00
if ( preg_match ( '/crypted:/i' , $dolibarr_main_db_pass ))
{
$dolibarr_main_db_pass = preg_replace ( '/crypted:/i' , '' , $dolibarr_main_db_pass );
$dolibarr_main_db_pass = dol_decode ( $dolibarr_main_db_pass );
$dolibarr_main_db_encrypted_pass = $dolibarr_main_db_pass ; // We need to set this as it is used to know the password was initially crypted
}
else $dolibarr_main_db_pass = dol_decode ( $dolibarr_main_db_encrypted_pass );
}
// $conf is already instancied inside inc.php
$conf -> db -> type = $dolibarr_main_db_type ;
$conf -> db -> host = $dolibarr_main_db_host ;
$conf -> db -> port = $dolibarr_main_db_port ;
$conf -> db -> name = $dolibarr_main_db_name ;
$conf -> db -> user = $dolibarr_main_db_user ;
$conf -> db -> pass = $dolibarr_main_db_pass ;
// Load type and crypt key
if ( empty ( $dolibarr_main_db_encryption )) $dolibarr_main_db_encryption = 0 ;
$conf -> db -> dolibarr_main_db_encryption = $dolibarr_main_db_encryption ;
if ( empty ( $dolibarr_main_db_cryptkey )) $dolibarr_main_db_cryptkey = '' ;
$conf -> db -> dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey ;
2007-05-10 20:58:52 +02:00
2011-09-28 16:26:49 +02:00
$db = getDoliDBInstance ( $conf -> db -> type , $conf -> db -> host , $conf -> db -> user , $conf -> db -> pass , $conf -> db -> name , $conf -> db -> port );
2011-09-28 19:41:08 +02:00
2013-03-06 17:21:51 +01:00
// Create the global $hookmanager object
include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php' ;
$hookmanager = new HookManager ( $db );
2011-09-28 19:41:08 +02:00
if ( $db -> connected == 1 )
2011-09-29 22:21:57 +02:00
{
2013-04-25 01:13:13 +02:00
print '<tr><td class="nowrap">' ;
2011-09-29 22:21:57 +02:00
print $langs -> trans ( " ServerConnection " ) . " : $dolibarr_main_db_host </td><td align= \" right \" > " . $langs -> trans ( " OK " ) . " </td></tr> \n " ;
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ServerConnection " ) . " : $dolibarr_main_db_host " . $langs -> transnoentities ( " OK " ));
$ok = 1 ;
}
else
{
print " <tr><td> " . $langs -> trans ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ) . " </td><td align= \" right \" > " . $langs -> transnoentities ( " Error " ) . " </td></tr> \n " ;
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ));
$ok = 0 ;
}
if ( $ok )
{
if ( $db -> database_selected == 1 )
{
2013-04-25 01:13:13 +02:00
print '<tr><td class="nowrap">' ;
2011-09-29 22:21:57 +02:00
print $langs -> trans ( " DatabaseConnection " ) . " : " . $dolibarr_main_db_name . " </td><td align= \" right \" > " . $langs -> trans ( " OK " ) . " </td></tr> \n " ;
dolibarr_install_syslog ( " upgrade: Database connection successfull : $dolibarr_main_db_name " );
$ok = 1 ;
}
else
{
print " <tr><td> " . $langs -> trans ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ) . " </td><td align= \" right \" > " . $langs -> trans ( " Error " ) . " </td></tr> \n " ;
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ));
$ok = 0 ;
}
}
// Affiche version
if ( $ok )
{
$version = $db -> getVersion ();
$versionarray = $db -> getVersionArray ();
print '<tr><td>' . $langs -> trans ( " ServerVersion " ) . '</td>' ;
print '<td align="right">' . $version . '</td></tr>' ;
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ServerVersion " ) . " : $version " );
2012-09-02 01:47:21 +02:00
// Test database version
2012-09-07 17:23:16 +02:00
$versionmindb = getStaticMember ( get_class ( $db ), 'versionmin' );
2012-09-02 01:47:21 +02:00
//print join('.',$versionarray).' - '.join('.',$versionmindb);
2012-09-07 17:23:16 +02:00
if ( count ( $versionmindb ) && count ( $versionarray )
2012-09-02 01:47:21 +02:00
&& versioncompare ( $versionarray , $versionmindb ) < 0 )
{
// Warning: database version too low.
2012-09-07 17:23:16 +02:00
print " <tr><td> " . $langs -> trans ( " ErrorDatabaseVersionTooLow " , join ( '.' , $versionarray ), join ( '.' , $versionmindb )) . " </td><td align= \" right \" > " . $langs -> trans ( " Error " ) . " </td></tr> \n " ;
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ErrorDatabaseVersionTooLow " , join ( '.' , $versionarray ), join ( '.' , $versionmindb )));
$ok = 0 ;
}
2012-09-02 01:47:21 +02:00
2011-09-29 22:21:57 +02:00
}
// Force l'affichage de la progression
2012-09-02 01:47:21 +02:00
if ( $ok )
{
print '<tr><td colspan="2">' . $langs -> trans ( " PleaseBePatient " ) . '</td></tr>' ;
flush ();
}
2011-09-29 22:21:57 +02:00
/*
* Delete duplicates in table categorie_association
*/
2012-09-02 01:47:21 +02:00
if ( $ok )
2011-09-29 22:21:57 +02:00
{
2012-10-17 14:15:11 +02:00
$result = $db -> DDLDescTable ( MAIN_DB_PREFIX . " categorie_association " );
2013-01-16 13:43:25 +01:00
if ( $result ) // result defined for version 3.2 or -
2012-09-02 01:47:21 +02:00
{
2013-01-16 13:43:25 +01:00
$obj = $db -> fetch_object ( $result );
if ( $obj ) // It table categorie_association exists
2012-10-17 14:15:11 +02:00
{
2013-01-16 13:43:25 +01:00
$couples = array ();
$filles = array ();
$sql = " SELECT fk_categorie_mere, fk_categorie_fille " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " categorie_association " ;
dolibarr_install_syslog ( " upgrade: search duplicate sql= " . $sql );
$resql = $db -> query ( $sql );
if ( $resql )
{
$num = $db -> num_rows ( $resql );
while ( $obj = $db -> fetch_object ( $resql ))
{
if ( ! isset ( $filles [ $obj -> fk_categorie_fille ])) // Only one record as child (a child has only on parent).
{
if ( $obj -> fk_categorie_mere != $obj -> fk_categorie_fille )
{
$filles [ $obj -> fk_categorie_fille ] = 1 ; // Set record for this child
$couples [ $obj -> fk_categorie_mere . '_' . $obj -> fk_categorie_fille ] = array ( 'mere' => $obj -> fk_categorie_mere , 'fille' => $obj -> fk_categorie_fille );
}
}
}
dolibarr_install_syslog ( " upgrade: result is num= " . $num . " count(couples)= " . count ( $couples ));
// If there is duplicates couples or child with two parents
if ( count ( $couples ) > 0 && $num > count ( $couples ))
{
$error = 0 ;
$db -> begin ();
// We delete all
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " categorie_association " ;
dolibarr_install_syslog ( " upgrade: delete association sql= " . $sql );
$resqld = $db -> query ( $sql );
if ( $resqld )
{
// And we insert only each record once
foreach ( $couples as $key => $val )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " categorie_association(fk_categorie_mere,fk_categorie_fille) " ;
$sql .= " VALUES( " . $val [ 'mere' ] . " , " . $val [ 'fille' ] . " ) " ;
dolibarr_install_syslog ( " upgrade: insert association sql= " . $sql );
$resqli = $db -> query ( $sql );
if ( ! $resqli ) $error ++ ;
}
}
if ( ! $error )
{
print '<tr><td>' . $langs -> trans ( " RemoveDuplicates " ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( " Success " ) . ' (' . $num . '=>' . count ( $couples ) . ')</td></tr>' ;
$db -> commit ();
}
else
{
print '<tr><td>' . $langs -> trans ( " RemoveDuplicates " ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( " Failed " ) . '</td></tr>' ;
$db -> rollback ();
}
}
}
else
{
print '<div class="error">' . $langs -> trans ( " Error " ) . ' ' . $db -> lasterror () . '</div>' ;
}
2012-10-17 14:15:11 +02:00
}
2012-09-02 01:47:21 +02:00
}
2011-09-29 22:21:57 +02:00
}
2014-01-15 19:12:55 +01:00
2012-01-07 08:28:52 +01:00
/*
* Remove deprecated indexes and constraints for Mysql
*/
2011-09-29 22:21:57 +02:00
if ( $ok && preg_match ( '/mysql/' , $db -> type ))
{
2012-09-02 01:47:21 +02:00
$versioncommande = array ( 4 , 0 , 0 );
2011-09-29 22:21:57 +02:00
if ( count ( $versioncommande ) && count ( $versionarray )
&& versioncompare ( $versioncommande , $versionarray ) <= 0 ) // Si mysql >= 4.0
{
// Suppression vieilles contraintes sans noms et en doubles
// Les contraintes indesirables ont un nom qui commence par 0_ ou se termine par ibfk_999
2012-01-07 08:28:52 +01:00
$listtables = array (
MAIN_DB_PREFIX . 'adherent_options' ,
MAIN_DB_PREFIX . 'bank_class' ,
MAIN_DB_PREFIX . 'c_ecotaxe' ,
MAIN_DB_PREFIX . 'c_methode_commande_fournisseur' , // table renamed
MAIN_DB_PREFIX . 'c_input_method'
);
2012-04-04 20:37:16 +02:00
2011-09-29 22:21:57 +02:00
$listtables = $db -> DDLListTables ( $conf -> db -> name , '' );
foreach ( $listtables as $val )
{
2012-01-07 08:28:52 +01:00
// Database prefix filter
if ( preg_match ( '/^' . MAIN_DB_PREFIX . '/' , $val ))
{
//print "x".$val."<br>";
$sql = " SHOW CREATE TABLE " . $val ;
$resql = $db -> query ( $sql );
if ( $resql )
{
$values = $db -> fetch_array ( $resql );
$i = 0 ;
$createsql = $values [ 1 ];
while ( preg_match ( '/CONSTRAINT `(0_[0-9a-zA-Z]+|[_0-9a-zA-Z]+_ibfk_[0-9]+)`/i' , $createsql , $reg ) && $i < 100 )
{
$sqldrop = " ALTER TABLE " . $val . " DROP FOREIGN KEY " . $reg [ 1 ];
$resqldrop = $db -> query ( $sqldrop );
if ( $resqldrop )
{
print '<tr><td colspan="2">' . $sqldrop . " ;</td></tr> \n " ;
}
$createsql = preg_replace ( '/CONSTRAINT `' . $reg [ 1 ] . '`/i' , 'XXX' , $createsql );
$i ++ ;
}
$db -> free ( $resql );
}
else
{
if ( $db -> lasterrno () != 'DB_ERROR_NOSUCHTABLE' )
{
print '<tr><td colspan="2"><font class="error">' . $sql . ' : ' . $db -> lasterror () . " </font></td></tr> \n " ;
}
}
}
2011-09-29 22:21:57 +02:00
}
}
}
/*
* Load sql files
*/
if ( $ok )
{
$dir = " mysql/migration/ " ; // We use mysql migration scripts whatever is database driver
2013-01-23 14:51:15 +01:00
if ( ! empty ( $versionmodule )) $dir = dol_buildpath ( '/' . $versionmodule . '/sql/' , 0 );
2011-09-29 22:21:57 +02:00
2013-03-13 11:06:28 +01:00
// Clean last part to exclude minor version x.y.z -> x.y
2012-09-07 17:23:16 +02:00
$newversionfrom = preg_replace ( '/(\.[0-9]+)$/i' , '.0' , $versionfrom );
2012-09-03 16:27:16 +02:00
$newversionto = preg_replace ( '/(\.[0-9]+)$/i' , '.0' , $versionto );
2011-09-29 22:21:57 +02:00
$filelist = array ();
$i = 0 ;
$ok = 0 ;
2012-09-03 16:27:16 +02:00
$from = '^' . $newversionfrom ;
$to = $newversionto . '\.sql$' ;
2011-09-29 22:21:57 +02:00
// Get files list
$filesindir = array ();
$handle = opendir ( $dir );
if ( is_resource ( $handle ))
{
while (( $file = readdir ( $handle )) !== false )
{
2014-01-15 19:12:55 +01:00
if ( preg_match ( '/\.sql$/i' , $file )) $filesindir [] = $file ;
2011-09-29 22:21:57 +02:00
}
sort ( $filesindir );
}
else
2014-01-15 19:12:55 +01:00
{
2011-09-29 22:21:57 +02:00
print '<div class="error">' . $langs -> trans ( " ErrorCanNotReadDir " , $dir ) . '</div>' ;
}
// Define which file to run
foreach ( $filesindir as $file )
{
if ( preg_match ( '/' . $from . '/i' , $file ))
{
$filelist [] = $file ;
}
else if ( preg_match ( '/' . $to . '/i' , $file )) // First test may be false if we migrate from x.y.* to x.y.*
{
$filelist [] = $file ;
}
}
2011-12-10 11:26:04 +01:00
// Loop on each migrate files
2011-09-29 22:21:57 +02:00
foreach ( $filelist as $file )
{
2013-03-13 11:06:28 +01:00
print '<tr><td colspan="2"><hr></td></tr>' ;
2013-07-19 14:23:42 +02:00
print '<tr><td class="nowrap">' . $langs -> trans ( " ChoosedMigrateScript " ) . '</td><td align="right">' . $file . '</td></tr>' . " \n " ;
2011-09-29 22:21:57 +02:00
// Run sql script
$ok = run_sql ( $dir . $file , 0 , '' , 1 );
2013-03-13 11:06:28 +01:00
2013-03-30 14:27:13 +01:00
// Scan if there is migration scripts for modules htdocs/module/sql or htdocs/custom/module/sql
$modulesfile = array ();
foreach ( $conf -> file -> dol_document_root as $type => $dirroot )
{
2013-04-07 13:44:04 +02:00
$handlemodule =@ opendir ( $dirroot ); // $dirroot may be '..'
2013-03-30 14:27:13 +01:00
if ( is_resource ( $handlemodule ))
2013-03-13 11:06:28 +01:00
{
2013-03-30 14:27:13 +01:00
while (( $filemodule = readdir ( $handlemodule )) !== false )
{
2013-04-07 13:44:04 +02:00
if ( ! preg_match ( '/\./' , $filemodule ) && is_dir ( $dirroot . '/' . $filemodule . '/sql' )) // We exclude filemodule that contains . (are not directories) and are not directories.
2013-03-13 11:06:28 +01:00
{
2013-03-30 14:27:13 +01:00
//print "Scan for ".$dirroot . '/' . $filemodule . '/sql/'.$file;
if ( is_file ( $dirroot . '/' . $filemodule . '/sql/' . $file ))
{
$modulesfile [ $dirroot . '/' . $filemodule . '/sql/' . $file ] = '/' . $filemodule . '/sql/' . $file ;
}
}
}
closedir ( $handlemodule );
}
}
2013-03-13 11:06:28 +01:00
foreach ( $modulesfile as $modulefilelong => $modulefileshort )
2013-03-30 14:27:13 +01:00
{
2013-03-13 11:06:28 +01:00
print '<tr><td colspan="2"><hr></td></tr>' ;
2013-07-19 14:23:42 +02:00
print '<tr><td class="nowrap">' . $langs -> trans ( " ChoosedMigrateScript " ) . ' (external modules)</td><td align="right">' . $modulefileshort . '</td></tr>' . " \n " ;
2013-03-13 11:06:28 +01:00
// Run sql script
$okmodule = run_sql ( $modulefilelong , 0 , '' , 1 ); // Note: Result of migration of external module should not decide if we continue migration of Dolibarr or not.
}
2011-09-29 22:21:57 +02:00
}
}
print '</table>' ;
if ( $db -> connected ) $db -> close ();
2006-02-26 16:31:11 +01:00
}
2009-06-15 20:16:22 +02:00
if ( empty ( $actiondone ))
2006-02-26 16:31:11 +01:00
{
print '<div class="error">' . $langs -> trans ( " ErrorWrongParameters " ) . '</div>' ;
}
2012-08-05 00:46:10 +02:00
$ret = 0 ;
if ( ! $ok && isset ( $argv [ 1 ])) $ret = 1 ;
dol_syslog ( " Exit " . $ret );
2013-01-23 14:51:15 +01:00
pFooter ((( ! $ok && empty ( $_GET [ " ignoreerrors " ])) || $versionmodule ), $setuplang );
2006-02-26 16:31:11 +01:00
2012-04-04 20:37:16 +02:00
if ( $db -> connected ) $db -> close ();
// Return code if ran from command line
2012-08-05 00:46:10 +02:00
if ( $ret ) exit ( $ret );
2012-09-03 18:03:39 +02:00
?>