Changeset 9239

Show
Ignore:
Timestamp:
03/16/10 20:54:36 (3 years ago)
Author:
p_lindheimer
Message:

add code to migrate from localprefixes.conf if they never did before here re #4144

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/trunk/upgrades/2.8.0alpha2/tables.php

    r9238 r9239  
    4343)  
    4444"; 
     45 
     46// Before doing anything, migrate check if we need to migrate from a state where no local prefix tables existed 
     47// if not, this will do nothing... 
     48// 
     49__migrate_legacy_localprefix(); 
     50 
    4551outn(_("Checking if trunk_dialpatterns table exists..")); 
    4652$check = $db->query($trunk_dialpatterns); 
     
    110116  } 
    111117} 
     118 
     119//---------------------------------------------------------------------------------------------- 
     120// Legacy migration if no tables existed prior to get info from localprefixes.conf 
     121 
     122/* If localprefixes.conf exists and there is no trunk_dialpatterns OR trunks_dialpatterns then we 
     123   are in an upgrade situation where the never had either since these were upgraded in core 
     124   originally so they won't have gone through the sequential upgrade migrations. So, we'll just yank 
     125   the orignal code from core's install and go though the silliness of upgrading to the old table 
     126   which we will then go and blow away. 
     127 */ 
     128function __migrate_legacy_localprefix() { 
     129  global $db; 
     130  global $amp_conf; 
     131 
     132  // Is the old table there? 
     133  $test = 'SELECT trunkid FROM trunks_dialpatterns LIMIT 1'; 
     134  $check = $db->getRow($test, DB_FETCHMODE_ASSOC); 
     135  if(!DB::IsError($check)) { 
     136    return true; 
     137  } 
     138  // Is the new table there? 
     139  $test = 'SELECT trunkid FROM trunk_dialpatterns LIMIT 1'; 
     140  $check = $db->getRow($test, DB_FETCHMODE_ASSOC); 
     141  if(!DB::IsError($check)) { 
     142    return true; 
     143  } 
     144  //Neither table, is there a fixlocalprefixes.conf file? 
     145  $localPrefixFile = $amp_conf['ASTETCDIR']."/localprefixes.conf"; 
     146  $conf == array(); 
     147  __parse_DialRulesFile($localPrefixFile, $conf, $section); 
     148  if (count($conf) == 0) { 
     149    return true; 
     150  } 
     151 
     152  // At this point, we have not tables a localprefixes file with something in it 
     153  // 
     154  $sql = " 
     155  CREATE TABLE `trunks_dialpatterns`  
     156  (  
     157    `trunkid` INTEGER, 
     158    `rule` VARCHAR( 255 ) NOT NULL,  
     159    `seq` INTEGER, 
     160    PRIMARY KEY  (`trunkid`, `rule`, `seq`)  
     161  )  
     162  "; 
     163  outn(_("Checking if trunks_dialpatterns table exists..")); 
     164  $check = $db->query($sql); 
     165  if(DB::IsError($check) && $check->getCode() != DB_ERROR_ALREADY_EXISTS) { 
     166    die_freepbx("Can not create trunks_dialpatterns table"); 
     167  } else if(DB::IsError($check) && $check->getCode() == DB_ERROR_ALREADY_EXISTS) { 
     168    out(_("already exists")); 
     169  } else { 
     170    out(_("created")); 
     171    outn(_("loading table from localprefixes.conf..")); 
     172    $rules_arr = array(); 
     173    foreach ($conf as $tname => $rules) { 
     174      $tid = ltrim($tname,'trunk-'); 
     175    uksort($rules,'__order_DialRules'); //make sure they are in order 
     176      $seq = 1; 
     177      foreach ($rules as $rule) { 
     178        $rules_arr[] = array($tid,$rule,$seq); 
     179        $seq++; 
     180      } 
     181    } 
     182    $compiled = $db->prepare("INSERT INTO `trunks_dialpatterns` (trunkid, rule, seq) VALUES (?,?,?)"); 
     183    $result = $db->executeMultiple($compiled,$rules_arr); 
     184    if(DB::IsError($result)) { 
     185      die_freepbx($result->getDebugInfo().'error populating trunks_dialpatterns table');   
     186    } 
     187    out(_("loaded")); 
     188  } 
     189} 
     190function __order_DialRules($a, $b) { 
     191  return substr($a,4) > substr($b,4); 
     192} 
     193// Get values from localprefix configuration file where values are stored 
     194// for fixlocalprefix macro 
     195// 
     196function __parse_DialRulesFile($filename, &$conf, &$section) { 
     197  if (is_null($conf)) { 
     198    $conf = array(); 
     199  } 
     200  if (is_null($section)) { 
     201    $section = "general"; 
     202  } 
     203   
     204  if (file_exists($filename)) { 
     205    $fd = fopen($filename, "r"); 
     206    while ($line = fgets($fd, 1024)) { 
     207      if (preg_match("/^\s*([a-zA-Z0-9-_]+)\s*=\s*(.*?)\s*([;#].*)?$/",$line,$matches)) { 
     208        // name = value 
     209        // option line 
     210        $conf[$section][ $matches[1] ] = $matches[2]; 
     211      } else if (preg_match("/^\s*\[(.+)\]/",$line,$matches)) { 
     212        // section name 
     213        $section = strtolower($matches[1]); 
     214      } else if (preg_match("/^\s*#include\s+(.*)\s*([;#].*)?/",$line,$matches)) { 
     215        // include another file 
     216         
     217        if ($matches[1][0] == "/") { 
     218          // absolute path 
     219          $filename = $matches[1]; 
     220        } else { 
     221          // relative path 
     222          $filename =  dirname($filename)."/".$matches[1]; 
     223        } 
     224        __parse_DialRulesFile($filename, $conf, $section); 
     225      } 
     226    } 
     227  } 
     228}