GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Branch dev (231e41)
by Liuta
03:38
created

Xcloner_Database::process_incremental()   C

Complexity

Conditions 13
Paths 64

Size

Total Lines 103
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 13
eloc 54
nc 64
nop 4
dl 0
loc 103
rs 6.6166
c 1
b 1
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 *      class-xcloner-database.php
4
 *
5
 *      Copyright 2017 Ovidiu Liuta <[email protected]>
6
 *
7
 *      This program is free software; you can redistribute it and/or modify
8
 *      it under the terms of the GNU General Public License as published by
9
 *      the Free Software Foundation; either version 2 of the License, or
10
 *      (at your option) any later version.
11
 *
12
 *      This program is distributed in the hope that it will be useful,
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *      GNU General Public License for more details.
16
 *
17
 *      You should have received a copy of the GNU General Public License
18
 *      along with this program; if not, write to the Free Software
19
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *      MA 02110-1301, USA.
21
 */
22
23
24
class Xcloner_Database extends wpdb {
0 ignored issues
show
Bug introduced by
The type wpdb was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
27
	public  $debug = 0;
28
	public  $recordsPerSession = 10000;
29
	public  $dbCompatibility = "";
30
	public  $dbDropSyntax				= 1;
31
	public  $countRecords				= 0;
32
33
	private  $link;
0 ignored issues
show
introduced by
The private property $link is not used, and could be removed.
Loading history...
34
	private  $db_selected;
0 ignored issues
show
introduced by
The private property $db_selected is not used, and could be removed.
Loading history...
35
	private  $logger;
36
	private  $fs;
37
38
	private   $TEMP_DBPROCESS_FILE = ".database";
39
	private   $TEMP_DUMP_FILE = "database-backup.sql";
40
	
41
	public function __construct(Xcloner $xcloner_container, $wp_user = "", $wp_pass = "", $wp_db = "", $wp_host = "")
42
	{
43
		$this->logger = $xcloner_container->get_xcloner_logger()->withName("xcloner_database");
44
		$this->xcloner_settings = $xcloner_container->get_xcloner_settings();
0 ignored issues
show
Bug Best Practice introduced by
The property xcloner_settings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
		$this->fs = $xcloner_container->get_xcloner_filesystem();
46
		
47
		if ($this->xcloner_settings->get_xcloner_option('xcloner_database_records_per_request'))
48
			$this->recordsPerSession = $this->xcloner_settings->get_xcloner_option('xcloner_database_records_per_request');
49
		
50
		if (!$this->recordsPerSession)
51
			$this->recordsPerSession = 100;
52
		
53
		if (!$wp_user && !$wp_pass && !$wp_host && !$wp_db)
54
		{
55
			$wp_host 	= $this->xcloner_settings->get_db_hostname();
56
			$wp_user 	= $this->xcloner_settings->get_db_username();
57
			$wp_pass 	= $this->xcloner_settings->get_db_password();
58
			$wp_db = $this->xcloner_settings->get_db_database();
59
		}
60
		
61
		parent::__construct($wp_user, $wp_pass, $wp_db, $wp_host);
62
		
63
		//$this->use_mysqli = true;
64
	}
65
	/*
66
	 * Initialize the database connection
67
	 *
68
	 * name: init
69
	 * @param array $data {'dbHostname', 'dbUsername', 'dbPassword', 'dbDatabase'}
70
	 * @return
71
	 */
72
	public function init($data, $start = 0)
73
	{
74
		if ($start and $this->fs->get_tmp_filesystem()->has($this->TEMP_DBPROCESS_FILE)) {
75
				$this->fs->get_tmp_filesystem()->delete($this->TEMP_DBPROCESS_FILE);
76
		}
77
		
78
		$this->headers();
79
		
80
		$this->suppress_errors = true;
0 ignored issues
show
Bug Best Practice introduced by
The property suppress_errors does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
	}
82
	
83
	public function start_database_recursion($params, $extra_params, $init = 0)
84
	{
85
		$tables = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $tables is dead and can be removed.
Loading history...
86
		$return['finished'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.
Loading history...
87
		$return['stats'] = array(
88
				"total_records"=>0,
89
				"tables_count"=>0,
90
				"database_count"=>0,
91
		);
92
		
93
		if (!$this->xcloner_settings->get_enable_mysql_backup())
94
		{
95
			$return['finished'] = 1;
96
			return $return;
97
		}
98
		
99
		$this->logger->debug(__("Starting database backup process"));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
		$this->logger->debug(/** @scrutinizer ignore-call */ __("Starting database backup process"));
Loading history...
100
		
101
		$this->init($params, $init);
102
		
103
		if ($init)
104
		{
105
			$db_count = 0;
106
			
107
			if (isset($params['#']))
108
			{
109
				foreach ($params['#'] as $database)
110
				{
111
					if (!isset($params[$database]) or !is_array($params[$database]))
112
						$params[$database] = array();
113
				}
114
				$db_count = -1;
115
			}
116
			
117
			if (isset($params) and is_array($params))
118
				foreach ($params as $database=>$tables)
119
				{	
120
					if ($database != "#")
121
					{
122
						$stats = $this->write_backup_process_list($database, $tables);	
123
						$return['stats']['tables_count'] += $stats['tables_count'];
124
						$return['stats']['total_records'] += $stats['total_records'];
125
					}
126
				}
127
128
			if (sizeof($params))
129
				$return['stats']['database_count'] = sizeof($params) + $db_count;
130
			else	
131
				$return['stats']['database_count'] = 0;
132
				
133
			return $return;
134
		}
135
		
136
		if (!isset($extra_params['startAtLine']))
137
			$extra_params['startAtLine'] = 0;
138
		if (!isset($extra_params['startAtRecord']))
139
			$extra_params['startAtRecord'] = 0;
140
		if (!isset($extra_params['dumpfile']))
141
			$extra_params['dumpfile'] = "";
142
		
143
		$return = $this->process_incremental($extra_params['startAtLine'], $extra_params['startAtRecord'], $extra_params['dumpfile']);
144
		
145
		return $return;
146
	}
147
	
148
	public function log($message = "")
149
	{
150
		
151
		if ($message) {
152
			$this->logger->info($message, array(""));
153
		} else {	
154
			if ($this->last_query)
155
				$this->logger->debug($this->last_query, array(""));
156
			if ($this->last_error)
157
				$this->logger->error($this->last_error, array(""));
158
		}
159
		
160
		return;
161
	}
162
	
163
	/*
164
	 *Return any error
165
	 *
166
	 * name: error
167
	 * @param string $message
168
	 * @return
169
	*/
170
	public function error($message)
171
	{
172
		$this->logger->error($message, array(""));
173
		
174
		return;
175
	}
176
177
	/*
178
	 * Send some special headers after the connection is initialized
179
	 *
180
	 * name: headers
181
	 * @param
182
	 * @return
183
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @return at position 0 could not be parsed: Unknown type name '@return' at position 0 in @return.
Loading history...
184
	private function headers()
185
	{
186
		$this->logger->debug(__("Setting mysql headers"));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
		$this->logger->debug(/** @scrutinizer ignore-call */ __("Setting mysql headers"));
Loading history...
187
		
188
		$this->query("SET SQL_QUOTE_SHOW_CREATE=1;");
189
		//$this->log();
190
		$this->query("SET sql_mode = 0;");
191
		//$this->log();
192
		
193
		$this->set_charset($this->dbh, 'utf8');
194
		//$this->log();
195
	
196
197
	}
