repair.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Database Repair and Optimization Script.
  4. *
  5. * @package WordPress
  6. * @subpackage Database
  7. */
  8. define( 'WP_REPAIRING', true );
  9. require_once( dirname( dirname( dirname( __FILE__ ) ) ) . '/wp-load.php' );
  10. header( 'Content-Type: text/html; charset=utf-8' );
  11. ?>
  12. <!DOCTYPE html>
  13. <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
  14. <head>
  15. <meta name="viewport" content="width=device-width" />
  16. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  17. <meta name="robots" content="noindex,nofollow" />
  18. <title><?php _e( 'WordPress &rsaquo; Database Repair' ); ?></title>
  19. <?php wp_admin_css( 'install', true ); ?>
  20. </head>
  21. <body class="wp-core-ui">
  22. <p id="logo"><a href="<?php echo esc_url( __( 'https://wordpress.org/' ) ); ?>"><?php _e( 'WordPress' ); ?></a></p>
  23. <?php
  24. if ( ! defined( 'WP_ALLOW_REPAIR' ) || ! WP_ALLOW_REPAIR ) {
  25. echo '<h1 class="screen-reader-text">' . __( 'Allow automatic database repair' ) . '</h1>';
  26. echo '<p>';
  27. printf(
  28. /* translators: %s: wp-config.php */
  29. __( 'To allow use of this page to automatically repair database problems, please add the following line to your %s file. Once this line is added to your config, reload this page.' ),
  30. '<code>wp-config.php</code>'
  31. );
  32. echo "</p><p><code>define('WP_ALLOW_REPAIR', true);</code></p>";
  33. $default_key = 'put your unique phrase here';
  34. $missing_key = false;
  35. $duplicated_keys = array();
  36. foreach ( array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT' ) as $key ) {
  37. if ( defined( $key ) ) {
  38. // Check for unique values of each key.
  39. $duplicated_keys[ constant( $key ) ] = isset( $duplicated_keys[ constant( $key ) ] );
  40. } else {
  41. // If a constant is not defined, it's missing.
  42. $missing_key = true;
  43. }
  44. }
  45. // If at least one key uses the default value, consider it duplicated.
  46. if ( isset( $duplicated_keys[ $default_key ] ) ) {
  47. $duplicated_keys[ $default_key ] = true;
  48. }
  49. // Weed out all unique, non-default values.
  50. $duplicated_keys = array_filter( $duplicated_keys );
  51. if ( $duplicated_keys || $missing_key ) {
  52. echo '<h2 class="screen-reader-text">' . __( 'Check secret keys' ) . '</h2>';
  53. /* translators: 1: wp-config.php, 2: Secret key service URL. */
  54. echo '<p>' . sprintf( __( 'While you are editing your %1$s file, take a moment to make sure you have all 8 keys and that they are unique. You can generate these using the <a href="%2$s">WordPress.org secret key service</a>.' ), '<code>wp-config.php</code>', 'https://api.wordpress.org/secret-key/1.1/salt/' ) . '</p>';
  55. }
  56. } elseif ( isset( $_GET['repair'] ) ) {
  57. echo '<h1 class="screen-reader-text">' . __( 'Database repair results' ) . '</h1>';
  58. $optimize = 2 == $_GET['repair'];
  59. $okay = true;
  60. $problems = array();
  61. $tables = $wpdb->tables();
  62. // Sitecategories may not exist if global terms are disabled.
  63. $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->sitecategories ) );
  64. if ( is_multisite() && ! $wpdb->get_var( $query ) ) {
  65. unset( $tables['sitecategories'] );
  66. }
  67. /**
  68. * Filters additional database tables to repair.
  69. *
  70. * @since 3.0.0
  71. *
  72. * @param string[] $tables Array of prefixed table names to be repaired.
  73. */
  74. $tables = array_merge( $tables, (array) apply_filters( 'tables_to_repair', array() ) );
  75. // Loop over the tables, checking and repairing as needed.
  76. foreach ( $tables as $table ) {
  77. $check = $wpdb->get_row( "CHECK TABLE $table" );
  78. echo '<p>';
  79. if ( 'OK' == $check->Msg_text ) {
  80. /* translators: %s: Table name. */
  81. printf( __( 'The %s table is okay.' ), "<code>$table</code>" );
  82. } else {
  83. /* translators: 1: Table name, 2: Error message. */
  84. printf( __( 'The %1$s table is not okay. It is reporting the following error: %2$s. WordPress will attempt to repair this table&hellip;' ), "<code>$table</code>", "<code>$check->Msg_text</code>" );
  85. $repair = $wpdb->get_row( "REPAIR TABLE $table" );
  86. echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
  87. if ( 'OK' == $check->Msg_text ) {
  88. /* translators: %s: Table name. */
  89. printf( __( 'Successfully repaired the %s table.' ), "<code>$table</code>" );
  90. } else {
  91. /* translators: 1: Table name, 2: Error message. */
  92. echo sprintf( __( 'Failed to repair the %1$s table. Error: %2$s' ), "<code>$table</code>", "<code>$check->Msg_text</code>" ) . '<br />';
  93. $problems[ $table ] = $check->Msg_text;
  94. $okay = false;
  95. }
  96. }
  97. if ( $okay && $optimize ) {
  98. $check = $wpdb->get_row( "ANALYZE TABLE $table" );
  99. echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
  100. if ( 'Table is already up to date' == $check->Msg_text ) {
  101. /* translators: %s: Table name. */
  102. printf( __( 'The %s table is already optimized.' ), "<code>$table</code>" );
  103. } else {
  104. $check = $wpdb->get_row( "OPTIMIZE TABLE $table" );
  105. echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
  106. if ( 'OK' == $check->Msg_text || 'Table is already up to date' == $check->Msg_text ) {
  107. /* translators: %s: Table name. */
  108. printf( __( 'Successfully optimized the %s table.' ), "<code>$table</code>" );
  109. } else {
  110. /* translators: 1: Table name. 2: Error message. */
  111. printf( __( 'Failed to optimize the %1$s table. Error: %2$s' ), "<code>$table</code>", "<code>$check->Msg_text</code>" );
  112. }
  113. }
  114. }
  115. echo '</p>';
  116. }
  117. if ( $problems ) {
  118. printf(
  119. /* translators: %s: URL to "Fixing WordPress" forum. */
  120. '<p>' . __( 'Some database problems could not be repaired. Please copy-and-paste the following list of errors to the <a href="%s">WordPress support forums</a> to get additional assistance.' ) . '</p>',
  121. __( 'https://wordpress.org/support/forum/how-to-and-troubleshooting' )
  122. );
  123. $problem_output = '';
  124. foreach ( $problems as $table => $problem ) {
  125. $problem_output .= "$table: $problem\n";
  126. }
  127. echo '<p><textarea name="errors" id="errors" rows="20" cols="60">' . esc_textarea( $problem_output ) . '</textarea></p>';
  128. } else {
  129. echo '<p>' . __( 'Repairs complete. Please remove the following line from wp-config.php to prevent this page from being used by unauthorized users.' ) . "</p><p><code>define('WP_ALLOW_REPAIR', true);</code></p>";
  130. }
  131. } else {
  132. echo '<h1 class="screen-reader-text">' . __( 'WordPress database repair' ) . '</h1>';
  133. if ( isset( $_GET['referrer'] ) && 'is_blog_installed' == $_GET['referrer'] ) {
  134. echo '<p>' . __( 'One or more database tables are unavailable. To allow WordPress to attempt to repair these tables, press the &#8220;Repair Database&#8221; button. Repairing can take a while, so please be patient.' ) . '</p>';
  135. } else {
  136. echo '<p>' . __( 'WordPress can automatically look for some common database problems and repair them. Repairing can take a while, so please be patient.' ) . '</p>';
  137. }
  138. ?>
  139. <p class="step"><a class="button button-large" href="repair.php?repair=1"><?php _e( 'Repair Database' ); ?></a></p>
  140. <p><?php _e( 'WordPress can also attempt to optimize the database. This improves performance in some situations. Repairing and optimizing the database can take a long time and the database will be locked while optimizing.' ); ?></p>
  141. <p class="step"><a class="button button-large" href="repair.php?repair=2"><?php _e( 'Repair and Optimize Database' ); ?></a></p>
  142. <?php
  143. }
  144. ?>
  145. </body>
  146. </html>