I have just completed an upgrade on a test box from 4.4.5 to Master. Part of this process requires the deletion of a number of dial plans from all the domains. This can be tedious and time consuming for 50+ domains, so I wrote a quick and simple php script to do the dialplan deletions.
The script is shown below, if anyone knows of a better way of doing this, do tell...
The script is shown below, if anyone knows of a better way of doing this, do tell...
PHP:
#!/usr/bin/php
<?php
/*
Script : remove-dialplans.php
Date : 2019-09-16
Author : Adrian Fretwell
Use at you rown risk!
Always back up your data first.
For use with PostgreSQL 9.0 and above.
Edit pg_connect line below for your database password.
Edit SQL below for the dialplan names that require deleting.
Comment out $dry_run = TRUE; to actually perform the delete.
*/
$dry_run = FALSE;
$dry_run = TRUE;
echo "Job remove-dialplans.php ";
if($dry_run) {
echo "(Dry run)";
}
echo " started.\n\n";
$dbconn = pg_connect("host=127.0.0.1 port=5432 dbname=fusionpbx user=fusionpbx password=xxxxxxxxxxxxxxxxxxxxx");
if (!$dbconn) {
echo "An error occurred connecting to the database.\n";
exit(1);
}
$sql = "select domain_uuid, dialplan_uuid, dialplan_name from v_dialplans where dialplan_name in (
'user_exists',
'vmain_user',
'is_loopback',
'tone_stream',
'is_local',
'recordings',
'user_record',
'valet_park',
'agent_status',
'call_screen',
'group_intercept',
'call_forward_not_registered',
'extension-to-voicemail',
'local_extension',
'vmain',
'voicemail'
)
order by domain_uuid";
$dp_result = pg_query($dbconn, $sql);
if (!$dp_result) {
echo "Error with the query: ".$sql."\n";
unset($sql);
exit(1);
}
unset($sql);
echo pg_num_rows($dp_result)." dialplan records to process.\n";
$last_domain_uuid = '0000';
while ($row = pg_fetch_row($dp_result)) {
if(strlen($row[0]) < 1) {
$domain_uuid = 'Global';
} else {
$domain_uuid = $row[0];
}
if (strcmp($domain_uuid, $last_domain_uuid) != 0) {
$last_domain_uuid = $domain_uuid;
echo "\nProcessing domain uuid: ".$domain_uuid."\n";
}
echo "Deleting ".$row[2].": ";
if($dry_run) {
$sql = "select * from v_dialplan_details where dialplan_uuid = '".$row[1]."'";
} else {
$sql = "delete from v_dialplan_details where dialplan_uuid = '".$row[1]."'";
}
$dpd_delete_result = pg_query($dbconn, $sql);
if(!$dpd_delete_result) {
echo "Error with the query: ".$sql.", ";
} else {
echo pg_affected_rows($dpd_delete_result)." v_dialplan_details, ";
unset($dpd_delete_result);
}
unset($sql);
if($dry_run) {
$sql = "select * from v_dialplans where dialplan_uuid = '".$row[1]."'";
} else {
$sql = "delete from v_dialplans where dialplan_uuid = '".$row[1]."'";
}
$dp_delete_result = pg_query($dbconn, $sql);
if(!$dp_delete_result) {
echo "Error with the query: ".$sql."\n";
} else {
echo pg_affected_rows($dp_delete_result)." v_dialplans.\n";
unset($dp_delete_result);
}
unset($sql);
}
pg_free_result($dp_result);
pg_close($dbconn);
echo "\nJob completed. ";
if($dry_run) {
echo "(Dry run)";
}
echo "\n\n";
exit(0);
?>