198
199
	public function get_database_num_tables($database)
200
	{
201
		$this->logger->debug(sprintf(__("Getting number of tables in %s"), $database));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
		$this->logger->debug(sprintf(/** @scrutinizer ignore-call */ __("Getting number of tables in %s"), $database));
Loading history...
202
		
203
		$query = "show tables in `".$database."`";
204
		
205
		$res = $this->get_results($query);
206
		$this->log();
207
			
208
		return count($res);
209
	}
210
	
211
	public function get_all_databases()
212
	{
213
		$this->logger->debug(("Getting all databases"));
214
		
215
		$query = "SHOW DATABASES;";
216
		
217
		$databases = $this->get_results($query);
218
		$this->log();
219
		
220
		$databases_list = array();
221
		
222
		$i = 0;
223
		
224
		$databases_list[$i]['name'] = $this->dbname;
225
		$databases_list[$i]['num_tables'] = $this->get_database_num_tables($this->dbname);
226
		$i++;
227
		
228
		if (is_array($databases))
229
		foreach ($databases as $db) {
230
			if ($db->Database != $this->dbname)
231
			{
232
				$databases_list[$i]['name'] = $db->Database;
233
				$databases_list[$i]['num_tables'] = $this->get_database_num_tables($db->Database);
234
				$i++;
235
			}
236
		}
237
		
238
		return $databases_list;
239
	}
