populateBacklinkNamespace.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Optional upgrade script to populate *_from_namespace fields
  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 *_from_namespace fields
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class PopulateBacklinkNamespace extends LoggedUpdateMaintenance {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Populate the *_from_namespace fields' );
  33. $this->addOption( 'lastUpdatedId', "Highest page_id with updated links", false, true );
  34. }
  35. protected function getUpdateKey() {
  36. return 'populate *_from_namespace';
  37. }
  38. protected function updateSkippedMessage() {
  39. return '*_from_namespace column of backlink tables already populated.';
  40. }
  41. public function doDBUpdates() {
  42. $force = $this->getOption( 'force' );
  43. $db = $this->getDB( DB_MASTER );
  44. $this->output( "Updating *_from_namespace fields in links tables.\n" );
  45. $start = $this->getOption( 'lastUpdatedId' );
  46. if ( !$start ) {
  47. $start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
  48. }
  49. if ( !$start ) {
  50. $this->output( "Nothing to do." );
  51. return false;
  52. }
  53. $end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
  54. # Do remaining chunk
  55. $end += $this->mBatchSize - 1;
  56. $blockStart = $start;
  57. $blockEnd = $start + $this->mBatchSize - 1;
  58. while ( $blockEnd <= $end ) {
  59. $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
  60. $cond = "page_id BETWEEN $blockStart AND $blockEnd";
  61. $res = $db->select( 'page', [ 'page_id', 'page_namespace' ], $cond, __METHOD__ );
  62. foreach ( $res as $row ) {
  63. $db->update( 'pagelinks',
  64. [ 'pl_from_namespace' => $row->page_namespace ],
  65. [ 'pl_from' => $row->page_id ],
  66. __METHOD__
  67. );
  68. $db->update( 'templatelinks',
  69. [ 'tl_from_namespace' => $row->page_namespace ],
  70. [ 'tl_from' => $row->page_id ],
  71. __METHOD__
  72. );
  73. $db->update( 'imagelinks',
  74. [ 'il_from_namespace' => $row->page_namespace ],
  75. [ 'il_from' => $row->page_id ],
  76. __METHOD__
  77. );
  78. }
  79. $blockStart += $this->mBatchSize - 1;
  80. $blockEnd += $this->mBatchSize - 1;
  81. wfWaitForSlaves();
  82. }
  83. return true;
  84. }
  85. }
  86. $maintClass = "PopulateBacklinkNamespace";
  87. require_once RUN_MAINTENANCE_IF_MAIN;