diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-07-09 22:14:39 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-08-13 23:48:06 +0200 |
commit | 01e48f269df59e02798dad4a698c125d76b0ed70 (patch) | |
tree | 9341487badd4ed14e0104ae338cf48c4ee0dc994 /index.php | |
parent | 5ac5349ac053b1e560b136c62f8c764fd3230039 (diff) | |
download | Shaarli-01e48f269df59e02798dad4a698c125d76b0ed70.tar.gz Shaarli-01e48f269df59e02798dad4a698c125d76b0ed70.tar.zst Shaarli-01e48f269df59e02798dad4a698c125d76b0ed70.zip |
CachedPage: move to a proper file, add tests
Modifications
- rename `pageCache` to `CachedPage`
- move utilities to `Cache`
- do not access globals
- apply coding rules
- update LinkDB and test code
- add test coverage
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'index.php')
-rwxr-xr-x | index.php | 113 |
1 files changed, 35 insertions, 78 deletions
@@ -70,6 +70,8 @@ if (is_file($GLOBALS['config']['CONFIG_FILE'])) { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | // Shaarli library | 72 | // Shaarli library |
73 | require_once 'application/Cache.php'; | ||
74 | require_once 'application/CachedPage.php'; | ||
73 | require_once 'application/LinkDB.php'; | 75 | require_once 'application/LinkDB.php'; |
74 | require_once 'application/TimeZone.php'; | 76 | require_once 'application/TimeZone.php'; |
75 | require_once 'application/Utils.php'; | 77 | require_once 'application/Utils.php'; |
@@ -203,63 +205,6 @@ function checkUpdate() | |||
203 | 205 | ||
204 | 206 | ||
205 | // ----------------------------------------------------------------------------------------------- | 207 | // ----------------------------------------------------------------------------------------------- |
206 | // Simple cache system (mainly for the RSS/ATOM feeds). | ||
207 | |||
208 | class pageCache | ||
209 | { | ||
210 | private $url; // Full URL of the page to cache (typically the value returned by pageUrl()) | ||
211 | private $shouldBeCached; // boolean: Should this url be cached? | ||
212 | private $filename; // Name of the cache file for this url. | ||
213 | |||
214 | /* | ||
215 | $url = URL (typically the value returned by pageUrl()) | ||
216 | $shouldBeCached = boolean. If false, the cache will be disabled. | ||
217 | */ | ||
218 | public function __construct($url,$shouldBeCached) | ||
219 | { | ||
220 | $this->url = $url; | ||
221 | $this->filename = $GLOBALS['config']['PAGECACHE'].'/'.sha1($url).'.cache'; | ||
222 | $this->shouldBeCached = $shouldBeCached; | ||
223 | } | ||
224 | |||
225 | // If the page should be cached and a cached version exists, | ||
226 | // returns the cached version (otherwise, return null). | ||
227 | public function cachedVersion() | ||
228 | { | ||
229 | if (!$this->shouldBeCached) return null; | ||
230 | if (is_file($this->filename)) { return file_get_contents($this->filename); exit; } | ||
231 | return null; | ||
232 | } | ||
233 | |||
234 | // Put a page in the cache. | ||
235 | public function cache($page) | ||
236 | { | ||
237 | if (!$this->shouldBeCached) return; | ||
238 | file_put_contents($this->filename,$page); | ||
239 | } | ||
240 | |||
241 | // Purge the whole cache. | ||
242 | // (call with pageCache::purgeCache()) | ||
243 | public static function purgeCache() | ||
244 | { | ||
245 | if (is_dir($GLOBALS['config']['PAGECACHE'])) | ||
246 | { | ||
247 | $handler = opendir($GLOBALS['config']['PAGECACHE']); | ||
248 | if ($handler!==false) | ||
249 | { | ||
250 | while (($filename = readdir($handler))!==false) | ||
251 | { | ||
252 | if (endsWith($filename,'.cache')) { unlink($GLOBALS['config']['PAGECACHE'].'/'.$filename); } | ||
253 | } | ||
254 | closedir($handler); | ||
255 | } | ||
256 | } | ||
257 | } | ||
258 | |||
259 | } | ||
260 | |||
261 | |||
262 | // ----------------------------------------------------------------------------------------------- | ||
263 | // Log to text file | 208 | // Log to text file |
264 | function logm($message) | 209 | function logm($message) |
265 | { | 210 | { |
@@ -718,8 +663,16 @@ function showRSS() | |||
718 | 663 | ||
719 | // Cache system | 664 | // Cache system |
720 | $query = $_SERVER["QUERY_STRING"]; | 665 | $query = $_SERVER["QUERY_STRING"]; |
721 | $cache = new pageCache(pageUrl(),startsWith($query,'do=rss') && !isLoggedIn()); | 666 | $cache = new CachedPage( |
722 | $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } | 667 | $GLOBALS['config']['PAGECACHE'], |
668 | pageUrl(), | ||
669 | startsWith($query,'do=rss') && !isLoggedIn() | ||
670 | ); | ||
671 | $cached = $cache->cachedVersion(); | ||
672 | if (! empty($cached)) { | ||
673 | echo $cached; | ||
674 | exit; | ||
675 | } | ||
723 | 676 | ||
724 | // If cached was not found (or not usable), then read the database and build the response: | 677 | // If cached was not found (or not usable), then read the database and build the response: |
725 | $LINKSDB = new LinkDB( | 678 | $LINKSDB = new LinkDB( |
@@ -798,11 +751,19 @@ function showATOM() | |||
798 | 751 | ||
799 | // Cache system | 752 | // Cache system |
800 | $query = $_SERVER["QUERY_STRING"]; | 753 | $query = $_SERVER["QUERY_STRING"]; |
801 | $cache = new pageCache(pageUrl(),startsWith($query,'do=atom') && !isLoggedIn()); | 754 | $cache = new CachedPage( |
802 | $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } | 755 | $GLOBALS['config']['PAGECACHE'], |
803 | // If cached was not found (or not usable), then read the database and build the response: | 756 | pageUrl(), |
757 | startsWith($query,'do=atom') && !isLoggedIn() | ||
758 | ); | ||
759 | $cached = $cache->cachedVersion(); | ||
760 | if (!empty($cached)) { | ||
761 | echo $cached; | ||
762 | exit; | ||
763 | } | ||
804 | 764 | ||
805 | // Read links from database (and filter private links if used it not logged in). | 765 | // If cached was not found (or not usable), then read the database and build the response: |
766 | // Read links from database (and filter private links if used it not logged in). | ||
806 | $LINKSDB = new LinkDB( | 767 | $LINKSDB = new LinkDB( |
807 | $GLOBALS['config']['DATASTORE'], | 768 | $GLOBALS['config']['DATASTORE'], |
808 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], | 769 | isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], |
@@ -884,7 +845,11 @@ function showATOM() | |||
884 | function showDailyRSS() { | 845 | function showDailyRSS() { |
885 | // Cache system | 846 | // Cache system |
886 | $query = $_SERVER["QUERY_STRING"]; | 847 | $query = $_SERVER["QUERY_STRING"]; |
887 | $cache = new pageCache(pageUrl(), startsWith($query, 'do=dailyrss') && !isLoggedIn()); | 848 | $cache = new CachedPage( |
849 | $GLOBALS['config']['PAGECACHE'], | ||
850 | pageUrl(), | ||
851 | startsWith($query,'do=dailyrss') && !isLoggedIn() | ||
852 | ); | ||
888 | $cached = $cache->cachedVersion(); | 853 | $cached = $cache->cachedVersion(); |
889 | if (!empty($cached)) { | 854 | if (!empty($cached)) { |
890 | echo $cached; | 855 | echo $cached; |
@@ -1076,7 +1041,7 @@ function renderPage() | |||
1076 | // -------- User wants to logout. | 1041 | // -------- User wants to logout. |
1077 | if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=logout')) | 1042 | if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=logout')) |
1078 | { | 1043 | { |
1079 | invalidateCaches(); | 1044 | invalidateCaches($GLOBALS['config']['PAGECACHE']); |
1080 | logout(); | 1045 | logout(); |
1081 | header('Location: ?'); | 1046 | header('Location: ?'); |
1082 | exit; | 1047 | exit; |
@@ -1383,7 +1348,7 @@ function renderPage() | |||
1383 | $value['tags']=trim(implode(' ',$tags)); | 1348 | $value['tags']=trim(implode(' ',$tags)); |
1384 | $LINKSDB[$key]=$value; | 1349 | $LINKSDB[$key]=$value; |
1385 | } | 1350 | } |
1386 | $LINKSDB->savedb(); // Save to disk. | 1351 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. |
1387 | echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?\';</script>'; | 1352 | echo '<script>alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?\';</script>'; |
1388 | exit; | 1353 | exit; |
1389 | } | 1354 | } |
@@ -1400,7 +1365,7 @@ function renderPage() | |||
1400 | $value['tags']=trim(implode(' ',$tags)); | 1365 | $value['tags']=trim(implode(' ',$tags)); |
1401 | $LINKSDB[$key]=$value; | 1366 | $LINKSDB[$key]=$value; |
1402 | } | 1367 | } |
1403 | $LINKSDB->savedb(); // Save to disk. | 1368 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. |
1404 | echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode($_POST['totag']).'\';</script>'; | 1369 | echo '<script>alert("Tag was renamed in '.count($linksToAlter).' links.");document.location=\'?searchtags='.urlencode($_POST['totag']).'\';</script>'; |
1405 | exit; | 1370 | exit; |
1406 | } | 1371 | } |
@@ -1429,7 +1394,7 @@ function renderPage() | |||
1429 | 'linkdate'=>$linkdate,'tags'=>str_replace(',',' ',$tags)); | 1394 | 'linkdate'=>$linkdate,'tags'=>str_replace(',',' ',$tags)); |
1430 | if ($link['title']=='') $link['title']=$link['url']; // If title is empty, use the URL as title. | 1395 | if ($link['title']=='') $link['title']=$link['url']; // If title is empty, use the URL as title. |
1431 | $LINKSDB[$linkdate] = $link; | 1396 | $LINKSDB[$linkdate] = $link; |
1432 | $LINKSDB->savedb(); // Save to disk. | 1397 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. |
1433 | pubsubhub(); | 1398 | pubsubhub(); |
1434 | 1399 | ||
1435 | // If we are called from the bookmarklet, we must close the popup: | 1400 | // If we are called from the bookmarklet, we must close the popup: |
@@ -1462,7 +1427,7 @@ function renderPage() | |||
1462 | // - we are protected from XSRF by the token. | 1427 | // - we are protected from XSRF by the token. |
1463 | $linkdate=$_POST['lf_linkdate']; | 1428 | $linkdate=$_POST['lf_linkdate']; |
1464 | unset($LINKSDB[$linkdate]); | 1429 | unset($LINKSDB[$linkdate]); |
1465 | $LINKSDB->savedb(); // save to disk | 1430 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // save to disk |
1466 | 1431 | ||
1467 | // If we are called from the bookmarklet, we must close the popup: | 1432 | // If we are called from the bookmarklet, we must close the popup: |
1468 | if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; } | 1433 | if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo '<script>self.close();</script>'; exit; } |
@@ -1751,7 +1716,7 @@ function importFile() | |||
1751 | } | 1716 | } |
1752 | } | 1717 | } |
1753 | } | 1718 | } |
1754 | $LINKSDB->savedb(); | 1719 | $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); |
1755 | 1720 | ||
1756 | echo '<script>alert("File '.json_encode($filename).' ('.$filesize.' bytes) was successfully processed: '.$import_count.' links imported.");document.location=\'?\';</script>'; | 1721 | echo '<script>alert("File '.json_encode($filename).' ('.$filesize.' bytes) was successfully processed: '.$import_count.' links imported.");document.location=\'?\';</script>'; |
1757 | } | 1722 | } |
@@ -2386,14 +2351,6 @@ function resizeImage($filepath) | |||
2386 | return true; | 2351 | return true; |
2387 | } | 2352 | } |
2388 | 2353 | ||
2389 | // Invalidate caches when the database is changed or the user logs out. | ||
2390 | // (e.g. tags cache). | ||
2391 | function invalidateCaches() | ||
2392 | { | ||
2393 | unset($_SESSION['tags']); // Purge cache attached to session. | ||
2394 | pageCache::purgeCache(); // Purge page cache shared by sessions. | ||
2395 | } | ||
2396 | |||
2397 | try { | 2354 | try { |
2398 | mergeDeprecatedConfig($GLOBALS, isLoggedIn()); | 2355 | mergeDeprecatedConfig($GLOBALS, isLoggedIn()); |
2399 | } catch(Exception $e) { | 2356 | } catch(Exception $e) { |