populateRevisionLength.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Populates the rev_len and ar_len fields when they are NULL.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
  26. * This is the case for all revisions created before MW 1.10, as well as those affected
  27. * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class PopulateRevisionLength extends LoggedUpdateMaintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addDescription( 'Populates the rev_len and ar_len fields' );
  35. $this->setBatchSize( 200 );
  36. }
  37. protected function getUpdateKey() {
  38. return 'populate rev_len and ar_len';
  39. }
  40. public function doDBUpdates() {
  41. $dbw = $this->getDB( DB_MASTER );
  42. if ( !$dbw->tableExists( 'revision' ) ) {
  43. $this->error( "revision table does not exist", true );
  44. } elseif ( !$dbw->tableExists( 'archive' ) ) {
  45. $this->error( "archive table does not exist", true );
  46. } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
  47. $this->output( "rev_len column does not exist\n\n", true );
  48. return false;
  49. }
  50. $this->output( "Populating rev_len column\n" );
  51. $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::selectFields() );
  52. $this->output( "Populating ar_len column\n" );
  53. $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::selectArchiveFields() );
  54. $this->output( "rev_len and ar_len population complete "
  55. . "[$rev revision rows, $ar archive rows].\n" );
  56. return true;
  57. }
  58. /**
  59. * @param string $table
  60. * @param string $idCol
  61. * @param string $prefix
  62. * @param array $fields
  63. * @return int
  64. */
  65. protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
  66. $dbr = $this->getDB( DB_REPLICA );
  67. $dbw = $this->getDB( DB_MASTER );
  68. $start = $dbw->selectField( $table, "MIN($idCol)", false, __METHOD__ );
  69. $end = $dbw->selectField( $table, "MAX($idCol)", false, __METHOD__ );
  70. if ( !$start || !$end ) {
  71. $this->output( "...$table table seems to be empty.\n" );
  72. return 0;
  73. }
  74. # Do remaining chunks
  75. $blockStart = intval( $start );
  76. $blockEnd = intval( $start ) + $this->mBatchSize - 1;
  77. $count = 0;
  78. while ( $blockStart <= $end ) {
  79. $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
  80. $res = $dbr->select(
  81. $table,
  82. $fields,
  83. [
  84. "$idCol >= $blockStart",
  85. "$idCol <= $blockEnd",
  86. "{$prefix}_len IS NULL"
  87. ],
  88. __METHOD__
  89. );
  90. if ( $res->numRows() > 0 ) {
  91. $this->beginTransaction( $dbw, __METHOD__ );
  92. # Go through and update rev_len from these rows.
  93. foreach ( $res as $row ) {
  94. if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
  95. $count++;
  96. }
  97. }
  98. $this->commitTransaction( $dbw, __METHOD__ );
  99. }
  100. $blockStart += $this->mBatchSize;
  101. $blockEnd += $this->mBatchSize;
  102. wfWaitForSlaves();
  103. }
  104. return $count;
  105. }
  106. /**
  107. * @param stdClass $row
  108. * @param string $table
  109. * @param string $idCol
  110. * @param string $prefix
  111. * @return bool
  112. */
  113. protected function upgradeRow( $row, $table, $idCol, $prefix ) {
  114. $dbw = $this->getDB( DB_MASTER );
  115. $rev = ( $table === 'archive' )
  116. ? Revision::newFromArchiveRow( $row )
  117. : new Revision( $row );
  118. $content = $rev->getContent();
  119. if ( !$content ) {
  120. # This should not happen, but sometimes does (bug 20757)
  121. $id = $row->$idCol;
  122. $this->output( "Content of $table $id unavailable!\n" );
  123. return false;
  124. }
  125. # Update the row...
  126. $dbw->update( $table,
  127. [ "{$prefix}_len" => $content->getSize() ],
  128. [ $idCol => $row->$idCol ],
  129. __METHOD__
  130. );
  131. return true;
  132. }
  133. }
  134. $maintClass = "PopulateRevisionLength";
  135. require_once RUN_MAINTENANCE_IF_MAIN;