This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,101 @@
<?php
namespace Yoast\WP\Lib\Migrations;
use Exception;
/**
* Yoast migrations column class.
*/
class Column {
/**
* The adapter.
*
* @var Adapter
*/
private $adapter;
/**
* The name.
*
* @var string
*/
public $name;
/**
* The type.
*
* @var mixed
*/
public $type;
/**
* The properties.
*
* @var mixed
*/
public $properties;
/**
* The options.
*
* @var array
*/
private $options = [];
/**
* Creates an instance of a column.
*
* @param Adapter $adapter The current adapter.
* @param string $name The name of the column.
* @param string $type The type of the column.
* @param array $options The column options.
*
* @throws Exception If invalid arguments provided.
*/
public function __construct( $adapter, $name, $type, $options = [] ) {
if ( ! $adapter instanceof Adapter ) {
throw new Exception( 'Invalid Adapter instance.' );
}
if ( empty( $name ) || ! \is_string( $name ) ) {
throw new Exception( "Invalid 'name' parameter" );
}
if ( empty( $type ) || ! \is_string( $type ) ) {
throw new Exception( "Invalid 'type' parameter" );
}
$this->adapter = $adapter;
$this->name = $name;
$this->type = $type;
$this->options = $options;
}
/**
* Returns the SQL of this column.
*
* @return string
*/
public function to_sql() {
$column_sql = \sprintf( '%s %s', $this->adapter->identifier( $this->name ), $this->sql_type() );
$column_sql .= $this->adapter->add_column_options( $this->type, $this->options );
return $column_sql;
}
/**
* The SQL string version.
*
* @return string
*/
public function __toString() {
return $this->to_sql();
}
/**
* The SQL type.
*
* @return string
*/
private function sql_type() {
return $this->adapter->type_to_sql( $this->type, $this->options );
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Yoast\WP\Lib\Migrations;
/**
* Yoast migrations constants class.
*/
class Constants {
const MYSQL_MAX_IDENTIFIER_LENGTH = 64;
const SQL_UNKNOWN_QUERY_TYPE = 1;
const SQL_SELECT = 2;
const SQL_INSERT = 4;
const SQL_UPDATE = 8;
const SQL_DELETE = 16;
const SQL_ALTER = 32;
const SQL_DROP = 64;
const SQL_CREATE = 128;
const SQL_SHOW = 256;
const SQL_RENAME = 512;
const SQL_SET = 1024;
}

View File

@ -0,0 +1,277 @@
<?php
namespace Yoast\WP\Lib\Migrations;
/**
* Base migration class.
*/
abstract class Migration {
/**
* The plugin this migration belongs to.
*
* @var string
*/
public static $plugin = 'unknown';
/**
* The adapter.
*
* @var Adapter
*/
private $adapter;
/**
* Performs the migration.
*
* @return void
*/
abstract public function up();
/**
* Reverts the migration.
*
* @return void
*/
abstract public function down();
/**
* Creates a new migration.
*
* @param Adapter $adapter The current adapter.
*/
public function __construct( Adapter $adapter ) {
$this->set_adapter( $adapter );
}
/**
* Sets an adapter.
*
* @param Adapter $adapter The adapter to set.
*
* @return $this|null
*/
public function set_adapter( $adapter ) {
if ( ! $adapter instanceof Adapter ) {
return;
}
$this->adapter = $adapter;
return $this;
}
/**
* Returns the current adapter.
*
* @return object
*/
public function get_adapter() {
return $this->adapter;
}
/**
* Creates a database.
*
* @param string $name The name of the database.
* @param array|null $options The options.
*
* @return bool
*/
public function create_database( $name, $options = null ) {
return $this->adapter->create_database( $name, $options );
}
/**
* Drops a database.
*
* @param string $name The name of the database.
*
* @return bool
*/
public function drop_database( $name ) {
return $this->adapter->drop_database( $name );
}
/**
* Drops a table.
*
* @param string $table_name The name of the table.
*
* @return bool
*/
public function drop_table( $table_name ) {
return $this->adapter->drop_table( $table_name );
}
/**
* Renames a table.
*
* @param string $name The name of the table.
* @param string $new_name The new name of the table.
*
* @return bool
*/
public function rename_table( $name, $new_name ) {
return $this->adapter->rename_table( $name, $new_name );
}
/**
* Renames a column.
*
* @param string $table_name The name of the table.
* @param string $column_name The column name.
* @param string $new_column_name The new column name.
*
* @return bool
*/
public function rename_column( $table_name, $column_name, $new_column_name ) {
return $this->adapter->rename_column( $table_name, $column_name, $new_column_name );
}
/**
* Adds a column.
*
* @param string $table_name The name of the table.
* @param string $column_name The column name.
* @param string $type The column type.
* @param array|string $options The options.
*
* @return bool
*/
public function add_column( $table_name, $column_name, $type, $options = [] ) {
return $this->adapter->add_column( $table_name, $column_name, $type, $options );
}
/**
* Removes a column.
*
* @param string $table_name The name of the table.
* @param string $column_name The column name.
*
* @return bool
*/
public function remove_column( $table_name, $column_name ) {
return $this->adapter->remove_column( $table_name, $column_name );
}
/**
* Changes a column.
*
* @param string $table_name The name of the table.
* @param string $column_name The column name.
* @param string $type The column type.
* @param array|string $options The options.
*
* @return bool
*/
public function change_column( $table_name, $column_name, $type, $options = [] ) {
return $this->adapter->change_column( $table_name, $column_name, $type, $options );
}
/**
* Adds an index.
*
* @param string $table_name The name of the table.
* @param array|string $column_name The column name.
* @param array|string $options The options.
*
* @return bool
*/
public function add_index( $table_name, $column_name, $options = [] ) {
return $this->adapter->add_index( $table_name, $column_name, $options );
}
/**
* Removes an index.
*
* @param string $table_name The name of the table.
* @param array|string $column_name The column name.
* @param array|string $options The options.
*
* @return bool
*/
public function remove_index( $table_name, $column_name, $options = [] ) {
return $this->adapter->remove_index( $table_name, $column_name, $options );
}
/**
* Adds timestamps.
*
* @param string $table_name The name of the table.
* @param string $created_column_name Created at column name.
* @param string $updated_column_name Updated at column name.
*
* @return bool
*/
public function add_timestamps( $table_name, $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) {
return $this->adapter->add_timestamps( $table_name, $created_column_name, $updated_column_name );
}
/**
* Removes timestamps.
*
* @param string $table_name The name of the table.
* @param string $created_column_name Created at column name.
* @param string $updated_column_name Updated at column name.
*
* @return bool
*/
public function remove_timestamps( $table_name, $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) {
return $this->adapter->remove_timestamps( $table_name, $created_column_name, $updated_column_name );
}
/**
* Creates a table.
*
* @param string $table_name The name of the table.
* @param array|string $options The options.
*
* @return bool|Table
*/
public function create_table( $table_name, $options = [] ) {
return $this->adapter->create_table( $table_name, $options );
}
/**
* Execute a query and return the first result.
*
* @param string $sql The query to run.
*
* @return array
*/
public function select_one( $sql ) {
return $this->adapter->select_one( $sql );
}
/**
* Execute a query and return all results.
*
* @param string $sql The query to run.
*
* @return array
*/
public function select_all( $sql ) {
return $this->adapter->select_all( $sql );
}
/**
* Execute a query.
*
* @param string $sql The query to run.
*
* @return bool
*/
public function query( $sql ) {
return $this->adapter->query( $sql );
}
/**
* Returns a quoted string.
*
* @param string $str The string to quote.
*
* @return string
*/
public function quote_string( $str ) {
return $this->adapter->quote_string( $str );
}
}

View File

@ -0,0 +1,256 @@
<?php
namespace Yoast\WP\Lib\Migrations;
use Exception;
/**
* Yoast migrations table class.
*/
class Table {
/**
* The adapter.
*
* @var Adapter
*/
private $adapter;
/**
* The name
*
* @var string
*/
private $name;
/**
* The options
*
* @var array
*/
private $options;
/**
* The SQL representation of this table.
*
* @var string
*/
private $sql = '';
/**
* Whether or not the table has been initialized.
*
* @var bool
*/
private $initialized = false;
/**
* The columns
*
* @var Column[]
*/
private $columns = [];
/**
* The primary keys.
*
* @var string[]
*/
private $primary_keys = [];
/**
* Whether or not to auto generate the id.
*
* @var bool
*/
private $auto_generate_id = true;
/**
* Creates an instance of the Adapter.
*
* @param Adapter $adapter The current adapter.
* @param string $name The table name.
* @param array $options The options.
*
* @throws Exception If invalid arguments are passed.
*/
public function __construct( $adapter, $name, $options = [] ) {
// Sanity checks.
if ( ! $adapter instanceof Adapter ) {
throw new Exception( 'Invalid MySQL Adapter instance.' );
}
if ( ! $name ) {
throw new Exception( "Invalid 'name' parameter" );
}
$this->adapter = $adapter;
$this->name = $name;
$this->options = $options;
$this->init_sql( $name, $options );
if ( \array_key_exists( 'id', $options ) ) {
if ( \is_bool( $options['id'] ) && $options['id'] === false ) {
$this->auto_generate_id = false;
}
// If its a string then we want to auto-generate an integer-based
// primary key with this name.
if ( \is_string( $options['id'] ) ) {
$this->auto_generate_id = true;
$this->primary_keys[] = $options['id'];
}
}
}
/**
* Create a column
*
* @param string $column_name The column name.
* @param string $type The column type.
* @param array $options The options.
*/
public function column( $column_name, $type, $options = [] ) {
// If there is already a column by the same name then silently fail and continue.
foreach ( $this->columns as $column ) {
if ( $column->name === $column_name ) {
return;
}
}
$column_options = [];
if ( \array_key_exists( 'primary_key', $options ) ) {
if ( $options['primary_key'] ) {
$this->primary_keys[] = $column_name;
}
}
if ( \array_key_exists( 'auto_increment', $options ) ) {
if ( $options['auto_increment'] ) {
$column_options['auto_increment'] = true;
}
}
$column_options = \array_merge( $column_options, $options );
$column = new Column( $this->adapter, $column_name, $type, $column_options );
$this->columns[] = $column;
}
/**
* Shortcut to create timestamps columns (default created_at, updated_at)
*
* @param string $created_column_name Created at column name.
* @param string $updated_column_name Updated at column name.
*/
public function timestamps( $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) {
$this->column( $created_column_name, 'datetime' );
$this->column(
$updated_column_name,
'timestamp',
[
'null' => false,
'default' => 'CURRENT_TIMESTAMP',
'extra' => 'ON UPDATE CURRENT_TIMESTAMP',
]
);
}
/**
* Get all primary keys
*
* @return string
*/
private function keys() {
if ( \count( $this->primary_keys ) > 0 ) {
$lead = ' PRIMARY KEY (';
$quoted = [];
foreach ( $this->primary_keys as $key ) {
$quoted[] = \sprintf( '%s', $this->adapter->identifier( $key ) );
}
$primary_key_sql = ",\n" . $lead . \implode( ',', $quoted ) . ')';
return $primary_key_sql;
}
return '';
}
/**
* Table definition
*
* @param bool $wants_sql Whether or not to return SQL or execute the query. Defaults to false.
*
* @return bool|string
*
* @throws Exception If the table definition has not been intialized.
*/
public function finish( $wants_sql = false ) {
if ( ! $this->initialized ) {
throw new Exception( \sprintf( "Table Definition: '%s' has not been initialized", $this->name ) );
}
$opt_str = '';
if ( \is_array( $this->options ) && \array_key_exists( 'options', $this->options ) ) {
$opt_str = $this->options['options'];
}
else {
if ( isset( $this->adapter->db_info['charset'] ) ) {
$opt_str = ' DEFAULT CHARSET=' . $this->adapter->db_info['charset'];
}
else {
$opt_str = ' DEFAULT CHARSET=utf8';
}
}
$close_sql = \sprintf( ') %s;', $opt_str );
$create_table_sql = $this->sql;
if ( $this->auto_generate_id === true ) {
$this->primary_keys[] = 'id';
$primary_id = new Column(
$this->adapter,
'id',
'integer',
[
'unsigned' => true,
'null' => false,
'auto_increment' => true,
]
);
$create_table_sql .= $primary_id->to_sql() . ",\n";
}
$create_table_sql .= $this->columns_to_str();
$create_table_sql .= $this->keys() . $close_sql;
if ( $wants_sql ) {
return $create_table_sql;
}
return $this->adapter->execute_ddl( $create_table_sql );
}
/**
* Get SQL for all columns.
*
* @return string The SQL.
*/
private function columns_to_str() {
$fields = [];
$len = \count( $this->columns );
for ( $i = 0; $i < $len; $i++ ) {
$c = $this->columns[ $i ];
$fields[] = $c->__toString();
}
return \implode( ",\n", $fields );
}
/**
* Init create sql statement.
*
* @param string $name The name.
* @param array $options The options.
*/
private function init_sql( $name, $options ) {
// Are we forcing table creation? If so, drop it first.
if ( \array_key_exists( 'force', $options ) && $options['force'] === true ) {
$this->adapter->drop_table( $name );
}
$temp = '';
if ( \array_key_exists( 'temporary', $options ) ) {
$temp = ' TEMPORARY';
}
$create_sql = \sprintf( 'CREATE%s TABLE ', $temp );
$create_sql .= \sprintf( "%s (\n", $this->adapter->identifier( $name ) );
$this->sql .= $create_sql;
$this->initialized = true;
}
}