From c8acf14b8d28b47fc00ba4c7783a33d8a6cc7122 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 5 Apr 2023 10:55:27 +0300 Subject: [PATCH] backport more changes --- modules/database/sqlite/LICENSE | 339 ++++++++++ modules/database/sqlite/activate.php | 213 ++---- modules/database/sqlite/admin-notices.php | 73 +++ modules/database/sqlite/admin-page.php | 122 ++++ modules/database/sqlite/admin.php | 84 --- modules/database/sqlite/can-load.php | 36 - modules/database/sqlite/constants.php | 11 +- modules/database/sqlite/db.copy | 59 ++ modules/database/sqlite/db.copy.php | 33 - modules/database/sqlite/deactivate.php | 39 +- .../{site-health.php => health-check.php} | 36 +- modules/database/sqlite/load.php | 27 +- modules/database/sqlite/readme.txt | 40 ++ ...s-wp-sqlite-pdo-user-defined-functions.php | 16 +- .../sqlite/class-wp-sqlite-translator.php | 620 ++++++++++++++++-- 15 files changed, 1339 insertions(+), 409 deletions(-) create mode 100644 modules/database/sqlite/LICENSE create mode 100644 modules/database/sqlite/admin-notices.php create mode 100644 modules/database/sqlite/admin-page.php delete mode 100644 modules/database/sqlite/admin.php delete mode 100644 modules/database/sqlite/can-load.php create mode 100644 modules/database/sqlite/db.copy delete mode 100644 modules/database/sqlite/db.copy.php rename modules/database/sqlite/{site-health.php => health-check.php} (70%) create mode 100644 modules/database/sqlite/readme.txt diff --git a/modules/database/sqlite/LICENSE b/modules/database/sqlite/LICENSE new file mode 100644 index 0000000000..d159169d10 --- /dev/null +++ b/modules/database/sqlite/LICENSE @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/modules/database/sqlite/activate.php b/modules/database/sqlite/activate.php index 71a6fe985d..e5bf29c5b8 100644 --- a/modules/database/sqlite/activate.php +++ b/modules/database/sqlite/activate.php @@ -1,17 +1,66 @@ base ) && 'settings_page_sqlite-integration' === $current_screen->base ) { + return; + } + if ( isset( $_GET['confirm-install'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'sqlite-install' ) ) { + sqlite_plugin_copy_db_file(); // phpcs:ignore PHPCompatibility.Extensions.RemovedExtensions + // WordPress will automatically redirect to the install screen here. + wp_redirect( admin_url() ); + exit; + } +} +add_action( 'admin_init', 'sqlite_activation' ); + +// Flush the cache at the last moment before the redirect. +add_filter( + 'x_redirect_by', + function ( $result ) { + wp_cache_flush(); + return $result; + }, + PHP_INT_MAX, + 1 +); + +/** + * Add the db.php file in wp-content. + * + * When the plugin gets merged in wp-core, this is not to be ported. */ -return function() { +function sqlite_plugin_copy_db_file() { // Bail early if the SQLite3 class does not exist. if ( ! class_exists( 'SQLite3' ) ) { return; @@ -21,7 +70,7 @@ // Place database drop-in if not present yet, except in case there is // another database drop-in present already. - if ( ! defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) { + if ( ! defined( 'SQLITE_DB_DROPIN_VERSION' ) && ! file_exists( $destination ) ) { // Init the filesystem to allow copying the file. global $wp_filesystem; @@ -32,148 +81,18 @@ // Get the db.copy.php file contents, replace placeholders and write it to the destination. $file_contents = str_replace( - '{SQLITE_IMPLEMENTATION_FOLDER_PATH}', - __DIR__, - file_get_contents( __DIR__ . '/db.copy.php' ) + array( + '{SQLITE_IMPLEMENTATION_FOLDER_PATH}', + '{SQLITE_PLUGIN}', + ), + array( + __DIR__, + str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ), + ), + file_get_contents( __DIR__ . '/db.copy' ) ); $wp_filesystem->put_contents( $destination, $file_contents ); } } - - // Do not automatically set up SQLite DB on a multisite as that is notably - // more complex. Potentially it can be added in the future. - if ( is_multisite() ) { - return; - } - - // As an extra safety check, bail if the current user cannot update - // (or install) WordPress core. - if ( ! current_user_can( 'update_core' ) ) { - return; - } - - // Otherwise install WordPress in the SQLite database before the redirect, - // so that the user does not end up on the WordPress installation screen - // but rather remains in the Performance Lab modules screen. - add_filter( - 'wp_redirect', - function( $redirect_location ) { - if ( ! defined( 'DB_ENGINE' ) ) { - define( 'DB_ENGINE', 'sqlite' ); - } - require_once __DIR__ . '/constants.php'; - - // If the SQLite DB already exists, simply ensure the module is - // active there. - if ( file_exists( FQDB ) ) { - require_once __DIR__ . '/wp-includes/sqlite/db.php'; - wp_set_wpdb_vars(); - global $wpdb; - $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", PERFLAB_MODULES_SETTING ) ); - if ( is_object( $row ) ) { - $value = maybe_unserialize( $row->option_value ); - if ( is_array( $value ) && ( ! isset( $value['database/sqlite'] ) || ! $value['database/sqlite']['enabled'] ) ) { - $value['database/sqlite'] = array( 'enabled' => true ); - $wpdb->update( $wpdb->options, array( 'option_value' => maybe_serialize( $value ) ), array( 'option_name' => PERFLAB_MODULES_SETTING ) ); - } - } - return $redirect_location; - } - - // Get current basic setup data to install WordPress in the new DB. - $blog_title = get_bloginfo( 'name' ); - $is_public = (bool) get_option( 'blog_public' ); - $language = get_option( 'WPLANG' ); - $admin_email = get_option( 'admin_email' ); - $admin_user = get_user_by( 'email', $admin_email ); - if ( ! $admin_user ) { - $admin_user = wp_get_current_user(); - } - - // Look up the data for the current user (in case they're not the - // admin email user). Additionally, attempt to keep them - // logged-in by retaining their current session. Depending on the - // site configuration, this is not 100% reliable as sites may store - // session tokens outside of user meta. However that does not lead - // to any problem, the user would simply be required to sign in - // again. - $current_user = wp_get_current_user(); - $current_user_sessions = get_user_meta( $current_user->ID, 'session_tokens', true ); - - // Get current data to keep the Performance Lab plugin and relevant - // modules active in the new DB. - $active_plugins_option = array( plugin_basename( PERFLAB_MAIN_FILE ) ); - $active_modules_option = get_option( PERFLAB_MODULES_SETTING, array() ); - - // Load and set up SQLite database. - require_once __DIR__ . '/wp-includes/sqlite/db.php'; - wp_set_wpdb_vars(); - - // Load WordPress installation API functions. - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; - - // Install WordPress in the SQLite database with the same base - // configuration as the MySQL database. - // Since $admin_user->user_pass is already hashed, add a filter to - // ensure it is inserted into the database like that, instead of - // being re-hashed. - $unhash_user_pass = function( $data, $update, $user_id, $userdata ) use ( $admin_user, $current_user ) { - // Double check this is actually the already hashed password, - // to prevent any chance of accidentally putting another - // password into the database which would then be plain text. - if ( - ! empty( $userdata['user_pass'] ) - && ( - $userdata['user_pass'] === $admin_user->user_pass - || $userdata['user_pass'] === $current_user->user_pass - ) - ) { - $data['user_pass'] = $userdata['user_pass']; - } - return $data; - }; - add_filter( 'wp_pre_insert_user_data', $unhash_user_pass, 10, 4 ); - wp_install( $blog_title, $admin_user->user_login, $admin_user->user_email, $is_public, '', $admin_user->user_pass, $language ); - // If the current user is not the admin user, create an account for that user too. - if ( $current_user->user_login !== $admin_user->user_login ) { - wp_create_user( $current_user->user_login, $current_user->user_pass, $current_user->user_email ); - } - remove_filter( 'wp_pre_insert_user_data', $unhash_user_pass ); - - // If the admin email was originally not attached to any user - // (e.g. when the original admin email user was deleted), reset - // the option to the original admin email again (by default - // `wp_install()` will set it to the same email as the initial - // admin user email). - if ( 0 !== strcasecmp( $admin_user->user_email, $admin_email ) ) { - update_option( 'admin_email', $admin_email ); - } - - // If user sessions are found, migrate them over so that the - // current user remains logged in. - if ( $current_user_sessions ) { - $session_user = get_user_by( 'login', $current_user->user_login ); - if ( $session_user ) { - update_user_meta( $session_user->ID, 'session_tokens', $current_user_sessions ); - } - } - - // Activate the Performance Lab plugin and its modules. - // Use direct database query for Performance Lab modules to - // prevent module activation logic from firing again. - update_option( 'active_plugins', $active_plugins_option ); - global $wpdb; - $wpdb->insert( - $wpdb->options, - array( - 'option_name' => PERFLAB_MODULES_SETTING, - 'option_value' => maybe_serialize( $active_modules_option ), - 'autoload' => 'yes', - ) - ); - - return $redirect_location; - } - ); -}; +} diff --git a/modules/database/sqlite/admin-notices.php b/modules/database/sqlite/admin-notices.php new file mode 100644 index 0000000000..36f7e5e232 --- /dev/null +++ b/modules/database/sqlite/admin-notices.php @@ -0,0 +1,73 @@ +base ) && 'settings_page_sqlite-integration' === $current_screen->base ) { + return; + } + + // If SQLite is not detected, bail early. + if ( ! class_exists( 'SQLite3' ) ) { + printf( + '

%s

', + esc_html__( 'The SQLite Integration plugin is active, but the SQLite3 class is missing from your server. Please make sure that SQLite is enabled in your PHP installation.', 'performance-lab' ) + ); + return; + } + + /* + * If the SQLITE_DB_DROPIN_VERSION constant is not defined + * but there's a db.php file in the wp-content directory, then the module can't be activated. + * The module should not have been activated in the first place + * (there's a check in the can-load.php file), but this is a fallback check. + */ + if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'SQLITE_DB_DROPIN_VERSION' ) ) { + printf( + '

%s

', + sprintf( + /* translators: 1: SQLITE_DB_DROPIN_VERSION constant, 2: db.php drop-in path */ + __( 'The SQLite Integration module is active, but the %1$s constant is missing. It appears you already have another %2$s file present on your site. ', 'performance-lab' ), + 'SQLITE_DB_DROPIN_VERSION', + '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' + ) + ); + + return; + } + + if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ) { + return; + } + + if ( ! wp_is_writable( WP_CONTENT_DIR ) ) { + printf( + '

%s

', + esc_html__( 'The SQLite Integration plugin is active, but the wp-content/db.php file is missing and the wp-content directory is not writable. Please ensure the wp-content folder is writable, then deactivate the plugin and try again.', 'performance-lab' ) + ); + return; + } + // The dropin db.php is missing. + printf( + '

%s

', + sprintf( + /* translators: 1: db.php drop-in path, 2: Admin URL to deactivate the module */ + __( 'The SQLite Integration plugin is active, but the %1$s file is missing. Please deactivate the plugin and re-activate it to try again.', 'performance-lab' ), + '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php', + esc_url( admin_url( 'plugins.php' ) ) + ) + ); +} +add_action( 'admin_notices', 'sqlite_plugin_admin_notice' ); // Add the admin notices. diff --git a/modules/database/sqlite/admin-page.php b/modules/database/sqlite/admin-page.php new file mode 100644 index 0000000000..44704f60e1 --- /dev/null +++ b/modules/database/sqlite/admin-page.php @@ -0,0 +1,122 @@ + +
+

+
+ +
+ +
+

+
+

+ deactivate the plugin. Alternatively, you can manually delete the %2$s file from your server.', 'performance-lab' ), + esc_url( admin_url( 'plugins.php' ) ), + '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' + ); + ?> +

+ + +
+

+
+ +
+

+ ' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' + ); + ?> +

+
+ +
+

+ ' . esc_html( basename( WP_CONTENT_DIR ) ) . '' + ); + ?> +

+
+ +
+

+
+

+

+

+ + + + +
+ ' . __( 'Database: SQLite', 'performance-lab' ) . ''; + } elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) { + $title = '' . __( 'Database: MariaDB', 'performance-lab' ) . ''; + } else { + $title = '' . __( 'Database: MySQL', 'performance-lab' ) . ''; + } + + $args = array( + 'id' => 'sqlite-db-integration', + 'parent' => 'top-secondary', + 'title' => $title, + 'href' => esc_url( admin_url( 'options-general.php?page=sqlite-integration' ) ), + 'meta' => false, + ); + $admin_bar->add_node( $args ); +} +add_action( 'admin_bar_menu', 'sqlite_plugin_adminbar_item', 999 ); diff --git a/modules/database/sqlite/admin.php b/modules/database/sqlite/admin.php deleted file mode 100644 index 571b8f43c1..0000000000 --- a/modules/database/sqlite/admin.php +++ /dev/null @@ -1,84 +0,0 @@ -

%s

', - sprintf( - /* translators: 1: PERFLAB_SQLITE_DB_DROPIN_VERSION constant, 2: db.php drop-in path */ - __( 'The SQLite Integration module is active, but the %1$s constant is missing. It appears you already have another %2$s file present on your site. ', 'performance-lab' ), - 'PERFLAB_SQLITE_DB_DROPIN_VERSION', - '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php' - ) - ); - - return; - } - - // The dropin db.php is missing. - printf( - '

%s

', - sprintf( - /* translators: 1: db.php drop-in path, 2: Admin URL to deactivate the module */ - __( 'The SQLite Integration module is active, but the %1$s file is missing. Please deactivate the module and re-activate it to try again.', 'performance-lab' ), - '' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php', - esc_url( admin_url( 'options-general.php?page=' . PERFLAB_MODULES_SCREEN ) ) - ) - ); -} -add_action( 'admin_notices', 'perflab_sqlite_plugin_admin_notice' ); // Add the admin notices. - -/** - * Adds a link to the admin bar. - * - * @since 2.0.0 - * - * @global wpdb $wpdb WordPress database abstraction object. - * - * @param WP_Admin_Bar $admin_bar The admin bar object. - */ -function perflab_sqlite_plugin_adminbar_item( $admin_bar ) { - global $wpdb; - - if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) && defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) { - $title = '' . __( 'Database: SQLite', 'performance-lab' ) . ''; - } elseif ( stripos( $wpdb->db_server_info(), 'maria' ) !== false ) { - $title = '' . __( 'Database: MariaDB', 'performance-lab' ) . ''; - } else { - $title = '' . __( 'Database: MySQL', 'performance-lab' ) . ''; - } - - $args = array( - 'id' => 'performance-lab-sqlite', - 'parent' => 'top-secondary', - 'title' => $title, - 'href' => esc_url( admin_url( 'options-general.php?page=' . PERFLAB_MODULES_SCREEN ) ), - 'meta' => false, - ); - $admin_bar->add_node( $args ); -} -add_action( 'admin_bar_menu', 'perflab_sqlite_plugin_adminbar_item', 999 ); diff --git a/modules/database/sqlite/can-load.php b/modules/database/sqlite/can-load.php deleted file mode 100644 index d375c1f181..0000000000 --- a/modules/database/sqlite/can-load.php +++ /dev/null @@ -1,36 +0,0 @@ -delete( WP_CONTENT_DIR . '/db.php' ); + // Flush the cache again to mitigate a possible race condition. + wp_cache_flush(); } // Run an action on `shutdown`, to deactivate the option in the MySQL database. @@ -42,14 +47,22 @@ function() { $wpdb_mysql->set_prefix( $table_prefix ); // Get the perflab options, remove the database/sqlite module and update the option. - $row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT option_value FROM $wpdb_mysql->options WHERE option_name = %s LIMIT 1", PERFLAB_MODULES_SETTING ) ); + $row = $wpdb_mysql->get_row( $wpdb_mysql->prepare( "SELECT option_value FROM $wpdb_mysql->options WHERE option_name = %s LIMIT 1", 'active_plugins' ) ); if ( is_object( $row ) ) { $value = maybe_unserialize( $row->option_value ); - if ( is_array( $value ) && isset( $value['database/sqlite'] ) ) { - unset( $value['database/sqlite'] ); - $wpdb_mysql->update( $wpdb_mysql->options, array( 'option_value' => maybe_serialize( $value ) ), array( 'option_name' => PERFLAB_MODULES_SETTING ) ); + if ( is_array( $value ) ) { + $value_flipped = array_flip( $value ); + $items = array_reverse( explode( DIRECTORY_SEPARATOR, SQLITE_MAIN_FILE ) ); + $item = $items[1] . DIRECTORY_SEPARATOR . $items[0]; + unset( $value_flipped[ $item ] ); + $value = array_flip( $value_flipped ); + $wpdb_mysql->update( $wpdb_mysql->options, array( 'option_value' => maybe_serialize( $value ) ), array( 'option_name' => 'active_plugins' ) ); } } - } + }, + PHP_INT_MAX ); -}; + // Flush any persistent cache. + wp_cache_flush(); +} +register_deactivation_hook( SQLITE_MAIN_FILE, 'sqlite_plugin_remove_db_file' ); // Remove db.php file on plugin deactivation. diff --git a/modules/database/sqlite/site-health.php b/modules/database/sqlite/health-check.php similarity index 70% rename from modules/database/sqlite/site-health.php rename to modules/database/sqlite/health-check.php index 8e04e290cf..b89a5e6923 100644 --- a/modules/database/sqlite/site-health.php +++ b/modules/database/sqlite/health-check.php @@ -1,26 +1,20 @@ 'dayofmonth', 'unix_timestamp' => 'unix_timestamp', 'now' => 'now', - 'char_length' => 'char_length', 'md5' => 'md5', 'curdate' => 'curdate', 'rand' => 'rand', @@ -135,17 +134,6 @@ public function curdate() { return gmdate( 'Y-m-d' ); } - /** - * Method to emulate MySQL CHAR_LENGTH() function. - * - * @param string $field The string to be measured. - * - * @return int unsigned integer for the length of the argument. - */ - public function char_length( $field ) { - return strlen( $field ); - } - /** * Method to emulate MySQL MD5() function. * @@ -574,7 +562,7 @@ public function log() { public function least() { $arg_list = func_get_args(); - return "min($arg_list)"; + return min( $arg_list ); } /** @@ -587,7 +575,7 @@ public function least() { public function greatest() { $arg_list = func_get_args(); - return "max($arg_list)"; + return max( $arg_list ); } /** diff --git a/modules/database/sqlite/wp-includes/sqlite/class-wp-sqlite-translator.php b/modules/database/sqlite/wp-includes/sqlite/class-wp-sqlite-translator.php index 3942a90f51..60aad31c02 100644 --- a/modules/database/sqlite/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/modules/database/sqlite/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -23,6 +23,11 @@ class WP_SQLite_Translator { PRIMARY KEY(`table`, `column_or_index`) );'; + /** + * We use the ASCII SUB character to escape LIKE literal _ and % + */ + const LIKE_ESCAPE_CHAR = "\x1a"; + /** * Class variable to reference to the PDO instance. * @@ -272,6 +277,50 @@ class WP_SQLite_Translator { */ private $last_reserved_keyword; + /** + * True if a VACUUM operation should be done on shutdown, + * to handle OPTIMIZE TABLE and similar operations. + * + * @var bool + */ + private $vacuum_requested = false; + + /** + * True if the present query is metadata + * + * @var bool + */ + private $is_information_schema_query = false; + + /** + * True if a GROUP BY clause is detected. + * + * @var bool + */ + private $has_group_by = false; + + /** + * 0 if no LIKE is in progress, otherwise counts nested parentheses. + * + * @todo A generic stack of expression would scale better. There's already a call_stack in WP_SQLite_Query_Rewriter. + * @var int + */ + private $like_expression_nesting = 0; + + /** + * 0 if no LIKE is in progress, otherwise counts nested parentheses. + * + * @var int + */ + private $like_escape_count = 0; + + /** + * Associative array with list of system (non-WordPress) tables. + * + * @var array [tablename => tablename] + */ + private $sqlite_system_tables = array(); + /** * Constructor. * @@ -292,8 +341,14 @@ public function __construct( $pdo = null ) { $err_message = ''; do { try { + $options = array( + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // phpcs:ignore WordPress.DB.RestrictedClasses + PDO::ATTR_STRINGIFY_FETCHES => true, // phpcs:ignore WordPress.DB.RestrictedClasses + PDO::ATTR_TIMEOUT => 5, // phpcs:ignore WordPress.DB.RestrictedClasses + ); + $dsn = 'sqlite:' . FQDB; - $pdo = new PDO( $dsn, null, null, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ) ); // phpcs:ignore WordPress.DB.RestrictedClasses + $pdo = new PDO( $dsn, null, null, $options ); // phpcs:ignore WordPress.DB.RestrictedClasses } catch ( PDOException $ex ) { $status = $ex->getCode(); if ( self::SQLITE_BUSY === $status || self::SQLITE_LOCKED === $status ) { @@ -323,6 +378,13 @@ public function __construct( $pdo = null ) { $pdo->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true ); // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO $pdo->query( WP_SQLite_Translator::CREATE_DATA_TYPES_CACHE_TABLE ); + /* + * A list of system tables lets us emulate information_schema + * queries without returning extra tables. + */ + $this->sqlite_system_tables ['sqlite_sequence'] = 'sqlite_sequence'; + $this->sqlite_system_tables [ self::DATA_TYPES_CACHE_TABLE ] = self::DATA_TYPES_CACHE_TABLE; + $this->pdo = $pdo; // Fixes a warning in the site-health screen. @@ -330,6 +392,7 @@ public function __construct( $pdo = null ) { register_shutdown_function( array( $this, '__destruct' ) ); + // WordPress happens to use no foreign keys. $statement = $this->pdo->query( 'PRAGMA foreign_keys' ); if ( $statement->fetchColumn( 0 ) == '0' ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison $this->pdo->query( 'PRAGMA foreign_keys = ON' ); @@ -476,6 +539,35 @@ private function prepare_directory() { */ public function query( $statement, $mode = PDO::FETCH_OBJ, ...$fetch_mode_args ) { // phpcs:ignore WordPress.DB.RestrictedClasses $this->flush(); + if ( function_exists( 'apply_filters' ) ) { + /** + * Filters queries before they are translated and run. + * + * Return a non-null value to cause query() to return early with that result. + * Use this filter to intercept queries that don't work correctly in SQLite. + * + * From within the filter you can do + * function filter_sql ($result, $translator, $statement, $mode, $fetch_mode_args) { + * if ( intercepting this query ) { + * return $translator->execute_sqlite_query( $statement ); + * } + * return $result; + * } + * + * @param null|array $result Default null to continue with the query. + * @param object $translator The translator object. You can call $translator->execute_sqlite_query(). + * @param string $statement The statement passed. + * @param int $mode Fetch mode: PDO::FETCH_OBJ, PDO::FETCH_CLASS, etc. + * @param array $fetch_mode_args Variable arguments passed to query. + * + * @returns null|array Null to proceed, or an array containing a resultset. + * @since 2.1.0 + */ + $pre = apply_filters( 'pre_query_sqlite_db', null, $this, $statement, $mode, $fetch_mode_args ); + if ( null !== $pre ) { + return $pre; + } + } $this->pdo_fetch_mode = $mode; $this->mysql_query = $statement; if ( @@ -695,6 +787,16 @@ private function execute_mysql_query( $query ) { $this->execute_describe(); break; + case 'CHECK': + $this->execute_check(); + break; + + case 'OPTIMIZE': + case 'REPAIR': + case 'ANALYZE': + $this->execute_optimize( $query_type ); + break; + default: throw new Exception( 'Unknown query type: ' . $query_type ); } @@ -1223,9 +1325,9 @@ private function execute_delete() { } $query = ( - count( $ids_to_delete ) - ? "DELETE FROM {$table_name} WHERE {$pk_name} IN (" . implode( ',', $ids_to_delete ) . ')' - : "DELETE FROM {$table_name} WHERE 0=1" + count( $ids_to_delete ) + ? "DELETE FROM {$table_name} WHERE {$pk_name} IN (" . implode( ',', $ids_to_delete ) . ')' + : "DELETE FROM {$table_name} WHERE 0=1" ); $this->execute_sqlite_query( $query ); $this->set_result_from_affected_rows( @@ -1275,22 +1377,9 @@ private function execute_select() { $updated_query = $this->rewriter->get_updated_query(); if ( $table_name && str_starts_with( strtolower( $table_name ), 'information_schema' ) ) { - // @TODO: Actually rewrite the columns. - if ( str_contains( $updated_query, 'bytes' ) ) { - // Count rows per table. - $tables = $this->execute_sqlite_query( "SELECT name as `table` FROM sqlite_master WHERE type='table' ORDER BY name" )->fetchAll(); - $rows = '(CASE '; - foreach ( $tables as $table ) { - $table_name = $table['table']; - $count = $this->execute_sqlite_query( "SELECT COUNT(*) as `count` FROM $table_name" )->fetch(); - $rows .= " WHEN name = '$table_name' THEN {$count['count']} "; - } - $rows .= 'ELSE 0 END) '; - $updated_query = "SELECT name as `table`, $rows as `rows`, 0 as `bytes` FROM sqlite_master WHERE type='table' ORDER BY name"; - } else { - $updated_query = "SELECT name, 'myisam' as `engine`, 0 as `data`, 0 as `index` FROM sqlite_master WHERE type='table' ORDER BY name"; - } - $params = array(); + $this->is_information_schema_query = true; + $updated_query = $this->get_information_schema_query( $updated_query ); + $params = array(); } elseif ( strpos( $updated_query, '@@SESSION.sql_mode' ) !== false || strpos( $updated_query, 'CONVERT( ' ) !== false @@ -1320,9 +1409,17 @@ private function execute_select() { } $stmt = $this->execute_sqlite_query( $updated_query, $params ); - $this->set_results_from_fetched_data( - $stmt->fetchAll( $this->pdo_fetch_mode ) - ); + if ( $this->is_information_schema_query ) { + $this->set_results_from_fetched_data( + $this->strip_sqlite_system_tables( + $stmt->fetchAll( $this->pdo_fetch_mode ) + ) + ); + } else { + $this->set_results_from_fetched_data( + $stmt->fetchAll( $this->pdo_fetch_mode ) + ); + } } /** @@ -1574,13 +1671,48 @@ private function preprocess_string_literal( $value ) { * and stop relying on this MySQL feature, */ if ( 1 === preg_match( '/^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$/', $value, $matches ) ) { - if ( false === strtotime( $value ) ) { + /* + * Calling strtotime("0000-00-00 00:00:00") in 32-bit environments triggers + * an "out of integer range" warning – let's avoid that call for the popular + * case of "zero" dates. + */ + if ( '0000-00-00 00:00:00' !== $value && false === strtotime( $value ) ) { $value = '0000-00-00 00:00:00'; } } return $value; } + /** + * Preprocesses a LIKE expression. + * + * @param WP_SQLite_Token $token The token to preprocess. + * @return string + */ + private function preprocess_like_expr( &$token ) { + /* + * This code handles escaped wildcards in LIKE clauses. + * If we are within a LIKE experession, we look for \_ and \%, the + * escaped LIKE wildcards, the ones where we want a literal, not a + * wildcard match. We change the \ escape for an ASCII \x1a (SUB) character, + * so the \ characters won't get munged. + * These \_ and \% escape sequences are in the token name, because + * the lexer has already done stripcslashes on the value. + */ + if ( $this->like_expression_nesting > 0 ) { + /* Remove the quotes around the name. */ + $unescaped_value = mb_substr( $token->token, 1, -1, 'UTF-8' ); + if ( str_contains( $unescaped_value, '\_' ) || str_contains( $unescaped_value, '\%' ) ) { + $this->like_escape_count ++; + return str_replace( + array( '\_', '\%' ), + array( self::LIKE_ESCAPE_CHAR . '_', self::LIKE_ESCAPE_CHAR . '%' ), + $unescaped_value + ); + } + } + return $token->value; + } /** * Translate CAST() function when we want to cast to BINARY. * @@ -1624,11 +1756,15 @@ private function translate_expression( $token ) { $this->skip_from_dual( $token ) || $this->translate_concat_function( $token ) || $this->translate_concat_comma_to_pipes( $token ) + || $this->translate_function_aliases( $token ) || $this->translate_cast_as_binary( $token ) || $this->translate_date_add_sub( $token ) || $this->translate_date_format( $token ) || $this->translate_interval( $token ) || $this->translate_regexp_functions( $token ) + || $this->capture_group_by( $token ) + || $this->translate_ungrouped_having( $token ) + || $this->translate_like_escape( $token ) ); } @@ -1740,7 +1876,9 @@ private function extract_bound_parameter( $token, &$params ) { } $param_name = ':param' . count( $params ); - $params[ $param_name ] = $this->preprocess_string_literal( $token->value ); + $value = $this->preprocess_like_expr( $token ); + $value = $this->preprocess_string_literal( $value ); + $params[ $param_name ] = $value; $this->rewriter->skip(); $this->rewriter->add( new WP_SQLite_Token( $param_name, WP_SQLite_Token::TYPE_STRING, WP_SQLite_Token::FLAG_STRING_SINGLE_QUOTES ) ); $this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_WHITESPACE ) ); @@ -1828,6 +1966,41 @@ private function translate_date_add_sub( $token ) { return true; } + /** + * Convert function aliases. + * + * @param object $token The current token. + * + * @return bool False when no match, true when this function consumes the token. + * + * @todo LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters. They are in SQLite. + */ + private function translate_function_aliases( $token ) { + if ( ! $token->matches( + WP_SQLite_Token::TYPE_KEYWORD, + WP_SQLite_Token::FLAG_KEYWORD_FUNCTION, + array( 'SUBSTRING', 'CHAR_LENGTH' ) + ) + ) { + return false; + } + switch ( $token->value ) { + case 'SUBSTRING': + $name = 'SUBSTR'; + break; + case 'CHAR_LENGTH': + $name = 'LENGTH'; + break; + default: + $name = $token->value; + break; + } + $this->rewriter->skip(); + $this->rewriter->add( new WP_SQLite_Token( $name, $token->type, $token->flags ) ); + + return true; + } + /** * Translate VALUES() function. * @@ -2080,6 +2253,185 @@ private function translate_regexp_functions( $token ) { return true; } + /** + * Detect GROUP BY. + * + * @todo edgecase Fails on a statement with GROUP BY nested in an outer HAVING without GROUP BY. + * + * @param WP_SQLite_Token $token The token to translate. + * + * @return bool + */ + private function capture_group_by( $token ) { + if ( + ! $token->matches( + WP_SQLite_Token::TYPE_KEYWORD, + WP_SQLite_Token::FLAG_KEYWORD_RESERVED, + array( 'GROUP' ) + ) + ) { + return false; + } + $next = $this->rewriter->peek_nth( 2 )->value; + if ( 'BY' !== strtoupper( $next ) ) { + return false; + } + + $this->has_group_by = true; + + return false; + } + + /** + * Translate WHERE something HAVING something to WHERE something AND something. + * + * @param WP_SQLite_Token $token The token to translate. + * + * @return bool + */ + private function translate_ungrouped_having( $token ) { + if ( + ! $token->matches( + WP_SQLite_Token::TYPE_KEYWORD, + WP_SQLite_Token::FLAG_KEYWORD_RESERVED, + array( 'HAVING' ) + ) + ) { + return false; + } + if ( $this->has_group_by ) { + return false; + } + $this->rewriter->skip(); + $this->rewriter->add( new WP_SQLite_Token( 'AND', WP_SQLite_Token::TYPE_KEYWORD ) ); + + return true; + } + + /** + * Rewrite LIKE '\_whatever' as LIKE '\_whatever' ESCAPE '\' . + * + * We look for keyword LIKE. On seeing it we set a flag. + * If the flag is set, we emit ESCAPE '\' before the next keyword. + * + * @param WP_SQLite_Token $token The token to translate. + * + * @return bool + */ + private function translate_like_escape( $token ) { + + if ( 0 === $this->like_expression_nesting ) { + $is_like = $token->matches( WP_SQLite_Token::TYPE_KEYWORD, null, array( 'LIKE' ) ); + /* is this the LIKE keyword? If so set the flag. */ + if ( $is_like ) { + $this->like_expression_nesting = 1; + } + } else { + /* open parenthesis during LIKE parameter, count it. */ + if ( $token->matches( WP_SQLite_Token::TYPE_OPERATOR, null, array( '(' ) ) ) { + $this->like_expression_nesting ++; + + return false; + } + + /* close parenthesis matching open parenthesis during LIKE parameter, count it. */ + if ( $this->like_expression_nesting > 1 && $token->matches( WP_SQLite_Token::TYPE_OPERATOR, null, array( ')' ) ) ) { + $this->like_expression_nesting --; + + return false; + } + + /* a keyword, a commo, a semicolon, the end of the statement, or a close parenthesis */ + $is_like_finished = $token->matches( WP_SQLite_Token::TYPE_KEYWORD ) + || $token->matches( WP_SQLite_Token::TYPE_DELIMITER, null, array( ';' ) ) || ( WP_SQLite_Token::TYPE_DELIMITER === $token->type && null === $token->value ) + || $token->matches( WP_SQLite_Token::TYPE_OPERATOR, null, array( ')', ',' ) ); + + if ( $is_like_finished ) { + /* + * Here we have another keyword encountered with the LIKE in progress. + * Emit the ESCAPE clause. + */ + if ( $this->like_escape_count > 0 ) { + /* If we need the ESCAPE clause emit it. */ + $this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_DELIMITER ) ); + $this->rewriter->add( new WP_SQLite_Token( 'ESCAPE', WP_SQLite_Token::TYPE_KEYWORD ) ); + $this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_DELIMITER ) ); + $this->rewriter->add( new WP_SQLite_Token( "'" . self::LIKE_ESCAPE_CHAR . "'", WP_SQLite_Token::TYPE_STRING ) ); + $this->rewriter->add( new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_DELIMITER ) ); + } + $this->like_escape_count = 0; + $this->like_expression_nesting = 0; + } + } + + return false; + } + + /** + * Rewrite a query from the MySQL information_schema. + * + * @param string $updated_query The query to rewrite. + * + * @return string The query for use by SQLite + */ + private function get_information_schema_query( $updated_query ) { + // @TODO: Actually rewrite the columns. + $normalized_query = preg_replace( '/\s+/', ' ', strtolower( $updated_query ) ); + if ( str_contains( $normalized_query, 'bytes' ) ) { + // Count rows per table. + $tables = + $this->execute_sqlite_query( "SELECT name as `table_name` FROM sqlite_master WHERE type='table' ORDER BY name" )->fetchAll(); + $tables = $this->strip_sqlite_system_tables( $tables ); + + $rows = '(CASE '; + foreach ( $tables as $table ) { + $table_name = $table['table_name']; + $count = $this->execute_sqlite_query( "SELECT COUNT(1) as `count` FROM $table_name" )->fetch(); + $rows .= " WHEN name = '$table_name' THEN {$count['count']} "; + } + $rows .= 'ELSE 0 END) '; + $updated_query = + "SELECT name as `table_name`, $rows as `rows`, 0 as `bytes` FROM sqlite_master WHERE type='table' ORDER BY name"; + } elseif ( str_contains( $normalized_query, 'count(*)' ) && ! str_contains( $normalized_query, 'table_name =' ) ) { + // @TODO This is a guess that the caller wants a count of tables. + $list = array(); + foreach ( $this->sqlite_system_tables as $system_table => $name ) { + $list [] = "'" . $system_table . "'"; + } + $list = implode( ', ', $list ); + $sql = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name NOT IN ($list)"; + $table_count = $this->execute_sqlite_query( $sql )->fetch(); + $updated_query = 'SELECT ' . $table_count[0] . ' AS num'; + + $this->is_information_schema_query = false; + } else { + $updated_query = + "SELECT name as `table_name`, 'myisam' as `engine`, 0 as `data_length`, 0 as `index_length`, 0 as `data_free` FROM sqlite_master WHERE type='table' ORDER BY name"; + } + + return $updated_query; + } + + /** + * Remove system table rows from resultsets of information_schema tables. + * + * @param array $tables The result set. + * + * @return array The filtered result set. + */ + private function strip_sqlite_system_tables( $tables ) { + return array_values( + array_filter( + $tables, + function ( $table ) { + $table_name = property_exists( $table, 'Name' ) ? $table->Name : $table->table_name; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + return ! array_key_exists( $table_name, $this->sqlite_system_tables ); + }, + ARRAY_FILTER_USE_BOTH + ) + ); + } + /** * Translate the ON DUPLICATE KEY UPDATE clause. * @@ -2194,7 +2546,7 @@ private function get_primary_keys( $table_name ) { /** * Get the keys for a table. * - * @param string $table_name Table name. + * @param string $table_name Table name. * @param bool $only_unique Only return unique keys. * * @return array @@ -2611,9 +2963,38 @@ private function execute_show() { $stmt = $this->execute_sqlite_query( "PRAGMA table_info(\"$table_name\");" ); - $this->set_results_from_fetched_data( - $stmt->fetchAll( $this->pdo_fetch_mode ) + /* @todo we may need to add the Extra column if anybdy needs it. 'auto_increment' is the value */ + $name_map = array( + 'name' => 'Field', + 'type' => 'Type', + 'dflt_value' => 'Default', + 'cid' => null, + 'notnull' => null, + 'pk' => null, ); + $columns = $stmt->fetchAll( $this->pdo_fetch_mode ); + $columns = array_map( + function ( $row ) use ( $name_map ) { + $new = array(); + $is_object = is_object( $row ); + $row = $is_object ? (array) $row : $row; + foreach ( $row as $k => $v ) { + $k = array_key_exists( $k, $name_map ) ? $name_map [ $k ] : $k; + if ( $k ) { + $new[ $k ] = $v; + } + } + if ( array_key_exists( 'notnull', $row ) ) { + $new['Null'] = ( '1' === $row ['notnull'] ) ? 'NO' : 'YES'; + } + if ( array_key_exists( 'pk', $row ) ) { + $new['Key'] = ( '1' === $row ['pk'] ) ? 'PRI' : ''; + } + return $is_object ? (object) $new : $new; + }, + $columns + ); + $this->set_results_from_fetched_data( $columns ); return; case 'INDEX FROM': @@ -2685,6 +3066,28 @@ private function execute_show() { $this->set_results_from_fetched_data( $results ); + + return; + + case 'TABLE STATUS': // FROM `database`. + $this->rewriter->skip(); + $database_expression = $this->rewriter->skip(); + $stmt = $this->execute_sqlite_query( + "SELECT name as `Name`, 'myisam' as `Engine`, 0 as `Data_length`, 0 as `Index_length`, 0 as `Data_free` FROM sqlite_master WHERE type='table' ORDER BY name" + ); + + $tables = $this->strip_sqlite_system_tables( $stmt->fetchAll( $this->pdo_fetch_mode ) ); + foreach ( $tables as $table ) { + $table_name = $table->Name; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $stmt = $this->execute_sqlite_query( "SELECT COUNT(1) as `Rows` FROM $table_name" ); + $rows = $stmt->fetchall( $this->pdo_fetch_mode ); + $table->Rows = $rows[0]->Rows; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + } + + $this->set_results_from_fetched_data( + $this->strip_sqlite_system_tables( $tables ) + ); + return; case 'TABLES LIKE': @@ -2840,11 +3243,11 @@ private function normalize_mysql_index_type( $index_type ) { $index_type = preg_replace( '/INDEX$/', 'KEY', $index_type ); $index_type = preg_replace( '/ KEY$/', '', $index_type ); if ( - 'KEY' === $index_type - || 'PRIMARY' === $index_type - || 'UNIQUE' === $index_type - || 'FULLTEXT' === $index_type - || 'SPATIAL' === $index_type + 'KEY' === $index_type + || 'PRIMARY' === $index_type + || 'UNIQUE' === $index_type + || 'FULLTEXT' === $index_type + || 'SPATIAL' === $index_type ) { return $index_type; } @@ -2871,6 +3274,98 @@ private function mysql_index_type_to_sqlite_type( $normalized_mysql_index_type ) return 'INDEX'; } + /** + * Executes a CHECK statement. + */ + private function execute_check() { + $this->rewriter->skip(); // CHECK. + $this->rewriter->skip(); // TABLE. + $table_name = $this->rewriter->consume()->value; // Τable_name. + + $tables = + $this->execute_sqlite_query( + "SELECT name as `table_name` FROM sqlite_master WHERE type='table' AND name = :table_name ORDER BY name", + array( $table_name ) + )->fetchAll(); + + if ( is_array( $tables ) && 1 === count( $tables ) && $table_name === $tables[0]['table_name'] ) { + + $this->set_results_from_fetched_data( + array( + (object) array( + 'Table' => $table_name, + 'Op' => 'check', + 'Msg_type' => 'status', + 'Msg_text' => 'OK', + ), + ) + ); + } else { + + $this->set_results_from_fetched_data( + array( + (object) array( + 'Table' => $table_name, + 'Op' => 'check', + 'Msg_type' => 'Error', + 'Msg_text' => "Table '$table_name' doesn't exist", + ), + (object) array( + 'Table' => $table_name, + 'Op' => 'check', + 'Msg_type' => 'status', + 'Msg_text' => 'Operation failed', + ), + ) + ); + } + } + + /** + * Handle an OPTIMIZE / REPAIR / ANALYZE TABLE statement, by using VACUUM just once, at shutdown. + * + * @param string $query_type The query type. + */ + private function execute_optimize( $query_type ) { + // OPTIMIZE TABLE tablename. + $this->rewriter->skip(); + $this->rewriter->skip(); + $table_name = $this->rewriter->skip()->value; + $status = ''; + + if ( ! $this->vacuum_requested ) { + $this->vacuum_requested = true; + if ( function_exists( 'add_action' ) ) { + $status = "SQLite does not support $query_type, doing VACUUM instead"; + add_action( + 'shutdown', + function () { + $this->execute_sqlite_query( 'VACUUM' ); + } + ); + } else { + /* add_action isn't available in the unit test environment, and we're deep in a transaction. */ + $status = "SQLite unit testing does not support $query_type."; + } + } + $resultset = array( + (object) array( + 'Table' => $table_name, + 'Op' => strtolower( $query_type ), + 'Msg_type' => 'note', + 'Msg_text' => $status, + ), + (object) array( + 'Table' => $table_name, + 'Op' => strtolower( $query_type ), + 'Msg_type' => 'status', + 'Msg_text' => 'OK', + ), + ); + + $this->set_results_from_fetched_data( $resultset ); + } + /** * Error handler. * @@ -2879,9 +3374,8 @@ private function mysql_index_type_to_sqlite_type( $normalized_mysql_index_type ) * @return bool Always false. */ private function handle_error( Exception $err ) { - $message = $err->getMessage(); - $err_message = sprintf( 'Problem preparing the PDO SQL Statement. Error was: %s. trace: %s', $message, $err->getTraceAsString() ); - $this->set_error( __LINE__, __FUNCTION__, $err_message ); + $message = $err->getMessage(); + $this->set_error( __LINE__, __FUNCTION__, $message ); $this->return_value = false; return false; } @@ -2939,7 +3433,7 @@ public function get_error_message() { } $output = '
 
