Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/247783

Change subject: Add script to fix content model of JSON pages
......................................................................

Add script to fix content model of JSON pages

MediaWiki:Foo.json and User:Foo/bar.json pages now have a default
content model of JSON, but existing pages using those names will be set
to defaults of wikitext.

The content models of those pages are now set to "json", unless it has
invalid syntax, in which case it will be set to "wikitext".

For convenience, the script is automatically run as part of update.php.

Bug: T108663
Change-Id: I1412937ccea8e65dba58580beec79cbf2286ae01
---
M autoload.php
M includes/installer/DatabaseUpdater.php
M includes/installer/MssqlUpdater.php
M includes/installer/MysqlUpdater.php
M includes/installer/OracleUpdater.php
M includes/installer/PostgresUpdater.php
M includes/installer/SqliteUpdater.php
A maintenance/fixDefaultJsonContentPages.php
8 files changed, 128 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/83/247783/1

diff --git a/autoload.php b/autoload.php
index b35b683..21130d5 100644
--- a/autoload.php
+++ b/autoload.php
@@ -446,6 +446,7 @@
        'FindHooks' => __DIR__ . '/maintenance/findHooks.php',
        'FindMissingFiles' => __DIR__ . '/maintenance/findMissingFiles.php',
        'FixBug20757' => __DIR__ . '/maintenance/storage/fixBug20757.php',
+       'FixDefaultJsonContentPages' => __DIR__ . 
'/maintenance/fixDefaultJsonContentPages.php',
        'FixDoubleRedirects' => __DIR__ . '/maintenance/fixDoubleRedirects.php',
        'FixExtLinksProtocolRelative' => __DIR__ . 
'/maintenance/fixExtLinksProtocolRelative.php',
        'FixTimestamps' => __DIR__ . '/maintenance/fixTimestamps.php',
diff --git a/includes/installer/DatabaseUpdater.php 
b/includes/installer/DatabaseUpdater.php
index 57084cb..41c2554 100644
--- a/includes/installer/DatabaseUpdater.php
+++ b/includes/installer/DatabaseUpdater.php
@@ -1136,4 +1136,9 @@
                        $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;
                }
        }
+
+       protected function doFixDefaultJsonContentPages() {
+               $fix = $this->maintenance->runChild( 
'FixDefaultJsonContentPages' );
+               $fix->execute();
+       }
 }
diff --git a/includes/installer/MssqlUpdater.php 
b/includes/installer/MssqlUpdater.php
index 164cfab..09d5e4f 100644
--- a/includes/installer/MssqlUpdater.php
+++ b/includes/installer/MssqlUpdater.php
@@ -64,6 +64,9 @@
                                'patch-oi_major_mime-chemical.sql' ),
                        array( 'modifyField', 'filearchive', 'fa_major_mime',
                                'patch-fa_major_mime-chemical.sql' ),
+
+                       // 1.26
+                       array( 'doFixDefaultJsonContentPages' ),
                );
        }
 
diff --git a/includes/installer/MysqlUpdater.php 
b/includes/installer/MysqlUpdater.php
index aa60c01..365cb40 100644
--- a/includes/installer/MysqlUpdater.php
+++ b/includes/installer/MysqlUpdater.php
@@ -275,6 +275,7 @@
                        array( 'dropTable', 'hitcounter' ),
                        array( 'dropField', 'site_stats', 'ss_total_views', 
'patch-drop-ss_total_views.sql' ),
                        array( 'dropField', 'page', 'page_counter', 
'patch-drop-page_counter.sql' ),
+                       array( 'doFixDefaultJsonContentPages' ),
                );
        }
 
diff --git a/includes/installer/OracleUpdater.php 
b/includes/installer/OracleUpdater.php
index 03dbd1c..9c36e48 100644
--- a/includes/installer/OracleUpdater.php
+++ b/includes/installer/OracleUpdater.php
@@ -108,6 +108,9 @@
                        array( 'dropField', 'site_stats', 'ss_total_views', 
'patch-drop-ss_total_views.sql' ),
                        array( 'dropField', 'page', 'page_counter', 
'patch-drop-page_counter.sql' ),
 
+                       // 1.26
+                       array( 'doFixDefaultJsonContentPages' ),
+
                        // KEEP THIS AT THE BOTTOM!!
                        array( 'doRebuildDuplicateFunction' ),
 
diff --git a/includes/installer/PostgresUpdater.php 
b/includes/installer/PostgresUpdater.php
index 7414d92..7f1397b 100644
--- a/includes/installer/PostgresUpdater.php
+++ b/includes/installer/PostgresUpdater.php
@@ -424,7 +424,10 @@
                        array( 'dropTable', 'hitcounter' ),
                        array( 'dropField', 'site_stats', 'ss_total_views', 
'patch-drop-ss_total_views.sql' ),
                        array( 'dropField', 'page', 'page_counter', 
'patch-drop-page_counter.sql' ),
-                       array( 'dropFkey', 'recentchanges', 'rc_cur_id' )
+                       array( 'dropFkey', 'recentchanges', 'rc_cur_id' ),
+
+                       // 1.26
+                       array( 'doFixDefaultJsonContentPages' ),
                );
        }
 
