Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.16 KB | None | 0 0
  1. <?php
  2.  
  3. class wpmuSetDomain {
  4.    
  5.     private $dbConn = false;
  6.    
  7.     public function __construct() {
  8.         error_reporting(E_ALL);
  9.     }
  10.    
  11.     public function setDatabaseConnection($host,$user,$password,$db) {
  12.         $host = '127.0.0.1';
  13.        
  14.         if($user == 'commons') {
  15.             $msg = 'This script will not run on databases named "commons." This is a precaution to prevent it from being run on the production database by mistake';
  16.             die($msg);
  17.         }
  18.         $this->dbConn = mysql_connect($host,$user,$password);
  19.         $this->db = $db;
  20.         mysql_select_db($db,$this->dbConn);
  21.     }
  22.    
  23.     private function getTables() {
  24.         $q = 'SHOW TABLES FROM '.$this->db;
  25.         $qres = mysql_query($q);
  26.         $tables = array();
  27.         while($row = mysql_fetch_array($qres)) {
  28.             $tables[] = $row[0];
  29.         }
  30.         return $tables;
  31.     }
  32.    
  33.     private function getFields($table) {
  34.         $q = 'SHOW FIELDS FROM '.$table;
  35.         $qres = mysql_query($q);
  36.         $fields = array();
  37.         while($row = mysql_fetch_assoc($qres)) {
  38.             $type = $row['Type'];
  39.             $typeOk = false;
  40.             if(stristr(strtolower($type),'char')) $typeOk = true;
  41.             elseif(stristr(strtolower($type),'text')) $typeOk = true;
  42.             elseif(stristr(strtolower($type),'blob')) $typeOk = true;
  43.             if($typeOk == true) $fields[] = $row;
  44.         }
  45.         return $fields;
  46.     }
  47.    
  48.     private function fatalError($msg) {
  49.         print $msg;
  50.         print 'TEST';
  51.         die('EXITING...');
  52.     }
  53.    
  54.     public function setDomain($oldDomain, $newDomain, $testOnly) {
  55.        
  56.         if($testOnly) {
  57.             $queryType = 'select';
  58.         } else {
  59.             $queryType = 'replace';
  60.         }
  61.        
  62.         // get all tables
  63.         $tables = $this->getTables();
  64.        
  65.         $i = 0;
  66.         foreach($tables as $table) {
  67.             $fields = $this->getFields($table);
  68.  
  69.             if(count($fields) > 0) {
  70.                 print 'Updating table "'.$table.'"'."\n";
  71.                
  72.                 foreach($fields as $field) {
  73.                     $i++;
  74.                     $fieldName = $field['Field'];
  75.                     if($queryType == 'replace') {
  76.                         $q = 'UPDATE '.$table.' SET '.$fieldName.' = REPLACE('.$fieldName.',\''.$oldDomain.'\',\''.$newDomain.'\')';
  77.                         #print $q.'</br>';
  78.                         mysql_query($q);
  79.                     } else {
  80.                         $q = 'SELECT * FROM '.$table.' WHERE '.$fieldName.' REGEXP(\''.$oldDomain.'\')';
  81.                         #print $q.'</br>';
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         if($i > 1000) {
  87.             print 'Executed '.$i.' queries. Wow, that\'s a lot!'."\n";
  88.         } else {
  89.             print 'Executed '.$i.' queries. Heck, that\'s nothing!'."\n";
  90.         }
  91.     }
  92.    
  93.     public function confirm($oldDomain, $newDomain) {
  94.         require_once('/sites/xcommons/cac-env-config.php');
  95.         echo "\n\n";
  96.         echo "Please confirm that you would like to do the following:\n\n";
  97.         echo "Replace '$oldDomain' in the Wordpress Database [".strtoupper(DB_NAME).'], the BuddyPress Database ['.strtoupper(BBDB_NAME).'] and in the MediaWiki database ['.strtoupper(MW_DB_NAME)."] with '$newDomain'\n";
  98.         echo "\nAre you sure you want me to do this? If so, type 'yes' and press return\n";
  99.         $confirm = trim(fgets(STDIN));
  100.         if($confirm != "yes") die("\n".'You did\'nt say yes! Alas, I am done for.'."\n\n");
  101.     }
  102.    
  103.     public function doUpdates($oldDomain, $newDomain, $testOnly) {
  104.         // include the configuration file with the local connection constants
  105.         require_once('/sites/xcommons/cac-env-config.php');
  106.        
  107.         $wpmu = true;
  108.         $bpress = true;
  109.         $mwiki = true;
  110.        
  111.         // WPMU
  112.         if($wpmu == true) {
  113.             print '######################'."\n";
  114.             print 'Updating WPMU Database: '.DB_NAME."\n";
  115.             print '######################'."\n";
  116.             $this->setDatabaseConnection(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
  117.             $this->setDomain($oldDomain,$newDomain,$testOnly);
  118.         }
  119.  
  120.         // BBPRESS, IF IT'S NOT IN THE SAME DB.
  121.         if($bpress == true && DB_NAME != BBDB_NAME && BBDB_USER && BBDB_PASSWORD && BBDB_NAME) {
  122.             print '######################'."\n";
  123.             print 'Updating BBPress Database: '.BBDB_NAME."\n";
  124.             print '######################'."\n";
  125.             $this->setDatabaseConnection(BBDB_HOST,BBDB_USER,BBDB_PASSWORD,BBDB_NAME);
  126.             $this->setDomain($oldDomain,$newDomain,$testOnly);
  127.         }
  128.  
  129.         // MEDIAWIKI, IF IT'S NOT IN THE SAME DB.
  130.         if($mwiki == true && MW_DB_NAME != BBDB_NAME && MW_DB_NAME != DB_NAME && MW_DB_SERVER && MW_DB_USER && MW_DB_PASSWORD && MW_DB_NAME) {
  131.             print '######################'."\n";
  132.             print 'Updating MediaWiki Database: '.MW_DB_PASSWORD."\n";
  133.             print '######################'."\n";
  134.             $this->setDatabaseConnection(MW_DB_SERVER,MW_DB_USER,MW_DB_PASSWORD,MW_DB_NAME);
  135.             $this->setDomain($oldDomain,$newDomain,$testOnly);
  136.         }
  137.  
  138.  
  139.  
  140.     }
  141. }
  142.  
  143. $wpmuSetDomain = new wpmuSetDomain;
  144. echo 'This script replaces all instances of one domain in the commons databases with another domain name.'."\n";
  145.  
  146. echo 'Enter the domain name that you want to change (press return to accept the default: "commons.gc.cuny.edu"):'."\n";
  147. $oldDomain = trim(fgets(STDIN));
  148. if($oldDomain == '') $oldDomain = 'commons.gc.cuny.edu';
  149.  
  150. $newDomain = false;
  151.  
  152. $first = true;
  153. while($newDomain == '') {
  154.     if($first == true) {
  155.         echo 'Enter the domain name that you want to change to:'."\n";
  156.     } else {
  157.         echo 'That\'s not a domain name. Enter the domain name that you want to change to or press ctrl+c to exit'."\n";
  158.     }
  159.     $first = false;
  160.     $newDomain = trim(fgets(STDIN));
  161. }
  162.  
  163. $wpmuSetDomain->confirm($oldDomain,$newDomain);
  164. $wpmuSetDomain->doUpdates($oldDomain,$newDomain,0);
  165.  
  166.  
  167. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement