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