fixExtLinksProtocolRelative.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Fixes any entries for protocol-relative URLs in the externallinks table,
  4. * replacing each protocol-relative entry with two entries, one for http
  5. * and one for https.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup Maintenance
  24. */
  25. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script that fixes any entriy for protocol-relative URLs
  28. * in the externallinks table.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class FixExtLinksProtocolRelative extends LoggedUpdateMaintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription(
  36. 'Fixes any entries in the externallinks table containing protocol-relative URLs' );
  37. }
  38. protected function getUpdateKey() {
  39. return 'fix protocol-relative URLs in externallinks';
  40. }
  41. protected function updateSkippedMessage() {
  42. return 'protocol-relative URLs in externallinks table already fixed.';
  43. }
  44. protected function doDBUpdates() {
  45. $db = $this->getDB( DB_MASTER );
  46. if ( !$db->tableExists( 'externallinks' ) ) {
  47. $this->error( "externallinks table does not exist" );
  48. return false;
  49. }
  50. $this->output( "Fixing protocol-relative entries in the externallinks table...\n" );
  51. $res = $db->select( 'externallinks', [ 'el_from', 'el_to', 'el_index' ],
  52. [ 'el_index' . $db->buildLike( '//', $db->anyString() ) ],
  53. __METHOD__
  54. );
  55. $count = 0;
  56. foreach ( $res as $row ) {
  57. $count++;
  58. if ( $count % 100 == 0 ) {
  59. $this->output( $count . "\n" );
  60. wfWaitForSlaves();
  61. }
  62. $db->insert( 'externallinks',
  63. [
  64. [
  65. 'el_id' => $db->nextSequenceValue( 'externallinks_el_id_seq' ),
  66. 'el_from' => $row->el_from,
  67. 'el_to' => $row->el_to,
  68. 'el_index' => "http:{$row->el_index}",
  69. ],
  70. [
  71. 'el_id' => $db->nextSequenceValue( 'externallinks_el_id_seq' ),
  72. 'el_from' => $row->el_from,
  73. 'el_to' => $row->el_to,
  74. 'el_index' => "https:{$row->el_index}",
  75. ]
  76. ], __METHOD__, [ 'IGNORE' ]
  77. );
  78. $db->delete(
  79. 'externallinks',
  80. [
  81. 'el_index' => $row->el_index,
  82. 'el_from' => $row->el_from,
  83. 'el_to' => $row->el_to
  84. ],
  85. __METHOD__
  86. );
  87. }
  88. $this->output( "Done, $count rows updated.\n" );
  89. return true;
  90. }
  91. }
  92. $maintClass = "FixExtLinksProtocolRelative";
  93. require_once RUN_MAINTENANCE_IF_MAIN;