/wp-content/plugins/backwpup/inc/class-jobtype-dbcheck.php

https://bitbucket.org/cesarmedrano/cesarmedrano · PHP · 162 lines · 118 code · 19 blank · 25 comment · 32 complexity · 3f7e3c3f7bd62c819e67404f9cecdad5 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. */
  5. class BackWPup_JobType_DBCheck extends BackWPup_JobTypes {
  6. /**
  7. *
  8. */
  9. public function __construct() {
  10. $this->info[ 'ID' ] = 'DBCHECK';
  11. $this->info[ 'name' ] = __( 'DB Check', 'backwpup' );
  12. $this->info[ 'description' ] = __( 'Check database tables', 'backwpup' );
  13. $this->info[ 'URI' ] = translate( BackWPup::get_plugin_data( 'PluginURI' ), 'backwpup' );
  14. $this->info[ 'author' ] = BackWPup::get_plugin_data( 'Author' );
  15. $this->info[ 'authorURI' ] = translate( BackWPup::get_plugin_data( 'AuthorURI' ), 'backwpup' );
  16. $this->info[ 'version' ] = BackWPup::get_plugin_data( 'Version' );
  17. }
  18. /**
  19. * @return array
  20. */
  21. public function option_defaults() {
  22. return array( 'dbcheckwponly' => TRUE, 'dbcheckmaintenance' => FALSE, 'dbcheckrepair' => FALSE );
  23. }
  24. /**
  25. * @param $jobid
  26. */
  27. public function edit_tab( $jobid ) {
  28. ?>
  29. <h3 class="title"><?php _e( 'Settings for database check', 'backwpup' ) ?></h3>
  30. <p></p>
  31. <table class="form-table">
  32. <tr valign="top">
  33. <th scope="row"><?php _e( 'WordPress tables only', 'backwpup' ); ?></th>
  34. <td>
  35. <label for="iddbcheckwponly">
  36. <input class="checkbox" value="1" id="iddbcheckwponly"
  37. type="checkbox" <?php checked( BackWPup_Option::get( $jobid, 'dbcheckwponly' ), TRUE ); ?>
  38. name="dbcheckwponly"/> <?php _e( 'Check WordPress database tables only', 'backwpup' ); ?>
  39. </label>
  40. </td>
  41. </tr>
  42. <tr valign="top">
  43. <th scope="row"><?php _e( 'Maintenance mode', 'backwpup' ); ?></th>
  44. <td>
  45. <label for="iddbcheckmaintenance">
  46. <input class="checkbox" value="1" id="iddbcheckmaintenance"
  47. type="checkbox" <?php checked( BackWPup_Option::get( $jobid, 'dbcheckmaintenance' ), TRUE ); ?>
  48. name="dbcheckmaintenance" /> <?php _e( 'Activate maintenance mode during table check', 'backwpup' ); ?>
  49. </label>
  50. </td>
  51. </tr>
  52. <tr valign="top">
  53. <th scope="row"><?php _e( 'Repair', 'backwpup' ); ?></th>
  54. <td>
  55. <label for="iddbcheckrepair">
  56. <input class="checkbox" value="1" id="iddbcheckrepair"
  57. type="checkbox" <?php checked( BackWPup_Option::get( $jobid, 'dbcheckrepair' ), TRUE ); ?>
  58. name="dbcheckrepair" /> <?php _e( 'Try to repair defect table', 'backwpup' ); ?>
  59. </label>
  60. </td>
  61. </tr>
  62. </table>
  63. <?php
  64. }
  65. /**
  66. * @param $jobid
  67. */
  68. public function edit_form_post_save( $jobid ) {
  69. BackWPup_Option::update( $jobid, 'dbcheckwponly', ( isset( $_POST[ 'dbcheckwponly' ] ) && $_POST[ 'dbcheckwponly' ] == 1 ) ? TRUE : FALSE );
  70. BackWPup_Option::update( $jobid, 'dbcheckmaintenance', ( isset( $_POST[ 'dbcheckmaintenance' ] ) && $_POST[ 'dbcheckmaintenance' ] == 1 ) ? TRUE : FALSE );
  71. BackWPup_Option::update( $jobid, 'dbcheckrepair', ( isset( $_POST[ 'dbcheckrepair' ] ) && $_POST[ 'dbcheckrepair' ] == 1 ) ? TRUE : FALSE );
  72. }
  73. /**
  74. * @param $job_object
  75. * @return bool
  76. */
  77. public function job_run( $job_object ) {
  78. global $wpdb;
  79. trigger_error( sprintf( __( '%d. Trying to check database&#160;&hellip;', 'backwpup' ), $job_object->steps_data[ $job_object->step_working ][ 'STEP_TRY' ] ), E_USER_NOTICE );
  80. if ( ! isset( $job_object->steps_data[ $job_object->step_working ][ 'DONETABLE' ] ) || ! is_array( $job_object->steps_data[ $job_object->step_working ][ 'DONETABLE' ] ) )
  81. $job_object->steps_data[ $job_object->step_working ][ 'DONETABLE' ] = array();
  82. //to check
  83. $tables = array();
  84. $restables = $wpdb->get_results( 'SHOW FULL TABLES FROM `' . DB_NAME . '`', ARRAY_N );
  85. foreach ( $restables as $table ) {
  86. if ( $job_object->job[ 'dbcheckwponly' ] && substr( $table[ 0 ], 0, strlen( $wpdb->prefix ) ) != $wpdb->prefix )
  87. continue;
  88. $tables[ ] = $table[ 0 ];
  89. $tablestype[ $table[ 0 ] ] = $table[ 1 ];
  90. }
  91. //Set num
  92. $job_object->substeps_todo = sizeof( $tables );
  93. //Get table status
  94. $resstatus = $wpdb->get_results( "SHOW TABLE STATUS FROM `" . DB_NAME . "`", ARRAY_A );
  95. foreach ( $resstatus as $tablestatus ) {
  96. $status[ $tablestatus[ 'Name' ] ] = $tablestatus;
  97. }
  98. //check tables
  99. if ( $job_object->substeps_todo > 0 ) {
  100. if ( ! empty( $job_object->job[ 'dbcheckmaintenance' ] ) )
  101. $job_object->set_maintenance_mode( TRUE );
  102. foreach ( $tables as $table ) {
  103. if ( in_array( $table, $job_object->steps_data[ $job_object->step_working ][ 'DONETABLE' ] ) )
  104. continue;
  105. if ( $tablestype[ $table ] == 'VIEW' ) {
  106. trigger_error( sprintf( __( 'Table %1$s is a view. Not checked.', 'backwpup' ), $table ), E_USER_NOTICE );
  107. continue;
  108. }
  109. if ( $status[ $table ][ 'Engine' ] != 'MyISAM' && $status[ $table ][ 'Engine' ] != 'InnoDB' ) {
  110. trigger_error( sprintf( __( 'Table %1$s is not a MyISAM/InnoDB table. Not checked.', 'backwpup' ), $table ), E_USER_NOTICE );
  111. continue;
  112. }
  113. //CHECK TABLE funktioniert bei MyISAM- und InnoDB-Tabellen (http://dev.mysql.com/doc/refman/5.1/de/check-table.html)
  114. $check = $wpdb->get_row( "CHECK TABLE `" . $table . "` MEDIUM", OBJECT );
  115. if ( $check->Msg_text == 'OK' )
  116. trigger_error( sprintf( __( 'Result of table check for %1$s is: %2$s', 'backwpup' ), $table, $check->Msg_text ), E_USER_NOTICE );
  117. elseif ( strtolower( $check->Msg_type ) == 'warning' )
  118. trigger_error( sprintf( __( 'Result of table check for %1$s is: %2$s', 'backwpup' ), $table, $check->Msg_text ), E_USER_WARNING );
  119. else
  120. trigger_error( sprintf( __( 'Result of table check for %1$s is: %2$s', 'backwpup' ), $table, $check->Msg_text ), E_USER_ERROR );
  121. //Try to Repair table
  122. if ( ! empty( $job_object->job[ 'dbcheckrepair' ] ) && $check->Msg_text != 'OK' && $status[ $table ][ 'Engine' ] == 'MyISAM' ) {
  123. $repair = $wpdb->get_row( 'REPAIR TABLE `' . $table . '` EXTENDED', OBJECT );
  124. if ( $repair->Msg_type == 'OK' )
  125. trigger_error( sprintf( __( 'Result of table repair for %1$s is: %2$s', 'backwpup' ), $table, $repair->Msg_text ), E_USER_NOTICE );
  126. elseif ( strtolower( $repair->Msg_type ) == 'warning' )
  127. trigger_error( sprintf( __( 'Result of table repair for %1$s is: %2$s', 'backwpup' ), $table, $repair->Msg_text ), E_USER_WARNING );
  128. else
  129. trigger_error( sprintf( __( 'Result of table repair for %1$s is: %2$s', 'backwpup' ), $table, $repair->Msg_text ), E_USER_ERROR );
  130. }
  131. $job_object->steps_data[ $job_object->step_working ][ 'DONETABLE' ][ ] = $table;
  132. $job_object->substeps_done ++;
  133. }
  134. $job_object->set_maintenance_mode( FALSE );
  135. trigger_error( __( 'Database check done!', 'backwpup' ), E_USER_NOTICE );
  136. }
  137. else {
  138. trigger_error( __( 'No tables to check.', 'backwpup' ), E_USER_WARNING );
  139. }
  140. unset( $job_object->steps_data[ $job_object->step_working ][ 'DONETABLE' ] );
  141. return TRUE;
  142. }
  143. }