aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/feed/CachedPage.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2018-12-03 00:08:04 +0100
committerVirtualTam <virtualtam@flibidi.net>2019-01-12 22:47:48 +0100
commitdfc650aa239d3a2c028d0ba13132ce75b4f4c0b4 (patch)
tree4b3f54ecfe27e57fd5d32f3543f688c545588aab /application/feed/CachedPage.php
parentf3d2f257946e2a3c8791c1ba99b379acbe934fec (diff)
downloadShaarli-dfc650aa239d3a2c028d0ba13132ce75b4f4c0b4.tar.gz
Shaarli-dfc650aa239d3a2c028d0ba13132ce75b4f4c0b4.tar.zst
Shaarli-dfc650aa239d3a2c028d0ba13132ce75b4f4c0b4.zip
namespacing: \Shaarli\Feed\{Cache,CachedPage,FeedBuilder}
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'application/feed/CachedPage.php')
-rw-r--r--application/feed/CachedPage.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/application/feed/CachedPage.php b/application/feed/CachedPage.php
new file mode 100644
index 00000000..1c51ac73
--- /dev/null
+++ b/application/feed/CachedPage.php
@@ -0,0 +1,61 @@
1<?php
2
3namespace Shaarli\Feed;
4/**
5 * Simple cache system, mainly for the RSS/ATOM feeds
6 */
7class CachedPage
8{
9 // Directory containing page caches
10 private $cacheDir;
11
12 // Should this URL be cached (boolean)?
13 private $shouldBeCached;
14
15 // Name of the cache file for this URL
16 private $filename;
17
18 /**
19 * Creates a new CachedPage
20 *
21 * @param string $cacheDir page cache directory
22 * @param string $url page URL
23 * @param bool $shouldBeCached whether this page needs to be cached
24 */
25 public function __construct($cacheDir, $url, $shouldBeCached)
26 {
27 // TODO: check write access to the cache directory
28 $this->cacheDir = $cacheDir;
29 $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache';
30 $this->shouldBeCached = $shouldBeCached;
31 }
32
33 /**
34 * Returns the cached version of a page, if it exists and should be cached
35 *
36 * @return string a cached version of the page if it exists, null otherwise
37 */
38 public function cachedVersion()
39 {
40 if (!$this->shouldBeCached) {
41 return null;
42 }
43 if (is_file($this->filename)) {
44 return file_get_contents($this->filename);
45 }
46 return null;
47 }
48
49 /**
50 * Puts a page in the cache
51 *
52 * @param string $pageContent XML content to cache
53 */
54 public function cache($pageContent)
55 {
56 if (!$this->shouldBeCached) {
57 return;
58 }
59 file_put_contents($this->filename, $pageContent);
60 }
61}