240
	
241
	/*
242
	 * Returns an array of tables from a database and mark $excluded ones
243
	 *
244
	 * name: list_tables
245
	 * @param string $database
246
	 * @param array $included
247
	 * @param int $get_num_records
248
	 * @return array $tablesList
249
	 */
250
	public function list_tables($database = "", $included = array(), $get_num_records = 0)
251
	{
252
		$tablesList[0] = array( );
0 ignored issues
show
Comprehensibility Best Practice introduced by
$tablesList was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tablesList = array(); before regardless.
Loading history...
253
		$inc = 0;
254
255
		if (!$database)
256
			$database = $this->dbname;
257
		
258
		$this->logger->debug(sprintf(("Listing tables in %s database"), $database));
259
		
260
		$tables = $this->get_results("SHOW TABLES in `".$database."`");
261
		$this->log();
262
263
		foreach ($tables as $table) {
264
			
265
			$table = array_values((array)$table)[0];
266
			
267
			$tablesList[$inc]['name'] = $table;
268
			$tablesList[$inc]['database'] = $database;
269
270
			if ($get_num_records)
271
			{
272
				$records_num_result = $this->get_var("SELECT count(*) FROM `".$database."`.`".$table."`");
273
				$this->log();
274
					
275
				$tablesList[$inc]['records'] = $records_num_result;
276
			}
277
			
278
			$tablesList[$inc]['excluded'] = 0;
279
						
280
			if (sizeof($included) and is_array($included))
281
				if (!in_array($table, $included))
282
				{
283
					$tablesList[$inc]['excluded'] = 1;
284
					$this->log(sprintf(__("Excluding table %s.%s from backup"), $table, $database));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

284
					$this->log(sprintf(/** @scrutinizer ignore-call */ __("Excluding table %s.%s from backup"), $table, $database));
Loading history...
285
				}
286
			$inc++;
287
		}
288
289
		return $tablesList;
290
291
	}
292
293
	public function write_backup_process_list($dbname, $incl_tables)
