All-in-One WP Migration - Version 4.10

Version Description

  • Add HTTPS URL replacement
  • Fix an issue when PDO is not available
Download this release

Release Info

Developer bangelov
Plugin Icon 128x128 All-in-One WP Migration
Version 4.10
Comparing to
See all releases

Code changes from version 4.9 to 4.10

all-in-one-wp-migration.php CHANGED
@@ -5,7 +5,7 @@
5
* Description: Migration tool for all your blog data. Import or Export your blog content with a single click.
6
* Author: ServMask
7
* Author URI: https://servmask.com/
8
- * Version: 4.9
9
* Text Domain: all-in-one-wp-migration
10
* Domain Path: /languages
11
* Network: True
@@ -32,11 +32,13 @@
32
* ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
33
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
34
*/
35
@set_time_limit( 0 );
36
@ini_set( 'max_input_time', '-1' );
37
38
// Plugin Basename
39
- define( 'AI1WM_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
40
41
// Plugin Path
42
define( 'AI1WM_PATH', dirname( __FILE__ ) );
@@ -53,10 +55,10 @@ define( 'AI1WM_BACKUPS_URL', content_url( 'ai1wm-backups', __FILE__ ) );
53
// Themes Absolute Path
54
define( 'AI1WM_THEMES_PATH', get_theme_root() );
55
56
- // include constants
57
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'constants.php';
58
59
- // include loader
60
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'loader.php';
61
62
// ==========================================================================
5
* Description: Migration tool for all your blog data. Import or Export your blog content with a single click.
6
* Author: ServMask
7
* Author URI: https://servmask.com/
8
+ * Version: 4.10
9
* Text Domain: all-in-one-wp-migration
10
* Domain Path: /languages
11
* Network: True
32
* ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
33
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
34
*/
35
+
36
+ @ignore_user_abort( true );
37
@set_time_limit( 0 );
38
@ini_set( 'max_input_time', '-1' );
39
40
// Plugin Basename
41
+ define( 'AI1WM_PLUGIN_BASENAME', basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ ) );
42
43
// Plugin Path
44
define( 'AI1WM_PATH', dirname( __FILE__ ) );
55
// Themes Absolute Path
56
define( 'AI1WM_THEMES_PATH', get_theme_root() );
57
58
+ // Include constants
59
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'constants.php';
60
61
+ // Include loader
62
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'loader.php';
63
64
// ==========================================================================
constants.php CHANGED
@@ -38,7 +38,7 @@ if ( function_exists( 'gethostname' ) && in_array( gethostname(), $local ) ) {
38
// ==================
39
// = Plugin Version =
40
// ==================
41
- define( 'AI1WM_VERSION', '4.9' );
42
43
// ===============
44
// = Plugin Name =
38
// ==================
39
// = Plugin Version =
40
// ==================
41
+ define( 'AI1WM_VERSION', '4.10' );
42
43
// ===============
44
// = Plugin Name =
lib/model/class-ai1wm-export-abstract.php CHANGED
@@ -80,18 +80,22 @@ abstract class Ai1wm_Export_Abstract {
80
81
// Default filters
82
$filters = array(
83
'ai1wm-backups',
84
'managewp',
85
- );
86
-
87
- // Exclude index.php
88
- $filters = array_merge( $filters, array(
89
- 'index.php',
90
'themes' . DIRECTORY_SEPARATOR . 'index.php',
91
'plugins' . DIRECTORY_SEPARATOR . 'index.php',
92
'uploads' . DIRECTORY_SEPARATOR . 'index.php',
93
- ) );
94
-
95
96
// Exclude media
97
if ( $this->should_exclude_media() ) {
@@ -407,7 +411,7 @@ abstract class Ai1wm_Export_Abstract {
407
}
408
409
// Resolve domain
410
- $url = admin_url( 'admin-ajax.php?action=ai1wm_export&' . http_build_query( $this->args ) );
411
$hostname = parse_url( $url, PHP_URL_HOST );
412
$port = parse_url( $url, PHP_URL_PORT );
413
$ip = gethostbyname( $hostname );
80
81
// Default filters
82
$filters = array(
83
+ 'index.php',
84
'ai1wm-backups',
85
+ 'envato-backups',
86
'managewp',
87
+ 'updraft',
88
+ 'cache',
89
+ 'backwpup',
90
+ 'pb_backupbuddy',
91
+ 'backupbuddy_backups',
92
'themes' . DIRECTORY_SEPARATOR . 'index.php',
93
'plugins' . DIRECTORY_SEPARATOR . 'index.php',
94
'uploads' . DIRECTORY_SEPARATOR . 'index.php',
95
+ 'uploads' . DIRECTORY_SEPARATOR . 'backwpup',
96
+ 'uploads' . DIRECTORY_SEPARATOR . 'pb_backupbuddy',
97
+ 'uploads' . DIRECTORY_SEPARATOR . 'backupbuddy_backups',
98
+ );
99
100
// Exclude media
101
if ( $this->should_exclude_media() ) {
411
}
412
413
// Resolve domain
414
+ $url = add_query_arg( $this->args, admin_url( 'admin-ajax.php?action=ai1wm_export' ) );
415
$hostname = parse_url( $url, PHP_URL_HOST );
416
$port = parse_url( $url, PHP_URL_PORT );
417
$ip = gethostbyname( $hostname );
lib/model/class-ai1wm-import-abstract.php CHANGED
@@ -396,7 +396,7 @@ abstract class Ai1wm_Import_Abstract {
396
}
397
398
// Resolve domain
399
- $url = admin_url( 'admin-ajax.php?action=ai1wm_import&' . http_build_query( $this->args ) );
400
$hostname = parse_url( $url, PHP_URL_HOST );
401
$port = parse_url( $url, PHP_URL_PORT );
402
$ip = gethostbyname( $hostname );
396
}
397
398
// Resolve domain
399
+ $url = add_query_arg( $this->args, admin_url( 'admin-ajax.php?action=ai1wm_import' ) );
400
$hostname = parse_url( $url, PHP_URL_HOST );
401
$port = parse_url( $url, PHP_URL_PORT );
402
$ip = gethostbyname( $hostname );
lib/model/service/class-ai1wm-service-database.php CHANGED
@@ -1,4 +1,5 @@
1
<?php
2
/**
3
* Copyright (C) 2014 ServMask Inc.
4
*
@@ -23,42 +24,14 @@
23
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
24
*/
25
26
- class Ai1wm_Service_Database implements Ai1wm_Service_Interface
27
- {
28
- protected $args = array();
29
30
- protected $storage = null;
31
32
- protected $connection = null;
33
34
public function __construct( array $args = array() ) {
35
$this->args = $args;
36
-
37
- // Make connection
38
- try {
39
- // Use PDO adapter
40
- $this->connection = MysqlDumpFactory::makeMysqlDump(
41
- DB_HOST,
42
- DB_USER,
43
- DB_PASSWORD,
44
- DB_NAME,
45
- (
46
- class_exists(
47
- 'PDO'
48
- ) && in_array( 'mysql', PDO::getAvailableDrivers() )
49
- )
50
- );
51
- $this->connection->getConnection();
52
- } catch ( Exception $e ) {
53
- // Use mysql adapter
54
- $this->connection = MysqlDumpFactory::makeMysqlDump(
55
- DB_HOST,
56
- DB_USER,
57
- DB_PASSWORD,
58
- DB_NAME,
59
- false
60
- );
61
- }
62
}
63
64
/**
@@ -78,20 +51,78 @@ class Ai1wm_Service_Database implements Ai1wm_Service_Interface
78
79
// Get Site URL
80
if ( isset( $config['SiteURL'] ) && ( $config['SiteURL'] !== site_url() ) ) {
81
- $old_values[] = $config['SiteURL'];
82
- $new_values[] = site_url();
83
}
84
85
// Get Home URL
86
if ( isset( $config['HomeURL'] ) && ( $config['HomeURL'] !== home_url() ) ) {
87
- $old_values[] = $config['HomeURL'];
88
- $new_values[] = home_url();
89
}
90
91
// Get WordPress Content
92
if ( isset( $config['WordPress']['Content'] ) && ( $config['WordPress']['Content'] !== WP_CONTENT_DIR ) ) {
93
$old_values[] = $config['WordPress']['Content'];
94
$new_values[] = WP_CONTENT_DIR;
95
}
96
97
// Get user details
@@ -128,15 +159,25 @@ class Ai1wm_Service_Database implements Ai1wm_Service_Interface
128
// Get secret key
129
$secret_key = get_site_option( AI1WM_SECRET_KEY, false, false );
130
131
// Flush database
132
- $this->connection->flush();
133
134
// Import database
135
- $this->connection->setOldTablePrefix( AI1WM_TABLE_PREFIX )
136
- ->setNewTablePrefix( $wpdb->prefix )
137
- ->setOldReplaceValues( $old_values )
138
- ->setNewReplaceValues( $new_values )
139
- ->import( $this->storage()->database() );
140
141
// Clear WP options cache
142
wp_cache_flush();
@@ -176,6 +217,11 @@ class Ai1wm_Service_Database implements Ai1wm_Service_Interface
176
activate_plugin( AI1WMFE_PLUGIN_BASENAME );
177
}
178
179
// Set new user identity
180
if ( isset( $config['Export']['User']['Id'] ) && ( $id = $config['Export']['User']['Id'] ) ) {
181
@@ -251,18 +297,21 @@ class Ai1wm_Service_Database implements Ai1wm_Service_Interface
251
}
252
}
253
254
- // Set dump options
255
- $this->connection->setFileName( $this->storage()->database() )
256
- ->setOldTablePrefix( $wpdb->prefix )
257
- ->setNewTablePrefix( AI1WM_TABLE_PREFIX )
258
- ->setOldReplaceValues( $old_values )
259
- ->setNewReplaceValues( $new_values )
260
- ->setQueryClauses( $clauses )
261
- ->setTablePrefixColumns( $wpdb->options, array( 'option_name' ) )
262
- ->setTablePrefixColumns( $wpdb->usermeta, array( 'meta_key' ) );
263
264
// Export database
265
- $this->connection->export();
266
}
267
268
/*
1
<?php
2
+
3
/**
4
* Copyright (C) 2014 ServMask Inc.
5
*
24
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
27
+ class Ai1wm_Service_Database implements Ai1wm_Service_Interface {
28
29
+ protected $args = array();
30
31
+ protected $storage = null;
32
33
public function __construct( array $args = array() ) {
34
$this->args = $args;
35
}
36
37
/**
51
52
// Get Site URL
53
if ( isset( $config['SiteURL'] ) && ( $config['SiteURL'] !== site_url() ) ) {
54
+
55
+ // Get domain
56
+ $old_domain = parse_url( $config['SiteURL'], PHP_URL_HOST );
57
+ $new_domain = parse_url( site_url(), PHP_URL_HOST );
58
+
59
+ // Add plain Site URL
60
+ $old_values[] = set_url_scheme( $config['SiteURL'], 'http' );
61
+ $new_values[] = set_url_scheme( site_url() );
62
+
63
+ // Add plain Site URL SSL
64
+ $old_values[] = set_url_scheme( $config['SiteURL'], 'https' );
65
+ $new_values[] = set_url_scheme( site_url() );
66
+
67
+ // Add encoded Site URL
68
+ $old_values[] = urlencode( set_url_scheme( $config['SiteURL'], 'http' ) );
69
+ $new_values[] = urlencode( set_url_scheme( site_url() ) );
70
+
71
+ // Add encoded Site URL SSL
72
+ $old_values[] = urlencode( set_url_scheme( $config['SiteURL'], 'https' ) );
73
+ $new_values[] = urlencode( set_url_scheme( site_url() ) );
74
+
75
+ // Add escaped Site URL
76
+ $old_values[] = addslashes( addcslashes( set_url_scheme( $config['SiteURL'], 'http' ), '/' ) );
77
+ $new_values[] = addslashes( addcslashes( set_url_scheme( site_url() ), '/' ) );
78
+
79
+ // Add escaped Site URL SSL
80
+ $old_values[] = addslashes( addcslashes( set_url_scheme( $config['SiteURL'], 'https' ), '/' ) );
81
+ $new_values[] = addslashes( addcslashes( set_url_scheme( site_url() ), '/' ) );
82
+
83
+ // Add email
84
+ $old_values[] = sprintf( "@%s", $old_domain );
85
+ $new_values[] = sprintf( "@%s", $new_domain );
86
}
87
88
// Get Home URL
89
if ( isset( $config['HomeURL'] ) && ( $config['HomeURL'] !== home_url() ) ) {
90
+
91
+ // Add plain Home URL
92
+ $old_values[] = set_url_scheme( $config['HomeURL'], 'http' );
93
+ $new_values[] = set_url_scheme( home_url() );
94
+
95
+ // Add plain Home URL SSL
96
+ $old_values[] = set_url_scheme( $config['HomeURL'], 'https' );
97
+ $new_values[] = set_url_scheme( home_url() );
98
+
99
+ // Add encoded Home URL
100
+ $old_values[] = urlencode( set_url_scheme( $config['HomeURL'], 'http' ) );
101
+ $new_values[] = urlencode( set_url_scheme( home_url() ) );
102
+
103
+ // Add encoded Home URL SSL
104
+ $old_values[] = urlencode( set_url_scheme( $config['HomeURL'], 'https' ) );
105
+ $new_values[] = urlencode( set_url_scheme( home_url() ) );
106
+
107
+ // Add escaped Home URL
108
+ $old_values[] = addslashes( addcslashes( set_url_scheme( $config['HomeURL'], 'http' ), '/' ) );
109
+ $new_values[] = addslashes( addcslashes( set_url_scheme( home_url() ), '/' ) );
110
+
111
+ // Add escaped Home URL SSL
112
+ $old_values[] = addslashes( addcslashes( set_url_scheme( $config['HomeURL'], 'https' ), '/' ) );
113
+ $new_values[] = addslashes( addcslashes( set_url_scheme( home_url() ), '/' ) );
114
}
115
116
// Get WordPress Content
117
if ( isset( $config['WordPress']['Content'] ) && ( $config['WordPress']['Content'] !== WP_CONTENT_DIR ) ) {
118
+
119
+ // Add plain WordPress Content
120
$old_values[] = $config['WordPress']['Content'];
121
$new_values[] = WP_CONTENT_DIR;
122
+
123
+ // Get escaped WordPress Content
124
+ $old_values[] = addslashes( addcslashes( $config['WordPress']['Content'], '\/' ) );
125
+ $new_values[] = addslashes( addcslashes( WP_CONTENT_DIR, '\/' ) );
126
}
127
128
// Get user details
159
// Get secret key
160
$secret_key = get_site_option( AI1WM_SECRET_KEY, false, false );
161
162
+ // Get database client
163
+ $client = MysqlDumpFactory::makeMysqlDump( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
164
+
165
+ // Set database options
166
+ $client->setOldTablePrefixes( array( AI1WM_TABLE_PREFIX ) )
167
+ ->setNewTablePrefixes( array( $wpdb->prefix ) )
168
+ ->setOldReplaceValues( $old_values )
169
+ ->setNewReplaceValues( $new_values );
170
+
171
// Flush database
172
+ if ( ( $version = $config['Plugin']['Version'] ) ) {
173
+ if ( $version !== 'develop' && version_compare( $version, '4.9', '<' ) ) {
174
+ $client->setIncludeTablePrefixes( array( $wpdb->prefix ) );
175
+ $client->flush();
176
+ }
177
+ }
178
179
// Import database
180
+ $client->import( $this->storage()->database() );
181
182
// Clear WP options cache
183
wp_cache_flush();
217
activate_plugin( AI1WMFE_PLUGIN_BASENAME );
218
}
219
220
+ // URL Extension
221
+ if ( is_plugin_active( AI1WMLE_PLUGIN_BASENAME ) ) {
222
+ activate_plugin( AI1WMLE_PLUGIN_BASENAME );
223
+ }
224
+
225
// Set new user identity
226
if ( isset( $config['Export']['User']['Id'] ) && ( $id = $config['Export']['User']['Id'] ) ) {
227
297
}
298
}
299
300
+ // Get database client
301
+ $client = MysqlDumpFactory::makeMysqlDump( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME );
302
+
303
+ // Set database options
304
+ $client->setOldTablePrefixes( array( $wpdb->prefix ) )
305
+ ->setNewTablePrefixes( array( AI1WM_TABLE_PREFIX ) )
306
+ ->setOldReplaceValues( $old_values )
307
+ ->setNewReplaceValues( $new_values )
308
+ ->setIncludeTablePrefixes( array( $wpdb->prefix ) )
309
+ ->setQueryClauses( $clauses )
310
+ ->setTablePrefixColumns( $wpdb->options, array( 'option_name' ) )
311
+ ->setTablePrefixColumns( $wpdb->usermeta, array( 'meta_key' ) );
312
313
// Export database
314
+ $client->export( $this->storage()->database() );
315
}
316
317
/*
lib/model/service/class-ai1wm-service-interface.php CHANGED
@@ -23,6 +23,7 @@
23
* ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
interface Ai1wm_Service_Interface {
27
28
public function import();
23
* ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
+
27
interface Ai1wm_Service_Interface {
28
29
public function import();
lib/model/service/class-ai1wm-service-package.php CHANGED
@@ -23,6 +23,7 @@
23
* ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
class Ai1wm_Service_Package implements Ai1wm_Service_Interface {
27
28
protected $args = array();
23
* ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
* ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
+
27
class Ai1wm_Service_Package implements Ai1wm_Service_Interface {
28
29
protected $args = array();
lib/vendor/mysqldump-factory/mysqldump-factory/lib/MysqlDumpFactory.php CHANGED
@@ -1,69 +1,50 @@
1
<?php
2
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
- * Factory class main file
6
*
7
- * PHP version 5
8
*
9
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the "Software"), to
11
- * deal in the Software without restriction, including without limitation the rights
12
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13
- * of the Software, and to permit persons to whom the Software is furnished to do so,
14
- * subject to the following conditions:
15
*
16
- * The above copyright notice and this permission notice shall be included in all
17
- * copies or substantial portions of the Software.
18
*
19
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
- *
26
- * @category Databases
27
- * @package MysqlDumpFactory
28
- * @author Yani Iliev <yani@iliev.me>
29
- * @author Bobby Angelov <bobby@servmask.com>
30
- * @copyright 2014 Yani Iliev, Bobby Angelov
31
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
32
- * @version GIT: 2.2.0
33
- * @link https://github.com/yani-/mysqldump-factory/
34
*/
35
36
- /**
37
- * Factory Main class
38
- *
39
- * @category Databases
40
- * @package MysqlDumpFactory
41
- * @author Yani Iliev <yani@iliev.me>
42
- * @author Bobby Angelov <bobby@servmask.com>
43
- * @copyright 2014 Yani Iliev, Bobby Angelov
44
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
45
- * @version GIT: 2.2.0
46
- * @link https://github.com/yani-/mysqldump-factory/
47
- */
48
class MysqlDumpFactory
49
{
50
- public static function makeMysqlDump($hostname = 'localhost', $username = '', $password = '', $database = '', $pdo = false)
51
{
52
- // is PDO class available?
53
- if ($pdo) {
54
- require_once
55
- dirname(__FILE__) .
56
- DIRECTORY_SEPARATOR .
57
- 'MysqlDumpPDO.php';
58
-
59
- return new MysqlDumpPDO($hostname, $username, $password, $database);
60
- } else {
61
- require_once
62
- dirname(__FILE__) .
63
- DIRECTORY_SEPARATOR .
64
- 'MysqlDumpSQL.php';
65
66
- return new MysqlDumpSQL($hostname, $username, $password, $database);
67
}
68
}
69
}
1
<?php
2
3
/**
4
+ * Copyright (C) 2014 ServMask Inc.
5
*
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
*
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
*
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*
19
+ * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗
20
+ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝
21
+ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝
22
+ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗
23
+ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
+ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
27
+ require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlDumpPDO.php';
28
+ require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlDumpSQL.php';
29
+
30
class MysqlDumpFactory
31
{
32
+ public static function makeMysqlDump($hostname = 'localhost', $username = '', $password = '', $database = '')
33
{
34
+ try {
35
+ // Is PDO available?
36
+ if (class_exists('PDO') && in_array('mysql', PDO::getAvailableDrivers())) {
37
+ $client = new MysqlDumpPDO($hostname, $username, $password, $database);
38
+ } else {
39
+ $client = new MysqlDumpSQL($hostname, $username, $password, $database);
40
+ }
41
42
+ // Test connection
43
+ $client->getConnection();
44
+ } catch (Exception $e) {
45
+ $client = new MysqlDumpSQL($hostname, $username, $password, $database);
46
}
47
+
48
+ return $client;
49
}
50
}
lib/vendor/mysqldump-factory/mysqldump-factory/lib/MysqlDumpInterface.php CHANGED
@@ -1,50 +1,29 @@
1
<?php
2
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
- * MysqlDump interface
6
*
7
- * PHP version 5
8
*
9
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the "Software"), to
11
- * deal in the Software without restriction, including without limitation the rights
12
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13
- * of the Software, and to permit persons to whom the Software is furnished to do so,
14
- * subject to the following conditions:
15
*
16
- * The above copyright notice and this permission notice shall be included in all
17
- * copies or substantial portions of the Software.
18
*
19
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
- *
26
- * @category Databases
27
- * @package MysqlDumpFactory
28
- * @author Yani Iliev <yani@iliev.me>
29
- * @author Bobby Angelov <bobby@servmask.com>
30
- * @copyright 2014 Yani Iliev, Bobby Angelov
31
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
32
- * @version GIT: 2.2.0
33
- * @link https://github.com/yani-/mysqldump-factory/
34
*/
35
36
- /**
37
- * MysqlDump interface
38
- *
39
- * @category Databases
40
- * @package MysqlDumpFactory
41
- * @author Yani Iliev <yani@iliev.me>
42
- * @author Bobby Angelov <bobby@servmask.com>
43
- * @copyright 2014 Yani Iliev, Bobby Angelov
44
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
45
- * @version GIT: 2.2.0
46
- * @link https://github.com/yani-/mysqldump-factory/
47
- */
48
interface MysqlDumpInterface
49
{
50
/**
@@ -58,57 +37,35 @@ interface MysqlDumpInterface
58
*/
59
public function __construct($hostname = 'localhost', $username = '', $password = '', $database = '');
60
61
- /**
62
- * Export database into a file
63
- *
64
- * @return void
65
- */
66
- public function export();
67
-
68
- /**
69
- * Set output file name
70
- *
71
- * @param string $fileName Name of the output file
72
- * @return MysqlDumpInterface
73
- */
74
- public function setFileName($fileName);
75
-
76
- /**
77
- * Get output file name
78
- *
79
- * @return string
80
- */
81
- public function getFileName();
82
-
83
/**
84
* Set old table prefix
85
*
86
- * @param string $prefix Name of the table prefix
87
* @return MysqlDumpInterface
88
*/
89
- public function setOldTablePrefix($prefix);
90
91
/**
92
- * Get old table prefix
93
*
94
- * @return string
95
*/
96
- public function getOldTablePrefix();
97
98
/**
99
- * Set new table prefix
100
*
101
- * @param string $prefix Name of the table prefix
102
* @return MysqlDumpInterface
103
*/
104
- public function setNewTablePrefix($prefix);
105
106
/**
107
- * Get new table prefix
108
*
109
- * @return string
110
*/
111
- public function getNewTablePrefix();
112
113
/**
114
* Set old replace values
@@ -173,88 +130,62 @@ interface MysqlDumpInterface
173
public function getTablePrefixColumns($table);
174
175
/**
176
- * Get MySQL version
177
*
178
- * @return string
179
- */
180
- public function getVersion();
181
-
182
- /**
183
- * Get MySQL max allowed packaet
184
- *
185
- * @return integer
186
- */
187
- public function getMaxAllowedPacket();
188
-
189
- /**
190
- * Get MySQL collation name
191
- *
192
- * @param string $collationName Collation name
193
- * @return string
194
- */
195
- public function getCollation($collationName);
196
-
197
- /**
198
- * Flush database
199
- *
200
- * @return void
201
*/
202
- public function flush();
203
204
/**
205
- * Import database from file
206
*
207
- * @param string $fileName Name of file
208
- * @return bool
209
*/
210
- public function import($fileName);
211
212
/**
213
- * Get list of tables
214
*
215
* @return array
216
*/
217
- public function listTables();
218
219
/**
220
- * Replace table prefix
221
*
222
- * @param string $input Table value
223
- * @param boolean $first Replace first occurrence
224
- * @param boolean $start Replace start occurrence
225
- * @return string
226
*/
227
- public function replaceTablePrefix($input, $first = false, $start = false);
228
229
/**
230
- * Replace table values
231
*
232
- * @param string $input Table value
233
- * @param boolean $parse Parse value
234
- * @return string
235
*/
236
- public function replaceTableValues($input, $parse = false);
237
238
/**
239
- * Replace table collation
240
*
241
- * @param string $input SQL statement
242
- * @return string
243
*/
244
- public function replaceTableCollation($input);
245
246
/**
247
- * Strip table constraints
248
*
249
- * @param string $input SQL statement
250
- * @return string
251
*/
252
- public function stripTableConstraints($input);
253
254
/**
255
- * Get MySQL connection (lazy loading)
256
*
257
- * @return resource
258
*/
259
- public function getConnection();
260
}
1
<?php
2
3
/**
4
+ * Copyright (C) 2014 ServMask Inc.
5
*
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
*
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
*
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*
19
+ * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗
20
+ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝
21
+ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝
22
+ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗
23
+ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
+ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
27
interface MysqlDumpInterface
28
{
29
/**
37
*/
38
public function __construct($hostname = 'localhost', $username = '', $password = '', $database = '');
39
40
/**
41
* Set old table prefix
42
*
43
+ * @param array $prefixes List of table prefixes
44
* @return MysqlDumpInterface
45
*/
46
+ public function setOldTablePrefixes($prefixes);
47
48
/**
49
+ * Get old table prefixes
50
*
51
+ * @return array
52
*/
53
+ public function getOldTablePrefixes();
54
55
/**
56
+ * Set new table prefixes
57
*
58
+ * @param array $prefixes List of table prefixes
59
* @return MysqlDumpInterface
60
*/
61
+ public function setNewTablePrefixes($prefixes);
62
63
/**
64
+ * Get new table prefixes
65
*
66
+ * @return array
67
*/
68
+ public function getNewTablePrefixes();
69
70
/**
71
* Set old replace values
130
public function getTablePrefixColumns($table);
131
132
/**
133
+ * Set exclude table prefixes
134
*
135
+ * @param array $prefixes List of table prefixes
136
+ * @return MysqlDumpInterface
137
*/
138
+ public function setExcludeTablePrefixes($prefixes);
139
140
/**
141
+ * Get exclude table prefixes
142
*
143
+ * @return array
144
*/
145
+ public function getExcludeTablePrefixes();
146
147
/**
148
+ * Get tables
149
*
150
* @return array
151
*/
152
+ public function getTables();
153
154
/**
155
+ * Get MySQL connection (lazy loading)
156
*
157
+ * @return resource
158
*/
159
+ public function getConnection();
160
161
/**
162
+ * Run MySQL query
163
*
164
+ * @param string $query SQL query
165
+ * @return resource
166
*/
167
+ public function query($query);
168
169
/**
170
+ * Export database into a file
171
*
172
+ * @param string $fileName Name of file
173
+ * @return bool
174
*/
175
+ public function export($fileName);
176
177
/**
178
+ * Import database from file
179
*
180
+ * @param string $fileName Name of file
181
+ * @return bool
182
*/
183
+ public function import($fileName);
184
185
/**
186
+ * Flush database
187
*
188
+ * @return void
189
*/
190
+ public function flush();
191
}
lib/vendor/mysqldump-factory/mysqldump-factory/lib/MysqlDumpPDO.php CHANGED
@@ -1,85 +1,63 @@
1
<?php
2
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
- * MysqlDumpPDO class
6
*
7
- * PHP version 5
8
*
9
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the "Software"), to
11
- * deal in the Software without restriction, including without limitation the rights
12
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13
- * of the Software, and to permit persons to whom the Software is furnished to do so,
14
- * subject to the following conditions:
15
*
16
- * The above copyright notice and this permission notice shall be included in all
17
- * copies or substantial portions of the Software.
18
*
19
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
- *
26
- * @category Databases
27
- * @package MysqlDumpFactory
28
- * @author Yani Iliev <yani@iliev.me>
29
- * @author Bobby Angelov <bobby@servmask.com>
30
- * @copyright 2014 Yani Iliev, Bobby Angelov
31
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
32
- * @version GIT: 2.2.0
33
- * @link https://github.com/yani-/mysqldump-factory/
34
*/
35
36
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlDumpInterface.php';
37
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlFileAdapter.php';
38
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlUtility.php';
39
40
- /**
41
- * MysqlDumpPDO class
42
- *
43
- * @category Databases
44
- * @package MysqlDumpFactory
45
- * @author Yani Iliev <yani@iliev.me>
46
- * @author Bobby Angelov <bobby@servmask.com>
47
- * @copyright 2014 Yani Iliev, Bobby Angelov
48
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
49
- * @version GIT: 2.2.0
50
- * @link https://github.com/yani-/mysqldump-factory/
51
- */
52
class MysqlDumpPDO implements MysqlDumpInterface
53
{
54
- protected $hostname = null;
55
56
- protected $port = null;
57
58
- protected $socket = null;
59
60
- protected $username = null;
61
62
- protected $password = null;
63
64
- protected $database = null;
65
66
- protected $fileName = 'database.sql';
67
68
- protected $fileAdapter = null;
69
70
- protected $connection = null;
71
72
- protected $oldTablePrefix = null;
73
74
- protected $newTablePrefix = null;
75
76
- protected $oldReplaceValues = array();
77
78
- protected $newReplaceValues = array();
79
80
- protected $queryClauses = array();
81
82
- protected $tablePrefixColumns = array();
83
84
/**
85
* Define MySQL credentials for the current connection
@@ -104,103 +82,49 @@ class MysqlDumpPDO implements MysqlDumpInterface
104
}
105
106
/**
107
- * Export database into a file
108
- *
109
- * @return void
110
- */
111
- public function export()
112
- {
113
- // Set File Adapter
114
- $this->fileAdapter = new MysqlFileAdapter();
115
-
116
- // Set output file
117
- $this->fileAdapter->open($this->getFileName());
118
-
119
- // Write Headers Formatting dump file
120
- $this->fileAdapter->write($this->getHeader());
121
-
122
- // Listing all tables from database
123
- $tables = $this->listTables();
124
-
125
- // Export Tables
126
- foreach ($tables as $table) {
127
- $isTable = $this->getTableStructure($table);
128
- if ($isTable) {
129
- $this->listValues($table);
130
- }
131
- }
132
-
133
- // Close File Adapter
134
- $this->fileAdapter->close();
135
- }
136
-
137
- /**
138
- * Set output file name
139
- *
140
- * @param string $fileName Name of the output file
141
- * @return MysqlDumpPDO
142
- */
143
- public function setFileName($fileName)
144
- {
145
- $this->fileName = $fileName;
146
-
147
- return $this;
148
- }
149
-
150
- /**
151
- * Get output file name
152
- *
153
- * @return string
154
- */
155
- public function getFileName()
156
- {
157
- return $this->fileName;
158
- }
159
-
160
- /**
161
- * Set old table prefix
162
*
163
- * @param string $prefix Name of the table prefix
164
* @return MysqlDumpPDO
165
*/
166
- public function setOldTablePrefix($prefix)
167
{
168
- $this->oldTablePrefix = $prefix;
169
170
return $this;
171
}
172
173
/**
174
- * Get old table prefix
175
*
176
- * @return string
177
*/
178
- public function getOldTablePrefix()
179
{
180
- return $this->oldTablePrefix;
181
}
182
183
/**
184
- * Set new table prefix
185
*
186
- * @param string $prefix Name of the table prefix
187
* @return MysqlDumpPDO
188
*/
189
- public function setNewTablePrefix($prefix)
190
{
191
- $this->newTablePrefix = $prefix;
192
193
return $this;
194
}
195
196
/**
197
- * Get new table prefix
198
*
199
- * @return string
200
*/
201
- public function getNewTablePrefix()
202
{
203
- return $this->newTablePrefix;
204
}
205
206
/**
@@ -304,97 +228,312 @@ class MysqlDumpPDO implements MysqlDumpInterface
304
}
305
306
/**
307
- * Get MySQL version
308
*
309
- * @return string
310
*/
311
- public function getVersion() {
312
- try {
313
- $result = $this->getConnection()->query("SELECT @@version AS VersionName");
314
- while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
315
- if (isset($row['VersionName'])) {
316
- return $row['VersionName'];
317
- }
318
- }
319
- } catch (Exception $e) {
320
- $result = $this->getConnection()->query("SHOW VARIABLES LIKE 'version'");
321
- while ($row = $result->fetch(PDO::FETCH_NUM)) {
322
- if (isset($row[1])) {
323
- return $row[1];
324
- }
325
- }
326
- }
327
}
328
329
/**
330
- * Get MySQL max allowed packaet
331
*
332
- * @return integer
333
*/
334
- public function getMaxAllowedPacket() {
335
try {
336
- $result = $this->getConnection()->query("SELECT @@max_allowed_packet AS MaxAllowedPacket");
337
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
338
- if (isset($row['MaxAllowedPacket'])) {
339
- return $row['MaxAllowedPacket'];
340
}
341
}
342
} catch (Exception $e) {
343
- $result = $this->getConnection()->query("SHOW VARIABLES LIKE 'max_allowed_packet'");
344
while ($row = $result->fetch(PDO::FETCH_NUM)) {
345
- if (isset($row[1])) {
346
- return $row[1];
347
}
348
}
349
}
350
}
351
352
/**
353
- * Get MySQL collation name
354
*
355
- * @param string $collationName Collation name
356
- * @return string
357
*/
358
- public function getCollation($collationName) {
359
- try {
360
- $result = $this->getConnection()->query(
361
- "SELECT COLLATION_NAME AS CollationName FROM `INFORMATION_SCHEMA`.`COLLATIONS` WHERE COLLATION_NAME = '$collationName'"
362
- );
363
- while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
364
- if (isset($row['CollationName'])) {
365
- return $row['CollationName'];
366
- }
367
- }
368
- } catch (Exception $e) {
369
- $result = $this->getConnection()->query("SHOW COLLATION LIKE '$collationName'");
370
- while ($row = $result->fetch(PDO::FETCH_NUM)) {
371
- if (isset($row[0])) {
372
- return $row[0];
373
}
374
}
375
}
376
}
377
378
/**
379
- * Flush database
380
*
381
- * @return void
382
*/
383
- public function flush()
384
{
385
- $dropTables = array();
386
- foreach ($this->listTables() as $tableName) {
387
- $dropTables[] = "DROP TABLE IF EXISTS `$tableName`";
388
}
389
390
- // Drop tables
391
- foreach ($dropTables as $dropQuery) {
392
- $this->getConnection()->query($dropQuery);
393
}
394
}
395
396
/**
397
- * Import database from file
398
*
399
* @param string $fileName Name of file
400
* @return bool
@@ -427,8 +566,8 @@ class MysqlDumpPDO implements MysqlDumpInterface
427
// Check max allowed packet
428
if (strlen($query) <= $maxAllowedPacket) {
429
430
- // Replace table prefix
431
- $query = $this->replaceTablePrefix($query);
432
433
// Replace table values
434
$query = $this->replaceTableValues($query, true);
@@ -446,17 +585,9 @@ class MysqlDumpPDO implements MysqlDumpInterface
446
} else {
447
$passed++;
448
}
449
} catch (PDOException $e) {
450
$failed++;
451
-
452
- // Log the error
453
- Ai1wm_Log::error(
454
- sprintf(
455
- 'Exception while importing: %s with query: %s',
456
- $e->getMessage(),
457
- $query
458
- )
459
- );
460
}
461
462
} else {
@@ -479,61 +610,130 @@ class MysqlDumpPDO implements MysqlDumpInterface
479
}
480
481
/**
482
- * Get list of tables
483
*
484
- * @return array
485
*/
486
- public function listTables()
487
{
488
- $tables = array();
489
490
try {
491
$result = $this->getConnection()->query(
492
- "SELECT TABLE_NAME AS TableName FROM `INFORMATION_SCHEMA`.`TABLES` WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '{$this->database}'"
493
);
494
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
495
- if (isset($row['TableName'])) {
496
- $tables[] = $row['TableName'];
497
}
498
}
499
} catch (Exception $e) {
500
- $result = $this->getConnection()->query("SHOW TABLES FROM `{$this->database}`");
501
while ($row = $result->fetch(PDO::FETCH_NUM)) {
502
if (isset($row[0])) {
503
- $tables[] = $row[0];
504
}
505
}
506
}
507
-
508
- return $tables;
509
}
510
511
/**
512
- * Replace table prefix
513
*
514
- * @param string $input Table value
515
- * @param boolean $first Replace first occurrence
516
- * @param boolean $start Replace start occurrence
517
* @return string
518
*/
519
- public function replaceTablePrefix($input, $first = false, $start = false)
520
{
521
- // Get table prefix
522
- $search = $this->getOldTablePrefix();
523
- $replace = $this->getNewTablePrefix();
524
525
- // Replace first occurrence
526
if ($first) {
527
- $pos = strpos($input, $search);
528
- if ($pos !== false) {
529
- return substr_replace($input, $replace, $pos, strlen($search));
530
}
531
532
- return $input;
533
- } else if ($start) {
534
- $pos = strpos($input, $search);
535
- if ($pos === 0) {
536
- return substr_replace($input, $replace, $pos, strlen($search));
537
}
538
539
return $input;
@@ -550,7 +750,7 @@ class MysqlDumpPDO implements MysqlDumpInterface
550
* @param boolean $parse Parse value
551
* @return string
552
*/
553
- public function replaceTableValues($input, $parse = false)
554
{
555
// Get replace values
556
$old = $this->getOldReplaceValues();
@@ -574,10 +774,10 @@ class MysqlDumpPDO implements MysqlDumpInterface
574
$input = $this->parseSerializedValues($input);
575
576
// Replace values
577
- return MysqlUtility::replaceValues($oldValues, $newValues, $input);
578
}
579
580
- return MysqlUtility::replaceSerializedValues($oldValues, $newValues, $input);
581
}
582
583
return $input;
@@ -589,7 +789,7 @@ class MysqlDumpPDO implements MysqlDumpInterface
589
* @param string $input Table value
590
* @return string
591
*/
592
- public function parseSerializedValues($input)
593
{
594
// Serialization format
595
$array = '(a:\d+:{.*?})';
@@ -606,13 +806,13 @@ class MysqlDumpPDO implements MysqlDumpInterface
606
* @param array $matches List of matches
607
* @return string
608
*/
609
- public function replaceSerializedValues($matches)
610
{
611
// Unescape MySQL special characters
612
- $input = MysqlUtility::unescapeMysql($matches[1]);
613
614
// Replace serialized values
615
- $input = MysqlUtility::replaceSerializedValues($this->getOldReplaceValues(), $this->getNewReplaceValues(), $input);
616
617
// Prepare query values
618
return $this->getConnection()->quote($input);
@@ -624,7 +824,7 @@ class MysqlDumpPDO implements MysqlDumpInterface
624
* @param string $input SQL statement
625
* @return string
626
*/
627
- public function replaceTableCollation($input)
628
{
629
return str_replace('utf8mb4', 'utf8', $input);
630
}
@@ -635,7 +835,7 @@ class MysqlDumpPDO implements MysqlDumpInterface
635
* @param string $input SQL statement
636
* @return string
637
*/
638
- public function stripTableConstraints($input)
639
{
640
$pattern = array(
641
'/\s+CONSTRAINT(.+)REFERENCES(.+),/i',
@@ -646,30 +846,6 @@ class MysqlDumpPDO implements MysqlDumpInterface
646
return preg_replace($pattern, $replace, $input);
647
}
648
649
- /**
650
- * Get MySQL connection (lazy loading)
651
- *
652
- * @return PDO
653
- */
654
- public function getConnection()
655
- {
656
- if ($this->connection === null) {
657
- try {
658
- // Make connection (Socket)
659
- $this->connection = $this->makeConnection();
660
- } catch (Exception $e) {
661
- try {
662
- // Make connection (TCP)
663
- $this->connection = $this->makeConnection(false);
664
- } catch (Exception $e) {
665
- throw new Exception('Unable to connect to MySQL database server: ' . $e->getMessage());
666
- }
667
- }
668
- }
669
-
670
- return $this->connection;
671
- }
672
-
673
/**
674
* Make MySQL connection
675
*
@@ -726,7 +902,8 @@ class MysqlDumpPDO implements MysqlDumpInterface
726
"-- http://servmask.com/\n" .
727
"--\n" .
728
"-- Host: {$this->hostname}\n" .
729
- "-- Generation Time: " . date('r') . "\n\n" .
730
"--\n" .
731
"-- Database: `{$this->database}`\n" .
732
"--\n\n";
@@ -734,90 +911,6 @@ class MysqlDumpPDO implements MysqlDumpInterface
734
return $header;
735
}
736
737
- /**
738
- * Table structure extractor
739
- *
740
- * @param string $tableName Name of table to export
741
- * @return bool
742
- */
743
- protected function getTableStructure($tableName)
744
- {
745
- $result = $this->getConnection()->query("SHOW CREATE TABLE `$tableName`");
746
- while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
747
- if (isset($row['Create Table'])) {
748
-
749
- // Replace table prefix
750
- $createTable = $this->replaceTablePrefix($row['Create Table'], true);
751
-
752
- // Strip table constraints
753
- $createTable = $this->stripTableConstraints($createTable);
754
-
755
- // Write table structure
756
- $this->fileAdapter->write($createTable);
757
-
758
- // Write end of statement
759
- $this->fileAdapter->write(";\n\n");
760
-
761
- return true;
762
- }
763
- }
764
-
765
- return false;
766
- }
767
-
768
- /**
769
- * Table rows extractor
770
- *
771
- * @param string $tableName Name of table to export
772
- * @return void
773
- */
774
- protected function listValues($tableName)
775
- {
776
- // Set query
777
- $query = "SELECT * FROM `$tableName` ";
778
-
779
- // Apply additional query clauses
780
- $clauses = $this->getQueryClauses();
781
- if (isset($clauses[$tableName]) && ($queryClause = $clauses[$tableName])) {
782
- $query .= $queryClause;
783
- }
784
-
785
- // Apply additional table prefix columns
786
- $columns = $this->getTablePrefixColumns($tableName);
787
-
788
- // Get results
789
- $result = $this->getConnection()->query($query);
790
-
791
- // Replace table name prefix
792
- $tableName = $this->replaceTablePrefix($tableName, true);
793
-
794
- // Generate insert statements
795
- while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
796
- $items = array();
797
- foreach ($row as $key => $value) {
798
- // Replace table prefix columns
799
- if (isset($columns[$key])) {
800
- $value = $this->replaceTablePrefix($value, false, true);
801
- }
802
-
803
- // Replace table values
804
- $items[] = is_null($value) ? 'NULL' : $this->getConnection()->quote($this->replaceTableValues($value));
805
- }
806
-
807
- // Set table values
808
- $tableValues = implode(',', $items);
809
-
810
- // Write insert statements
811
- $this->fileAdapter->write("INSERT INTO `$tableName` VALUES ($tableValues);\n");
812
- }
813
-
814
- // Write end of statements
815
- $this->fileAdapter->write("\n");
816
-
817
- // Close result cursor
818
- $result->closeCursor();
819
- }
820
-
821
/**
822
* Parse data source name
823
*
1
<?php
2
3
/**
4
+ * Copyright (C) 2014 ServMask Inc.
5
*
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
*
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
*
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
*
19
+ * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗
20
+ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝
21
+ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝
22
+ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗
23
+ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗
24
+ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
25
*/
26
27
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlDumpInterface.php';
28
+ require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlDumpUtility.php';
29
30
class MysqlDumpPDO implements MysqlDumpInterface
31
{
32
+ protected $hostname = null;
33
34
+ protected $port = null;
35
36
+ protected $socket = null;
37
38
+ protected $username = null;
39
40
+ protected $password = null;
41
42
+ protected $database = null;
43
44
+ protected $connection = null;
45
46
+ protected $oldTablePrefixes = null;
47
48
+ protected $newTablePrefixes = null;
49
50
+ protected $oldReplaceValues = array();
51
52
+ protected $newReplaceValues = array();
53
54
+ protected $queryClauses = array();
55
56
+ protected $tablePrefixColumns = array();
57
58
+ protected $includeTablePrefixes = array();
59
60
+ protected $excludeTablePrefixes = array();
61
62
/**
63
* Define MySQL credentials for the current connection
82
}
83
84
/**
85
+ * Set old table prefixes
86
*
87
+ * @param array $prefixes List of table prefixes
88
* @return MysqlDumpPDO
89
*/
90
+ public function setOldTablePrefixes($prefixes)
91
{
92
+ $this->oldTablePrefixes = $prefixes;
93
94
return $this;
95
}
96
97
/**
98
+ * Get old table prefixes
99
*
100
+ * @return array
101
*/
102
+ public function getOldTablePrefixes()
103
{
104
+ return $this->oldTablePrefixes;
105
}
106
107
/**
108
+ * Set new table prefixes
109
*
110
+ * @param array $prefix List of table prefixes
111
* @return MysqlDumpPDO
112
*/
113
+ public function setNewTablePrefixes($prefixes)
114
{
115
+ $this->newTablePrefixes = $prefixes;
116
117
return $this;
118
}
119
120
/**
121
+ * Get new table prefixes
122
*
123
+ * @return array
124
*/
125
+ public function getNewTablePrefixes()
126
{
127
+ return $this->newTablePrefixes;
128
}
129
130
/**
228
}
229
230
/**
231
+ * Set include table prefixes
232
*
233
+ * @param array $prefixes List of table prefixes
234
+ * @return MysqlDumpPDO
235
*/
236
+ public function setIncludeTablePrefixes($prefixes)
237
+ {
238
+ $this->includeTablePrefixes = $prefixes;
239
+
240
+ return $this;
241
}
242
243
/**
244
+ * Get include table prefixes
245
*
246
+ * @return array
247
+ */
248
+ public function getIncludeTablePrefixes()
249
+ {
250
+ return $this->includeTablePrefixes;
251
+ }
252
+
253
+ /**
254
+ * Set exclude table prefixes
255
+ *
256
+ * @param array $prefixes List of table prefixes
257
+ * @return MysqlDumpPDO
258
+ */
259
+ public function setExcludeTablePrefixes($prefixes)
260
+ {
261
+ $this->excludeTablePrefixes = $prefixes;
262
+
263
+ return $this;
264
+ }
265
+
266
+ /**
267
+ * Get exclude table prefixes
268
+ *
269
+ * @return array
270
*/
271
+ public function getExcludeTablePrefixes()
272
+ {
273
+ return $this->excludeTablePrefixes;
274
+ }
275
+
276
+ /**
277
+ * Get tables
278
+ *
279
+ * @return array
280
+ */
281
+ public function getTables()
282
+ {
283
+ $tables = array();
284
+
285
try {
286
+ $result = $this->getConnection()->query(
287
+ "SELECT TABLE_NAME AS TableName FROM `INFORMATION_SCHEMA`.`TABLES` WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '{$this->database}'"
288
+ );
289
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
290
+ if (isset($row['TableName']) && ($tableName = $row['TableName'])) {
291
+
292
+ // Include table prefixes
293
+ if ($this->getIncludeTablePrefixes()) {
294
+ $include = false;
295
+
296
+ // Check table prefixes
297
+ foreach ($this->getIncludeTablePrefixes() as $prefix) {
298
+ if (strpos($tableName, $prefix) === 0) {
299
+ $include = true;
300
+ break;
301
+ }
302
+ }
303
+
304
+ // Skip current table
305
+ if ($include === false) {
306
+ continue;
307
+ }
308
+ }
309
+
310
+ // Exclude table prefixes
311
+ if ($this->getExcludeTablePrefixes()) {
312
+ $exclude = false;
313
+
314
+ // Check table prefixes
315
+ foreach ($this->getExcludeTablePrefixes() as $prefix) {
316
+ if (strpos($tableName, $prefix) === 0) {
317
+ $exclude = true;
318
+ break;
319
+ }
320
+ }
321
+
322
+ // Skip current table
323
+ if ($exclude === true) {
324
+ continue;
325
+ }
326
+ }
327
+
328
+ // Add table name
329
+ $tables[] = $tableName;
330
}
331
}
332
} catch (Exception $e) {
333
+ $result = $this->getConnection()->query("SHOW TABLES FROM `{$this->database}`");
334
while ($row = $result->fetch(PDO::FETCH_NUM)) {
335
+ if (isset($row[0]) && ($tableName = $row[0])) {
336
+
337
+ // Include table prefixes
338
+ if ($this->getIncludeTablePrefixes()) {
339
+ $include = false;
340
+
341
+ // Check table prefixes
342
+ foreach ($this->getIncludeTablePrefixes() as $prefix) {
343
+ if (strpos($tableName, $prefix) === 0) {
344
+ $include = true;
345
+ break;
346
+ }
347
+ }
348
+
349
+ // Skip current table
350
+ if ($include === false) {
351
+ continue;
352
+ }
353
+ }
354
+
355
+ // Exclude table prefixes
356
+ if ($this->getExcludeTablePrefixes()) {
357
+ $exclude = false;
358
+
359
+ // Check table prefixes
360
+ foreach ($this->getExcludeTablePrefixes() as $prefix) {
361
+ if (strpos($tableName, $prefix) === 0) {
362
+ $exclude = true;
363
+ break;
364
+ }
365
+ }
366
+
367
+ // Skip current table
368
+ if ($exclude === true) {
369
+ continue;
370
+ }
371
+ }
372
+
373
+ // Add table name
374
+ $tables[] = $tableName;
375
}
376
}
377
}
378
+
379
+ return $tables;
380
}
381
382
/**
383
+ * Get MySQL connection (lazy loading)
384
*
385
+ * @return PDO
386
*/
387
+ public function getConnection()
388
+ {
389
+ if ($this->connection === null) {
390
+ try {
391
+ // Make connection (Socket)
392
+ $this->connection = $this->makeConnection();
393
+ } catch (Exception $e) {
394
+ try {
395
+ // Make connection (TCP)
396
+ $this->connection = $this->makeConnection(false);
397
+ } catch (Exception $e) {
398
+ throw new Exception('Unable to connect to MySQL database server: ' . $e->getMessage());
399
}
400
}
401
}
402
+
403
+ return $this->connection;
404
}
405
406
/**
407
+ * Run MySQL query
408
*
409
+ * @param string $query SQL query
410
+ * @return resource
411
*/
412
+ public function query($query)
413
{
414
+ return $this->getConnection()->query($query);
415
+ }
416
+
417
+ /**
418
+ * Export database into a file
419
+ *
420
+ * @param string $fileName Name of file
421
+ * @return bool
422
+ */
423
+ public function export($fileName)
424
+ {
425
+ // Set file handler
426
+ $fileHandler = fopen($fileName, 'wb');
427
+ if ($fileHandler === false) {
428
+ throw new Exception('Unable to open database file');
429
}
430
431
+ // Write headers
432
+ if (fwrite($fileHandler, $this->getHeader()) === false) {
433
+ throw new Exception('Unable to write database header information');
434
}
435
+
436
+ // Export tables
437
+ foreach ($this->getTables() as $tableName) {
438
+
439
+ // Replace table name prefixes
440
+ $newTableName = $this->replaceTablePrefixes($tableName, true);
441
+
442
+ // Get table structure
443
+ $structure = $this->getConnection()->query("SHOW CREATE TABLE `$tableName`");
444
+ $table = $structure->fetch(PDO::FETCH_ASSOC);
445
+ if (isset($table['Create Table'])) {
446
+
447
+ // Write table drop statement
448
+ $dropTable = "DROP TABLE IF EXISTS `$newTableName`;\n";
449
+
450
+ // Write table statement
451
+ if (fwrite($fileHandler, $dropTable) === false) {
452
+ throw new Exception('Unable to write database table statement');
453
+ }
454
+
455
+ // Replace create table prefixes
456
+ $createTable = $this->replaceTablePrefixes($table['Create Table'], true);
457
+
458
+ // Strip table constraints
459
+ $createTable = $this->stripTableConstraints($createTable);
460
+
461
+ // Write table structure
462
+ if (fwrite($fileHandler, $createTable) === false) {
463
+ throw new Exception('Unable to write database table structure');
464
+ }
465
+
466
+ // Write end of statement
467
+ if (fwrite($fileHandler, ";\n\n") === false) {
468
+ throw new Exception('Unable to write database end of statement');
469
+ }
470
+
471
+ // Close structure cursor
472
+ $structure->closeCursor();
473
+ } else {
474
+ // Close structure cursor
475
+ $structure->closeCursor();
476
+
477
+ break;
478
+ }
479
+
480
+ // Set query
481
+ $query = "SELECT * FROM `$tableName` ";
482
+
483
+ // Apply additional query clauses
484
+ $clauses = $this->getQueryClauses();
485
+ if (isset($clauses[$tableName]) && ($queryClause = $clauses[$tableName])) {
486
+ $query .= $queryClause;
487
+ }
488
+
489
+ // Apply additional table prefix columns
490
+ $columns = $this->getTablePrefixColumns($tableName);
491
+
492
+ // Get results
493
+ $result = $this->getConnection()->query($query);
494
+
495
+ // Generate insert statements
496
+ while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
497
+ $items = array();
498
+ foreach ($row as $key => $value) {
499
+ // Replace table prefix columns
500
+ if (isset($columns[$key])) {
501
+ $value = $this->replaceTablePrefixes($value, true, 0);
502
+ }
503
+
504
+ // Replace table values
505
+ $items[] = is_null($value) ? 'NULL' : $this->getConnection()->quote($this->replaceTableValues($value));
506
+ }
507
+
508
+ // Set table values
509
+ $tableValues = implode(',', $items);
510
+
511
+ // Set insert statement
512
+ $tableInsert = "INSERT INTO `$newTableName` VALUES ($tableValues);\n";
513
+
514
+ // Write insert statement
515
+ if (fwrite($fileHandler, $tableInsert) === false) {
516
+ throw new Exception('Unable to write database insert statement');
517
+ }
518
+ }
519
+
520
+ // Write end of statements
521
+ if (fwrite($fileHandler, "\n") === false) {
522
+ throw new Exception('Unable to write database end of statement');
523
+ }
524
+
525
+ // Close result cursor
526
+ $result->closeCursor();
527
+ }
528
+
529
+ // Close file handler
530
+ fclose($fileHandler);
531
+
532
+ return true;
533
}
534
535
/**
536
+ * Import database from a file
537
*
538
* @param string $fileName Name of file
539
* @return bool
566
// Check max allowed packet
567
if (strlen($query) <= $maxAllowedPacket) {
568
569
+ // Replace table prefixes
570
+ $query = $this->replaceTablePrefixes($query);
571
572
// Replace table values
573
$query = $this->replaceTableValues($query, true);
585
} else {
586
$passed++;
587
}
588
+
589
} catch (PDOException $e) {
590
$failed++;
591
}
592
593
} else {
610
}
611
612
/**
613
+ * Flush database
614
*
615
+ * @return void
616
*/
617
+ public function flush()
618
{
619
+ $dropTables = array();
620
+ foreach ($this->getTables() as $tableName) {
621
+ $dropTables[] = "DROP TABLE IF EXISTS `$tableName`";
622
+ }
623
+
624
+ // Drop tables
625
+ foreach ($dropTables as $dropQuery) {
626
+ $this->getConnection()->query($dropQuery);
627
+ }
628
+ }
629
+
630
+ /**
631
+ * Get MySQL version
632
+ *
633
+ * @return string
634
+ */
635
+ protected function getVersion() {
636
+ try {
637
+ $result = $this->getConnection()->query("SELECT @@version AS VersionName");
638
+ while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
639
+ if (isset($row['VersionName'])) {
640
+ return $row['VersionName'];
641
+ }
642
+ }
643
+ } catch (Exception $e) {
644
+ $result = $this->getConnection()->query("SHOW VARIABLES LIKE 'version'");
645
+ while ($row = $result->fetch(PDO::FETCH_NUM)) {
646
+ if (isset($row[1])) {
647
+ return $row[1];
648
+ }
649
+ }
650
+ }
651
+ }
652
653
+ /**
654
+ * Get MySQL max allowed packaet
655
+ *
656
+ * @return integer
657
+ */
658
+ protected function getMaxAllowedPacket() {
659
+ try {
660
+ $result = $this->getConnection()->query("SELECT @@max_allowed_packet AS MaxAllowedPacket");
661
+ while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
662
+ if (isset($row['MaxAllowedPacket'])) {
663
+ return $row['MaxAllowedPacket'];
664
+ }
665
+ }
666
+ } catch (Exception $e) {
667
+ $result = $this->getConnection()->query("SHOW VARIABLES LIKE 'max_allowed_packet'");
668
+ while ($row = $result->fetch(PDO::FETCH_NUM)) {
669
+ if (isset($row[1])) {
670
+ return $row[1];
671
+ }
672
+ }
673
+ }
674
+ }
675
+
676
+ /**
677
+ * Get MySQL collation name
678
+ *
679
+ * @param string $collationName Collation name
680
+ * @return string
681
+ */
682
+ protected function getCollation($collationName) {
683
try {
684
$result = $this->getConnection()->query(
685
+ "SELECT COLLATION_NAME AS CollationName FROM `INFORMATION_SCHEMA`.`COLLATIONS` WHERE COLLATION_NAME = '$collationName'"
686
);
687
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
688
+ if (isset($row['CollationName'])) {
689
+ return $row['CollationName'];
690
}
691
}
692
} catch (Exception $e) {
693
+ $result = $this->getConnection()->query("SHOW COLLATION LIKE '$collationName'");
694
while ($row = $result->fetch(PDO::FETCH_NUM)) {
695
if (isset($row[0])) {
696
+ return $row[0];
697
}
698
}
699
}
700
}
701
702
/**
703
+ * Replace table prefixes
704
*
705
+ * @param string $input Table value
706
+ * @param boolean $first Replace first occurrence at any position
707
+ * @param boolean $position Replace first occurrence at a specified position
708
* @return string
709
*/
710
+ protected function replaceTablePrefixes($input, $first = false, $position = false)
711
{
712
+ // Get table prefixes
713
+ $search = $this->getOldTablePrefixes();
714
+ $replace = $this->getNewTablePrefixes();
715
716
+ // Replace table prefixes
717
if ($first) {
718
+
719
+ // Replace first occurance at a specified position
720
+ if ($position !== false) {
721
+ for ($i = 0; $i < count($search); $i++) {
722
+ $current = strpos($input, $search[$i]);
723
+ if ($current === $position) {
724
+ $input = substr_replace($input, $replace[$i], $current, strlen($search[$i]));
725
+ }
726
+ }
727
+
728
+ return $input;
729
}
730
731
+ // Replace first occurance at any position
732
+ for ($i = 0; $i < count($search); $i++) {
733
+ $current = strpos($input, $search[$i]);
734
+ if ($current !== $position) {
735
+ $input = substr_replace($input, $replace[$i], $current, strlen($search[$i]));
736
+ }
737
}
738
739
return $input;
750
* @param boolean $parse Parse value
751
* @return string
752
*/
753
+ protected function replaceTableValues($input, $parse = false)
754
{
755
// Get replace values
756
$old = $this->getOldReplaceValues();
774
$input = $this->parseSerializedValues($input);
775
776
// Replace values
777
+ return MysqlDumpUtility::replaceValues($oldValues, $newValues, $input);
778
}
779
780
+ return MysqlDumpUtility::replaceSerializedValues($oldValues, $newValues, $input);
781
}
782
783
return $input;
789
* @param string $input Table value
790
* @return string
791
*/
792
+ protected function parseSerializedValues($input)
793
{
794
// Serialization format
795
$array = '(a:\d+:{.*?})';
806
* @param array $matches List of matches
807
* @return string
808
*/
809
+ protected function replaceSerializedValues($matches)
810
{
811
// Unescape MySQL special characters
812
+ $input = MysqlDumpUtility::unescapeMysql($matches[1]);
813
814
// Replace serialized values
815
+ $input = MysqlDumpUtility::replaceSerializedValues($this->getOldReplaceValues(), $this->getNewReplaceValues(), $input);
816
817
// Prepare query values
818
return $this->getConnection()->quote($input);
824
* @param string $input SQL statement
825
* @return string
826
*/
827
+ protected function replaceTableCollation($input)
828
{
829
return str_replace('utf8mb4', 'utf8', $input);
830
}
835
* @param string $input SQL statement
836
* @return string
837
*/
838
+ protected function stripTableConstraints($input)
839
{
840
$pattern = array(
841
'/\s+CONSTRAINT(.+)REFERENCES(.+),/i',
846
return preg_replace($pattern, $replace, $input);
847
}
848
849
/**
850
* Make MySQL connection
851
*
902
"-- http://servmask.com/\n" .
903
"--\n" .
904
"-- Host: {$this->hostname}\n" .
905
+ "-- Generation Time: " . date('r') . "\n" .
906
+ "-- PDO Extension\n\n" .
907
"--\n" .
908
"-- Database: `{$this->database}`\n" .
909
"--\n\n";
911
return $header;
912
}
913
914
/**
915
* Parse data source name
916
*
lib/vendor/mysqldump-factory/mysqldump-factory/lib/MysqlDumpSQL.php CHANGED
@@ -1,81 +1,59 @@
1
<?php
2
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
- * MysqlDumpSQL class
6
*
7
- * PHP version 5
8
*
9
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining
10
- * a copy of this software and associated documentation files (the "Software"), to
11
- * deal in the Software without restriction, including without limitation the rights
12
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13
- * of the Software, and to permit persons to whom the Software is furnished to do so,
14
- * subject to the following conditions:
15
*
16
- * The above copyright notice and this permission notice shall be included in all
17
- * copies or substantial portions of the Software.
18
*
19
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
- *
26
- * @category Databases
27
- * @package MysqlDumpFactory
28
- * @author Yani Iliev <yani@iliev.me>
29
- * @author Bobby Angelov <bobby@servmask.com>
30
- * @copyright 2014 Yani Iliev, Bobby Angelov
31
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
32
- * @version GIT: 2.2.0
33
- * @link https://github.com/yani-/mysqldump-factory/
34
*/
35
36
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlDumpInterface.php';
37
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlFileAdapter.php';
38
- require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MysqlUtility.php';
39
40
- /**
41
- * MysqlDumpSQL class
42
- *
43
- * @category Databases
44
- * @package MysqlDumpFactory
45
- * @author Yani Iliev <yani@iliev.me>
46
- * @author Bobby Angelov <bobby@servmask.com>
47
- * @copyright 2014 Yani Iliev, Bobby Angelov
48
- * @license https://raw.github.com/yani-/mysqldump-factory/master/LICENSE The MIT License (MIT)
49
- * @version GIT: 2.2.0
50
- * @link https://github.com/yani-/mysqldump-factory/
51
- */
52
class MysqlDumpSQL implements MysqlDumpInterface
53
{
54
- protected $hostname = null;
55
56
- protected $username = null;
57
58
- protected $password = null;
59
60
- protected $database = null;
61
62
- protected $fileName = 'database.sql';
63
64
- protected $fileAdapter = null;
65
66
- protected $connection = null;
67
68
- protected $oldTablePrefix = null;
69
70
- protected $newTablePrefix = null;
71
72
- protected $oldReplaceValues = array();
73
74
- protected $newReplaceValues = array();
75
76
- protected $queryClauses = array();
77
78
- protected $tablePrefixColumns = array();
79
80
/**
81
* Define MySQL credentials for the current connection
@@ -96,103 +74,49 @@ class MysqlDumpSQL implements MysqlDumpInterface
96
}
97
98
/**
99
- * Export database into a file
100
*
101
- * @return void
102
- */
103
- public function export()
104
- {
105
- // Set File Adapter
106
- $this->fileAdapter = new MysqlFileAdapter();
107
-
108
- // Set output file
109
- $this->fileAdapter->open($this->getFileName());
110
-
111
- // Write Headers Formatting dump file
112
- $this->fileAdapter->write($this->getHeader());
113
-
114
- // Listing all tables from database
115
- $tables = $this->listTables();
116
-
117
- // Export Tables
118
- foreach ($tables as $table) {
119
- $isTable = $this->getTableStructure($table);
120
- if ($isTable) {
121
- $this->listValues($table);
122
- }
123
- }
124
-
125
- // Close File Adapter
126
- $this->fileAdapter->close();
127
- }
128
-
129
- /**
130
- * Set output file name
131
- *
132
- * @param string $fileName Name of the output file
133
* @return MysqlDumpSQL
134
*/
135
- public function setFileName($fileName)
136
{
137
- $this->fileName = $fileName;
138
139
return $this;
140
}
141
142
/**
143
- * Get output file name
144
*
145
- * @return string
146
- */
147
- public function getFileName()
148
- {
149
- return $this->fileName;
150
- }
151
-
152
- /**
153
- * Set old table prefix
154
- *
155
- * @param string $prefix Name of the table prefix
156
- * @return MysqlDumpSQL
157
- */
158
- public function setOldTablePrefix($prefix)
159
- {
160
- $this->oldTablePrefix = $prefix;
161
-
162
- return $this;
163
- }
164
-
165
- /**
166
- * Get old table prefix
167
- *
168
- * @return string
169
*/
170
- public function getOldTablePrefix()
171
{
172
- return $this->oldTablePrefix;
173
}
174
175
/**
176
- * Set new table prefix
177
*
178
- * @param string $prefix Name of the table prefix
179
* @return MysqlDumpSQL
180
*/
181
- public function setNewTablePrefix($prefix)
182
{
183
- $this->newTablePrefix = $prefix;
184
185
return $this;
186
}
187
188
/**
189
- * Get new table prefix
190
*
191
- * @return string
192
*/
193
- public function getNewTablePrefix()
194
{
195
- return $this->newTablePrefix;
196
}
197
198
/**
@@ -296,98 +220,316 @@ class MysqlDumpSQL implements MysqlDumpInterface
296
}
297
298
/**
299
- * Get MySQL version
300
*
301
- * @return string
302
*/
303
- public function getVersion() {
304
- if (($result = mysql_unbuffered_query("SELECT @@version AS VersionName", $this->getConnection()))) {
305
- while ($row = mysql_fetch_assoc($result)) {
306
- if (isset($row['VersionName'])) {
307
- return $row['VersionName'];
308
- }
309
- }
310
- } else {
311
- $result = mysql_unbuffered_query("SHOW VARIABLES LIKE 'version'", $this->getConnection());
312
- while ($row = mysql_fetch_row($result)) {
313
- if (isset($row[1])) {
314
- return $row[1];
315
- }
316
- }
317
- }
318
}
319
320
/**
321
- * Get MySQL max allowed packaet
322
*
323
- * @return integer
324
*/
325
- public function getMaxAllowedPacket() {
326
- if (($result = mysql_unbuffered_query("SELECT @@max_allowed_packet AS MaxAllowedPacket", $this->getConnection()))) {
327
- while ($row = mysql_fetch_assoc($result)) {
328
- if (isset($row['MaxAllowedPacket'])) {
329
- return $row['MaxAllowedPacket'];
330
- }
331
- }
332
- } else {
333
- $result = mysql_unbuffered_query("SHOW VARIABLES LIKE 'max_allowed_packet'", $this->getConnection());
334
- while ($row = mysql_fetch_row($result)) {
335
- if (isset($row[1])) {
336
- return $row[1];
337
- }
338
- }
339
- }
340
}
341
342
/**
343
- * Get MySQL collation name
344
*
345
- * @param string $collationName Collation name
346
- * @return string
347
*/
348
- public function getCollation($collationName) {
349
- // Get collation name
350
$result = mysql_unbuffered_query(
351
- "SELECT COLLATION_NAME AS CollationName FROM `INFORMATION_SCHEMA`.`COLLATIONS` WHERE COLLATION_NAME = '$collationName'",
352
$this->getConnection()
353
);
354
355
if ($result) {
356
while ($row = mysql_fetch_assoc($result)) {
357
- if (isset($row['CollationName'])) {
358
- return $row['CollationName'];
359
}
360
}
361
} else {
362
- $result = mysql_unbuffered_query("SHOW COLLATION LIKE '$collationName'", $this->getConnection());
363
while ($row = mysql_fetch_row($result)) {
364
- if (isset($row[0])) {
365
- return $row[0];
366
}
367
}
368
}
369
}
370
371
/**
372
- * Flush database
373
*
374
- * @return void
375
*/
376
- public function flush()
377
{
378
- $dropTables = array();
379
- foreach ($this->listTables() as $tableName) {
380
- $dropTables[] = "DROP TABLE IF EXISTS `$tableName`";
381
}
382
383
- // Drop tables
384
- foreach ($dropTables as $dropQuery) {
385
- mysql_unbuffered_query($dropQuery, $this->getConnection());
386
}
387
}
388
389
/**
390
- * Import database from file
391
*
392
* @param string $fileName Name of file
393
* @return bool
@@ -420,8 +562,8 @@ class MysqlDumpSQL implements MysqlDumpInterface
420
// Check max allowed packet
421
if (strlen($query) <= $maxAllowedPacket) {
422
423
- // Replace table prefix
424
- $query = $this->replaceTablePrefix($query);
425
426
// Replace table values
427
$query = $this->replaceTableValues($query, true);
@@ -431,21 +573,17 @@ class MysqlDumpSQL implements MysqlDumpInterface
431
$query = $this->replaceTableCollation($query);
432
}
433
434
- // Run SQL query
435
- $result = mysql_unbuffered_query($query, $this->getConnection());
436
- if ($result === false) {
437
- $failed++;
438
439
- // Log the error
440
- Ai1wm_Log::error(
441
- sprintf(
442
- 'Exception while importing: %s with query: %s',
443
- mysql_error($this->getConnection()),
444
- $query
445
- )
446
- );
447
- } else {
448
- $passed++;
449
}
450
451
} else {
@@ -468,64 +606,131 @@ class MysqlDumpSQL implements MysqlDumpInterface
468
}
469
470
/**
471
- * Get list of tables
472
*
473
- * @return array
474
*/
475
- public function listTables()
476
{
477
- $tables = array();
478
479
- // Get list of tables
480
$result = mysql_unbuffered_query(
481
- "SELECT TABLE_NAME AS TableName FROM `INFORMATION_SCHEMA`.`TABLES` WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '{$this->database}'",
482
$this->getConnection()
483
);
484
485
if ($result) {
486
while ($row = mysql_fetch_assoc($result)) {
487
- if (isset($row['TableName'])) {
488
- $tables[] = $row['TableName'];
489
}
490
}
491
} else {
492
- $result = mysql_unbuffered_query("SHOW TABLES FROM `{$this->database}`", $this->getConnection());
493
while ($row = mysql_fetch_row($result)) {
494