Aude has uploaded a new change for review.

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

Change subject: Add script for rebuilding site_identifiers table
......................................................................

Add script for rebuilding site_identifiers table

this is absolutely not anything fancy, but
think this is a safe approach for this task.

we don't want to touch contents of the sites table.

Bug: T99796
Change-Id: Ie09953baf38d78b00896aafb014231f3f7ea6094
---
A fixSiteIdentifiers.php
1 file changed, 124 insertions(+), 0 deletions(-)


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

diff --git a/fixSiteIdentifiers.php b/fixSiteIdentifiers.php
new file mode 100644
index 0000000..bae3dc9
--- /dev/null
+++ b/fixSiteIdentifiers.php
@@ -0,0 +1,124 @@
+<?php
+
+$IP = getenv( 'MW_INSTALL_PATH' );
+
+if ( $IP === false ) {
+       $IP = __DIR__ . '/../..';
+}
+
+require_once "$IP/maintenance/Maintenance.php";
+
+/**
+ * Rebuild the site_identifiers table based on contents
+ * of the sites table.
+ *
+ * bug: T99796
+ */
+class SiteIdentifiersBuilder extends Maintenance {
+
+       /**
+        * @var SiteStore
+        */
+       private $siteStore;
+
+       public function __construct( SiteStore $siteStore, $siteId ) {
+               parent::__construct();
+
+               $this->siteStore = $siteStore;
+               $this->siteId = $siteId;
+       }
+
+       public function rebuild() {
+               $thisSite = $this->siteStore->getSite( $this->siteId );
+               $thisSiteGroup = $thisSite->getGroup();
+
+               $dbw = wfGetDB( DB_MASTER );
+
+               $dbw->startAtomic( __METHOD__ );
+
+               $this->purgeSiteIdentifiers( $dbw );
+               $this->populateSiteIdentifiers( $dbw, $thisSiteGroup );
+
+               $dbw->endAtomic( __METHOD__ );
+
+               $this->siteStore->reset();
+       }
+
+       private function purgeSiteIdentifiers( DatabaseBase $dbw ) {
+               $siteRows = $dbw->select(
+                       'site_identifiers',
+                       'DISTINCT(si_key)',
+                       '',
+                       __METHOD__,
+                       array(
+                               'ORDER BY' => 'si_key'
+                       )
+               );
+
+               $languages = array();
+
+               foreach( $siteRows as $siteRow ) {
+                       $languages[] = $siteRow->si_key;
+               }
+
+               foreach( $languages as $language ) {
+                       $dbw->delete(
+                               'site_identifiers',
+                               array( 'si_key' => $language ),
+                               __METHOD__
+                       );
+               }
+       }
+
+       private function populateSiteIdentifiers( DatabaseBase $dbw, 
$thisSiteGroup ) {
+               $rows = array();
+
+               $sites = $this->siteStore->getSites();
+
+               foreach( $sites as $site ) {
+                       $rows = array();
+
+                       if ( $site->getGroup() === $thisSiteGroup ) {
+                               $rows[] = $this->getEquivalentRow( $site );
+                               $rows[] = $this->getInterwikiRow( $site );
+                       }
+
+                       $dbw->insert(
+                               'site_identifiers',
+                               $rows
+                       );
+
+               }
+       }
+
+       private function getEquivalentRow( Site $site ) {
+               return array(
+                       'si_site' => $site->getInternalId(),
+                       'si_type' => 'equivalent',
+                       'si_key' => $site->getLanguageCode()
+               );
+       }
+
+       private function getInterwikiRow( Site $site ) {
+               return array(
+                       'si_site' => $site->getInternalId(),
+                       'si_type' => 'interwiki',
+                       'si_key' => $site->getLanguageCode()
+               );
+       }
+
+       public function execute() {
+               $wikibaseClient = 
\Wikibase\Client\WikibaseClient::getDefaultInstance();
+
+               $builder = new SiteIdentifiersBuilder(
+                       $wikibaseClient->getSiteStore(),
+                       $wikibaseClient->getSettings()->getSetting( 
'siteGlobalID' )
+               );
+
+               $builder->rebuild();
+       }
+
+}
+
+$maintClass = "SiteIdentifiersBuilder";
+require_once RUN_MAINTENANCE_IF_MAIN;

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

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

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

Reply via email to