comparison index.php @ 167:074a4a00af08

Refactor course cache updating.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 24 Aug 2015 10:49:57 +0300
parents 0c3c27de6710
children 66c871dacbc2
comparison
equal deleted inserted replaced
166:0c3c27de6710 167:074a4a00af08
186 " - ". 186 " - ".
187 lukGetHourStamp($classHourTimes[$end]["end"]); 187 lukGetHourStamp($classHourTimes[$end]["end"]);
188 } 188 }
189 189
190 190
191 function lukFetchCourseData($uri, &$cache)
192 {
193 global $pageCharset;
194
195 if (($data = @file_get_contents($uri)) !== FALSE)
196 {
197 $data = @iconv("iso8859-15", $pageCharset, $data);
198 // <td id="oj_nimi" class="smallheadercell"><strong>Korjausrakentamisen rakennussuunnittelu 3 op</strong></td>
199 if (preg_match("#<td id=\"oj_nimi\" class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m))
200 {
201 $cache = array("desc" => $m[1], "op" => intval($m[2]), "uri" => $uri);
202 return TRUE;
203 }
204 else
205 // <td><strong>... (N op)</strong></td>
206 if (preg_match("#<td><strong>(.+?)\s+\((\d+)\s*(op|ECTS\s+cr)\)\s*</strong></td>#", $data, $m))
207 {
208 $cache = array("desc" => $m[1], "op" => intval($m[2]), "uri" => $uri);
209 return TRUE;
210 }
211 }
212 return FALSE;
213 }
214
215
191 function lukMatchCourse($id) 216 function lukMatchCourse($id)
192 { 217 {
193 global $cache, $cacheDirty, $pageCharset, $pageLang, $mobileMode; 218 global $cache, $cacheDirty, $pageLang, $mobileMode;
194
195 //$uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id;
196 $uri = "http://www.oamk.fi/koulutus_ja_hakeminen/opiskelu_oamkissa/opinto-opas/koulutusohjelmat/?sivu=oj_kuvaus&koodi1=".$id."&kieli=".strtoupper($pageLang);
197 219
198 // Create the index 220 // Create the index
199 if (!isset($cache[$id])) 221 if (!isset($cache[$id]))
200 $cache[$id] = array(); 222 $cache[$id] = array();
201 223
202 // Check if course exists in cache 224 // Check if course exists in cache
203 if (!isset($cache[$id][$pageLang])) 225 if (!isset($cache[$id][$pageLang]))
204 { 226 {
205 // Not cached, try to fetch data 227 // Not cached, try to fetch data
206 $cacheDirty = TRUE; 228 $uri = "http://www.oamk.fi/opinto-opas/opintojaksohaku/?sivu=oj_kuvaus&koodi1=".$id."&kieli=".strtoupper($pageLang);
207 $data = @file_get_contents($uri); 229 if (lukFetchCourseData($uri."&opas=2015-2016", $cache[$id][$pageLang]) ||
208 if ($data !== FALSE) 230 lukFetchCourseData($uri."&opas=2014-2015", $cache[$id][$pageLang]))
209 { 231 $cacheDirty = TRUE;
210 $data = @iconv("iso-8859-15", $pageCharset, $data); 232 }
211 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m)) 233
212 {
213 // Add data to cache
214 $cache[$id][$pageLang] = array("desc" => $m[1], "op" => intval($m[2]));
215 }
216 }
217 }
218
219 if (isset($cache[$id]) && isset($cache[$id][$pageLang])) 234 if (isset($cache[$id]) && isset($cache[$id][$pageLang]))
220 { 235 {
221 if ($mobileMode) 236 if ($mobileMode)
222 { 237 {
223 return "<b>".chentities($cache[$id][$pageLang]["desc"])."</b>"; 238 return "<b>".chentities($cache[$id][$pageLang]["desc"])."</b>";
224 } 239 }
225 else 240 else
226 { 241 {
227 return 242 return
228 "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id][$pageLang]["op"]." op"). 243 "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id][$pageLang]["op"]." op").
229 "\" href=\"".chentities($uri)."\">".chentities($cache[$id][$pageLang]["desc"])."</a>"; 244 "\" href=\"".chentities($cache[$id][$pageLang]["uri"])."\">".chentities($cache[$id][$pageLang]["desc"])."</a>";
230 } 245 }
231 } 246 }
232 else 247 else
233 return chentities($id); 248 return chentities($id);
234 } 249 }