Passed
Push — master ( 92b079...56770a )
by Tomasz
15:05
created

updateFromMonitor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 109
c 2
b 0
f 0
dl 0
loc 139
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A escape() 0 6 1
A __construct() 0 5 1
A fill_table() 0 11 3
B update_table() 0 35 8
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
22
/**
23
 * This script will download all data from the views in eduroam database and
24
 * update the local copy
25
 */
26
27
require_once dirname(dirname(__FILE__)) . "/config/_config.php";
28
setlocale(LC_CTYPE, "en_US.UTF-8");
29
30
class updateFromMonitor {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
Class name "updateFromMonitor" is not in PascalCase format

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
31
    private $db;
32
    private $db_local;
33
34
    private $tablesource = [
35
        'admin' => 'eduroam',
36
        'active_institution' => 'eduroam',
37
        'active_idp_institution' => 'eduroam',
38
        'active_SP_location_eduroamdb' => 'eduroam',
39
        'country_eduroamdb' => 'eduroam',
40
        'tls_inst' => 'eduroamv2',
41
        'tls_ro' => 'eduroamv2'
42
    ];
43
44
    public $fields = [
45
        'admin' => [
46
            ['id', 'i'],
47
            ['eptid', 's'],
48
            ['email', 's'],
49
            ['common_name', 's'],
50
            ['id_role', 'i'],
51
            ['role', 's'],
52
            ['realm', 's']
53
        ],
54
        'active_institution' => [
55
            ['id_institution', 'i'],
56
            ['ROid', 's'],
57
            ['inst_realm', 's'],
58
            ['country', 's'],
59
            ['name', 's'],
60
            ['contact', 's'],
61
            ['type', 's']
62
        ],
63
        'active_idp_institution' => [
64
            ['id_institution', 'i'],
65
            ['inst_realm', 's'],
66
            ['country', 's'],
67
            ['name', 's'],
68
            ['contact', 's']
69
        ],
70
        'active_SP_location_eduroamdb' => [
71
            ['country', 's'],
72
            ['country_eng', 's'],
73
            ['institutionid', 'i'],
74
            ['inst_name', 's'],
75
            ['sp_location', 's'],
76
            ['sp_location_contact', 's']
77
        ],
78
        'country_eduroamdb' => [
79
            ['country', 's'],
80
            ['country_eng', 's'],
81
            ['map_group', 's']
82
        ],
83
        'tls_ro' => [
84
            ['ROid', 's'],
85
            ['country', 's'],
86
            ['stage', 'i'],
87
            ['servers', 's'],
88
            ['contacts', 's'],
89
            ['ts', 's']
90
        ],
91
        'tls_inst' => [
92
            ['ROid', 's'],
93
            ['country', 's'],
94
            ['instid', 's'],
95
            ['stage', 'i'],
96
            ['type', 'i'],
97
            ['inst_name', 's'],
98
            ['servers', 's'],
99
            ['contacts', 's'],
100
            ['ts', 's']
101
        ]
102
    ];
103
104
    public function __construct() {
105
            $DB = \config\Master::DB['EXTERNAL_SOURCE'];
106
            $DB_LOCAL = \config\Master::DB['EXTERNAL'];
107
            $this->db = new mysqli($DB['host'], $DB['user'], $DB['pass'], $DB['db']);
108
            $this->db_local = new mysqli($DB_LOCAL['host'], $DB_LOCAL['user'], $DB_LOCAL['pass']);
109
    }
110
111
    public function update_table($table_name) {
0 ignored issues
show
Coding Style introduced by
Method name "updateFromMonitor::update_table" is not in camel caps format
Loading history...
112
        $table = 'view_'.$table_name;
113
        $tmp_table = 'tmp_'.$table_name;
114
        if ($this->tablesource[$table_name] == 'eduroam') {
115
            $this->db_local->select_db('monitor_copy');
116
        } elseif($this->tablesource[$table_name] == 'eduroamv2') {
117
            $this->db_local->select_db('eduroamv2');
118
        }
119
        $this->db_local->query("CREATE TEMPORARY TABLE $tmp_table SELECT * FROM $table LIMIT 0");
120
        $sourceDB = $this->tablesource[$table_name];
121
        $this->db->select_db($sourceDB);
122
        $this->db->query("SET NAMES 'utf8'");
123
        $this->db_local->query("SET NAMES 'utf8mb4'");
124
        $result = $this->db->query("SELECT * FROM $table");
125
        $queryFields = implode(',', array_column($this->fields[$table_name],0));
126
        while ($row = $result->fetch_assoc()) {
127
            $v = [];
128
            foreach ($this->fields[$table_name] as $field) {
129
                if ($field[1] === 's') {
130
                    if (isset($row[$field[0]])) {
131
                        $v[] = $this->escape($row[$field[0]]);
132
                    } else {
133
                        $v[] = "NULL";
134
                    }
135
                } else {
136
                    if (isset($row[$field[0]])) {
137
                        $v[] = $row[$field[0]];
138
                    } else {
139
                        $v[] = "NULL";
140
                    }
141
                }
142
            }
143
            $queryValues = implode(',',$v);
144
            $query = "INSERT INTO $tmp_table (".$queryFields.") VALUES (".$queryValues.")";
145
            $this->db_local->query($query);
146
        }
147
148
    }
149
150
    public function fill_table($table_name) {
0 ignored issues
show
Coding Style introduced by
Method name "updateFromMonitor::fill_table" is not in camel caps format
Loading history...
151
        $table = 'view_'.$table_name;
152
        $tmp_table = 'tmp_'.$table_name;
153
        if ($this->tablesource[$table_name] == 'eduroam') {
154
                $this->db_local->select_db('monitor_copy');
155
        } elseif($this->tablesource[$table_name] == 'eduroamv2') {
156
                $this->db_local->select_db('eduroamv2');
157
        }
158
        $this->db_local->query("SET NAMES 'utf8mb4'");
159
        $this->db_local->query("DELETE FROM $table");
160
        $this->db_local->query("INSERT INTO $table SELECT * from $tmp_table");
161
    }
162
163
    private function escape($inp) {
164
        $out=str_replace('\\','\\\\',$inp);
165
        $out=str_replace('"','\"',$out);
166
        $out=str_replace('?','\?',$out);
167
        $out = 'convert(cast(convert("'.$out.'" using latin1) as binary) using utf8)';
168
        return($out);
169
    }
170
}
171
172
$myDB = new updateFromMonitor();
173
174
foreach (array_keys($myDB->fields) as $table) {
175
    $myDB->update_table($table);
176
}
177
178
foreach (array_keys($myDB->fields) as $table) {
179
    $myDB->fill_table($table);
180
}
181
182