76458066 |
1 | <?php // $Id$ |
2 | |
3 | // Automatic update of DST presets |
4 | |
5 | require_once('../config.php'); |
6 | require_once($CFG->libdir.'/filelib.php'); |
7 | require_once($CFG->libdir.'/olson.php'); |
8 | |
9 | define('STEP_OLSON_TO_CSV', 1); |
10 | define('STEP_DOWNLOAD_CSV', 2); |
11 | define('STEP_IMPORT_CSV_LIB', 3); |
12 | define('STEP_IMPORT_CSV_TEMP', 4); |
13 | define('STEP_COMPLETED', 5); |
14 | |
15 | require_login(); |
16 | |
17 | if (!isadmin()) { |
18 | error('Only administrators can use this page!'); |
19 | } |
20 | |
21 | if (!$site = get_site()) { |
22 | error('Site isn\'t defined!'); |
23 | } |
24 | |
25 | $ddd = olson_todst($CFG->dataroot.'/temp/olson.txt'); |
26 | |
27 | execute_sql('TRUNCATE TABLE '.$CFG->prefix.'timezone'); |
28 | |
29 | foreach($ddd as $rec) { |
30 | print_object($rec); |
31 | insert_record('timezone', $rec); |
32 | } |
33 | |
34 | die(); |
35 | |
36 | // These control what kind of operations import_dst_records will be allowed |
37 | $insert = true; |
38 | $update = true; |
39 | |
40 | // Actions in REVERSE ORDER of execution |
41 | $actions = array(STEP_IMPORT_CSV_LIB, STEP_DOWNLOAD_CSV, STEP_IMPORT_CSV_TEMP, STEP_OLSON_TO_CSV); |
42 | |
43 | while(!empty($actions)) { |
44 | $action = array_pop($actions); |
45 | switch($action) { |
46 | |
47 | case STEP_OLSON_TO_CSV: |
48 | if(is_writable($CFG->dataroot.'/temp/olson.txt')) { |
49 | $records = olson_simple_rule_parser($CFG->dataroot.'/temp/olson.txt'); |
50 | if(put_records_csv('dst.txt', $records, 'dst_preset')) { |
51 | // Successful convert |
52 | unlink($CFG->dataroot.'/temp/olson.txt'); |
53 | array_push($actions, STEP_IMPORT_CSV_TEMP); |
54 | } |
55 | else { |
56 | // Error: put_records_csv complained |
57 | error('44'); |
58 | } |
59 | } |
60 | break; |
61 | |
62 | case STEP_IMPORT_CSV_TEMP: |
63 | if(is_writable($CFG->dataroot.'/temp/dst.txt')) { |
64 | $records = get_records_csv($CFG->dataroot.'/temp/dst.txt', 'dst_preset'); |
65 | // Import and go to summary page |
66 | $results = import_dst_records($records, $insert, $update); |
67 | unlink($CFG->dataroot.'/temp/dst.txt'); |
68 | array_push($actions, STEP_COMPLETED); |
69 | } |
70 | break; |
71 | |
72 | case STEP_DOWNLOAD_CSV: |
73 | if(ini_get('allow_url_fopen')) { |
74 | $contents = @file_get_contents('http://download.moodle.org/dst/'); |
75 | if(!empty($contents)) { |
76 | // Got something |
77 | if($fp = fopen($CFG->dataroot.'/temp/dst.txt', 'w')) { |
78 | fwrite($fp, $contents); |
79 | fclose($fp); |
80 | array_push($actions, STEP_IMPORT_CSV_TEMP); |
81 | } |
82 | else { |
83 | // Error: Couldn't open file correctly |
84 | error('74'); |
85 | } |
86 | } |
87 | else { |
88 | // Error: nothing from download.moodle.org |
89 | error('73'); |
90 | } |
91 | } |
92 | break; |
93 | |
94 | case STEP_IMPORT_CSV_LIB: |
95 | if(file_exists($CFG->dirroot.'/lib/dst.txt')) { |
96 | $records = get_records_csv($CFG->dirroot.'/lib/dst.txt', 'dst_preset'); |
97 | $results = import_dst_records($records, $insert, $update); |
98 | array_push($actions, STEP_COMPLETED); |
99 | } |
100 | break; |
101 | |
102 | case STEP_COMPLETED: |
103 | echo get_string('updatedstpresetscompleted'); |
104 | print_object($results); |
105 | die(); |
106 | break; |
107 | } |
108 | } |
109 | |
110 | function import_dst_records(&$records, $allowinsert = true, $allowupdate = true) { |
111 | $results = array(); |
112 | $proto = array('insert' => 0, 'update' => 0, 'errors' => 0); |
113 | |
114 | foreach($records as $record) { |
115 | |
116 | if(!check_dst_preset($record)) { |
117 | continue; |
118 | } |
119 | |
120 | $dbpreset = get_record('dst_preset', 'family', $record->family, 'year', $record->year); |
121 | |
122 | if(empty($dbpreset)) { |
123 | |
124 | if(!$allowinsert) { |
125 | continue; |
126 | } |
127 | |
128 | if(!isset($results[$record->family])) { |
129 | $results[$record->family] = $proto; |
130 | } |
131 | |
132 | |
133 | unset($record->id); |
134 | if(insert_record('dst_preset', $record)) { |
135 | ++$results[$record->family]['insert']; |
136 | } |
137 | else { |
138 | ++$results[$record->family]['errors']; |
139 | } |
140 | } |
141 | |
142 | else { |
143 | // Already exists |
144 | |
145 | if(!$allowupdate) { |
146 | continue; |
147 | } |
148 | |
149 | if(hash_dst_preset($record) != hash_dst_preset($dbpreset)) { |
150 | |
151 | // And is different |
152 | |
153 | if(!isset($results[$record->family])) { |
154 | $results[$record->family] = $proto; |
155 | } |
156 | |
157 | $record->id = $dbpreset->id; |
158 | if(update_record('dst_preset', $record)) { |
159 | ++$results[$record->family]['update']; |
160 | } |
161 | else { |
162 | ++$results[$record->family]['update']; |
163 | } |
164 | } |
165 | |
166 | } |
167 | |
168 | } |
169 | return $results; |
170 | } |
171 | |
172 | function hash_dst_preset($record) { |
173 | return md5(implode('!', array( |
174 | $record->family, |
175 | $record->year, |
176 | $record->apply_offset, |
177 | $record->activate_index, |
178 | $record->activate_day, |
179 | $record->activate_month, |
180 | $record->activate_time, |
181 | $record->deactivate_index, |
182 | $record->deactivate_day, |
183 | $record->deactivate_month, |
184 | $record->deactivate_time |
185 | ))); |
186 | } |
187 | |
188 | function check_dst_preset($record) { |
189 | // TODO: make this a real check |
190 | return true; |
191 | } |
192 | |
193 | ?> |