Withoutaname has uploaded a new change for review.

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

Change subject: Merge rebuildInterwiki.php into dumpInterwiki.php
......................................................................

Merge rebuildInterwiki.php into dumpInterwiki.php

This change merges rebuildInterwiki.php into dumpInterwiki.php,
both in WikimediaMaintenance. A lot of the code was duplicated,
but it was mostly rebuildInterwiki's SQL file creation function
that was imported. Probably still need to fix any calls from
other files to point to dumpInterwiki.php, and mark deprecated.

Bug: 33839
Change-Id: I10ba3ba1912730cc840a871ddd8212b5b6298a3a
---
M dumpInterwiki.php
1 file changed, 101 insertions(+), 21 deletions(-)


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

diff --git a/dumpInterwiki.php b/dumpInterwiki.php
index 83d2e4d..58d79d9 100644
--- a/dumpInterwiki.php
+++ b/dumpInterwiki.php
@@ -46,6 +46,7 @@
                $this->addOption( 'langlist', 'File with one language code per 
line', false, true );
                $this->addOption( 'dblist', 'File with one db per line', false, 
true );
                $this->addOption( 'specialdbs', "File with one 'special' db per 
line", false, true );
+               $this->addOption( 'd', 'Output folder', false, true );
                $this->addOption( 'o', 'Cdb output file', false, true );
                $this->addOption( 'protocolrelative', 'Output wikimedia 
interwiki urls as protocol relative', false, false );
        }
@@ -77,11 +78,15 @@
                        $this->urlprotocol = 'http:';
                }
 
-               $this->getRebuildInterwikiDump();
+               $this->getRebuildInterwikiDump( $this->getOption( 'd', 
'/a/conf/interwiki/sql' ) );
        }
 
-       function getRebuildInterwikiDump() {
+       /**
+        * @param $destDir string
+        */
+       function getRebuildInterwikiDump( $destDir ) {
                global $wgContLang;
+               $this->output( "Making new interwiki SQL files in $destDir\n" );
 
                # Multi-language sites
                # db suffix => db suffix, iw prefix, hostname
@@ -152,6 +157,7 @@
                }
 
                # Global interwiki map
+               $iwArray = array();
                foreach ( $lines as $line ) {
                        if ( preg_match( '/^\|\s*(.*?)\s*\|\|\s*(.*?)\s*$/', 
$line, $matches ) ) {
                                $prefix = $wgContLang->lc( $matches[1] );
@@ -165,7 +171,8 @@
                                }
 
                                if ( empty( $reserved[$prefix] ) ) {
-                                       $imap  = array( "iw_prefix" => $prefix, 
"iw_url" => $url, "iw_local" => $local );
+                                       $imap = array( "iw_prefix" => $prefix, 
"iw_url" => $url, "iw_local" => $local );
+                                       $iwArray[$prefix] = array( "iw_prefix" 
=> $prefix, "iw_url" => $url, "iw_local" => $local );
                                        $this->makeLink ( $imap, "__global" );
                                }
                        }
@@ -180,20 +187,44 @@
                }
 
                foreach ( $this->dblist as $db ) {
+                       $sql = "-- Generated by dumpInterwiki.php (previously 
rebuildInterwiki.php)";
                        if ( isset( $this->specials[$db] ) && !isset( 
$siteOverrides[$db] ) ) {
+                               # Merged SQL creation function from 
rebuildInterwiki.php
+                               $host = $this->specials[$db];
+                               $sql .= "\n--$host\n\n";
+                               $sql .= "USE $db;\n" .
+                                               "TRUNCATE TABLE interwiki;\n" .
+                                               "INSERT IGNORE INTO interwiki 
(iw_prefix, iw_url, iw_local) VALUES\n";
+                               $first = true;
+
                                # Special wiki
                                # Has interwiki links and interlanguage links 
to wikipedia
-
                                $this->makeLink( array( 'iw_prefix' => $db, 
'iw_url' => "wiki" ), "__sites" );
+
+                               # Intermap links
+                               foreach ( $iwArray as $iwEntry ) {
+                                       $sql .= $this->makeLink( $iwEntry, 
$first, $db );
+                               }
+
                                # Links to multilanguage sites
                                /**
                                 * @var $targetSite WMFSite
                                 */
                                foreach ( $sites as $targetSite ) {
-                                       $this->makeLink( array( 'iw_prefix' => 
$targetSite->lateral,
+                                       $sql .= $this->makeLink( array( 
'iw_prefix' => $targetSite->lateral,
                                                'iw_url' => 
$targetSite->getURL( 'en', $this->urlprotocol ),
-                                               'iw_local' => 1 ), $db );
+                                               'iw_local' => 1 ), $first, $db 
);
                                }
+
+                               # Interlanguage links to Wikipedia
+                               $sql .= $this->makeLanguageLinks( 
$sites['wiki'], $first, $db );
+
+                               # Extra links
+                               foreach ( $extraLinks as $link ) {
+                                       $sql .= $this->makeLink( $link, $first, 
$db );
+                               }
+
+                               $sql .= ";\n"; # End merge
                        } else {
                                # Find out which site this DB belongs to
                                $site = false;
@@ -218,22 +249,52 @@
                                        continue;
                                }
 
