Debian Codegraph @

contrib/scripts/retrieve_extensions_from_mysql.pl

raw
1 #!/usr/bin/perl -Tw
2 # Use these commands to create the appropriate tables in MySQL
3 # If flags is 1 then this record is not included in the output extensions file
4 #
5 #CREATE TABLE extensions (
6 # context CHAR(20) DEFAULT 'default' NOT NULL,
7 # extension CHAR(20) NOT NULL,
8 # priority INT(2) DEFAULT '1' NOT NULL,
9 # application CHAR(20) NOT NULL,
10 # args CHAR(50),
11 # descr TEXT,
12 # flags INT(1) DEFAULT '0' NOT NULL,
13 # PRIMARY KEY(context, extension, priority)
14 #);
15 #
16 #CREATE TABLE globals (
17 # variable CHAR(20) NOT NULL,
18 # value CHAR(50) NOT NULL,
19 # PRIMARY KEY(variable, value)
20 #);
21
22 use DBI;
23 ################### BEGIN OF CONFIGURATION ####################
24
25 # the name of the extensions table
26 $table_name = "extensions";
27 # the name of the globals table
28 $global_table_name = "globals";
29 # the path to the extensions.conf file
30 # WARNING: this file will be substituted by the output of this program
31 $extensions_conf = "/etc/asterisk/extensions.conf";
32 # the name of the box the MySQL database is running on
33 $hostname = "localhost";
34 # the name of the database our tables are kept
35 $database = "user";
36 # username to connect to the database
37 $username = "";
38 # password to connect to the database
39 $password = "";
40
41 ################### END OF CONFIGURATION #######################
42
43 open EXTEN, ">$extensions_conf" || die "Cannot create/overwrite extensions file: $extensions_conf\n";
44
45 $dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password");
46 $statement = "SELECT * from $global_table_name order by variable";
47 my $result = $dbh->selectall_arrayref($statement);
48 unless ($result) {
49 # check for errors after every single database call
50 print "dbh->selectall_arrayref($statement) failed!\n";
51 print "DBI::err=[$DBI::err]\n";
52 print "DBI::errstr=[$DBI::errstr]\n";
53 exit;
54 }
55 my @resultSet = @{$result};
56 if ( $#resultSet > -1 ) {
57 print EXTEN "[globals]\n";
58 foreach $row (@{ $result }) {
59 my @result = @{ $row };
60 print EXTEN "$result[0] = $result[1]\n";
61 }
62 print EXTEN "\n";
63 }
64
65 $statement = "SELECT context from $table_name group by context";
66
67 $result = $dbh->selectall_arrayref($statement);
68 unless ($result) {
69 # check for errors after every single database call
70 print "dbh->selectall_arrayref($statement) failed!\n";
71 print "DBI::err=[$DBI::err]\n";
72 print "DBI::errstr=[$DBI::errstr]\n";
73 }
74
75 @resultSet = @{$result};
76 if ( $#resultSet == -1 ) {
77 print "No extensions defined in $table_name\n";
78 exit;
79 }
80
81 foreach my $row ( @{ $result } ) {
82 my $context = @{ $row }[0];
83 print EXTEN "[$context]\n";
84 $statement = "SELECT * from $table_name where context='$context' order by extension, priority";
85 my $result = $dbh->selectall_arrayref($statement);
86 unless ($result) {
87 # check for errors after every single database call
88 print "dbh->selectall_arrayref($statement) failed!\n";
89 print "DBI::err=[$DBI::err]\n";
90 print "DBI::errstr=[$DBI::errstr]\n";
91 exit;
92 }
93
94 my @resSet = @{$result};
95 if ( $#resSet == -1 ) {
96 print "no results\n";
97 exit;
98 }
99 foreach my $row ( @{ $result } ) {
100 my @result = @{ $row };
101 if ($result[6] == 0) {
102 print EXTEN "exten => $result[1],$result[2],$result[3]";
103 print EXTEN "($result[4])" if defined $result[4];
104 print EXTEN "\t" if not defined $result[4];
105 print EXTEN "\t; $result[5]" if defined $result[5];
106 print EXTEN "\n";
107 }
108 }
109 print EXTEN "\n";
110 }
111
112 exit 0;