Mercurial > hg > lukkari
changeset 167:074a4a00af08
Refactor course cache updating.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 24 Aug 2015 10:49:57 +0300 |
parents | 0c3c27de6710 |
children | 8b2107f0d50b |
files | index.php |
diffstat | 1 files changed, 32 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/index.php Sun Aug 23 19:39:48 2015 +0300 +++ b/index.php Mon Aug 24 10:49:57 2015 +0300 @@ -188,12 +188,34 @@ } +function lukFetchCourseData($uri, &$cache) +{ + global $pageCharset; + + if (($data = @file_get_contents($uri)) !== FALSE) + { + $data = @iconv("iso8859-15", $pageCharset, $data); + // <td id="oj_nimi" class="smallheadercell"><strong>Korjausrakentamisen rakennussuunnittelu 3 op</strong></td> + if (preg_match("#<td id=\"oj_nimi\" class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m)) + { + $cache = array("desc" => $m[1], "op" => intval($m[2]), "uri" => $uri); + return TRUE; + } + else + // <td><strong>... (N op)</strong></td> + if (preg_match("#<td><strong>(.+?)\s+\((\d+)\s*(op|ECTS\s+cr)\)\s*</strong></td>#", $data, $m)) + { + $cache = array("desc" => $m[1], "op" => intval($m[2]), "uri" => $uri); + return TRUE; + } + } + return FALSE; +} + + function lukMatchCourse($id) { - global $cache, $cacheDirty, $pageCharset, $pageLang, $mobileMode; - - //$uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id; - $uri = "http://www.oamk.fi/koulutus_ja_hakeminen/opiskelu_oamkissa/opinto-opas/koulutusohjelmat/?sivu=oj_kuvaus&koodi1=".$id."&kieli=".strtoupper($pageLang); + global $cache, $cacheDirty, $pageLang, $mobileMode; // Create the index if (!isset($cache[$id])) @@ -203,19 +225,12 @@ if (!isset($cache[$id][$pageLang])) { // Not cached, try to fetch data - $cacheDirty = TRUE; - $data = @file_get_contents($uri); - if ($data !== FALSE) - { - $data = @iconv("iso-8859-15", $pageCharset, $data); - if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m)) - { - // Add data to cache - $cache[$id][$pageLang] = array("desc" => $m[1], "op" => intval($m[2])); - } - } + $uri = "http://www.oamk.fi/opinto-opas/opintojaksohaku/?sivu=oj_kuvaus&koodi1=".$id."&kieli=".strtoupper($pageLang); + if (lukFetchCourseData($uri."&opas=2015-2016", $cache[$id][$pageLang]) || + lukFetchCourseData($uri."&opas=2014-2015", $cache[$id][$pageLang])) + $cacheDirty = TRUE; } - + if (isset($cache[$id]) && isset($cache[$id][$pageLang])) { if ($mobileMode) @@ -226,7 +241,7 @@ { return "<a target=\"_blank\" title=\"".chentities($id." - ".$cache[$id][$pageLang]["op"]." op"). - "\" href=\"".chentities($uri)."\">".chentities($cache[$id][$pageLang]["desc"])."</a>"; + "\" href=\"".chentities($cache[$id][$pageLang]["uri"])."\">".chentities($cache[$id][$pageLang]["desc"])."</a>"; } } else