294
	{
295
		$return['total_records'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.
Loading history...
296
		$return['tables_count'] = 0;
297
		
298
		$this->log(__("Preparing the database recursion file"));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

298
		$this->log(/** @scrutinizer ignore-call */ __("Preparing the database recursion file"));
Loading history...
299
		
300
		$tables = $this->list_tables($dbname, $incl_tables, 1);
301
		
302
		if ($this->dbname != $dbname)
303
			$dumpfile = $dbname."-backup.sql";
304
		else
305
			$dumpfile = $this->TEMP_DUMP_FILE;
306
		
307
		$line = sprintf("###newdump###\t%s\t%s\n", $dbname, $dumpfile);
308
		$this->fs->get_tmp_filesystem_append()->write($this->TEMP_DBPROCESS_FILE, $line);
309
			
310
		// write this to the class and write to $TEMP_DBPROCESS_FILE file as database.table records
311
		foreach ($tables as $key=>$table) 
312
		if ($table != "" and !$tables[$key]['excluded']) {
313
314
			$line = sprintf("`%s`.`%s`\t%s\t%s\n", $dbname, $tables[$key]['name'], $tables[$key]['records'], $tables[$key]['excluded']);
315
			$this->fs->get_tmp_filesystem_append()->write($this->TEMP_DBPROCESS_FILE, $line);
316
			$return['tables_count']++;
317
			$return['total_records'] += $tables[$key]['records'];
318
		}
319
320
		$line = sprintf("###enddump###\t%s\t%s\n", $dbname, $dumpfile);
321
		$this->fs->get_tmp_filesystem_append()->write($this->TEMP_DBPROCESS_FILE, $line);
322
		
323
		return $return;
324
	}
325
326
	/*
327
	 * Returns the number of records from a table
328
	 *
329
	 * name: countRecords
330
	 * @param string $table - the source table
331
	 * @return int $count
332
	 */
333
	public function countRecords($table)
334
	{
335
336
			$table = "`".$this->dbname."`.`$table`";
337
338
			$result = $this->get_var("SELECT count(*) FROM $table;");
339
340
			return intval($result) ;// not max limit on 32 bit systems 2147483647; on 64 bit 9223372036854775807
341
342
	}
343
344
	/*
345
	 *	Processing the mysql backup incrementally
346
	 *
347
	 * name: processIncremental
348
	 * @param
349
	 * 		int $startAtLine - at which line from the perm.txt file to start reading
350
	 * 		int startAtRecord - at which record to start from the table found at $startAtLine
351
	 * 		string $dumpfie	- where to save the data
352
	 * 		string $dbCompatibility - MYSQL40, MYSQ32, none=default
353
	 * 		int $dbDropSyntax	- check if the DROP TABLE syntax should be added
354
	 * @return array $return
355
	 */
356
	public function process_incremental($startAtLine = 0, $startAtRecord = 0, $dumpfile = "", $dbCompatibility = "") {
357
358
		$count = 0;
359
		$return['finished'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.
Loading history...
360
		$lines = array();
361
		
362
		if ($this->fs->get_tmp_filesystem()->has($this->TEMP_DBPROCESS_FILE))
363
			$lines = array_filter(explode("\n", $this->fs->get_tmp_filesystem()->read($this->TEMP_DBPROCESS_FILE)));
364
	
365
		foreach ($lines as $buffer) {
366
			
367
			if ($count == $startAtLine)
368
			{
369
	
370
				$tableInfo = explode("\t", $buffer);
371
				
372
				if ($tableInfo[0] == "###newdump###") {
373
						// we create a new mysql dump file
374
						if ($dumpfile != "") {
375
								// we finished a previous one and write the footers
376
								$return['dumpsize'] = $this->data_footers($dumpfile);
377
						}
378
	
379
						$dumpfile = $tableInfo[2];
380
						
381
						$this->log(sprintf(__("Starting new backup dump to file %s"), $dumpfile));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

381
						$this->log(sprintf(/** @scrutinizer ignore-call */ __("Starting new backup dump to file %s"), $dumpfile));
Loading history...
382
						
383
						$this->data_headers($dumpfile, $tableInfo[1]);
384
						$dumpfile = $tableInfo[2];
385
						$startAtLine++;
386
						$return['new_dump'] = 1;
387
						//break;
388
				} else {
389
						//we export the table
390
						if ($tableInfo[0] == "###enddump###")
391
							$return['endDump'] = 1;
392
	
393
						//table is excluded
394
						if ($tableInfo[2])
395
							continue;
396
							
397
						$next = $startAtRecord + $this->recordsPerSession;
398
						
399
						// $tableInfo[1] number of records in the table
400
						$table = explode("`.`", $tableInfo[0]);
401
						$tableName = str_replace("`", "", $table[1]);
402
						$databaseName = str_replace("`", "", $table[0]);
403
404
						//return something to the browser
405
						$return['databaseName'] 	= $databaseName;
406
						$return['tableName'] = $tableName;
407
						$return['totalRecords'] 	= $tableInfo[1];
408
409
						$processed_records = 0;
410
						
411
						if (trim($tableName) != "" and !$tableInfo[2])
412
							$processed_records = $this->export_table($databaseName, $tableName, $startAtRecord, $this->recordsPerSession, $dumpfile);
413
						
414
						$return['processedRecords'] = $startAtRecord + $processed_records;
415
						
416
						if ($next >= $tableInfo[1]) //we finished loading the records for next sessions, will go to the new record
417
						{
418
								$startAtLine++;
419
								$startAtRecord = 0;
420
						} else {
421
								$startAtRecord = $startAtRecord + $this->recordsPerSession;
422
							}
423
424
						//$return['dbCompatibility'] 	= self::$dbCompatibility;
425
						
426
						$return['startAtLine'] = $startAtLine;
427
						$return['startAtRecord'] = $startAtRecord;
428
						$return['dumpfile']			= $dumpfile;
429
						$return['dumpsize']			= $this->fs->get_tmp_filesystem_append()->getSize($dumpfile);
430
431
						return $return;
432
						break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
433
	
434
						
435
					}
436
	
437
			}
438
	
439
			$count++;
440
	
441
	
442
		}
443
	
444
		//while is finished, lets go home...
445
		if ($dumpfile != "") {
446
			// we finished a previous one and write the footers
447
			$return['dumpsize'] = $this->data_footers($dumpfile);
448
			$return['dumpfile'] = ($dumpfile);
449
		}
450
		$return['finished'] = 1;
451
		$return['startAtLine'] = $startAtLine;
452
		
453
		if ($this->fs->get_tmp_filesystem()->has($this->TEMP_DBPROCESS_FILE))
454
			$this->fs->get_tmp_filesystem()->delete($this->TEMP_DBPROCESS_FILE);
455
		
456
		$this->logger->debug(sprintf(("Database backup finished!")));
457
		
458
		return $return;
459
460
461
	}
462
463
464
	/*
465
	 * Exporting the table records
466
	 *
467
	 * name: exportTable
468
	 * @param
469
	 * 		string $databaseName - database name of the table
470
	 * 		string tableName - table name
471
	 * 		int $start - where to start from
472
	 * 		int $limit - how many records
473
	 * 		handler $fd - file handler where to write the records
474
	 * @return
475
	 */
476
477
	/**
478
	 * @param integer $start
479
	 * @param integer $limit
480
	 */
481
	public function export_table($databaseName, $tableName, $start, $limit, $dumpfile)
482
	{
483
		$this->logger->debug(sprintf(("Exporting table  %s.%s data"), $databaseName, $tableName));
484
		
485
		$records = 0;
486
		
487
		if ($start == 0)
488
			$this->dump_structure($databaseName, $tableName, $dumpfile);
489
490
		$start = intval($start);
491
		$limit = intval($limit);
492
		//exporting the table content now
493
494
		$query = "SELECT * from `$databaseName`.`$tableName` Limit $start, $limit ;";
495
		if ($this->use_mysqli)
496
		{
497
			$result = mysqli_query($this->dbh, $query);
498
			$mysql_fetch_function = "mysqli_fetch_array";
499
		
500
		} else {
501
			$result = mysql_query($query, $this->dbh);
0 ignored issues
show
Deprecated Code introduced by
The function mysql_query() has been deprecated: 5.5 Send a MySQL query ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

501
			$result = /** @scrutinizer ignore-deprecated */ mysql_query($query, $this->dbh);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
502
			$mysql_fetch_function = "mysqli_fetch_array";
503
		}
504
		//$result = $this->get_results($query, ARRAY_N);
505
		//print_r($result); exit;
506
		
507
		if ($result) {
508
			while ($row = $mysql_fetch_function($result, MYSQLI_ASSOC)) {
509
					
510
					$this->fs->get_tmp_filesystem_append()->write($dumpfile, "INSERT INTO `$tableName` VALUES (");
511
					$arr = $row;
512
					$buffer = "";
513
					$this->countRecords++;
514
515
					foreach ($arr as $key => $value) {
516
						$value = $this->_real_escape($value);
517
						
518
						if (method_exists($this, 'remove_placeholder_escape')) {
519
							$value = $this->remove_placeholder_escape($value);
520
						}
521
						
522
						$buffer .= "'".$value."', ";
523
					}
524
					$buffer = rtrim($buffer, ', ').");\n";
525
					$this->fs->get_tmp_filesystem_append()->write($dumpfile, $buffer);
526
					unset($buffer);
527
					
528
					$records++;
529
530
				}
531
		}
532
		
533
		$this->log(sprintf(__("Dumping %s records starting position %s from %s.%s table"), $records, $start, $databaseName, $tableName));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

533
		$this->log(sprintf(/** @scrutinizer ignore-call */ __("Dumping %s records starting position %s from %s.%s table"), $records, $start, $databaseName, $tableName));
Loading history...
534
		
535
		return $records;
536
537
	}
538
539
	public function dump_structure($databaseName, $tableName, $dumpfile)
540
	{
541
		$this->log(sprintf(__("Dumping the structure for %s.%s table"), $databaseName, $tableName));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

541
		$this->log(sprintf(/** @scrutinizer ignore-call */ __("Dumping the structure for %s.%s table"), $databaseName, $tableName));
Loading history...
542
		
543
		$line = ("\n#\n# Table structure for table `$tableName`\n#\n\n");
544
		$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line);
545
546
		if ($this->dbDropSyntax)
547
		{
548
			$line = ("\nDROP table IF EXISTS `$tableName`;\n");
549
			$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line);
550
		}
551
552
		//$result = mysqli_query($this->dbh,"SHOW CREATE table `$databaseName`.`$tableName`;");
553
		$result = $this->get_row("SHOW CREATE table `$databaseName`.`$tableName`;", ARRAY_N);
0 ignored issues
show
Bug introduced by
The constant ARRAY_N was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
554
		if ($result) {
555
			//$row = mysqli_fetch_row( $result);
556
			$line = ($result[1].";\n");
557
			$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line);
558
		}
