public/wordpress/wp-content/plugins/backupbuddy/lib/dbreplace/dbreplace.php
/**
		 *	bruteforce_table()
		 *
		 *	!!! HANDLES SERIALIZED DATA !!!!
		 *	Replaces text, serialized or not, within the entire table. Bruteforce method iterates through every row & column in the entire table and replaces if needed.
		 *	
		 *	@param		string		$table		Text (possibly serialized) to update.
		 *	@param		mixed		$olds		Text to search for to replace. May be an array of strings to search for.
		 *	@param		mixed		$news		New value(s) to be replaced with. May be a string or array. If array there must be the same number of values as $olds.
		 *	@return		int						Number of rows changed.
		 *
		 */
bruteforce_table()
HANDLES SERIALIZED DATA
Replaces text, serialized or not, within the entire table. Bruteforce method iterates through every row & column in the entire table and replaces if needed.

@param          string          $table          Text (possibly serialized) to update.
@param          mixed           $olds           Text to search for to replace. May be an array of strings to search for.
@param          mixed           $news           New value(s) to be replaced with. May be a string or array. If array there must be the same number of values as $olds.
@return         int                                             Number of rows changed.
bruteforce_table () HANDLES SERIALIZED DATA Replaces text serialized or not within the entire table . Bruteforce method iterates through every row & column in the entire table and replaces if needed . @param string $table Text ( possibly serialized ) to update . @param mixed $olds Text to search for to replace . May be an array of strings to search for . @param mixed $news New value ( s ) to be replaced with . May be a string or array . If array there must be the same number of values as $olds . @return int Number of rows changed .
function bruteforce_table( $table, $olds, $news, $rows_start = '' ) {
			pb_backupbuddy::status( 'message', 'Starting brute force data migration for table `' . $table . '`...' );
			if ( !is_array( $olds ) ) {
				$olds = array( $olds );
			}
			if ( !is_array( $news ) ) {
				$news = array( $news );
			}
			$count_items_checked = 0;
			$count_items_changed = 0;
			global $wpdb;
			$total_rows = 0;
			$tables_status = $wpdb->get_results( "SHOW TABLE STATUS", ARRAY_A );
			foreach ( $tables_status as $table_status ) {
				if ( $table === $table_status[ 'Name' ] ) {
					if ( 'InnoDB' === $table_status[ 'Engine' ] ) {
						if ( false !== ( $count = $wpdb->get_var( "SELECT COUNT(1) FROM `{$table_status['Name']}`" ) ) ) {
							$table_status[ 'Rows' ] = $count;
						}
					}
					$total_rows = $table_status[ 'Rows' ];
				}
			}
			$fields = $wpdb->get_results( "DESCRIBE `{$table}`", ARRAY_A );
			$column_name = array();
			$table_index = array();
			$i = 0;
			$found_primary_key = false;
			foreach( $fields as $field ) {
				$column_name[$i++] = $field['Field'];
				if ( $field['Key'] == 'PRI' ) {
					$table_index[$i] = true;
					$found_primary_key = true;
				}
			}
			if ( $found_primary_key === false ) {
				pb_backupbuddy::status( 'warning', 'Error #9029b: Warning only! Table `' . $table . '` does not contain a primary key; BackupBuddy cannot safely modify the contents of this table. Skipping migration of this table. (bruteforce_table()).' );
				return true;
			}
			$row_loop = 0;
			$rows_remain = true; 
			if ( '' == $rows_start ) {
				$rows_start = 0;
			}
			while ( true === $rows_remain ) { 
				$data = $wpdb->get_results( "SELECT * FROM `{$table}` LIMIT {$rows_start}," . self::MAX_ROWS_PER_SELECT, ARRAY_A );
				if ( false === $data ) {
					pb_backupbuddy::status( 'error', 'ERROR #44545343 ... SQL ERROR: ' . $wpdb->last_error );
				}
				$rowsCount = count( $data );
				if ( 0 === $rowsCount ) {
					pb_backupbuddy::status( 'details', 'Table: `'. $table . '` - processing ' . $rowsCount . ' rows ( of ' . $total_rows . ' )' );				
				} else {
					pb_backupbuddy::status( 'details', 'Table: `'. $table . '` - processing ' . $rowsCount . ' rows ( Rows ' . $rows_start . '-' . ( $rows_start + $rowsCount - 1 ) . ' of ' . $total_rows . ' )' );	
				}
				$rows_start += self::MAX_ROWS_PER_SELECT; 
				if ( ( 0 == $rowsCount ) || ( $rowsCount < self::MAX_ROWS_PER_SELECT ) ) {
					$rows_remain = false;
				}
				foreach( $data as $row ) {
					$need_to_update = false;
					$UPDATE_SQL = 'UPDATE `' . $table . '` SET ';
					$WHERE_SQL = ' WHERE ';
					$j = 0;
					foreach ( $column_name as $current_column ) {
						$j++;
						$count_items_checked++;
						$data_to_fix = $row[$current_column];
						if ( false !== ( $edited_data = $this->replace_maybe_serialized( $data_to_fix, $olds, $news ) ) ) { 
							$count_items_changed++;
							if ( $need_to_update != false ) { 
								$UPDATE_SQL = $UPDATE_SQL . ',';
							}
							$UPDATE_SQL = $UPDATE_SQL . ' ' . $current_column . ' = "' . backupbuddy_core::dbEscape( $edited_data ) . '"';
							$need_to_update = true; 
						}
						if ( isset( $table_index[$j] ) ) {
							$WHERE_SQL = $WHERE_SQL . '`' . $current_column . '` = "' . backupbuddy_core::dbEscape( $row[$current_column] ) . '" AND ';
						}
					}
					if ( $need_to_update ) {
						$WHERE_SQL = substr( $WHERE_SQL , 0, -4 ); 
						$UPDATE_SQL = $UPDATE_SQL . $WHERE_SQL;
						$result = $wpdb->query( $UPDATE_SQL );
						if ( false === $result ) {
							if ( ! empty( $wpdb->use_mysqli ) ) {
								$mysql_error = mysqli_error( $wpdb->dbh );
							} else {
								$mysql_error = mysql_error( $wpdb->dbh );
							}
							pb_backupbuddy::status( 'error', 'ERROR: mysql error updating db: ' . $mysql_error . '. SQL Query: ' . htmlentities( $UPDATE_SQL ) );
						} 
					}
				}
				unset( $data );
				if ( ( ( microtime( true ) - $this->startTime ) + $this->timeWiggleRoom ) >= $this->maxExecutionTime ) {
					return array( $rows_start );
				}
			}
			pb_backupbuddy::status( 'message', 'Brute force data migration for table `' . $table . '` complete. Checked ' . $count_items_checked . ' items; ' . $count_items_changed . ' changed.' );
			return true;
		}