' . PHP_EOL; - $output .= '
' . PHP_EOL; + $output .= '
' . PHP_EOL; $output .= '

MySQL query:

' . PHP_EOL; $output .= '

' . $this->mysql_query . '

' . PHP_EOL; $output .= '

Queries made or created this session were:

' . PHP_EOL; @@ -2952,7 +3446,7 @@ public function get_error_message() { $output .= '' . PHP_EOL; $output .= '
' . PHP_EOL; foreach ( $this->error_messages as $num => $m ) { - $output .= '
' . PHP_EOL; + $output .= '
' . PHP_EOL; $output .= sprintf( 'Error occurred at line %1$d in Function %2$s. Error message was: %3$s.', (int) $this->errors[ $num ]['line'], @@ -2966,14 +3460,14 @@ public function get_error_message() { throw new Exception(); } catch ( Exception $e ) { $output .= '

Backtrace:

' . PHP_EOL; - $output .= '
' . htmlspecialchars( $e->getTraceAsString() ) . '
' . PHP_EOL; + $output .= '
' . $e->getTraceAsString() . '
' . PHP_EOL; } return $output; } /** - * Executes a query in SQLite – for internal use only. + * Executes a query in SQLite. * * @param mixed $sql The query to execute. * @param mixed $params The parameters to bind to the query. @@ -2985,14 +3479,27 @@ public function get_error_message() { * @type * $result The value returned by $stmt. * } */ - private function execute_sqlite_query( $sql, $params = array() ) { + public function execute_sqlite_query( $sql, $params = array() ) { $this->executed_sqlite_queries[] = array( 'sql' => $sql, 'params' => $params, ); - $stmt = $this->pdo->prepare( $sql ); - $this->last_exec_returned = $stmt->execute( $params ); + $stmt = $this->pdo->prepare( $sql ); + if ( false === $stmt || null === $stmt ) { + $this->last_exec_returned = null; + $info = $this->pdo->errorInfo(); + $this->last_sqlite_error = $info[0] . ' ' . $info[2]; + throw new PDOException( implode( ' ', array( 'Error:', $info[0], $info[2], 'SQLite:', $sql ) ), $info[1] ); + } + $returned = $stmt->execute( $params ); + $this->last_exec_returned = $returned; + if ( ! $returned ) { + $info = $stmt->errorInfo(); + $this->last_sqlite_error = $info[0] . ' ' . $info[2]; + throw new PDOException( implode( ' ', array( 'Error:', $info[0], $info[2], 'SQLite:', $sql ) ), $info[1] ); + } + return $stmt; } @@ -3038,18 +3545,21 @@ private function set_result_from_affected_rows( $override = null ) { * Method to clear previous data. */ private function flush() { - $this->mysql_query = ''; - $this->results = null; - $this->last_exec_returned = null; - $this->last_insert_id = null; - $this->affected_rows = null; - $this->column_data = array(); - $this->num_rows = null; - $this->return_value = null; - $this->error_messages = array(); - $this->is_error = false; - $this->executed_sqlite_queries = array(); - $this->last_exec_returned = null; + $this->mysql_query = ''; + $this->results = null; + $this->last_exec_returned = null; + $this->last_insert_id = null; + $this->affected_rows = null; + $this->column_data = array(); + $this->num_rows = null; + $this->return_value = null; + $this->error_messages = array(); + $this->is_error = false; + $this->executed_sqlite_queries = array(); + $this->like_expression_nesting = 0; + $this->like_escape_count = 0; + $this->is_information_schema_query = false; + $this->has_group_by = false; } /**