repair.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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( __DIR__ ) ) . '/wp-load.php';
  10. header( 'Content-Type: text/html; charset=utf-8' );
  11. ?>
  12. <!DOCTYPE html>
  13. <html <?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_keys = array_unique(
  34. array(
  35. 'put your unique phrase here',
  36. /*
  37. * translators: This string should only be translated if wp-config-sample.php is localized.
  38. * You can check the localized release package or
  39. * https://i18n.svn.wordpress.org/<locale code>/branches/<wp version>/dist/wp-config-sample.php
  40. */
  41. __( 'put your unique phrase here' ),
  42. )
  43. );
  44. $missing_key = false;
  45. $duplicated_keys = array();
  46. foreach ( array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT' ) as $key ) {
  47. if ( defined( $key ) ) {
  48. // Check for unique values of each key.
  49. $duplicated_keys[ constant( $key ) ] = isset( $duplicated_keys[ constant( $key ) ] );
  50. } else {
  51. // If a constant is not defined, it's missing.
  52. $missing_key = true;
  53. }
  54. }
  55. // If at least one key uses a default value, consider it duplicated.
  56. foreach ( $default_keys as $default_key ) {
  57. if ( isset( $duplicated_keys[ $default_key ] ) ) {
  58. $duplicated_keys[ $default_key ] = true;
  59. }
  60. }
  61. // Weed out all unique, non-default values.
  62. $duplicated_keys = array_filter( $duplicated_keys );
  63. if ( $duplicated_keys || $missing_key ) {
  64. echo '<h2 class="screen-reader-text">' . __( 'Check secret keys' ) . '</h2>';
  65. /* translators: 1: wp-config.php, 2: Secret key service URL. */
  66. 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>';
  67. }
  68. } elseif ( isset( $_GET['repair'] ) ) {
  69. echo '<h1 class="screen-reader-text">' . __( 'Database repair results' ) . '</h1>';
  70. $optimize = 2 == $_GET['repair'];
  71. $okay = true;
  72. $problems = array();
  73. $tables = $wpdb->tables();
  74. // Sitecategories may not exist if global terms are disabled.
  75. $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->sitecategories ) );
  76. if ( is_multisite() && ! $wpdb->get_var( $query ) ) {
  77. unset( $tables['sitecategories'] );
  78. }
  79. /**
  80. * Filters additional database tables to repair.
  81. *
  82. * @since 3.0.0
  83. *
  84. * @param string[] $tables Array of prefixed table names to be repaired.
  85. */
  86. $tables = array_merge( $tables, (array) apply_filters( 'tables_to_repair', array() ) );
  87. // Loop over the tables, checking and repairing as needed.
  88. foreach ( $tables as $table ) {
  89. $check = $wpdb->get_row( "CHECK TABLE $table" );
  90. echo '<p>';
  91. if ( 'OK' === $check->Msg_text ) {
  92. /* translators: %s: Table name. */
  93. printf( __( 'The %s table is okay.' ), "<code>$table</code>" );
  94. } else {
  95. /* translators: 1: Table name, 2: Error message. */
  96. 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>" );
  97. $repair = $wpdb->get_row( "REPAIR TABLE $table" );
  98. echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
  99. if ( 'OK' === $repair->Msg_text ) {
  100. /* translators: %s: Table name. */
  101. printf( __( 'Successfully repaired the %s table.' ), "<code>$table</code>" );
  102. } else {
  103. /* translators: 1: Table name, 2: Error message. */
  104. printf( __( 'Failed to repair the %1$s table. Error: %2$s' ), "<code>$table</code>", "<code>$repair->Msg_text</code>" ) . '<br />';
  105. $problems[ $table ] = $repair->Msg_text;
  106. $okay = false;
  107. }
  108. }
  109. if ( $okay && $optimize ) {
  110. $analyze = $wpdb->get_row( "ANALYZE TABLE $table" );
  111. echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
  112. if ( 'Table is already up to date' === $analyze->Msg_text ) {
  113. /* translators: %s: Table name. */
  114. printf( __( 'The %s table is already optimized.' ), "<code>$table</code>" );
  115. } else {
  116. $optimize = $wpdb->get_row( "OPTIMIZE TABLE $table" );
  117. echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;';
  118. if ( 'OK' === $optimize->Msg_text || 'Table is already up to date' === $optimize->Msg_text ) {
  119. /* translators: %s: Table name. */
  120. printf( __( 'Successfully optimized the %s table.' ), "<code>$table</code>" );
  121. } else {
  122. /* translators: 1: Table name. 2: Error message. */
  123. printf( __( 'Failed to optimize the %1$s table. Error: %2$s' ), "<code>$table</code>", "<code>$optimize->Msg_text</code>" );
  124. }
  125. }
  126. }
  127. echo '</p>';
  128. }
  129. if ( $problems ) {
  130. printf(
  131. /* translators: %s: URL to "Fixing WordPress" forum. */
  132. '<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>',
  133. __( 'https://wordpress.org/support/forum/how-to-and-troubleshooting' )
  134. );
  135. $problem_output = '';
  136. foreach ( $problems as $table => $problem ) {
  137. $problem_output .= "$table: $problem\n";
  138. }
  139. echo '<p><textarea name="errors" id="errors" rows="20" cols="60">' . esc_textarea( $problem_output ) . '</textarea></p>';
  140. } else {
  141. 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>";
  142. }
  143. } else {
  144. echo '<h1 class="screen-reader-text">' . __( 'WordPress database repair' ) . '</h1>';
  145. if ( isset( $_GET['referrer'] ) && 'is_blog_installed' === $_GET['referrer'] ) {
  146. 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>';
  147. } else {
  148. echo '<p>' . __( 'WordPress can automatically look for some common database problems and repair them. Repairing can take a while, so please be patient.' ) . '</p>';
  149. }
  150. ?>
  151. <p class="step"><a class="button button-large" href="repair.php?repair=1"><?php _e( 'Repair Database' ); ?></a></p>
  152. <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>
  153. <p class="step"><a class="button button-large" href="repair.php?repair=2"><?php _e( 'Repair and Optimize Database' ); ?></a></p>
  154. <?php
  155. }
  156. ?>
  157. </body>
  158. </html>