function bruteforce_table ( $ table , $ olds , $ news , $ rows_start = '' ) { pb_backupbuddy :: status ( 'message' , 'Starting brute force data migration for table `' . $ table . '`...' ) ; if ( ! is_array ( $ olds ) ) { $ olds = array ( $ olds ) ; } if ( ! is_array ( $ news ) ) { $ news = array ( $ news ) ; } $ count_items_checked = 0 ; $ count_items_changed = 0 ; global $ wpdb ; $ total_rows = 0 ; $ tables_status = $ wpdb -> get_results ( " SHOW TABLE STATUS " , ARRAY_A ) ; foreach ( $ tables_status as $ table_status ) { if ( $ table === $ table_status [ 'Name' ] ) { if ( 'InnoDB' === $ table_status [ 'Engine' ] ) { if ( false !== ( $ count = $ wpdb -> get_var ( " SELECT COUNT(1) FROM ` { $ table_status [ 'Name' ] } ` " ) ) ) { $ table_status [ 'Rows' ] = $ count ; } } $ total_rows = $ table_status [ 'Rows' ] ; } } $ fields = $ wpdb -> get_results ( " DESCRIBE ` { $ table } ` " , ARRAY_A ) ; $ column_name = array ( ) ; $ table_index = array ( ) ; $ i = 0 ; $ found_primary_key = false ; foreach ( $ fields as $ field ) { $ column_name [ $ i ++ ] = $ field [ 'Field' ] ; if ( $ field [ 'Key' ] == 'PRI' ) { $ table_index [ $ i ] = true ; $ found_primary_key = true ; } } if ( $ found_primary_key === false ) { pb_backupbuddy :: status ( 'warning' , 'Error #9029b: Warning only! Table `' . $ table . '` does not contain a primary key; BackupBuddy cannot safely modify the contents of this table. Skipping migration of this table. (bruteforce_table()).' ) ; return true ; } $ row_loop = 0 ; $ rows_remain = true ; if ( '' == $ rows_start ) { $ rows_start = 0 ; } while ( true === $ rows_remain ) { $ data = $ wpdb -> get_results ( " SELECT * FROM ` { $ table } ` LIMIT  { $ rows_start } , " . self :: MAX_ROWS_PER_SELECT , ARRAY_A ) ; if ( false === $ data ) { pb_backupbuddy :: status ( 'error' , 'ERROR #44545343 ... SQL ERROR: ' . $ wpdb -> last_error ) ; } $ rowsCount = count ( $ data ) ; if ( 0 === $ rowsCount ) { pb_backupbuddy :: status ( 'details' , 'Table: `' . $ table . '` - processing ' . $ rowsCount . ' rows ( of ' . $ total_rows . ' )' ) ; } else { pb_backupbuddy :: status ( 'details' , 'Table: `' . $ table . '` - processing ' . $ rowsCount . ' rows ( Rows ' . $ rows_start . '-' . ( $ rows_start + $ rowsCount - 1 ) . ' of ' . $ total_rows . ' )' ) ; } $ rows_start += self :: MAX_ROWS_PER_SELECT ; if ( ( 0 == $ rowsCount ) || ( $ rowsCount < self :: MAX_ROWS_PER_SELECT ) ) { $ rows_remain = false ; } foreach ( $ data as $ row ) { $ need_to_update = false ; $ UPDATE_SQL = 'UPDATE `' . $ table . '` SET ' ; $ WHERE_SQL = ' WHERE ' ; $ j = 0 ; foreach ( $ column_name as $ current_column ) { $ j ++ ; $ count_items_checked ++ ; $ data_to_fix = $ row [ $ current_column ] ; if ( false !== ( $ edited_data = $ this -> replace_maybe_serialized ( $ data_to_fix , $ olds , $ news ) ) ) { $ count_items_changed ++ ; if ( $ need_to_update != false ) { $ UPDATE_SQL = $ UPDATE_SQL . ',' ; } $ UPDATE_SQL = $ UPDATE_SQL . ' ' . $ current_column . ' = "' . backupbuddy_core :: dbEscape ( $ edited_data ) . '"' ; $ need_to_update = true ; } if ( isset ( $ table_index [ $ j ] ) ) { $ WHERE_SQL = $ WHERE_SQL . '`' . $ current_column . '` = "' . backupbuddy_core :: dbEscape ( $ row [ $ current_column ] ) . '" AND ' ; } } if ( $ need_to_update ) { $ WHERE_SQL = substr ( $ WHERE_SQL , 0 , - 4 ) ; $ UPDATE_SQL = $ UPDATE_SQL . $ WHERE_SQL ; $ result = $ wpdb -> query ( $ UPDATE_SQL ) ; if ( false === $ result ) { if ( ! empty ( $ wpdb -> use_mysqli ) ) { $ mysql_error = mysqli_error ( $ wpdb -> dbh ) ; } else { $ mysql_error = mysql_error ( $ wpdb -> dbh ) ; } pb_backupbuddy :: status ( 'error' , 'ERROR: mysql error updating db: ' . $ mysql_error . '. SQL Query: ' . htmlentities ( $ UPDATE_SQL ) ) ; } } } unset ( $ data ) ; if ( ( ( microtime ( true ) - $ this -> startTime ) + $ this -> timeWiggleRoom ) >= $ this -> maxExecutionTime ) { return array ( $ rows_start ) ; } } pb_backupbuddy :: status ( 'message' , 'Brute force data migration for table `' . $ table . '` complete. Checked ' . $ count_items_checked . ' items; ' . $ count_items_changed . ' changed.' ) ; return true ; }
bruteforce_table()
      !
bruteforce_table () !
// Get the total row count for this table // Fix up row count and average row length for InnoDB engine which returns inaccurate // (and changing) values for these // Skips migration of this table if there is no primary key. Modifying on any other key is not safe. mysql automatically returns a PRIMARY if a UNIQUE non-primary is found according to http://dev.mysql.com/doc/refman/5.1/en/create-table.html  @since 2.2.32. // More rows remaining / aka another query for more rows needed. // Keep looping through rows until none remain. Looping through like this to limit memory usage as wpdb classes loads all results into memory. // Provide an update on progress // Next loop we will begin at this offset. // no change needed // If this isn't our first time here, we need to add a comma. // Only set if we need to update - avoids wasted UPDATE statements. // Strip off the excess AND - the easiest way to code this without extra flags, etc. // See how we are doing on time. Trigger chunking if needed.