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 >
2010-09-23 12:02:27 +02:00
* Copyright ( C ) 2005 - 2010 Regis Houssin < regis @ dolibarr . fr >
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
* the Free Software Foundation ; either version 2 of the License , or
* ( 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
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
/**
2009-06-15 20:16:22 +02:00
* \file htdocs / install / upgrade . php
* \brief Run migration script
* \version $Id $
*/
2006-02-26 16:31:11 +01:00
include_once ( " ./inc.php " );
2009-09-11 17:23:42 +02:00
if ( ! file_exists ( $conffile ))
{
2010-08-31 23:43:00 +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
}
2010-01-09 02:22:16 +01:00
require_once ( $conffile ); if ( ! isset ( $dolibarr_main_db_type )) $dolibarr_main_db_type = 'mysql' ; // For backward compatibility
2008-01-12 19:00:48 +01:00
require_once ( $dolibarr_main_document_root . " /lib/databases/ " . $dolibarr_main_db_type . " .lib.php " );
require_once ( $dolibarr_main_document_root . " /lib/admin.lib.php " );
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 );
$setuplang = isset ( $_POST [ " selectlang " ]) ? $_POST [ " selectlang " ] : ( isset ( $_GET [ " selectlang " ]) ? $_GET [ " selectlang " ] : 'auto' );
2005-10-30 02:07:00 +01:00
$langs -> setDefaultLang ( $setuplang );
2009-12-15 14:02:19 +01:00
$versionfrom = isset ( $_POST [ " versionfrom " ]) ? $_POST [ " versionfrom " ] : ( isset ( $_GET [ " versionfrom " ]) ? $_GET [ " versionfrom " ] : '' );
$versionto = isset ( $_POST [ " versionto " ]) ? $_POST [ " versionto " ] : ( isset ( $_GET [ " versionto " ]) ? $_GET [ " versionto " ] : '' );
2006-02-26 16:31:11 +01:00
$langs -> load ( " admin " );
$langs -> load ( " install " );
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
*/
2010-07-20 20:14:53 +02:00
pHeader ( '' , " upgrade2 " , isset ( $_REQUEST [ 'action' ]) ? $_REQUEST [ '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
2009-06-15 20:16:22 +02:00
// Action to launch the repair or 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
{
2009-06-15 20:16:22 +02:00
$actiondone = 1 ;
2009-08-08 18:26:06 +02:00
print '<h3>' . $langs -> trans ( " DatabaseMigration " ) . '</h3>' ;
2007-05-10 20:58:52 +02:00
2009-09-11 00:00:20 +02:00
if ( ! $versionfrom && ! $versionto )
2007-05-10 20:58:52 +02:00
{
2010-08-31 23:43:00 +02:00
print '<div class="error">Parameter versionfrom or version to missing. Upgrade is launched from page install/index.php (like a first install) instead of install/upgrade.php</div>' ;
2007-05-10 20:58:52 +02:00
exit ;
}
print '<table cellspacing="0" cellpadding="1" border="0" width="100%">' ;
$error = 0 ;
2009-04-21 00:16:56 +02:00
2009-08-21 21:09:20 +02:00
// If password is encoded, we decode it
2009-10-21 16:02:14 +02:00
if ( preg_match ( '/crypted:/i' , $dolibarr_main_db_pass ) || ! empty ( $dolibarr_main_db_encrypted_pass ))
2008-04-29 23:13:49 +02:00
{
require_once ( $dolibarr_main_document_root . " /lib/security.lib.php " );
2009-10-21 16:02:14 +02:00
if ( preg_match ( '/crypted:/i' , $dolibarr_main_db_pass ))
2009-08-21 21:09:20 +02:00
{
2009-10-21 16:02:14 +02:00
$dolibarr_main_db_pass = preg_replace ( '/crypted:/i' , '' , $dolibarr_main_db_pass );
2009-08-21 21:09:20 +02:00
$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 );
2008-04-29 23:13:49 +02:00
}
2007-05-10 20:58:52 +02:00
2007-10-04 19:07:15 +02:00
// $conf is already instancied inside inc.php
2007-05-10 20:58:52 +02:00
$conf -> db -> type = $dolibarr_main_db_type ;
$conf -> db -> host = $dolibarr_main_db_host ;
2008-03-10 23:38:43 +01:00
$conf -> db -> port = $dolibarr_main_db_port ;
2007-05-10 20:58:52 +02:00
$conf -> db -> name = $dolibarr_main_db_name ;
$conf -> db -> user = $dolibarr_main_db_user ;
$conf -> db -> pass = $dolibarr_main_db_pass ;
2008-03-10 23:38:43 +01:00
$db = new DoliDb ( $conf -> db -> type , $conf -> db -> host , $conf -> db -> user , $conf -> db -> pass , $conf -> db -> name , $conf -> db -> port );
2007-05-10 20:58:52 +02:00
if ( $db -> connected == 1 )
{
2009-08-08 18:26:06 +02:00
print '<tr><td nowrap="nowrap">' ;
2010-07-13 21:16:09 +02:00
print $langs -> trans ( " ServerConnection " ) . " : $dolibarr_main_db_host </td><td align= \" right \" > " . $langs -> trans ( " OK " ) . " </td></tr> \n " ;
2007-10-04 19:07:15 +02:00
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ServerConnection " ) . " : $dolibarr_main_db_host " . $langs -> transnoentities ( " OK " ));
2007-05-10 20:58:52 +02:00
$ok = 1 ;
}
else
{
2010-07-13 21:16:09 +02:00
print " <tr><td> " . $langs -> trans ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ) . " </td><td align= \" right \" > " . $langs -> transnoentities ( " Error " ) . " </td></tr> \n " ;
2007-10-04 19:07:15 +02:00
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ));
2007-05-09 10:16:41 +02:00
$ok = 0 ;
2007-05-10 20:58:52 +02:00
}
if ( $ok )
{
if ( $db -> database_selected == 1 )
{
2009-08-08 18:26:06 +02:00
print '<tr><td nowrap="nowrap">' ;
2010-07-13 21:16:09 +02:00
print $langs -> trans ( " DatabaseConnection " ) . " : " . $dolibarr_main_db_name . " </td><td align= \" right \" > " . $langs -> trans ( " OK " ) . " </td></tr> \n " ;
2007-05-10 20:58:52 +02:00
dolibarr_install_syslog ( " upgrade: Database connection successfull : $dolibarr_main_db_name " );
2009-04-21 00:16:56 +02:00
$ok = 1 ;
2007-05-09 10:16:41 +02:00
}
2007-05-10 20:58:52 +02:00
else
{
2010-07-13 21:16:09 +02:00
print " <tr><td> " . $langs -> trans ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ) . " </td><td align= \" right \" > " . $langs -> trans ( " Error " ) . " </td></tr> \n " ;
2007-10-04 19:07:15 +02:00
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ErrorFailedToConnectToDatabase " , $dolibarr_main_db_name ));
2007-05-10 20:58:52 +02:00
$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>' ;
2007-10-04 19:07:15 +02:00
dolibarr_install_syslog ( " upgrade: " . $langs -> transnoentities ( " ServerVersion " ) . " : $version " );
2007-05-10 20:58:52 +02:00
//print '<td align="right">'.join('.',$versionarray).'</td></tr>';
}
2009-04-21 00:16:56 +02:00
// Force l'affichage de la progression
print '<tr><td colspan="2">' . $langs -> trans ( " PleaseBePatient " ) . '</td></tr>' ;
2007-10-14 11:09:35 +02:00
flush ();
2007-05-10 20:58:52 +02:00
2009-06-15 20:16:22 +02:00
2009-09-11 00:00:20 +02:00
/*
* Delete duplicates in table categorie_association
*/
$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 )
2009-04-21 00:16:56 +02:00
{
2009-09-11 00:00:20 +02:00
$num = $db -> num_rows ( $resql );
while ( $obj = $db -> fetch_object ( $resql ))
2009-04-21 00:16:56 +02:00
{
2009-09-11 00:00:20 +02:00
if ( ! isset ( $filles [ $obj -> fk_categorie_fille ])) // Only one record as child (a child has only on parent).
2009-04-21 00:16:56 +02:00
{
2009-09-11 00:00:20 +02:00
if ( $obj -> fk_categorie_mere != $obj -> fk_categorie_fille )
2009-04-21 00:16:56 +02:00
{
2009-09-11 00:00:20 +02:00
$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 );
2009-04-21 00:16:56 +02:00
}
}
2009-09-11 00:00:20 +02:00
}
2009-04-21 00:16:56 +02:00
2009-09-11 00:00:20 +02:00
dolibarr_install_syslog ( " upgrade: result is num= " . $num . " sizeof(couples)= " . sizeof ( $couples ));
2009-04-21 00:16:56 +02:00
2009-09-11 00:00:20 +02:00
// If there is duplicates couples or child with two parents
if ( sizeof ( $couples ) > 0 && $num > sizeof ( $couples ))
{
$error = 0 ;
2009-04-21 00:16:56 +02:00
2009-09-11 00:00:20 +02:00
$db -> begin ();
2009-04-21 00:16:56 +02:00
2009-09-11 00:00:20 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " categorie_association " ;
dolibarr_install_syslog ( " upgrade: delete association sql= " . $sql );
$resqld = $db -> query ( $sql );
if ( $resqld )
{
foreach ( $couples as $key => $val )
2009-04-21 00:16:56 +02:00
{
2009-09-11 00:00:20 +02:00
$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 ++ ;
2009-04-21 00:16:56 +02:00
}
2009-09-11 00:00:20 +02:00
}
2009-04-21 00:16:56 +02:00
2009-09-11 00:00:20 +02:00
if ( ! $error )
{
print '<tr><td>' . $langs -> trans ( " RemoveDuplicates " ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( " Success " ) . ' (' . $num . '=>' . sizeof ( $couples ) . ')</td></tr>' ;
$db -> commit ();
}
else
{
print '<tr><td>' . $langs -> trans ( " RemoveDuplicates " ) . '</td>' ;
print '<td align="right">' . $langs -> trans ( " Failed " ) . '</td></tr>' ;
$db -> rollback ();
2009-04-21 00:16:56 +02:00
}
}
2009-09-11 00:00:20 +02:00
}
else
{
print '<div class="error">' . $langs -> trans ( " Error " ) . '</div>' ;
}
2009-04-21 00:16:56 +02:00
2009-09-11 00:00:20 +02:00
/*
2010-04-30 20:00:39 +02:00
* Remove deprecated indexes and constraints for Mysql
2009-09-11 00:00:20 +02:00
*/
2010-04-30 20:00:39 +02:00
if ( $ok && preg_match ( '/mysql/' , $db -> type ))
2009-09-11 00:00:20 +02:00
{
2009-10-20 15:14:44 +02:00
$versioncommande = explode ( '.' , '4.0' );
2009-09-11 00:00:20 +02:00
if ( sizeof ( $versioncommande ) && sizeof ( $versionarray )
&& versioncompare ( $versioncommande , $versionarray ) <= 0 ) // Si mysql >= 4.0
2007-06-13 21:03:17 +02:00
{
2009-09-11 00:00:20 +02:00
// Suppression vieilles contraintes sans noms et en doubles
// Les contraintes indesirables ont un nom qui commence par 0_ ou se termine par ibfk_999
2009-10-24 18:44:35 +02:00
$listtables = array ( 'llx_adherent_options' ,
'llx_bank_class' ,
2009-10-24 19:32:21 +02:00
'llx_c_ecotaxe' ,
'llx_c_methode_commande_fournisseur' );
2009-10-24 18:44:35 +02:00
2009-09-11 00:00:20 +02:00
$listtables = $db -> DDLListTables ( $conf -> db -> name , '' );
foreach ( $listtables as $val )
2007-06-15 00:28:53 +02:00
{
2009-09-11 00:00:20 +02:00
//print "x".$val."<br>";
$sql = " SHOW CREATE TABLE " . $val ;
$resql = $db -> query ( $sql );
if ( $resql )
2007-06-15 00:28:53 +02:00
{
2009-09-11 00:00:20 +02:00
$values = $db -> fetch_array ( $resql );
$i = 0 ;
$createsql = $values [ 1 ];
2009-10-21 16:02:14 +02:00
while ( preg_match ( '/CONSTRAINT `(0_[0-9a-zA-Z]+|[_0-9a-zA-Z]+_ibfk_[0-9]+)`/i' , $createsql , $reg ) && $i < 100 )
2007-06-15 00:28:53 +02:00
{
2009-09-11 00:00:20 +02:00
$sqldrop = " ALTER TABLE " . $val . " DROP FOREIGN KEY " . $reg [ 1 ];
$resqldrop = $db -> query ( $sqldrop );
if ( $resqldrop )
2007-06-15 00:28:53 +02:00
{
2009-09-11 00:00:20 +02:00
print '<tr><td colspan="2">' . $sqldrop . " ;</td></tr> \n " ;
2007-06-15 00:28:53 +02:00
}
2009-10-21 16:02:14 +02:00
$createsql = preg_replace ( '/CONSTRAINT `' . $reg [ 1 ] . '`/i' , 'XXX' , $createsql );
2009-09-11 00:00:20 +02:00
$i ++ ;
2007-06-15 00:28:53 +02:00
}
2009-09-11 00:00:20 +02:00
$db -> free ( $resql );
}
else
{
if ( $db -> lasterrno () != 'DB_ERROR_NOSUCHTABLE' )
2007-11-28 22:39:11 +01:00
{
2009-09-11 00:00:20 +02:00
print '<tr><td colspan="2"><font class="error">' . $sql . ' : ' . $db -> lasterror () . " </font></td></tr> \n " ;
2007-11-28 22:39:11 +01:00
}
2007-06-15 00:28:53 +02:00
}
}
2007-06-13 21:03:17 +02:00
}
}
2007-05-10 20:58:52 +02:00
2009-06-15 20:16:22 +02:00
/*
* Load sql files
*/
2007-05-10 20:58:52 +02:00
if ( $ok )
{
2010-04-30 20:00:39 +02:00
$dir = " mysql/migration/ " ; // We use mysql migration scripts whatever is database driver
2009-04-21 00:16:56 +02:00
2007-05-10 20:58:52 +02:00
$filelist = array ();
$i = 0 ;
$ok = 0 ;
$from = '^' . $versionfrom ;
$to = $versionto . '\.sql$' ;
# Recupere list fichier
$filesindir = array ();
$handle = opendir ( $dir );
2010-12-15 19:15:08 +01:00
if ( is_resource ( $handle ))
2010-04-30 20:00:39 +02:00
{
while (( $file = readdir ( $handle )) !== false )
{
if ( preg_match ( '/\.sql$/i' , $file )) $filesindir [] = $file ;
}
sort ( $filesindir );
}
else
2007-05-10 20:58:52 +02:00
{
2010-04-30 20:00:39 +02:00
print '<div class="error">' . $langs -> trans ( " ErrorCanNotReadDir " , $dir ) . '</div>' ;
2007-05-10 20:58:52 +02:00
}
2009-06-15 20:16:22 +02:00
# Define which file to run
2009-09-11 00:00:20 +02:00
foreach ( $filesindir as $file )
2007-05-10 20:58:52 +02:00
{
2009-10-22 19:56:57 +02:00
if ( preg_match ( '/' . $from . '/i' , $file ))
2007-05-10 20:58:52 +02:00
{
2009-09-11 00:00:20 +02:00
$filelist [] = $file ;
2007-05-10 20:58:52 +02:00
}
2009-10-22 19:56:57 +02:00
else if ( preg_match ( '/' . $to . '/i' , $file )) // First test may be false if we migrate from x.y.* to x.y.*
2007-05-10 20:58:52 +02:00
{
2009-09-11 00:00:20 +02:00
$filelist [] = $file ;
2007-05-10 20:58:52 +02:00
}
}
# Boucle sur chaque fichier
foreach ( $filelist as $file )
{
print '<tr><td nowrap>' ;
2010-07-13 21:16:09 +02:00
print $langs -> trans ( " ChoosedMigrateScript " ) . '</td><td align="right">' . $file . '</td></tr>' . " \n " ;
2009-04-21 00:16:56 +02:00
2010-08-24 16:42:18 +02:00
$name = substr ( $file , 0 , dol_strlen ( $file ) - 4 );
2009-04-21 00:16:56 +02:00
2008-01-12 19:00:48 +01:00
// Run sql script
2010-04-30 19:31:46 +02:00
$ok = run_sql ( $dir . $file , 0 , '' , 1 );
2007-05-10 20:58:52 +02:00
}
2007-05-06 16:12:36 +02:00
}
2006-02-26 16:31:11 +01:00
2007-05-10 20:58:52 +02:00
print '</table>' ;
2006-02-26 16:31:11 +01:00
2007-05-10 20:58:52 +02:00
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>' ;
}
2010-03-28 01:26:01 +01:00
pFooter ( ! $ok && empty ( $_GET [ " ignoreerrors " ]), $setuplang );
2006-02-26 16:31:11 +01:00
?>