Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Database optimization to CLI #786

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
'cli/option.cls.php',
'cli/presets.cls.php',
'cli/purge.cls.php',
'cli/database.cls.php',

// 3rd party libraries
'lib/css-min/colors.cls.php',
Expand Down
210 changes: 210 additions & 0 deletions cli/database.cls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php

namespace LiteSpeed\CLI;

defined('WPINC') || exit();

use LiteSpeed\Debug2;
use LiteSpeed\DB_Optm;
use WP_CLI;

/**
* LiteSpeed Cache Database CLI
*/
class Database
{
private $__current_blog = false;
private $__db;

public function __construct()
{
Debug2::debug('CLI_Database init');

$this->__db = DB_Optm::cls();
}

/**
* List all site domains and ids on the network.
*/
public function network_list()
{
if (!is_multisite()) {
WP_CLI::error('This is not a multisite installation!');

return;
}
$buf = WP_CLI::colorize("%CThe list of installs:%n\n");

if (version_compare($GLOBALS['wp_version'], '4.6', '<')) {
$sites = wp_get_sites();
foreach ($sites as $site) {
$buf .= WP_CLI::colorize('%Y' . $site['domain'] . $site['path'] . ':%n ID ' . $site['blog_id']) . "\n";
}
} else {
$sites = get_sites();
foreach ($sites as $site) {
$buf .= WP_CLI::colorize('%Y' . $site->domain . $site->path . ':%n ID ' . $site->blog_id) . "\n";
}
}

WP_CLI::line($buf);
}

/**
* Change to blog sent as param.
*/
private function change_to_blog($args)
{
if (isset($args[0]) && $args[0] === 'blog') {
$this->__current_blog = get_current_blog_id();
$blogid = $args[1];
if (!is_numeric($blogid)) {
$error = WP_CLI::colorize('%RError: invalid blog id entered.%n');
WP_CLI::line($error);
$this->network_list($args);
return;
}
$site = get_blog_details($blogid);
if ($site === false) {
$error = WP_CLI::colorize('%RError: invalid blog id entered.%n');
WP_CLI::line($error);
$this->network_list($args);
return;
}
switch_to_blog($blogid);
}
}

/**
* Change to previous blog.
*/
private function change_to_default()
{
// Check if previous blog set.
if ($this->__current_blog) {
switch_to_blog($this->__current_blog);
// Switched to previous blog.
$this->__current_blog = false;
}
}

/**
* Show response.
*/
private function show_response($result, $action)
{
if ($result) {
WP_CLI::success($result);
} else {
WP_CLI::error("Error running optimization: " . $action);
}
}

/**
* Show response.
*/
private function clean_action($args, $types)
{
$this->change_to_blog($args);
foreach ($types as $type) {
$result = $this->__db->handler_clean_db_cli($type);
$this->show_response($result, $type);
}
$this->change_to_default();
}

/**
* Clear posts data(revisions, orphaned, auto drafts, trashed posts).
*
* # Start clearing posts data.
* $ wp litespeed-database clear_posts
* $ wp litespeed-database clear_posts blog 2
*/
public function clear_posts($args)
{
$types = [
'revision',
'orphaned_post_meta',
'auto_draft',
'trash_post'
];
$this->clean_action($args, $types);
}

/**
* Clear comments(spam and trash comments).
*
* # Start clearing comments.
* $ wp litespeed-database clear_comments
* $ wp litespeed-database clear_comments blog 2
*/
public function clear_comments($args)
{
$types = [
'spam_comment',
'trash_comment'
];
$this->clean_action($args, $types);
}

/**
* Clear trackbacks/pingbacks.
*
* # Start clearing trackbacks/pingbacks.
* $ wp litespeed-database clear_trackbacks
* $ wp litespeed-database clear_trackbacks blog 2
*/
public function clear_trackbacks($args)
{
$types = [
'trackback-pingback'
];
$this->clean_action($args, $types);
}

/**
* Clear transients.
*
* # Start clearing transients.
* $ wp litespeed-database clear_transients
* $ wp litespeed-database clear_transients blog 2
*/
public function clear_transients($args)
{
$types = [
'expired_transient',
'all_transients'
];
$this->clean_action($args, $types);
}

/**
* Optimize tables.
*
* # Start optimizing tables.
* $ wp litespeed-database optimize_tables
* $ wp litespeed-database optimize_tables blog 2
*/
public function optimize_tables($args)
{
$types = [
'optimize_tables'
];
$this->clean_action($args, $types);
}

/**
* Optimize database by running all possible opreations.
*
* # Start optimizing all.
* $ wp litespeed-database optimize_all
* $ wp litespeed-database optimize_all blog 2
*/
public function optimize_all($args)
{
$types = [
'all'
];
$this->clean_action($args, $types);
}
}
1 change: 1 addition & 0 deletions litespeed-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
WP_CLI::add_command('litespeed-debug', 'LiteSpeed\CLI\Debug');
WP_CLI::add_command('litespeed-presets', 'LiteSpeed\CLI\Presets');
WP_CLI::add_command('litespeed-crawler', 'LiteSpeed\CLI\Crawler');
WP_CLI::add_command('litespeed-database', 'LiteSpeed\CLI\Database');
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/db-optm.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,20 @@ public function handler()

Admin::redirect();
}

/**
* Clean DB
*
* @since 7.0
* @access public
*/
public function handler_clean_db_cli($args)
{
if (defined('WP_CLI') && WP_CLI) {
return $this->_db_clean($args);
}

return false;
}
}
=
Loading