559
560
		$line = ("\n#\n# End Structure for table `$tableName`\n#\n\n");
561
		$line .= ("#\n# Dumping data for table `$tableName`\n#\n\n");
562
		$this->fs->get_tmp_filesystem_append()->write($dumpfile, $line);
563
		
564
		return;
565
566
	}
567
568
	public function data_footers($dumpfile)
569
	{
570
		$this->logger->debug(sprintf(("Writing dump footers in file"), $dumpfile));
571
		// we finished the dump file, not return the size of it
572
		$this->fs->get_tmp_filesystem_append()->write($dumpfile, "\n#\n# Finished at: ".date("M j, Y \a\\t H:i")."\n#");
573
		$size = $this->fs->get_tmp_filesystem_append()->getSize($dumpfile);
574
		
575
		$metadata_dumpfile = $this->fs->get_tmp_filesystem()->getMetadata($dumpfile);
576
		
577
		//adding dump file to the included files list
578
		$this->fs->store_file($metadata_dumpfile, 'tmp_filesystem');
579
		
580
		return $size;
581
582
	}
583
584
	public function resetcountRecords() {
585
		
586
		$this->countRecords = 0;
587
588
		return $this->countRecords;
589
	
590
	}
591
592
	public function getcountRecords() {
593
		
594
		return $this->countRecords;
595
		
596
	}
