fixDefaultJsonContentPages.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Fix instances of pre-existing JSON pages
  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. * Usage:
  26. * fixDefaultJsonContentPages.php
  27. *
  28. * It is automatically run by update.php
  29. */
  30. class FixDefaultJsonContentPages extends LoggedUpdateMaintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription(
  34. 'Fix instances of JSON pages prior to them being the ContentHandler default' );
  35. $this->setBatchSize( 100 );
  36. }
  37. protected function getUpdateKey() {
  38. return __CLASS__;
  39. }
  40. protected function doDBUpdates() {
  41. if ( !$this->getConfig()->get( 'ContentHandlerUseDB' ) ) {
  42. $this->output( "\$wgContentHandlerUseDB is not enabled, nothing to do.\n" );
  43. return true;
  44. }
  45. $dbr = $this->getDB( DB_REPLICA );
  46. $namespaces = [
  47. NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), '.json' ),
  48. NS_USER => $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString(), '.json' ),
  49. ];
  50. foreach ( $namespaces as $ns => $like ) {
  51. $lastPage = 0;
  52. do {
  53. $rows = $dbr->select(
  54. 'page',
  55. [ 'page_id', 'page_title', 'page_namespace', 'page_content_model' ],
  56. [
  57. 'page_namespace' => $ns,
  58. 'page_title ' . $like,
  59. 'page_id > ' . $dbr->addQuotes( $lastPage )
  60. ],
  61. __METHOD__,
  62. [ 'ORDER BY' => 'page_id', 'LIMIT' => $this->getBatchSize() ]
  63. );
  64. foreach ( $rows as $row ) {
  65. $this->handleRow( $row );
  66. }
  67. } while ( $rows->numRows() >= $this->getBatchSize() );
  68. }
  69. return true;
  70. }
  71. protected function handleRow( stdClass $row ) {
  72. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  73. $this->output( "Processing {$title} ({$row->page_id})...\n" );
  74. $rev = Revision::newFromTitle( $title );
  75. $content = $rev->getContent( Revision::RAW );
  76. $dbw = $this->getDB( DB_MASTER );
  77. if ( $content instanceof JsonContent ) {
  78. if ( $content->isValid() ) {
  79. // Yay, actually JSON. We need to just change the
  80. // page_content_model because revision will automatically
  81. // use the default, which is *now* JSON.
  82. $this->output( "Setting page_content_model to json..." );
  83. $dbw->update(
  84. 'page',
  85. [ 'page_content_model' => CONTENT_MODEL_JSON ],
  86. [ 'page_id' => $row->page_id ],
  87. __METHOD__
  88. );
  89. $this->output( "done.\n" );
  90. wfWaitForSlaves();
  91. } else {
  92. // Not JSON...force it to wikitext. We need to update the
  93. // revision table so that these revisions are always processed
  94. // as wikitext in the future. page_content_model is already
  95. // set to "wikitext".
  96. $this->output( "Setting rev_content_model to wikitext..." );
  97. // Grab all the ids for batching
  98. $ids = $dbw->selectFieldValues(
  99. 'revision',
  100. 'rev_id',
  101. [ 'rev_page' => $row->page_id ],
  102. __METHOD__
  103. );
  104. foreach ( array_chunk( $ids, 50 ) as $chunk ) {
  105. $dbw->update(
  106. 'revision',
  107. [ 'rev_content_model' => CONTENT_MODEL_WIKITEXT ],
  108. [ 'rev_page' => $row->page_id, 'rev_id' => $chunk ]
  109. );
  110. wfWaitForSlaves();
  111. }
  112. $this->output( "done.\n" );
  113. }
  114. } else {
  115. $this->output( "not a JSON page? Skipping\n" );
  116. }
  117. }
  118. }
  119. $maintClass = FixDefaultJsonContentPages::class;
  120. require_once RUN_MAINTENANCE_IF_MAIN;