fixTimestamps.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Fixes timestamp corruption caused by one or more webservers temporarily
  4. * being set to the wrong time.
  5. * The time offset must be known and consistent. Start and end times
  6. * (in 14-character format) restrict the search, and must bracket the damage.
  7. * There must be a majority of good timestamps in the search period.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. * @ingroup Maintenance
  26. */
  27. require_once __DIR__ . '/Maintenance.php';
  28. /**
  29. * Maintenance script that fixes timestamp corruption caused by one or
  30. * more webservers temporarily being set to the wrong time.
  31. *
  32. * @ingroup Maintenance
  33. */
  34. class FixTimestamps extends Maintenance {
  35. public function __construct() {
  36. parent::__construct();
  37. $this->addDescription( '' );
  38. $this->addArg( 'offset', '' );
  39. $this->addArg( 'start', 'Starting timestamp' );
  40. $this->addArg( 'end', 'Ending timestamp' );
  41. }
  42. public function execute() {
  43. $offset = $this->getArg( 0 ) * 3600;
  44. $start = $this->getArg( 1 );
  45. $end = $this->getArg( 2 );
  46. $grace = 60; // maximum normal clock offset
  47. # Find bounding revision IDs
  48. $dbw = $this->getDB( DB_MASTER );
  49. $revisionTable = $dbw->tableName( 'revision' );
  50. $res = $dbw->query( "SELECT MIN(rev_id) as minrev, MAX(rev_id) as maxrev FROM $revisionTable " .
  51. "WHERE rev_timestamp BETWEEN '{$start}' AND '{$end}'", __METHOD__ );
  52. $row = $dbw->fetchObject( $res );
  53. if ( is_null( $row->minrev ) ) {
  54. $this->error( "No revisions in search period.", true );
  55. }
  56. $minRev = $row->minrev;
  57. $maxRev = $row->maxrev;
  58. # Select all timestamps and IDs
  59. $sql = "SELECT rev_id, rev_timestamp FROM $revisionTable " .
  60. "WHERE rev_id BETWEEN $minRev AND $maxRev";
  61. if ( $offset > 0 ) {
  62. $sql .= " ORDER BY rev_id DESC";
  63. $expectedSign = -1;
  64. } else {
  65. $expectedSign = 1;
  66. }
  67. $res = $dbw->query( $sql, __METHOD__ );
  68. $lastNormal = 0;
  69. $badRevs = [];
  70. $numGoodRevs = 0;
  71. foreach ( $res as $row ) {
  72. $timestamp = wfTimestamp( TS_UNIX, $row->rev_timestamp );
  73. $delta = $timestamp - $lastNormal;
  74. $sign = $delta == 0 ? 0 : $delta / abs( $delta );
  75. if ( $sign == 0 || $sign == $expectedSign ) {
  76. // Monotonic change
  77. $lastNormal = $timestamp;
  78. ++$numGoodRevs;
  79. continue;
  80. } elseif ( abs( $delta ) <= $grace ) {
  81. // Non-monotonic change within grace interval
  82. ++$numGoodRevs;
  83. continue;
  84. } else {
  85. // Non-monotonic change larger than grace interval
  86. $badRevs[] = $row->rev_id;
  87. }
  88. }
  89. $numBadRevs = count( $badRevs );
  90. if ( $numBadRevs > $numGoodRevs ) {
  91. $this->error(
  92. "The majority of revisions in the search interval are marked as bad.
  93. Are you sure the offset ($offset) has the right sign? Positive means the clock
  94. was incorrectly set forward, negative means the clock was incorrectly set back.
  95. If the offset is right, then increase the search interval until there are enough
  96. good revisions to provide a majority reference.", true );
  97. } elseif ( $numBadRevs == 0 ) {
  98. $this->output( "No bad revisions found.\n" );
  99. exit( 0 );
  100. }
  101. $this->output( sprintf( "Fixing %d revisions (%.2f%% of revisions in search interval)\n",
  102. $numBadRevs, $numBadRevs / ( $numGoodRevs + $numBadRevs ) * 100 ) );
  103. $fixup = -$offset;
  104. $sql = "UPDATE $revisionTable " .
  105. "SET rev_timestamp="
  106. . "DATE_FORMAT(DATE_ADD(rev_timestamp, INTERVAL $fixup SECOND), '%Y%m%d%H%i%s') " .
  107. "WHERE rev_id IN (" . $dbw->makeList( $badRevs ) . ')';
  108. $dbw->query( $sql, __METHOD__ );
  109. $this->output( "Done\n" );
  110. }
  111. }
  112. $maintClass = "FixTimestamps";
  113. require_once RUN_MAINTENANCE_IF_MAIN;