-                               # Lateral links
-                               foreach ( $sites as $targetSite ) {
-                                       if ( $targetSite->suffix != 
$site->suffix ) {
-                                               $this->makeLink( array( 
'iw_prefix' => $targetSite->lateral,
-                                                       'iw_url' => 
$targetSite->getURL( $lang, $this->urlprotocol ),
-                                                       'iw_local' => 1 ), $db 
);
+                               # Merged SQL creation function from 
rebuildInterwiki.php
+                               $host = "$lang." . $site->url;
+                               $sql .= "\n--$host\n\n";
+                               $sql .= "USE $db;\n" .
+                                               "TRUNCATE TABLE interwiki;\n" .
+                                               "INSERT IGNORE INTO interwiki 
(iw_prefix, iw_url, iw_local) VALUES\n";
+                               $first = true;
+
+                               # Intermap links
+                               foreach ( $iwArray as $iwEntry ) {
+                                       # Suppress links with the same name as 
the site
+                                       if ( ( $site->suffix == 'wiki' && 
$iwEntry['iw_prefix'] != 'wikipedia' ) ||
+                                               ( $site->suffix != 'wiki' && 
$site->suffix != $iwEntry['iw_prefix'] ) )
+                                       {
+                                               $sql .= $this->makeLink( 
$iwEntry, $first, $db );
                                        }
                                }
 
-                               if ( $site->suffix == "wiki" ) {
-                                       $this->makeLink( array( 'iw_prefix' => 
'w',
-                                               'iw_url' => $this->urlprotocol 
. "//en.wikipedia.org/wiki/$1",
-                                               'iw_local' => 1 ), $db );
+                               # Lateral links
+                               foreach ( $sites as $targetSite ) {
+                                       # Suppress link to self
+                                       if ( $targetSite->suffix != 
$site->suffix ) {
+                                               $sql .= $this->makeLink( array( 
'iw_prefix' => $targetSite->lateral,
+                                                       'iw_url' => 
$targetSite->getURL( $lang, $this->urlprotocol ),
+                                                       'iw_local' => 1 ), 
$first, $db );
+                                       }
                                }
 
+                               # Interlanguage links
+                               $sql .= $this->makeLanguageLinks( $site, 
$first, $db );
+
+                               # w link within Wikipedias
+                               # Other sites already have it as a lateral link
+                               if ( $site->suffix == "wiki" ) {
+                                       $sql .= $this->makeLink( array( 
'iw_prefix' => 'w',
+                                               'iw_url' => $this->urlprotocol 
. "//en.wikipedia.org/wiki/$1",
+                                               'iw_local' => 1 ), $first, $db 
);
+                               }
+
+                               # Extra links
+                               foreach ( $extraLinks as $link ) {
+                                               $sql .= $this->makeLink( $link, 
$first, $db );
+                               }
+                               $sql .= ";\n";
                        }
+                       file_put_contents( "$destDir/$db.sql", $sql ); # End 
merge
                }
                foreach ( $extraLinks as $link ) {
                        $this->makeLink( $link, "__global" );
@@ -259,25 +320,33 @@
         * Executes part of an INSERT statement, corresponding to all 
interlanguage links to a particular site
         *
         * @param $site WMFSite
+        * @param $first
         * @param $source
+        * @return string
         */
-       function makeLanguageLinks( &$site, $source ) {
+       function makeLanguageLinks( &$site, &$first, $source ) {
+               $sql = "";
+
                # Actual languages with their own databases
                foreach ( $this->langlist as $targetLang ) {
-                       $this->makeLink( array( $targetLang, $site->getURL( 
$targetLang, $this->urlprotocol ), 1 ), $source );
+                       $sql .= $this->makeLink( array( $targetLang, 
$site->getURL( $targetLang, $this->urlprotocol ), 1 ), $first, $source );
                }
 
                # Language aliases
                foreach ( $this->languageAliases as $alias => $lang ) {
-                       $this->makeLink( array( $alias, $site->getURL( $lang, 
$this->urlprotocol ), 1 ), $source );
+                       $sql .= $this->makeLink( array( $alias, $site->getURL( 
$lang, $this->urlprotocol ), 1 ), $first, $source );
                }
+
+               return $sql;
        }
 
        /**
         * @param $entry
+        * @param $first
         * @param $source
+        * @return string
         */
-       function makeLink( $entry, $source ) {
+       function makeLink( $entry, &$first, $source ) {
                if ( isset( $this->prefixRewrites[$source] ) && isset( 
$entry[0] ) && isset( $this->prefixRewrites[$source][$entry[0]] ) ) {
                        $entry[0] = $this->prefixRewrites[$source][$entry[0]];
                }
@@ -307,9 +376,20 @@
                }
                # Add to list of prefixes
                $this->prefixLists[$source][$entry['iw_prefix']] = 1;
+
+               # Merged SQL creation function from rebuildInterwiki.php
+               $sql = "";
+               # Add comma
+               if ( $first ) {
+                       $first = false;
+               } else {
+                       $sql .= ",\n";
+               }
+               $dbr = wfGetDB( DB_SLAVE );
+               $sql .= "(" . $dbr->makeList( $entry ) . ")";
+               return $sql; # End merge
        }
 }
 
 $maintClass = "DumpInterwiki";
 require_once( RUN_MAINTENANCE_IF_MAIN );
-

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10ba3ba1912730cc840a871ddd8212b5b6298a3a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikimediaMaintenance
Gerrit-Branch: master
Gerrit-Owner: Withoutaname <drevit...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to