populateRecentChangesSource.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Upgrade script to populate the rc_source field
  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 to populate the rc_source field.
  26. *
  27. * @ingroup Maintenance
  28. * @since 1.22
  29. */
  30. class PopulateRecentChangesSource extends LoggedUpdateMaintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription(
  34. 'Populates rc_source field of the recentchanges table with the data in rc_type.' );
  35. $this->setBatchSize( 100 );
  36. }
  37. protected function doDBUpdates() {
  38. $dbw = $this->getDB( DB_MASTER );
  39. if ( !$dbw->fieldExists( 'recentchanges', 'rc_source' ) ) {
  40. $this->error( 'rc_source field in recentchanges table does not exist.' );
  41. }
  42. $start = $dbw->selectField( 'recentchanges', 'MIN(rc_id)', false, __METHOD__ );
  43. if ( !$start ) {
  44. $this->output( "Nothing to do.\n" );
  45. return true;
  46. }
  47. $end = $dbw->selectField( 'recentchanges', 'MAX(rc_id)', false, __METHOD__ );
  48. $end += $this->mBatchSize - 1;
  49. $blockStart = $start;
  50. $blockEnd = $start + $this->mBatchSize - 1;
  51. $updatedValues = $this->buildUpdateCondition( $dbw );
  52. while ( $blockEnd <= $end ) {
  53. $cond = "rc_id BETWEEN $blockStart AND $blockEnd";
  54. $dbw->update(
  55. 'recentchanges',
  56. [ $updatedValues ],
  57. [
  58. "rc_source = ''",
  59. "rc_id BETWEEN $blockStart AND $blockEnd"
  60. ],
  61. __METHOD__
  62. );
  63. $this->output( "." );
  64. wfWaitForSlaves();
  65. $blockStart += $this->mBatchSize;
  66. $blockEnd += $this->mBatchSize;
  67. }
  68. $this->output( "\nDone.\n" );
  69. }
  70. protected function getUpdateKey() {
  71. return __CLASS__;
  72. }
  73. protected function buildUpdateCondition( DatabaseBase $dbw ) {
  74. $rcNew = $dbw->addQuotes( RC_NEW );
  75. $rcSrcNew = $dbw->addQuotes( RecentChange::SRC_NEW );
  76. $rcEdit = $dbw->addQuotes( RC_EDIT );
  77. $rcSrcEdit = $dbw->addQuotes( RecentChange::SRC_EDIT );
  78. $rcLog = $dbw->addQuotes( RC_LOG );
  79. $rcSrcLog = $dbw->addQuotes( RecentChange::SRC_LOG );
  80. $rcExternal = $dbw->addQuotes( RC_EXTERNAL );
  81. $rcSrcExternal = $dbw->addQuotes( RecentChange::SRC_EXTERNAL );
  82. return "rc_source = CASE
  83. WHEN rc_type = $rcNew THEN $rcSrcNew
  84. WHEN rc_type = $rcEdit THEN $rcSrcEdit
  85. WHEN rc_type = $rcLog THEN $rcSrcLog
  86. WHEN rc_type = $rcExternal THEN $rcSrcExternal
  87. ELSE ''
  88. END";
  89. }
  90. }
  91. $maintClass = "PopulateRecentChangesSource";
  92. require_once RUN_MAINTENANCE_IF_MAIN;