Repository navigation
Expand file tree
/
Copy pathSQLiteDatabaseIntegrationLoader.php
More file actions
142 lines (123 loc) · 5.82 KB
/
Copy pathSQLiteDatabaseIntegrationLoader.php
File metadata and controls
142 lines (123 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Automattic\WP_CLI\SQLite;
use WP_CLI;
final class SQLiteDatabaseIntegrationLoader {
/**
* Get the version of the SQLite integration plugin if it is installed
* and activated.
*
* @return false|string The version of the SQLite integration plugin or false if not found/activated.
*/
public static function get_plugin_version() {
// Check if there is a db.php file in the wp-content directory.
if ( ! file_exists( ABSPATH . '/wp-content/db.php' ) ) {
return false;
}
// If the file is found, we need to check that it is the sqlite integration plugin.
$plugin_file = file_get_contents( ABSPATH . '/wp-content/db.php' );
if ( ! preg_match( '/define\( \'SQLITE_DB_DROPIN_VERSION\', \'([0-9.]+)\' \)/', $plugin_file ) ) {
return false;
}
$plugin_path = self::get_plugin_directory();
if ( ! $plugin_path ) {
return false;
}
// Try to get the version number from readme.txt
$plugin_file = file_get_contents( $plugin_path . '/readme.txt' );
preg_match( '/^Stable tag:\s*?(.+)$/m', $plugin_file, $matches );
return isset( $matches[1] ) ? trim( $matches[1] ) : false;
}
/**
* Find the directory where the SQLite integration plugin is installed.
*
* @return string|null The directory where the SQLite integration plugin is installed or null if not found.
*/
protected static function get_plugin_directory() {
$plugin_folders = [
ABSPATH . '/wp-content/plugins/sqlite-database-integration',
ABSPATH . '/wp-content/mu-plugins/sqlite-database-integration',
];
foreach ( $plugin_folders as $folder ) {
if ( file_exists( $folder ) && is_dir( $folder ) ) {
return $folder;
}
}
return null;
}
/**
* Load the necessary classes from the SQLite integration plugin.
*
* @return void
* @throws WP_CLI\ExitException
*/
public static function load_plugin() {
$plugin_directory = self::get_plugin_directory();
if ( ! $plugin_directory ) {
WP_CLI::error( 'Could not locate the SQLite integration plugin.' );
}
$sqlite_plugin_version = self::get_plugin_version();
if ( ! $sqlite_plugin_version ) {
WP_CLI::error( 'Could not determine the version of the SQLite integration plugin.' );
}
if ( version_compare( $sqlite_plugin_version, '2.1.11', '<' ) ) {
WP_CLI::error( 'The SQLite integration plugin must be version 2.1.11 or higher.' );
}
// Load the translator class from the plugin.
if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) {
define( 'SQLITE_DB_DROPIN_VERSION', $sqlite_plugin_version ); // phpcs:ignore
}
$old_structure = file_exists( $plugin_directory . '/php-polyfills.php' );
if ( $old_structure ) {
require_once $plugin_directory . '/php-polyfills.php';
}
try {
require_once $plugin_directory . '/constants.php';
} catch ( \Throwable $exception ) {
// Newer plugin releases reject invalid database settings.
WP_CLI::error( 'Could not open the SQLite database: ' . $exception->getMessage() );
}
$storage_file = $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-storage.php';
if ( file_exists( $storage_file ) ) {
require_once $storage_file;
}
$mysql_on_sqlite_class_file = $plugin_directory . '/wp-includes/database/sqlite/class-wp-mysql-on-sqlite.php';
$mysql_on_sqlite_loader_file = $plugin_directory . '/wp-includes/database/load.php';
// The loader also exists in older plugin releases without WP_MySQL_On_SQLite.
// Select the current API automatically only when both files are available;
// otherwise use the compatibility paths below.
if ( file_exists( $mysql_on_sqlite_class_file ) && file_exists( $mysql_on_sqlite_loader_file ) ) {
require_once $mysql_on_sqlite_loader_file;
return;
}
$new_driver_enabled = defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
if ( $new_driver_enabled && file_exists( $plugin_directory . '/wp-pdo-mysql-on-sqlite.php' ) ) {
require_once $plugin_directory . '/wp-pdo-mysql-on-sqlite.php';
} elseif ( $new_driver_enabled && file_exists( $mysql_on_sqlite_loader_file ) ) {
require_once $mysql_on_sqlite_loader_file;
} elseif ( $new_driver_enabled ) {
require_once $plugin_directory . '/version.php';
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-grammar.php';
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser.php';
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-node.php';
require_once $plugin_directory . '/wp-includes/parser/class-wp-parser-token.php';
require_once $plugin_directory . '/wp-includes/mysql/class-wp-mysql-token.php';
require_once $plugin_directory . '/wp-includes/mysql/class-wp-mysql-lexer.php';
require_once $plugin_directory . '/wp-includes/mysql/class-wp-mysql-parser.php';
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-connection.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-configurator.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-exception.php';
require_once $plugin_directory . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';
} else {
$sqlite = $plugin_directory . '/wp-includes/sqlite';
require_once "$sqlite/class-wp-sqlite-lexer.php";
require_once "$sqlite/class-wp-sqlite-query-rewriter.php";
require_once "$sqlite/class-wp-sqlite-translator.php";
require_once "$sqlite/class-wp-sqlite-token.php";
require_once "$sqlite/class-wp-sqlite-pdo-user-defined-functions.php";
}
}
}