597
598
599
	public function data_headers($file, $database)
600
	{
601
		$this->logger->debug(sprintf(("Writing dump header for %s database in file"), $database, $file));
602
		
603
		$return = "";
604
605
		$return .= "#\n";
606
		$return .= "# Powered by XCloner Site Backup\n";
607
		$return .= "# http://www.xcloner.com\n";
608
		$return .= "#\n";
609
		$return .= "# Host: ".get_site_url()."\n";
0 ignored issues
show
Bug introduced by
The function get_site_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

609
		$return .= "# Host: "./** @scrutinizer ignore-call */ get_site_url()."\n";
Loading history...
610
		$return .= "# Generation Time: ".date("M j, Y \a\\t H:i")."\n";
611
		$return .= "# PHP Version: ".phpversion()."\n";
612
		$return .= "# Database Charset: ".$this->charset."\n";
613
		
614
		$results = $this->get_results("SHOW VARIABLES LIKE \"%version%\";", ARRAY_N);
0 ignored issues
show
Bug introduced by
The constant ARRAY_N was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
615
		if (isset($results)) {
616
			foreach ($results as $result) {
617
618
					$return .= "# MYSQL ".$result[0].": ".$result[1]."\n";
619
620
				}
621
		}
622
		
623
		$results = $this->get_results("SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME
624
					FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".$database."';");
625
		
626
		if (isset($results[0])) {
627
628
			$return .= "# MYSQL DEFAULT_CHARACTER_SET_NAME: ".$results[0]->DEFAULT_CHARACTER_SET_NAME."\n";
629
			$return .= "# MYSQL SCHEMA_NAME: ".$results[0]->DEFAULT_COLLATION_NAME."\n";
630
		}
631
632
		$return .= "#\n# Database : `".$database."`\n# --------------------------------------------------------\n\n";
633
		
634
		$this->log(sprintf(__("Writing %s database dump headers"), $database));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

634
		$this->log(sprintf(/** @scrutinizer ignore-call */ __("Writing %s database dump headers"), $database));
Loading history...
635
		
636
		$return = $this->fs->get_tmp_filesystem()->write($file, $return);
637
		return $return['size'];
638
639
	}
640
641
642
}
643