Make WordPress Core

Ticket #22884: 22884-handle_error-2.diff

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

moving require outside of the loop + improving readability

  • 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                        $rebuild_tables = false;
     1288
     1289                        foreach ( $wp_tables as $table ) {
     1290                                // The existence of custom user tables shouldn't suggest an insane state or prevent a clean install.
     1291                                if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table )
     1292                                        continue;
     1293                                if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table )
     1294                                        continue;
     1295
     1296                                // If we have results, the table exists
     1297                                $sql_check = $this->prepare( "SHOW TABLES LIKE %s", $table );
     1298                                $table_exists = ( $this->get_var( $sql_check ) != null );
     1299                                if ( ! $table_exists ) {
     1300                                        // There is a missing table we should be able to fix
     1301                                        $rebuild_tables = true;
     1302                                        // We're done here.
     1303                                        break;
     1304                                }
     1305                        }
     1306
     1307                        // Recreate missing tables.
     1308                        if ( $rebuild_tables ) {
     1309                                // Get dependencies to be able to execute dbDelta
     1310                                require( ABSPATH . 'wp-admin/includes/upgrade.php' );
     1311
     1312                                // Make sure we have the latest (and all) tables
     1313                                dbDelta();
     1314                        }
     1315
     1316                        $this->suppress_errors( $suppress );
     1317
     1318                        $wp_tables_verified = true;
     1319                }
     1320
     1321        }
     1322
     1323        /**
    12361324         * Print SQL/DB error.
    12371325         *
    12381326         * @since 0.71
     
    13521440                $this->last_query  = null;
    13531441                $this->rows_affected = $this->num_rows = 0;
    13541442                $this->last_error  = '';
     1443                $this->last_error_no = 0;
    13551444
    13561445                if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
    13571446                        mysqli_free_result( $this->result );
     
    16421731                        $this->last_error = mysql_error( $this->dbh );
    16431732                }
    16441733
     1734                $this->last_error_no = $mysql_errno;
     1735
    16451736                if ( $this->last_error ) {
    16461737                        // Clear insert_id on a subsequent failed insert.
    16471738                        if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
    16481739                                $this->insert_id = 0;
    16491740
    1650                         $this->print_error();
     1741                        $this->handle_error();
    16511742                        return false;
    16521743                }
    16531744