diff --git a/includes/installer/SqliteUpdater.php 
b/includes/installer/SqliteUpdater.php
index 0de1fd7..114c40c 100644
--- a/includes/installer/SqliteUpdater.php
+++ b/includes/installer/SqliteUpdater.php
@@ -144,6 +144,9 @@
                        array( 'dropField', 'site_stats', 'ss_total_views', 
'patch-drop-ss_total_views.sql' ),
                        array( 'dropField', 'page', 'page_counter', 
'patch-drop-page_counter.sql' ),
                        array( 'modifyField', 'filearchive', 
'fa_deleted_reason', 'patch-editsummary-length.sql' ),
+
+                       // 1.26
+                       array( 'doFixDefaultJsonContentPages' ),
                );
        }
 
diff --git a/maintenance/fixDefaultJsonContentPages.php 
b/maintenance/fixDefaultJsonContentPages.php
new file mode 100644
index 0000000..efb4c89
--- /dev/null
+++ b/maintenance/fixDefaultJsonContentPages.php
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Fix instances of pre-existing JSON pages
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Maintenance
+ */
+
+require_once __DIR__ . '/Maintenance.php';
+
+/**
+ * Usage:
+ *  fixDefaultJsonContentPages.php
+ *
+ * It is automatically run by update.php
+ */
+class FixDefaultJsonContentPages extends LoggedUpdateMaintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = 'Fix instances of JSON pages prior to 
them being the ContentHandler default';
+               $this->setBatchSize( 100 );
+       }
+
+       protected function getUpdateKey() {
+               return __CLASS__;
+       }
+
+       protected function doDBUpdates() {
+               $dbr = wfGetDB( DB_SLAVE );
+               $namespaces = array(
+                       NS_MEDIAWIKI => $dbr->buildLike( $dbr->anyString(), 
'.json' ),
+                       NS_USER => $dbr->buildLike( $dbr->anyString(), '/', 
$dbr->anyString(), '.json' ),
+               );
+               foreach ( $namespaces as $ns => $like ) {
+                       $lastPage = 0;
+                       do {
+                               $rows = $dbr->select(
+                                               'page',
+                                               array( 'page_id', 'page_title', 
'page_namespace', 'page_content_model' ),
+                                               array(
+                                                               
'page_namespace' => $ns,
+                                                               'page_title ' . 
$like,
+                                                               'page_id > ' . 
$dbr->addQuotes( $lastPage )
+                                               ),
+                                               __METHOD__,
+                                               array( 'ORDER BY' => 'page_id', 
'LIMIT' => $this->mBatchSize )
+                               );
+                               foreach ( $rows as $row ) {
+                                       $this->handleRow( $row );
+                               }
+                       } while ( $rows->numRows() >= $this->mBatchSize );
+               }
+
+               return true;
+       }
+
+       protected function handleRow( stdClass $row ) {
+               $title = Title::makeTitle( $row->page_namespace, 
$row->page_title );
+               $this->output( "Processing {$title} ({$row->page_id})...\n" );
+               $rev = Revision::newFromTitle( $title );
+               $content = $rev->getContent( Revision::RAW );
+               if ( $content instanceof JsonContent ) {
+                       if ( $content->isValid() ) {
+                               // Yay, actually JSON.
+                               $this->output( "Setting page_content_model to 
json..." );
+                               wfGetDB( DB_MASTER )->update(
+                                       'page',
+                                       array( 'page_content_model' => 'json' ),
+                                       array( 'page_id' => $row->page_id ),
+                                       __METHOD__
+                               );
+                               $this->output( "done.\n" );
+                               wfWaitForSlaves();
+                       } else {
+                               // Not JSON...force it to wikitext.
+                               $this->output( "Setting rev_content_model to 
wikitext..." );
+                               wfGetDB( DB_MASTER )->update(
+                                       'revision',
+                                       array( 'rev_content_model' => 
'wikitext' ),
+                                       array( 'rev_page' => $row->page_id ),
+                                       __METHOD__
+                               );
+                               $this->output( "done.\n" );
+                               wfWaitForSlaves();
+                       }
+               } else {
+                       $this->output( "not a JSON page? Skipping\n" );
+               }
+       }
+}
+
+$maintClass = 'FixDefaultJsonContentPages';
+require_once RUN_MAINTENANCE_IF_MAIN;

-- 
To view, visit https://gerrit.wikimedia.org/r/247783
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1412937ccea8e65dba58580beec79cbf2286ae01
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to