Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../lib/migratio...
File: table.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\Lib\Migrations;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Yoast migrations table class.
[7] Fix | Delete
*/
[8] Fix | Delete
class Table {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* The adapter.
[12] Fix | Delete
*
[13] Fix | Delete
* @var Adapter
[14] Fix | Delete
*/
[15] Fix | Delete
private $adapter;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* The name
[19] Fix | Delete
*
[20] Fix | Delete
* @var string
[21] Fix | Delete
*/
[22] Fix | Delete
private $name;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* The options
[26] Fix | Delete
*
[27] Fix | Delete
* @var array
[28] Fix | Delete
*/
[29] Fix | Delete
private $options;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* The SQL representation of this table.
[33] Fix | Delete
*
[34] Fix | Delete
* @var string
[35] Fix | Delete
*/
[36] Fix | Delete
private $sql = '';
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Whether or not the table has been initialized.
[40] Fix | Delete
*
[41] Fix | Delete
* @var bool
[42] Fix | Delete
*/
[43] Fix | Delete
private $initialized = false;
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* The columns
[47] Fix | Delete
*
[48] Fix | Delete
* @var Column[]
[49] Fix | Delete
*/
[50] Fix | Delete
private $columns = [];
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* The primary keys.
[54] Fix | Delete
*
[55] Fix | Delete
* @var string[]
[56] Fix | Delete
*/
[57] Fix | Delete
private $primary_keys = [];
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Whether or not to auto generate the id.
[61] Fix | Delete
*
[62] Fix | Delete
* @var bool
[63] Fix | Delete
*/
[64] Fix | Delete
private $auto_generate_id = true;
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Creates an instance of the Adapter.
[68] Fix | Delete
*
[69] Fix | Delete
* @param Adapter $adapter The current adapter.
[70] Fix | Delete
* @param string $name The table name.
[71] Fix | Delete
* @param array $options The options.
[72] Fix | Delete
*
[73] Fix | Delete
* @throws Exception If invalid arguments are passed.
[74] Fix | Delete
*/
[75] Fix | Delete
public function __construct( $adapter, $name, $options = [] ) {
[76] Fix | Delete
// Sanity checks.
[77] Fix | Delete
if ( ! $adapter instanceof Adapter ) {
[78] Fix | Delete
throw new Exception( 'Invalid MySQL Adapter instance.' );
[79] Fix | Delete
}
[80] Fix | Delete
if ( ! $name ) {
[81] Fix | Delete
throw new Exception( "Invalid 'name' parameter" );
[82] Fix | Delete
}
[83] Fix | Delete
$this->adapter = $adapter;
[84] Fix | Delete
$this->name = $name;
[85] Fix | Delete
$this->options = $options;
[86] Fix | Delete
$this->init_sql( $name, $options );
[87] Fix | Delete
if ( \array_key_exists( 'id', $options ) ) {
[88] Fix | Delete
if ( \is_bool( $options['id'] ) && $options['id'] === false ) {
[89] Fix | Delete
$this->auto_generate_id = false;
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
// If its a string then we want to auto-generate an integer-based
[93] Fix | Delete
// primary key with this name.
[94] Fix | Delete
if ( \is_string( $options['id'] ) ) {
[95] Fix | Delete
$this->auto_generate_id = true;
[96] Fix | Delete
$this->primary_keys[] = $options['id'];
[97] Fix | Delete
}
[98] Fix | Delete
}
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
/**
[102] Fix | Delete
* Create a column
[103] Fix | Delete
*
[104] Fix | Delete
* @param string $column_name The column name.
[105] Fix | Delete
* @param string $type The column type.
[106] Fix | Delete
* @param array $options The options.
[107] Fix | Delete
*/
[108] Fix | Delete
public function column( $column_name, $type, $options = [] ) {
[109] Fix | Delete
// If there is already a column by the same name then silently fail and continue.
[110] Fix | Delete
foreach ( $this->columns as $column ) {
[111] Fix | Delete
if ( $column->name === $column_name ) {
[112] Fix | Delete
return;
[113] Fix | Delete
}
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
$column_options = [];
[117] Fix | Delete
if ( \array_key_exists( 'primary_key', $options ) ) {
[118] Fix | Delete
if ( $options['primary_key'] ) {
[119] Fix | Delete
$this->primary_keys[] = $column_name;
[120] Fix | Delete
}
[121] Fix | Delete
}
[122] Fix | Delete
if ( \array_key_exists( 'auto_increment', $options ) ) {
[123] Fix | Delete
if ( $options['auto_increment'] ) {
[124] Fix | Delete
$column_options['auto_increment'] = true;
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
$column_options = \array_merge( $column_options, $options );
[128] Fix | Delete
$column = new Column( $this->adapter, $column_name, $type, $column_options );
[129] Fix | Delete
$this->columns[] = $column;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Shortcut to create timestamps columns (default created_at, updated_at)
[134] Fix | Delete
*
[135] Fix | Delete
* @param string $created_column_name Created at column name.
[136] Fix | Delete
* @param string $updated_column_name Updated at column name.
[137] Fix | Delete
*/
[138] Fix | Delete
public function timestamps( $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) {
[139] Fix | Delete
$this->column( $created_column_name, 'datetime' );
[140] Fix | Delete
$this->column(
[141] Fix | Delete
$updated_column_name,
[142] Fix | Delete
'timestamp',
[143] Fix | Delete
[
[144] Fix | Delete
'null' => false,
[145] Fix | Delete
'default' => 'CURRENT_TIMESTAMP',
[146] Fix | Delete
'extra' => 'ON UPDATE CURRENT_TIMESTAMP',
[147] Fix | Delete
]
[148] Fix | Delete
);
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* Get all primary keys
[153] Fix | Delete
*
[154] Fix | Delete
* @return string
[155] Fix | Delete
*/
[156] Fix | Delete
private function keys() {
[157] Fix | Delete
if ( \count( $this->primary_keys ) > 0 ) {
[158] Fix | Delete
$lead = ' PRIMARY KEY (';
[159] Fix | Delete
$quoted = [];
[160] Fix | Delete
foreach ( $this->primary_keys as $key ) {
[161] Fix | Delete
$quoted[] = \sprintf( '%s', $this->adapter->identifier( $key ) );
[162] Fix | Delete
}
[163] Fix | Delete
$primary_key_sql = ",\n" . $lead . \implode( ',', $quoted ) . ')';
[164] Fix | Delete
return $primary_key_sql;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
return '';
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
/**
[171] Fix | Delete
* Table definition
[172] Fix | Delete
*
[173] Fix | Delete
* @param bool $wants_sql Whether or not to return SQL or execute the query. Defaults to false.
[174] Fix | Delete
*
[175] Fix | Delete
* @return bool|string
[176] Fix | Delete
*
[177] Fix | Delete
* @throws Exception If the table definition has not been intialized.
[178] Fix | Delete
*/
[179] Fix | Delete
public function finish( $wants_sql = false ) {
[180] Fix | Delete
if ( ! $this->initialized ) {
[181] Fix | Delete
throw new Exception( \sprintf( "Table Definition: '%s' has not been initialized", $this->name ) );
[182] Fix | Delete
}
[183] Fix | Delete
$opt_str = '';
[184] Fix | Delete
if ( \is_array( $this->options ) && \array_key_exists( 'options', $this->options ) ) {
[185] Fix | Delete
$opt_str = $this->options['options'];
[186] Fix | Delete
}
[187] Fix | Delete
else {
[188] Fix | Delete
if ( isset( $this->adapter->db_info['charset'] ) ) {
[189] Fix | Delete
$opt_str = ' DEFAULT CHARSET=' . $this->adapter->db_info['charset'];
[190] Fix | Delete
}
[191] Fix | Delete
else {
[192] Fix | Delete
$opt_str = ' DEFAULT CHARSET=utf8';
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
$close_sql = \sprintf( ') %s;', $opt_str );
[196] Fix | Delete
$create_table_sql = $this->sql;
[197] Fix | Delete
if ( $this->auto_generate_id === true ) {
[198] Fix | Delete
$this->primary_keys[] = 'id';
[199] Fix | Delete
$primary_id = new Column(
[200] Fix | Delete
$this->adapter,
[201] Fix | Delete
'id',
[202] Fix | Delete
'integer',
[203] Fix | Delete
[
[204] Fix | Delete
'unsigned' => true,
[205] Fix | Delete
'null' => false,
[206] Fix | Delete
'auto_increment' => true,
[207] Fix | Delete
]
[208] Fix | Delete
);
[209] Fix | Delete
$create_table_sql .= $primary_id->to_sql() . ",\n";
[210] Fix | Delete
}
[211] Fix | Delete
$create_table_sql .= $this->columns_to_str();
[212] Fix | Delete
$create_table_sql .= $this->keys() . $close_sql;
[213] Fix | Delete
if ( $wants_sql ) {
[214] Fix | Delete
return $create_table_sql;
[215] Fix | Delete
}
[216] Fix | Delete
return $this->adapter->execute_ddl( $create_table_sql );
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Get SQL for all columns.
[221] Fix | Delete
*
[222] Fix | Delete
* @return string The SQL.
[223] Fix | Delete
*/
[224] Fix | Delete
private function columns_to_str() {
[225] Fix | Delete
$str = '';
[226] Fix | Delete
$fields = [];
[227] Fix | Delete
$len = \count( $this->columns );
[228] Fix | Delete
for ( $i = 0; $i < $len; $i++ ) {
[229] Fix | Delete
$c = $this->columns[ $i ];
[230] Fix | Delete
$fields[] = $c->__toString();
[231] Fix | Delete
}
[232] Fix | Delete
return \implode( ",\n", $fields );
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Init create sql statement.
[237] Fix | Delete
*
[238] Fix | Delete
* @param string $name The name.
[239] Fix | Delete
* @param array $options The options.
[240] Fix | Delete
*/
[241] Fix | Delete
private function init_sql( $name, $options ) {
[242] Fix | Delete
// Are we forcing table creation? If so, drop it first.
[243] Fix | Delete
if ( \array_key_exists( 'force', $options ) && $options['force'] === true ) {
[244] Fix | Delete
$this->adapter->drop_table( $name );
[245] Fix | Delete
}
[246] Fix | Delete
$temp = '';
[247] Fix | Delete
if ( \array_key_exists( 'temporary', $options ) ) {
[248] Fix | Delete
$temp = ' TEMPORARY';
[249] Fix | Delete
}
[250] Fix | Delete
$create_sql = \sprintf( 'CREATE%s TABLE ', $temp );
[251] Fix | Delete
$create_sql .= \sprintf( "%s (\n", $this->adapter->identifier( $name ) );
[252] Fix | Delete
$this->sql .= $create_sql;
[253] Fix | Delete
$this->initialized = true;
[254] Fix | Delete
}
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function