public/wordpress/wp-content/plugins/backupbuddy/lib/dbreplace/dbreplace.php
/**
		 *	serialized()
		 *	
		 *	Replaces serialized text within a table by specifying the table, rows to replace within and the old and new value(s).
		 *	
		 *	@param		string		$table		Table to replace text in.
		 *	@param		mixed		$olds		Old value(s) to find for replacement. May be a string or array of values.
		 *	@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.
		 *	@param		mixed		$rows		Table row(s) to replace within. May be an array of tables.
		 *	@param		int			$rows_start	Row to start at. Used for resuming.
		 *	@return		null
		 *
		 */
serialized()

Replaces serialized text within a table by specifying the table, rows to replace within and the old and new value(s).

@param          string          $table          Table to replace text in.
@param          mixed           $olds           Old value(s) to find for replacement. May be a string or array of values.
@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.
@param          mixed           $rows           Table row(s) to replace within. May be an array of tables.
@param          int                     $rows_start     Row to start at. Used for resuming.
@return         null
serialized () Replaces serialized text within a table by specifying the table rows to replace within and the old and new value ( s ) . @param string $table Table to replace text in . @param mixed $olds Old value ( s ) to find for replacement . May be a string or array of values . @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 . @param mixed $rows Table row ( s ) to replace within . May be an array of tables . @param int $rows_start Row to start at . Used for resuming . @return null
public function serialized( $table, $olds, $news, $rows, $rows_start = '' ) {
			if ( !is_array( $olds ) ) {
				$olds = array( $olds );
			}
			if ( !is_array( $news ) ) {
				$news = array( $news );
			}
			if ( !is_array( $rows ) ) {
				$rows = array( $rows );
			}
			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' ];
				}
			}
			$this->remove_matching_array_elements( $olds, $news );
			$key_results = $wpdb->get_results( "SHOW KEYS FROM `{$table}` WHERE Key_name='PRIMARY';", ARRAY_A );
			if ( $key_results === false ) {
				pb_backupbuddy::status( 'details', 'Table `' . $table . '` does not exist; skipping migration of this table.' );
				return;
			}
			if ( count( $key_results ) == 0 ) {
				pb_backupbuddy::status( 'message', 'Error #9029: Warning only! Table `'.  $table .'` does not contain a primary key; BackupBuddy cannot safely modify the contents of this table. Skipping migration of this table. (serialized()).' );
				return true;
			}
			$primary_key = $key_results[0]['Column_name'];
			unset( $key_result );
			$updated = false; 			$rows_remain = true; 
			if ( '' == $rows_start ) {
				$rows_start = 0;
			}
			pb_backupbuddy::status( 'details', 'Finding rows in table `' . $table . '` to update.' );
			while ( true === $rows_remain ) { 
				$rowsResult = $wpdb->get_results( "SELECT `" . implode( '`,`', $rows ) . "`,`{$primary_key}` FROM `{$table}` LIMIT " . $rows_start . ',' . self::MAX_ROWS_PER_SELECT );
				$rowsCount = count( $rowsResult );
				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( $rowsResult as $row ) {
					$needs_update = false;
					$sql_update = array();
					foreach( $row as $column => $value ) {
						if ( $column != $primary_key ) {
							if ( false !== ( $edited_data = $this->replace_maybe_serialized( $value, $olds, $news ) ) ) { 
								$needs_update = true;
								$sql_update[] = $column . "= '" . backupbuddy_core::dbEscape( $edited_data ) . "'";
							}
						} else {
							$primary_key_value = $value;
						}
					}
					if ( $needs_update === true ) {
						$updated = true;
						$wpdb->query( "UPDATE `{$table}` SET " . implode( ',', $sql_update ) . " WHERE `{$primary_key}` = '{$primary_key_value}' LIMIT 1" );
					}
				}
				unset( $rowsResult );
				if ( ( ( microtime( true ) - $this->startTime ) + $this->timeWiggleRoom ) >= $this->maxExecutionTime ) {
					return array( $rows_start );
				}
			}
			if ( $updated === true ) {
				pb_backupbuddy::status( 'details', 'Updated serialized data in table `' . $table . '`.' );
			} else {
				pb_backupbuddy::status( 'details', 'Nothing found to update in table `' . $table . '`.' );
			}
			return true;
		}
public function serialized ( $ table , $ olds , $ news , $ rows , $ rows_start = '' ) { if ( ! is_array ( $ olds ) ) { $ olds = array ( $ olds ) ; } if ( ! is_array ( $ news ) ) { $ news = array ( $ news ) ; } if ( ! is_array ( $ rows ) ) { $ rows = array ( $ rows ) ; } 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' ] ; } } $ this -> remove_matching_array_elements ( $ olds , $ news ) ; $ key_results = $ wpdb -> get_results ( " SHOW KEYS FROM ` { $ table } ` WHERE Key_name='PRIMARY'; " , ARRAY_A ) ; if ( $ key_results === false ) { pb_backupbuddy :: status ( 'details' , 'Table `' . $ table . '` does not exist; skipping migration of this table.' ) ; return ; } if ( count ( $ key_results ) == 0 ) { pb_backupbuddy :: status ( 'message' , 'Error #9029: Warning only! Table `' . $ table . '` does not contain a primary key; BackupBuddy cannot safely modify the contents of this table. Skipping migration of this table. (serialized()).' ) ; return true ; } $ primary_key = $ key_results [ 0 ] [ 'Column_name' ] ; unset ( $ key_result ) ; $ updated = false ; $ rows_remain = true ; if ( '' == $ rows_start ) { $ rows_start = 0 ; } pb_backupbuddy :: status ( 'details' , 'Finding rows in table `' . $ table . '` to update.' ) ; while ( true === $ rows_remain ) { $ rowsResult = $ wpdb -> get_results ( " SELECT ` " . implode ( '`,`' , $ rows ) . " `,` { $ primary_key } ` FROM ` { $ table } ` LIMIT  " . $ rows_start . ',' . self :: MAX_ROWS_PER_SELECT ) ; $ rowsCount = count ( $ rowsResult ) ; 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 ( $ rowsResult as $ row ) { $ needs_update = false ; $ sql_update = array ( ) ; foreach ( $ row as $ column => $ value ) { if ( $ column != $ primary_key ) { if ( false !== ( $ edited_data = $ this -> replace_maybe_serialized ( $ value , $ olds , $ news ) ) ) { $ needs_update = true ; $ sql_update [ ] = $ column . " = ' " . backupbuddy_core :: dbEscape ( $ edited_data ) . " ' " ; } } else { $ primary_key_value = $ value ; } } if ( $ needs_update === true ) { $ updated = true ; $ wpdb -> query ( " UPDATE ` { $ table } ` SET  " . implode ( ',' , $ sql_update ) . "  WHERE ` { $ primary_key } ` = ' { $ primary_key_value } ' LIMIT 1 " ) ; } } unset ( $ rowsResult ) ; if ( ( ( microtime ( true ) - $ this -> startTime ) + $ this -> timeWiggleRoom ) >= $ this -> maxExecutionTime ) { return array ( $ rows_start ) ; } } if ( $ updated === true ) { pb_backupbuddy :: status ( 'details' , 'Updated serialized data in table `' . $ table . '`.' ) ; } else { pb_backupbuddy :: status ( 'details' , 'Nothing found to update in table `' . $ table . '`.' ) ; } return true ; }
serialized()
serialized ()
// 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 // Prevent trying to replace data with the same data for performance. // No primary key found; unsafe to edit this table. @since 2.2.32. // Was something in DB updated?
 // 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. // Data changed. // See how we are doing on time. Trigger chunking if needed.