Make WordPress Core

Ticket #22884: 22884-handle_error.diff

File 22884-handle_error.diff, 3.3 KB (added by jipmoors, 10 years ago)

handle_error added to wp-db.php

  • trunk/src/wp-includes/wp-db.php

     
    8181        public $last_error = '';
    8282
    8383        /**
     84         * The last error code during query
     85         *
     86         * @since 4.2.0
     87         * @var int
     88         */
     89        public $last_error_no = 0;
     90
     91        /**
    8492         * Amount of queries made
    8593         *
    8694         * @since 1.2.0
     
    12331241        }
    12341242
    12351243        /**
     1244         * Handle SQL/DB error.
     1245         *
     1246         * This function adds the possibility to react to specific database errors
     1247         * Initial problem had to do with accidentally removed WP tables (causes: bad backup, user fault, etc)
     1248         * This allows for a check to verify all core tables are in place
     1249         *
     1250         * @since 4.2.0
     1251         * @access public
     1252         *
     1253         */
     1254        public function handle_error() {
     1255                if ( $this->suppress_errors || 0 == $this->last_error_no ) {
     1256                        return;
     1257                }
     1258
     1259                // Make sure the error is logged and optionally printed.
     1260                $this->print_error();
     1261
     1262
     1263                /*
     1264                 * Check for missing tables; only once per execution.
     1265                 * If this passes, some custom table is missing and we can't really do anything about that.
     1266                 */
     1267                static $wp_tables_verified = false;
     1268
     1269                /**
     1270                 * For all MySQL Error codes see:
     1271                 * http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
     1272                 *
     1273                 * Implemented
     1274                 *  Error: 1146 SQLSTATE: 42S02 (ER_NO_SUCH_TABLE)
     1275                 *
     1276                 */
     1277                if ( false === $wp_tables_verified && 1146 == $this->last_error_no ) {
     1278                        $suppress = $this->suppress_errors();
     1279
     1280                        /*
     1281                         * Loop over the WP tables.
     1282                         * If one or more are missing, suggest table repair.
     1283                         *
     1284                         * Because we got this far, the global is_blog_installed() passed, so options table should be there.
     1285                         */
     1286                        $wp_tables = $this->tables();
     1287
     1288                        foreach ( $wp_tables as $table ) {
     1289                                // The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
     1290                                if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
     1291                                        continue;
     1292                                if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table )
     1293                                        continue;
     1294
     1295                                // If we have results, the table exists
     1296                                $sql_check = $this->prepare( "SHOW TABLES LIKE %s", $table );
     1297                                $table_exists = ( $this->get_var( $sql_check ) != null );
     1298                                if ( $table_exists ) {
     1299                                        continue;
     1300                                }
     1301
     1302                                // Recreate missing tables.
     1303                                require(ABSPATH . 'wp-admin/includes/upgrade.php');
     1304                                // Make sure we have the latest (and all) tables
     1305                                dbDelta();
     1306
     1307                                // We're done now, all missing tables should be restored
     1308                                break;
     1309                        }
     1310
     1311                        $this->suppress_errors( $suppress );
     1312
     1313                        $wp_tables_verified = true;
     1314                }
     1315
     1316        }
     1317
     1318        /**
    12361319         * Print SQL/DB error.
    12371320         *
    12381321         * @since 0.71
     
    13521435                $this->last_query  = null;
    13531436                $this->rows_affected = $this->num_rows = 0;
    13541437                $this->last_error  = '';
     1438                $this->last_error_no = 0;
    13551439
    13561440                if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
    13571441                        mysqli_free_result( $this->result );
     
    16421726                        $this->last_error = mysql_error( $this->dbh );
    16431727                }
    16441728
     1729                $this->last_error_no = $mysql_errno;
     1730
    16451731                if ( $this->last_error ) {
    16461732                        // Clear insert_id on a subsequent failed insert.
    16471733                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
    16481734                                $this->insert_id = 0;
    16491735
    1650                         $this->print_error();
     1736                        $this->handle_error();
    16511737                        return false;
    16521738                }
    16531739