Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../lib/migratio...
File: adapter.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\Lib\Migrations;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use Yoast\WP\Lib\Model;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Yoast migrations adapter class.
[8] Fix | Delete
*/
[9] Fix | Delete
class Adapter {
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* The version of this adapter.
[13] Fix | Delete
*
[14] Fix | Delete
* @var string
[15] Fix | Delete
*/
[16] Fix | Delete
private $version = '1.0';
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Whether or not a transaction has been started.
[20] Fix | Delete
*
[21] Fix | Delete
* @var bool
[22] Fix | Delete
*/
[23] Fix | Delete
private $in_transaction = false;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Returns the current database name.
[27] Fix | Delete
*
[28] Fix | Delete
* @return string
[29] Fix | Delete
*/
[30] Fix | Delete
public function get_database_name() {
[31] Fix | Delete
global $wpdb;
[32] Fix | Delete
[33] Fix | Delete
return $wpdb->dbname;
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* Checks support for migrations.
[38] Fix | Delete
*
[39] Fix | Delete
* @return bool
[40] Fix | Delete
*/
[41] Fix | Delete
public function supports_migrations() {
[42] Fix | Delete
return true;
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* Returns all column native types.
[47] Fix | Delete
*
[48] Fix | Delete
* @return array
[49] Fix | Delete
*/
[50] Fix | Delete
public function native_database_types() {
[51] Fix | Delete
$types = [
[52] Fix | Delete
'primary_key' => [
[53] Fix | Delete
'name' => 'integer',
[54] Fix | Delete
'limit' => 11,
[55] Fix | Delete
'null' => false,
[56] Fix | Delete
],
[57] Fix | Delete
'string' => [
[58] Fix | Delete
'name' => 'varchar',
[59] Fix | Delete
'limit' => 255,
[60] Fix | Delete
],
[61] Fix | Delete
'text' => [ 'name' => 'text' ],
[62] Fix | Delete
'tinytext' => [ 'name' => 'tinytext' ],
[63] Fix | Delete
'mediumtext' => [ 'name' => 'mediumtext' ],
[64] Fix | Delete
'integer' => [
[65] Fix | Delete
'name' => 'int',
[66] Fix | Delete
'limit' => 11,
[67] Fix | Delete
],
[68] Fix | Delete
'tinyinteger' => [ 'name' => 'tinyint' ],
[69] Fix | Delete
'smallinteger' => [ 'name' => 'smallint' ],
[70] Fix | Delete
'mediuminteger' => [ 'name' => 'mediumint' ],
[71] Fix | Delete
'biginteger' => [ 'name' => 'bigint' ],
[72] Fix | Delete
'float' => [ 'name' => 'float' ],
[73] Fix | Delete
'decimal' => [
[74] Fix | Delete
'name' => 'decimal',
[75] Fix | Delete
'scale' => 0,
[76] Fix | Delete
'precision' => 10,
[77] Fix | Delete
],
[78] Fix | Delete
'datetime' => [ 'name' => 'datetime' ],
[79] Fix | Delete
'timestamp' => [ 'name' => 'timestamp' ],
[80] Fix | Delete
'time' => [ 'name' => 'time' ],
[81] Fix | Delete
'date' => [ 'name' => 'date' ],
[82] Fix | Delete
'binary' => [ 'name' => 'blob' ],
[83] Fix | Delete
'tinybinary' => [ 'name' => 'tinyblob' ],
[84] Fix | Delete
'mediumbinary' => [ 'name' => 'mediumblob' ],
[85] Fix | Delete
'longbinary' => [ 'name' => 'longblob' ],
[86] Fix | Delete
'boolean' => [
[87] Fix | Delete
'name' => 'tinyint',
[88] Fix | Delete
'limit' => 1,
[89] Fix | Delete
],
[90] Fix | Delete
'enum' => [
[91] Fix | Delete
'name' => 'enum',
[92] Fix | Delete
'values' => [],
[93] Fix | Delete
],
[94] Fix | Delete
'uuid' => [
[95] Fix | Delete
'name' => 'char',
[96] Fix | Delete
'limit' => 36,
[97] Fix | Delete
],
[98] Fix | Delete
'char' => [ 'name' => 'char' ],
[99] Fix | Delete
];
[100] Fix | Delete
[101] Fix | Delete
return $types;
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Checks if a table exists.
[106] Fix | Delete
*
[107] Fix | Delete
* @param string $table The table name.
[108] Fix | Delete
*
[109] Fix | Delete
* @return bool
[110] Fix | Delete
*/
[111] Fix | Delete
public function has_table( $table ) {
[112] Fix | Delete
return $this->table_exists( $table );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Allows overriding the hardcoded schema table name constant in case of parallel migrations.
[117] Fix | Delete
*
[118] Fix | Delete
* @return string
[119] Fix | Delete
*/
[120] Fix | Delete
public function get_schema_version_table_name() {
[121] Fix | Delete
return Model::get_table_name( 'migrations' );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Create the schema table, if necessary.
[126] Fix | Delete
*/
[127] Fix | Delete
public function create_schema_version_table() {
[128] Fix | Delete
if ( ! $this->has_table( $this->get_schema_version_table_name() ) ) {
[129] Fix | Delete
$t = $this->create_table( $this->get_schema_version_table_name() );
[130] Fix | Delete
$t->column( 'version', 'string', [ 'limit' => 191 ] );
[131] Fix | Delete
$t->finish();
[132] Fix | Delete
$this->add_index( $this->get_schema_version_table_name(), 'version', [ 'unique' => true ] );
[133] Fix | Delete
}
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
/**
[137] Fix | Delete
* Starts a transaction.
[138] Fix | Delete
*/
[139] Fix | Delete
public function start_transaction() {
[140] Fix | Delete
if ( $this->in_transaction() === false ) {
[141] Fix | Delete
$this->begin_transaction();
[142] Fix | Delete
}
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Commits a transaction.
[147] Fix | Delete
*/
[148] Fix | Delete
public function commit_transaction() {
[149] Fix | Delete
if ( $this->in_transaction() ) {
[150] Fix | Delete
$this->commit();
[151] Fix | Delete
}
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Rollbacks a transaction.
[156] Fix | Delete
*/
[157] Fix | Delete
public function rollback_transaction() {
[158] Fix | Delete
if ( $this->in_transaction() ) {
[159] Fix | Delete
$this->rollback();
[160] Fix | Delete
}
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Quotes a table name string.
[165] Fix | Delete
*
[166] Fix | Delete
* @param string $string Table name.
[167] Fix | Delete
*
[168] Fix | Delete
* @return string
[169] Fix | Delete
*/
[170] Fix | Delete
public function quote_table( $string ) {
[171] Fix | Delete
return '`' . $string . '`';
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Return the SQL definition of a column.
[176] Fix | Delete
*
[177] Fix | Delete
* @param string $column_name The column name.
[178] Fix | Delete
* @param string $type The type of the column.
[179] Fix | Delete
* @param array|null $options Column options.
[180] Fix | Delete
*
[181] Fix | Delete
* @return string
[182] Fix | Delete
*/
[183] Fix | Delete
public function column_definition( $column_name, $type, $options = null ) {
[184] Fix | Delete
$col = new Column( $this, $column_name, $type, $options );
[185] Fix | Delete
[186] Fix | Delete
return $col->__toString();
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Checks if a database exists.
[191] Fix | Delete
*
[192] Fix | Delete
* @param string $database The database name.
[193] Fix | Delete
*
[194] Fix | Delete
* @return bool
[195] Fix | Delete
*/
[196] Fix | Delete
public function database_exists( $database ) {
[197] Fix | Delete
$ddl = 'SHOW DATABASES';
[198] Fix | Delete
$result = $this->select_all( $ddl );
[199] Fix | Delete
if ( \count( $result ) === 0 ) {
[200] Fix | Delete
return false;
[201] Fix | Delete
}
[202] Fix | Delete
foreach ( $result as $dbrow ) {
[203] Fix | Delete
if ( $dbrow['Database'] === $database ) {
[204] Fix | Delete
return true;
[205] Fix | Delete
}
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
return false;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/**
[212] Fix | Delete
* Creates a database.
[213] Fix | Delete
*
[214] Fix | Delete
* @param string $db The database name.
[215] Fix | Delete
*
[216] Fix | Delete
* @return bool
[217] Fix | Delete
*/
[218] Fix | Delete
public function create_database( $db ) {
[219] Fix | Delete
if ( $this->database_exists( $db ) ) {
[220] Fix | Delete
return false;
[221] Fix | Delete
}
[222] Fix | Delete
$ddl = \sprintf( 'CREATE DATABASE %s', $this->identifier( $db ) );
[223] Fix | Delete
$result = $this->query( $ddl );
[224] Fix | Delete
[225] Fix | Delete
return $result === true;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
/**
[229] Fix | Delete
* Drops a database.
[230] Fix | Delete
*
[231] Fix | Delete
* @param string $db The database name.
[232] Fix | Delete
*
[233] Fix | Delete
* @return bool
[234] Fix | Delete
*/
[235] Fix | Delete
public function drop_database( $db ) {
[236] Fix | Delete
if ( ! $this->database_exists( $db ) ) {
[237] Fix | Delete
return false;
[238] Fix | Delete
}
[239] Fix | Delete
$ddl = \sprintf( 'DROP DATABASE IF EXISTS %s', $this->identifier( $db ) );
[240] Fix | Delete
$result = $this->query( $ddl );
[241] Fix | Delete
[242] Fix | Delete
return $result === true;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Checks if a table exists.
[247] Fix | Delete
*
[248] Fix | Delete
* @param string $table The table name.
[249] Fix | Delete
*
[250] Fix | Delete
* @return bool
[251] Fix | Delete
*/
[252] Fix | Delete
public function table_exists( $table ) {
[253] Fix | Delete
global $wpdb;
[254] Fix | Delete
[255] Fix | Delete
// We need last error to be clear so we can check against it easily.
[256] Fix | Delete
$previous_last_error = $wpdb->last_error;
[257] Fix | Delete
$previous_suppress_errors = $wpdb->suppress_errors;
[258] Fix | Delete
$wpdb->last_error = '';
[259] Fix | Delete
$wpdb->suppress_errors = true;
[260] Fix | Delete
[261] Fix | Delete
$result = $wpdb->query( "SELECT * FROM $table LIMIT 1" );
[262] Fix | Delete
[263] Fix | Delete
// Restore the last error, as this is not truly an error and we don't want to alarm people.
[264] Fix | Delete
$wpdb->last_error = $previous_last_error;
[265] Fix | Delete
$wpdb->suppress_errors = $previous_suppress_errors;
[266] Fix | Delete
[267] Fix | Delete
return $result !== false;
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
/**
[271] Fix | Delete
* Wrapper to execute a query.
[272] Fix | Delete
*
[273] Fix | Delete
* @param string $query The query to run.
[274] Fix | Delete
*
[275] Fix | Delete
* @return bool
[276] Fix | Delete
*/
[277] Fix | Delete
public function execute( $query ) {
[278] Fix | Delete
return $this->query( $query );
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
/**
[282] Fix | Delete
* Executes a query.
[283] Fix | Delete
*
[284] Fix | Delete
* @param string $query The query to run.
[285] Fix | Delete
*
[286] Fix | Delete
* @return bool Whether or not the query was performed succesfully.
[287] Fix | Delete
*/
[288] Fix | Delete
public function query( $query ) {
[289] Fix | Delete
global $wpdb;
[290] Fix | Delete
[291] Fix | Delete
$query_type = $this->determine_query_type( $query );
[292] Fix | Delete
$data = [];
[293] Fix | Delete
if ( $query_type === Constants::SQL_SELECT || $query_type === Constants::SQL_SHOW ) {
[294] Fix | Delete
$data = $wpdb->get_results( $query, ARRAY_A );
[295] Fix | Delete
if ( $data === false ) {
[296] Fix | Delete
return false;
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
return $data;
[300] Fix | Delete
}
[301] Fix | Delete
else {
[302] Fix | Delete
// INSERT, DELETE, etc...
[303] Fix | Delete
$result = $wpdb->query( $query );
[304] Fix | Delete
if ( $result === false ) {
[305] Fix | Delete
return false;
[306] Fix | Delete
}
[307] Fix | Delete
if ( $query_type === Constants::SQL_INSERT ) {
[308] Fix | Delete
return $wpdb->insert_id;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
return true;
[312] Fix | Delete
}
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Returns a single result for a query.
[317] Fix | Delete
*
[318] Fix | Delete
* @param string $query The query to run.
[319] Fix | Delete
*
[320] Fix | Delete
* @return array|false An associative array of the result.
[321] Fix | Delete
*/
[322] Fix | Delete
public function select_one( $query ) {
[323] Fix | Delete
global $wpdb;
[324] Fix | Delete
[325] Fix | Delete
$query_type = $this->determine_query_type( $query );
[326] Fix | Delete
if ( $query_type === Constants::SQL_SELECT || $query_type === Constants::SQL_SHOW ) {
[327] Fix | Delete
$result = $wpdb->query( $query );
[328] Fix | Delete
if ( $result === false ) {
[329] Fix | Delete
return false;
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
return $wpdb->last_result[0];
[333] Fix | Delete
}
[334] Fix | Delete
[335] Fix | Delete
return false;
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
/**
[339] Fix | Delete
* Returns all results for a query.
[340] Fix | Delete
*
[341] Fix | Delete
* @param string $query The query to run.
[342] Fix | Delete
*
[343] Fix | Delete
* @return array An array of associative arrays.
[344] Fix | Delete
*/
[345] Fix | Delete
public function select_all( $query ) {
[346] Fix | Delete
return $this->query( $query );
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
/**
[350] Fix | Delete
* Use this method for non-SELECT queries.
[351] Fix | Delete
* Or anything where you dont necessarily expect a result string, e.g. DROPs, CREATEs, etc.
[352] Fix | Delete
*
[353] Fix | Delete
* @param string $ddl The query to run.
[354] Fix | Delete
*
[355] Fix | Delete
* @return bool
[356] Fix | Delete
*/
[357] Fix | Delete
public function execute_ddl( $ddl ) {
[358] Fix | Delete
return $this->query( $ddl );
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
/**
[362] Fix | Delete
* Drops a table
[363] Fix | Delete
*
[364] Fix | Delete
* @param string $table The table name.
[365] Fix | Delete
*
[366] Fix | Delete
* @return bool Whether or not the table was succesfully dropped.
[367] Fix | Delete
*/
[368] Fix | Delete
public function drop_table( $table ) {
[369] Fix | Delete
$ddl = \sprintf( 'DROP TABLE IF EXISTS %s', $this->identifier( $table ) );
[370] Fix | Delete
return $this->query( $ddl );
[371] Fix | Delete
}
[372] Fix | Delete
[373] Fix | Delete
/**
[374] Fix | Delete
* Creates a table.
[375] Fix | Delete
*
[376] Fix | Delete
* @param string $table_name The table name.
[377] Fix | Delete
* @param array $options The options.
[378] Fix | Delete
*
[379] Fix | Delete
* @return Table
[380] Fix | Delete
*/
[381] Fix | Delete
public function create_table( $table_name, $options = [] ) {
[382] Fix | Delete
return new Table( $this, $table_name, $options );
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
/**
[386] Fix | Delete
* Escapes a string for usage in queries.
[387] Fix | Delete
*
[388] Fix | Delete
* @param string $string The string.
[389] Fix | Delete
*
[390] Fix | Delete
* @return string
[391] Fix | Delete
*/
[392] Fix | Delete
public function quote_string( $string ) {
[393] Fix | Delete
global $wpdb;
[394] Fix | Delete
[395] Fix | Delete
return $wpdb->_escape( $string );
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
/**
[399] Fix | Delete
* Returns a quoted string.
[400] Fix | Delete
*
[401] Fix | Delete
* @param string $string The string.
[402] Fix | Delete
*
[403] Fix | Delete
* @return string
[404] Fix | Delete
*/
[405] Fix | Delete
public function identifier( $string ) {
[406] Fix | Delete
return '`' . $string . '`';
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
/**
[410] Fix | Delete
* Renames a table.
[411] Fix | Delete
*
[412] Fix | Delete
* @param string $name The current table name.
[413] Fix | Delete
* @param string $new_name The new table name.
[414] Fix | Delete
*
[415] Fix | Delete
* @return bool
[416] Fix | Delete
*/
[417] Fix | Delete
public function rename_table( $name, $new_name ) {
[418] Fix | Delete
if ( empty( $name ) || empty( $new_name ) ) {
[419] Fix | Delete
return false;
[420] Fix | Delete
}
[421] Fix | Delete
$sql = \sprintf( 'RENAME TABLE %s TO %s', $this->identifier( $name ), $this->identifier( $new_name ) );
[422] Fix | Delete
[423] Fix | Delete
return $this->execute_ddl( $sql );
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
/**
[427] Fix | Delete
* Adds a column.
[428] Fix | Delete
*
[429] Fix | Delete
* @param string $table_name The table name.
[430] Fix | Delete
* @param string $column_name The column name.
[431] Fix | Delete
* @param string $type The column type.
[432] Fix | Delete
* @param array $options Column options.
[433] Fix | Delete
*
[434] Fix | Delete
* @return bool
[435] Fix | Delete
*/
[436] Fix | Delete
public function add_column( $table_name, $column_name, $type, $options = [] ) {
[437] Fix | Delete
if ( empty( $table_name ) || empty( $column_name ) || empty( $type ) ) {
[438] Fix | Delete
return false;
[439] Fix | Delete
}
[440] Fix | Delete
// Default types.
[441] Fix | Delete
if ( ! \array_key_exists( 'limit', $options ) ) {
[442] Fix | Delete
$options['limit'] = null;
[443] Fix | Delete
}
[444] Fix | Delete
if ( ! \array_key_exists( 'precision', $options ) ) {
[445] Fix | Delete
$options['precision'] = null;
[446] Fix | Delete
}
[447] Fix | Delete
if ( ! \array_key_exists( 'scale', $options ) ) {
[448] Fix | Delete
$options['scale'] = null;
[449] Fix | Delete
}
[450] Fix | Delete
$sql = \sprintf( 'ALTER TABLE %s ADD `%s` %s', $this->identifier( $table_name ), $column_name, $this->type_to_sql( $type, $options ) );
[451] Fix | Delete
$sql .= $this->add_column_options( $type, $options );
[452] Fix | Delete
[453] Fix | Delete
return $this->execute_ddl( $sql );
[454] Fix | Delete
}
[455] Fix | Delete
[456] Fix | Delete
/**
[457] Fix | Delete
* Drops a column.
[458] Fix | Delete
*
[459] Fix | Delete
* @param string $table_name The table name.
[460] Fix | Delete
* @param string $column_name The column name.
[461] Fix | Delete
*
[462] Fix | Delete
* @return bool
[463] Fix | Delete
*/
[464] Fix | Delete
public function remove_column( $table_name, $column_name ) {
[465] Fix | Delete
$sql = \sprintf( 'ALTER TABLE %s DROP COLUMN %s', $this->identifier( $table_name ), $this->identifier( $column_name ) );
[466] Fix | Delete
[467] Fix | Delete
return $this->execute_ddl( $sql );
[468] Fix | Delete
}
[469] Fix | Delete
[470] Fix | Delete
/**
[471] Fix | Delete
* Renames a column.
[472] Fix | Delete
*
[473] Fix | Delete
* @param string $table_name The table name.
[474] Fix | Delete
* @param string $column_name The column name.
[475] Fix | Delete
* @param string $new_column_name The new column name.
[476] Fix | Delete
*
[477] Fix | Delete
* @return bool
[478] Fix | Delete
*/
[479] Fix | Delete
public function rename_column( $table_name, $column_name, $new_column_name ) {
[480] Fix | Delete
if ( empty( $table_name ) || empty( $column_name ) || empty( $new_column_name ) ) {
[481] Fix | Delete
return false;
[482] Fix | Delete
}
[483] Fix | Delete
$column_info = $this->column_info( $table_name, $column_name );
[484] Fix | Delete
$current_type = $column_info['type'];
[485] Fix | Delete
$sql = \sprintf( 'ALTER TABLE %s CHANGE %s %s %s', $this->identifier( $table_name ), $this->identifier( $column_name ), $this->identifier( $new_column_name ), $current_type );
[486] Fix | Delete
$sql .= $this->add_column_options( $current_type, $column_info );
[487] Fix | Delete
[488] Fix | Delete
return $this->execute_ddl( $sql );
[489] Fix | Delete
}
[490] Fix | Delete
[491] Fix | Delete
/**
[492] Fix | Delete
* Changes a column.
[493] Fix | Delete
*
[494] Fix | Delete
* @param string $table_name The table name.
[495] Fix | Delete
* @param string $column_name The column name.
[496] Fix | Delete
* @param string $type The column type.
[497] Fix | Delete
* @param array $options Column options.
[498] Fix | Delete
*
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function