| 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 | /** |