* This file is part of the ManageWP Worker plugin.
* (c) ManageWP LLC <contact@managewp.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
class MWP_Action_IncrementalBackup_GetViewSchema
const ERROR_VIEW_NOT_FOUND = 1;
public function execute(array $params = array())
$views = $params['views'];
foreach ($views as $view) {
if (!$this->viewExists($view)) {
'error' => self::ERROR_VIEW_NOT_FOUND,
$createTableSql = $this->getCreateViewSql($view);
'createViewSql' => $createTableSql,
private function viewExists($table)
$exists = (int) mwp_context()->getDb()->get_var(
sprintf('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "%s" AND table_name = "%s"',
mwp_context()->escapeParameter(mwp_context()->getDbName()),
mwp_context()->escapeParameter($table))
private function getCreateViewSql($table)
$createTableResults = mwp_context()->getDb()->get_results(
sprintf('SHOW CREATE VIEW `%s`', $this->escapeTableName($table)),
return isset($createTableResults[0]['Create View']) ? $createTableResults[0]['Create View'] : null;
* Escape backticks (`) in table names.
* A table can contain a backtick character (`) and it has to be escaped with another backtick.
* e.g. asd`asd should be converted into asd``asd.
* The resulting SQL query should look like: SELECT * FROM `asd``asd`;
private function escapeTableName($table)
return str_replace('`', '``', $table);