Commit inicial powerservice
This commit is contained in:
+608
@@ -0,0 +1,608 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\RUCSS\Database\Queries;
|
||||
|
||||
use WP_Rocket\Dependencies\Database\Query;
|
||||
use WP_Rocket\Engine\Optimization\RUCSS\Database\Row\UsedCSS as UsedCSSRow;
|
||||
use WP_Rocket\Engine\Optimization\RUCSS\Database\Schemas\UsedCSS as UsedCSSSchema;
|
||||
|
||||
/**
|
||||
* RUCSS UsedCSS Query.
|
||||
*/
|
||||
class UsedCSS extends Query {
|
||||
|
||||
/**
|
||||
* Name of the database table to query.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table_name = 'wpr_rucss_used_css';
|
||||
|
||||
/**
|
||||
* String used to alias the database table in MySQL statement.
|
||||
*
|
||||
* Keep this short, but descriptive. I.E. "tr" for term relationships.
|
||||
*
|
||||
* This is used to avoid collisions with JOINs.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table_alias = 'wpr_rucss';
|
||||
|
||||
/**
|
||||
* Name of class used to setup the database schema.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table_schema = UsedCSSSchema::class;
|
||||
|
||||
/** Item ******************************************************************/
|
||||
|
||||
/**
|
||||
* Name for a single item.
|
||||
*
|
||||
* Use underscores between words. I.E. "term_relationship"
|
||||
*
|
||||
* This is used to automatically generate action hooks.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $item_name = 'used_css';
|
||||
|
||||
/**
|
||||
* Plural version for a group of items.
|
||||
*
|
||||
* Use underscores between words. I.E. "term_relationships"
|
||||
*
|
||||
* This is used to automatically generate action hooks.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $item_name_plural = 'used_css';
|
||||
|
||||
/**
|
||||
* Name of class used to turn IDs into first-class objects.
|
||||
*
|
||||
* This is used when looping through return values to guarantee their shape.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $item_shape = UsedCSSRow::class;
|
||||
|
||||
/**
|
||||
* Table status.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public static $table_exists = false;
|
||||
|
||||
/**
|
||||
* Get pending jobs.
|
||||
*
|
||||
* @param int $count Number of rows.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_pending_jobs( int $count = 100 ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$inprogress_count = $this->query(
|
||||
[
|
||||
'count' => true,
|
||||
'status' => 'in-progress',
|
||||
]
|
||||
);
|
||||
|
||||
if ( $inprogress_count >= $count ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->query(
|
||||
[
|
||||
'number' => ( $count - $inprogress_count ),
|
||||
'status' => 'pending',
|
||||
'fields' => [
|
||||
'id',
|
||||
'url',
|
||||
],
|
||||
'job_id__not_in' => [
|
||||
'not_in' => '',
|
||||
],
|
||||
'orderby' => 'modified',
|
||||
'order' => 'asc',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch on submit jobs.
|
||||
*
|
||||
* @param int $count Number of jobs to fetch.
|
||||
* @return array|int
|
||||
*/
|
||||
public function get_on_submit_jobs( int $count = 100 ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$in_progress_count = $this->query(
|
||||
[
|
||||
'count' => true,
|
||||
'status' => [ 'in-progress' ],
|
||||
]
|
||||
);
|
||||
$pending_count = $this->query(
|
||||
[
|
||||
'count' => true,
|
||||
'status' => [ 'pending' ],
|
||||
]
|
||||
);
|
||||
|
||||
$processing_count = $in_progress_count + $pending_count;
|
||||
|
||||
if ( 0 !== $count && $processing_count >= $count ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$query_params = [
|
||||
'status' => 'to-submit',
|
||||
'orderby' => 'modified',
|
||||
'order' => 'asc',
|
||||
];
|
||||
|
||||
if ( 0 !== $count ) {
|
||||
$query_params['number'] = ( $count - $processing_count );
|
||||
}
|
||||
|
||||
return $this->query( $query_params );
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment retries number and change status back to pending.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
* @param int $retries Current number of retries.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function increment_retries( $id, $retries = 0 ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$update_data = [
|
||||
'retries' => $retries + 1,
|
||||
'status' => 'pending',
|
||||
];
|
||||
return $this->update_item( $id, $update_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Job ID.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
* @param int $new_job_id new job id.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_job_id( $id, $new_job_id ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$update_data['job_id'] = $new_job_id;
|
||||
return $this->update_item( $id, $update_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new DB row for specific url.
|
||||
*
|
||||
* @param string $url Current page url.
|
||||
* @param string $job_id API job_id.
|
||||
* @param string $queue_name API Queue name.
|
||||
* @param bool $is_mobile if the request is for mobile page.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function create_new_job( string $url, string $job_id = '', string $queue_name = '', bool $is_mobile = false ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$item = [
|
||||
'url' => untrailingslashit( $url ),
|
||||
'is_mobile' => $is_mobile,
|
||||
'job_id' => $job_id,
|
||||
'queue_name' => $queue_name,
|
||||
'status' => 'to-submit',
|
||||
'retries' => 0,
|
||||
'last_accessed' => current_time( 'mysql', true ),
|
||||
'submitted_at' => current_time( 'mysql', true ),
|
||||
];
|
||||
return $this->add_item( $item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the status to be in-progress.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function make_status_inprogress( int $id ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'status' => 'in-progress',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the job and add new job_id pending.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
* @param string $job_id API job_id.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function reset_job( int $id, string $job_id = '' ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'job_id' => $job_id,
|
||||
'status' => 'to-submit',
|
||||
'error_code' => '',
|
||||
'error_message' => '',
|
||||
'retries' => 0,
|
||||
'modified' => current_time( 'mysql', true ),
|
||||
'submitted_at' => current_time( 'mysql', true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the status to be failed.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
* @param string $error_code error code.
|
||||
* @param string $error_message error message.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function make_status_failed( int $id, string $error_code, string $error_message ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'status' => 'failed',
|
||||
'error_code' => $error_code,
|
||||
'error_message' => current_time( 'mysql', true ) . " {$error_code}: {$error_message}",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete a job.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
* @param string $hash Hash.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function make_status_completed( int $id, string $hash = '' ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'hash' => $hash,
|
||||
'status' => 'completed',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Used CSS for specific url.
|
||||
*
|
||||
* @param string $url Page Url.
|
||||
* @param bool $is_mobile if the request is for mobile page.
|
||||
*
|
||||
* @return false|mixed
|
||||
*/
|
||||
public function get_row( string $url, bool $is_mobile = false ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = $this->query(
|
||||
[
|
||||
'url' => untrailingslashit( $url ),
|
||||
'is_mobile' => $is_mobile,
|
||||
]
|
||||
);
|
||||
|
||||
if ( empty( $query[0] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $query[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all rows with the same url (desktop and mobile versions).
|
||||
*
|
||||
* @param string $url Page url.
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
public function get_rows_by_url( string $url ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = $this->query(
|
||||
[
|
||||
'url' => untrailingslashit( $url ),
|
||||
]
|
||||
);
|
||||
|
||||
if ( empty( $query ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of rows with the same hash value.
|
||||
*
|
||||
* @param string $hash Hash.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count_rows_by_hash( string $hash ): int {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->query(
|
||||
[
|
||||
'hash' => $hash,
|
||||
'count' => true,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update UsedCSS Row last_accessed date to current date.
|
||||
*
|
||||
* @param int $id Used CSS id.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_last_accessed( int $id ): bool {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'last_accessed' => current_time( 'mysql', true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete DB row by url.
|
||||
*
|
||||
* @param string $url Page url to be deleted.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete_by_url( string $url ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$items = $this->get_rows_by_url( $url );
|
||||
|
||||
if ( ! $items ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$deleted = true;
|
||||
foreach ( $items as $item ) {
|
||||
$deleted = $deleted && $this->delete_item( $item->id );
|
||||
}
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of not completed rows.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_not_completed_count() {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->query(
|
||||
[
|
||||
'count' => true,
|
||||
'status__in' => [ 'pending', 'in-progress' ],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of completed rows.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_completed_count() {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->query(
|
||||
[
|
||||
'count' => true,
|
||||
'status' => 'completed',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all failed rows.
|
||||
*
|
||||
* @param float $delay delay before the urls are deleted.
|
||||
* @param string $unit unit from the delay.
|
||||
* @return array|false
|
||||
*/
|
||||
public function get_failed_rows( float $delay = 3, string $unit = 'days' ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = $this->query(
|
||||
[
|
||||
'status' => 'failed',
|
||||
'date_query' => [
|
||||
[
|
||||
'column' => 'modified',
|
||||
'before' => "$delay $unit ago",
|
||||
'inclusive' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
false
|
||||
);
|
||||
|
||||
if ( empty( $query ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revert status to pending.
|
||||
*
|
||||
* @param integer $id Used CSS id.
|
||||
* @return boolean
|
||||
*/
|
||||
public function revert_to_pending( int $id ): bool {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'error_code' => '',
|
||||
'error_message' => '',
|
||||
'retries' => 0,
|
||||
'status' => 'pending',
|
||||
'modified' => current_time( 'mysql', true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current status of `wpr_rucss_used_css` table; true if it exists, false otherwise.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
private function table_exists(): bool {
|
||||
|
||||
if ( self::$table_exists ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get the database interface.
|
||||
$db = $this->get_db();
|
||||
|
||||
// Bail if no database interface is available.
|
||||
if ( empty( $db ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Query statement.
|
||||
$query = 'SHOW TABLES LIKE %s';
|
||||
$like = $db->esc_like( $db->{$this->table_name} );
|
||||
$prepared = $db->prepare( $query, $like );
|
||||
$result = $db->get_var( $prepared );
|
||||
|
||||
// Does the table exist?
|
||||
$exists = $this->is_success( $result );
|
||||
|
||||
if ( $exists ) {
|
||||
self::$table_exists = $exists;
|
||||
}
|
||||
|
||||
return $exists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the error message.
|
||||
*
|
||||
* @param int $job_id Job ID.
|
||||
* @param int $code Response code.
|
||||
* @param string $message Response message.
|
||||
* @param string $previous_message Previous saved message.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_message( int $job_id, int $code, string $message, string $previous_message = '' ): bool {
|
||||
return $this->update_item(
|
||||
$job_id,
|
||||
[
|
||||
'error_message' => $previous_message . ' - ' . current_time( 'mysql', true ) . " {$code}: {$message}",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the status to be pending.
|
||||
*
|
||||
* @param int $id DB row ID.
|
||||
* @param string $job_id API job_id.
|
||||
* @param string $queue_name API Queue name.
|
||||
* @param bool $is_mobile if the request is for mobile page.
|
||||
* @return bool
|
||||
*/
|
||||
public function make_status_pending( int $id, string $job_id = '', string $queue_name = '', bool $is_mobile = false ) {
|
||||
if ( ! self::$table_exists && ! $this->table_exists() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update_item(
|
||||
$id,
|
||||
[
|
||||
'job_id' => $job_id,
|
||||
'queue_name' => $queue_name,
|
||||
'status' => 'pending',
|
||||
'is_mobile' => $is_mobile,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\RUCSS\Database\Row;
|
||||
|
||||
use WP_Rocket\Dependencies\Database\Row;
|
||||
|
||||
/**
|
||||
* RUCSS UsedCSS Row.
|
||||
*/
|
||||
class UsedCSS extends Row {
|
||||
/**
|
||||
* Row ID
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* URL
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* CSS
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $css;
|
||||
|
||||
/**
|
||||
* Hash storage value
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $hash;
|
||||
|
||||
/**
|
||||
* Error code
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $error_code;
|
||||
|
||||
/**
|
||||
* Error message
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $error_message;
|
||||
|
||||
/**
|
||||
* Number of retries
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $retries;
|
||||
|
||||
/**
|
||||
* Is CSS for mobile
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $is_mobile;
|
||||
|
||||
/**
|
||||
* Job ID
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $job_id;
|
||||
|
||||
/**
|
||||
* Job queue name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $queue_name;
|
||||
|
||||
/**
|
||||
* Status
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* Last modified time
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $modified;
|
||||
|
||||
/**
|
||||
* Last accessed time
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $last_accessed;
|
||||
|
||||
/**
|
||||
* Unused variable
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $unprocessedcss;
|
||||
|
||||
/**
|
||||
* Submitted date
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $submitted_at;
|
||||
|
||||
/**
|
||||
* UsedCSS constructor.
|
||||
*
|
||||
* @param mixed $item Object Row.
|
||||
*/
|
||||
public function __construct( $item ) {
|
||||
parent::__construct( $item );
|
||||
|
||||
// Set the type of each column, and prepare.
|
||||
$this->id = (int) $this->id;
|
||||
$this->url = (string) $this->url;
|
||||
$this->css = (string) $this->css;
|
||||
$this->hash = (string) $this->hash;
|
||||
$this->error_code = (string) $this->error_code;
|
||||
$this->error_message = (string) $this->error_message;
|
||||
$this->retries = (int) $this->retries;
|
||||
$this->is_mobile = (bool) $this->is_mobile;
|
||||
$this->job_id = (string) $this->job_id;
|
||||
$this->queue_name = (string) $this->queue_name;
|
||||
$this->status = (string) $this->status;
|
||||
$this->modified = empty( $this->modified ) ? 0 : strtotime( $this->modified );
|
||||
$this->last_accessed = empty( $this->last_accessed ) ? 0 : strtotime( $this->last_accessed );
|
||||
$this->submitted_at = empty( $this->submitted_at ) ? 0 : strtotime( $this->submitted_at );
|
||||
}
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\RUCSS\Database\Schemas;
|
||||
|
||||
use WP_Rocket\Dependencies\Database\Schema;
|
||||
|
||||
/**
|
||||
* RUCSS UsedCSS Schema.
|
||||
*/
|
||||
class UsedCSS extends Schema {
|
||||
|
||||
/**
|
||||
* Array of database column objects
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $columns = [
|
||||
|
||||
// ID column.
|
||||
[
|
||||
'name' => 'id',
|
||||
'type' => 'bigint',
|
||||
'length' => '20',
|
||||
'unsigned' => true,
|
||||
'extra' => 'auto_increment',
|
||||
'primary' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// URL column.
|
||||
[
|
||||
'name' => 'url',
|
||||
'type' => 'varchar',
|
||||
'length' => '2000',
|
||||
'default' => '',
|
||||
'cache_key' => true,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// CSS column.
|
||||
[
|
||||
'name' => 'css',
|
||||
'type' => 'longtext',
|
||||
'default' => null,
|
||||
'cache_key' => false,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// Hash column.
|
||||
[
|
||||
'name' => 'hash',
|
||||
'type' => 'varchar',
|
||||
'length' => '32',
|
||||
'default' => '',
|
||||
'cache_key' => false,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// error_code column.
|
||||
[
|
||||
'name' => 'error_code',
|
||||
'type' => 'varchar',
|
||||
'length' => '32',
|
||||
'default' => null,
|
||||
'cache_key' => false,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// error_message column.
|
||||
[
|
||||
'name' => 'error_message',
|
||||
'type' => 'longtext',
|
||||
'default' => null,
|
||||
'cache_key' => false,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// RETRIES column.
|
||||
[
|
||||
'name' => 'retries',
|
||||
'type' => 'tinyint',
|
||||
'length' => '1',
|
||||
'default' => 1,
|
||||
'cache_key' => false,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// IS_MOBILE column.
|
||||
[
|
||||
'name' => 'is_mobile',
|
||||
'type' => 'tinyint',
|
||||
'length' => '1',
|
||||
'default' => 0,
|
||||
'cache_key' => true,
|
||||
'searchable' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// JOB_ID column.
|
||||
[
|
||||
'name' => 'job_id',
|
||||
'type' => 'varchar',
|
||||
'length' => '255',
|
||||
'default' => null,
|
||||
'cache_key' => true,
|
||||
'searchable' => false,
|
||||
'sortable' => false,
|
||||
],
|
||||
|
||||
// QUEUE_NAME column.
|
||||
[
|
||||
'name' => 'queue_name',
|
||||
'type' => 'varchar',
|
||||
'length' => '255',
|
||||
'default' => null,
|
||||
'cache_key' => true,
|
||||
'searchable' => false,
|
||||
'sortable' => false,
|
||||
],
|
||||
|
||||
// STATUS column.
|
||||
[
|
||||
'name' => 'status',
|
||||
'type' => 'varchar',
|
||||
'length' => '255',
|
||||
'default' => null,
|
||||
'cache_key' => true,
|
||||
'searchable' => true,
|
||||
'sortable' => false,
|
||||
],
|
||||
|
||||
// MODIFIED column.
|
||||
[
|
||||
'name' => 'modified',
|
||||
'type' => 'timestamp',
|
||||
'default' => '0000-00-00 00:00:00',
|
||||
'created' => true,
|
||||
'date_query' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// LAST_ACCESSED column.
|
||||
[
|
||||
'name' => 'last_accessed',
|
||||
'type' => 'timestamp',
|
||||
'default' => '0000-00-00 00:00:00',
|
||||
'created' => true,
|
||||
'date_query' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
|
||||
// SUBMITTED_AT column.
|
||||
[
|
||||
'name' => 'submitted_at',
|
||||
'type' => 'timestamp',
|
||||
'default' => null,
|
||||
'created' => true,
|
||||
'date_query' => true,
|
||||
'sortable' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
+345
@@ -0,0 +1,345 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\RUCSS\Database\Tables;
|
||||
|
||||
use WP_Rocket\Dependencies\Database\Table;
|
||||
|
||||
/**
|
||||
* RUCSS UsedCSS Table.
|
||||
*/
|
||||
class UsedCSS extends Table {
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'wpr_rucss_used_css';
|
||||
|
||||
/**
|
||||
* Database version key (saved in _options or _sitemeta)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $db_version_key = 'wpr_rucss_used_css_version';
|
||||
|
||||
/**
|
||||
* Database version
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $version = 20231010;
|
||||
|
||||
/**
|
||||
* Key => value array of versions => methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $upgrades = [
|
||||
20220121 => 'add_async_rucss_columns',
|
||||
20220131 => 'make_status_column_index',
|
||||
20220513 => 'add_hash_column',
|
||||
20220920 => 'make_status_column_index_instead_queue_name',
|
||||
20221104 => 'add_error_columns',
|
||||
20231010 => 'add_submitted_at_column',
|
||||
];
|
||||
|
||||
/**
|
||||
* Instantiate class.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
add_action( 'admin_init', [ $this, 'maybe_trigger_recreate_table' ], 9 );
|
||||
add_action( 'init', [ $this, 'maybe_upgrade' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the database schema
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function set_schema() {
|
||||
$this->schema = "
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
url varchar(2000) NOT NULL default '',
|
||||
css longtext default NULL,
|
||||
hash varchar(32) default '',
|
||||
error_code varchar(32) NULL default NULL,
|
||||
error_message longtext NULL default NULL,
|
||||
unprocessedcss longtext NULL,
|
||||
retries tinyint(1) NOT NULL default 1,
|
||||
is_mobile tinyint(1) NOT NULL default 0,
|
||||
job_id varchar(255) NOT NULL default '',
|
||||
queue_name varchar(255) NOT NULL default '',
|
||||
status varchar(255) NOT NULL default '',
|
||||
modified timestamp NOT NULL default '0000-00-00 00:00:00',
|
||||
last_accessed timestamp NOT NULL default '0000-00-00 00:00:00',
|
||||
submitted_at timestamp NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY url (url(150), is_mobile),
|
||||
KEY modified (modified),
|
||||
KEY last_accessed (last_accessed),
|
||||
INDEX `status_index` (`status`(191)),
|
||||
INDEX `error_code_index` (`error_code`(32)),
|
||||
KEY hash (hash)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all used_css which were not accessed in the last month.
|
||||
*
|
||||
* @return bool|int
|
||||
*/
|
||||
public function delete_old_used_css() {
|
||||
// Get the database interface.
|
||||
$db = $this->get_db();
|
||||
|
||||
// Bail if no database interface is available.
|
||||
if ( empty( $db ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the old RUCSS deletion interval
|
||||
*
|
||||
* @param int $delete_interval Old RUCSS deletion interval in months
|
||||
*/
|
||||
$delete_interval = (int) apply_filters( 'rocket_rucss_delete_interval', 1 );
|
||||
|
||||
if ( $delete_interval <= 0 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prefixed_table_name = $this->apply_prefix( $this->table_name );
|
||||
$query = "DELETE FROM `$prefixed_table_name` WHERE `last_accessed` <= date_sub(now(), interval $delete_interval month)";
|
||||
$rows_affected = $db->query( $query );
|
||||
|
||||
return $rows_affected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all used_css which were not accessed in the last month.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_old_used_css(): array {
|
||||
// Get the database interface.
|
||||
$db = $this->get_db();
|
||||
|
||||
// Bail if no database interface is available.
|
||||
if ( empty( $db ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prefixed_table_name = $this->apply_prefix( $this->table_name );
|
||||
$query = "SELECT * FROM `$prefixed_table_name` WHERE `last_accessed` <= date_sub(now(), interval 1 month)";
|
||||
$rows_affected = $db->get_results( $query );
|
||||
|
||||
return $rows_affected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add queue columns.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function add_async_rucss_columns() {
|
||||
$jobid_column_exists = $this->column_exists( 'job_id' );
|
||||
$queuename_column_exists = $this->column_exists( 'queue_name' );
|
||||
$status_column_exists = $this->column_exists( 'status' );
|
||||
|
||||
$created = true;
|
||||
|
||||
if ( ! $jobid_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN job_id VARCHAR(255) NULL default '' AFTER is_mobile " );
|
||||
}
|
||||
if ( ! $queuename_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN queue_name VARCHAR(255) NULL default '' AFTER job_id " );
|
||||
}
|
||||
if ( ! $status_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN status VARCHAR(255) NULL default '' AFTER queue_name " );
|
||||
}
|
||||
|
||||
return $this->is_success( $created );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make status column as index.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function make_status_column_index() {
|
||||
$queuename_column_exists = $this->column_exists( 'queue_name' );
|
||||
if ( ! $queuename_column_exists ) {
|
||||
return $this->is_success( false );
|
||||
}
|
||||
|
||||
if ( $this->index_exists( 'queue_name_index' ) ) {
|
||||
return $this->is_success( true );
|
||||
}
|
||||
|
||||
$index_added = $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX `queue_name_index` (`queue_name`) " );
|
||||
return $this->is_success( $index_added );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hash column and index
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function add_hash_column() {
|
||||
$hash_column_exists = $this->column_exists( 'hash' );
|
||||
|
||||
$created = true;
|
||||
|
||||
if ( ! $hash_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN hash VARCHAR(32) NULL default '' AFTER css, ADD KEY hash (hash) " );
|
||||
}
|
||||
|
||||
return $this->is_success( $created );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make status column as index.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function make_status_column_index_instead_queue_name() {
|
||||
$queuename_column_exists = $this->column_exists( 'status' );
|
||||
if ( ! $queuename_column_exists ) {
|
||||
return $this->is_success( false );
|
||||
}
|
||||
|
||||
if ( $this->index_exists( 'status_index' ) ) {
|
||||
return $this->is_success( true );
|
||||
}
|
||||
|
||||
if ( $this->index_exists( 'queue_name_index' ) ) {
|
||||
if ( ! $this->get_db()->query( "ALTER TABLE {$this->table_name} DROP INDEX `queue_name_index`" ) ) {
|
||||
return $this->is_success( false );
|
||||
}
|
||||
}
|
||||
|
||||
$index_added = $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX `status_index` (`status`(191)) " );
|
||||
|
||||
return $this->is_success( $index_added );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all completed rows.
|
||||
*
|
||||
* @return bool|int
|
||||
*/
|
||||
public function remove_all_completed_rows() {
|
||||
// Get the database interface.
|
||||
$db = $this->get_db();
|
||||
|
||||
// Bail if no database interface is available.
|
||||
if ( empty( $db ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prefixed_table_name = $this->apply_prefix( $this->table_name );
|
||||
return $db->query( "DELETE FROM `$prefixed_table_name` WHERE status IN ( 'failed', 'completed' )" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns name from table.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return $this->apply_prefix( $this->table_name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger recreation of cache table if not exist.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_trigger_recreate_table() {
|
||||
if ( $this->exists() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
delete_option( $this->db_version_key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error columns
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function add_error_columns() {
|
||||
return $this->add_error_message_column() && $this->add_error_code_column() && $this->make_error_code_column_index();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error_message column and index
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function add_error_message_column() {
|
||||
$error_message_column_exists = $this->column_exists( 'error_message' );
|
||||
|
||||
$created = true;
|
||||
|
||||
if ( ! $error_message_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE `{$this->table_name}` ADD COLUMN error_message longtext NULL default NULL AFTER hash" );
|
||||
}
|
||||
|
||||
return $this->is_success( $created );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error_code column and index
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function add_error_code_column() {
|
||||
$error_code_column_exists = $this->column_exists( 'error_code' );
|
||||
|
||||
$created = true;
|
||||
|
||||
if ( ! $error_code_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE `{$this->table_name}` ADD COLUMN error_code VARCHAR(32) NULL default NULL AFTER hash" );
|
||||
}
|
||||
|
||||
return $this->is_success( $created );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make status column as index.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function make_error_code_column_index() {
|
||||
$error_code_column_exists = $this->column_exists( 'error_code' );
|
||||
if ( ! $error_code_column_exists ) {
|
||||
return $this->is_success( false );
|
||||
}
|
||||
|
||||
if ( $this->index_exists( 'error_code_index' ) ) {
|
||||
return $this->is_success( true );
|
||||
}
|
||||
|
||||
$index_added = $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX `error_code_index` (`error_code`) " );
|
||||
return $this->is_success( $index_added );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the submitted_at column
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function add_submitted_at_column() {
|
||||
$submitted_at_column_exists = $this->column_exists( 'submitted_at' );
|
||||
|
||||
$created = true;
|
||||
|
||||
if ( ! $submitted_at_column_exists ) {
|
||||
$created &= $this->get_db()->query( "ALTER TABLE `{$this->table_name}` ADD COLUMN submitted_at timestamp NULL AFTER last_accessed" );
|
||||
}
|
||||
|
||||
return $this->is_success( $created );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user