MaxSem has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/371587 )

Change subject: Add deleteWiki.php
......................................................................

Add deleteWiki.php

Maintains database integrity to remove references to a wiki being
deleted from various global databases.

Change-Id: I2dce175d5f9a357b5b9de2bfc519e5f759dfdce7
---
A deleteWiki.php
1 file changed, 99 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikimediaMaintenance 
refs/changes/87/371587/1

diff --git a/deleteWiki.php b/deleteWiki.php
new file mode 100644
index 0000000..d1c11aa
--- /dev/null
+++ b/deleteWiki.php
@@ -0,0 +1,99 @@
+<?php
+/**
+ * Cleanup references to a deleted wiki. Assumes MySQL because it's written 
for WMF usage.
+ *
+ * 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
+ * @ingroup Wikimedia
+ */
+
+use MediaWiki\MediaWikiServices;
+
+require_once __DIR__ . '/WikimediaMaintenance.php';
+
+class DeleteWiki extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->addDescription( 'Cleans up databases from references to 
a deleted wiki, but does '
+                       . 'not delete anything from the wiki itself.'
+               );
+
+               $this->addArg( 'wiki', 'Wiki being deleted', true );
+               $this->setBatchSize( 500 );
+       }
+
+       /**
+        * Nuke all the datas!
+        */
+       public function execute() {
+               $wiki = $this->getArg( 0 );
+
+               $this->output( "ATTENTION: All references to $wiki are going to 
be irreversibly DELETED! "
+                       . "Press Ctrl+C to abort.\n"
+               );
+               if ( !$this->isQuiet() ) {
+                       wfCountDown( 10 );
+               }
+
+               $this->output( "Cleaning up global image links...\n" );
+               $this->cleanupWikiDb( 'commonswiki', "DELETE FROM 
globalimagelinks WHERE gil_wiki='$wiki'" );
+
+               $this->output( "Cleaning up CentralAuth localnames...\n" );
+               $this->cleanupWikiDb( 'centralauth', "DELETE FROM localnames 
WHERE ln_wiki='$wiki'" );
+
+               $this->output( "Cleaning up CentralAuth localuser...\n" );
+               $this->cleanupWikiDb( 'centralauth', "DELETE FROM localuser 
WHERE lu_wiki='$wiki'" );
+
+               $this->output( "Cleaning up CentralAuth renameuser_status...\n" 
);
+               $this->cleanupWikiDb( 'centralauth', "DELETE FROM 
renameuser_status WHERE ru_wiki='$wiki'" );
+
+               // @TODO: wikiset
+
+               $this->output( "Cleaning up CentralAuth renameuser_queue...\n" 
);
+               $this->cleanupWikiDb( 'centralauth', "DELETE FROM 
renameuser_queue WHERE rq_wiki='$wiki'" );
+
+               $this->output( "Cleaning up CentralAuth users_to_rename...\n" );
+               $this->cleanupWikiDb( 'centralauth', "DELETE FROM 
users_to_rename WHERE utr_wiki='$wiki'" );
+       }
+
+       /**
+        * Perform cleanup of a wiki
+        *
+        * @param string $wiki Wiki to operate on (NOT the wiki being deleted!)
+        * @param string $query Query to run in batches
+        */
+       private function cleanupWikiDb( $wiki, $query ) {
+               $lbFactory = 
MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $lb = $lbFactory->getMainLB( $wiki );
+               $dbw = $lb->getConnection( DB_MASTER );
+
+               $query .= " LIMIT {$this->mBatchSize}";
+               $count = 0;
+               do {
+                       $dbw->query( $query, __METHOD__ );
+                       $lbFactory->waitForReplication( [ 'wiki' => $wiki ] );
+                       $count += $dbw->affectedRows();
+                       $this->output( "  $count\n" );
+               } while ( $dbw->affectedRows() >= $this->mBatchSize );
+
+               $lb->reuseConnection( $dbw );
+       }
+}
+
+$maintClass = 'DeleteWiki';
+require_once RUN_MAINTENANCE_IF_MAIN;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2dce175d5f9a357b5b9de2bfc519e5f759dfdce7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikimediaMaintenance
Gerrit-Branch: master
Gerrit-Owner: MaxSem <maxsem.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to