changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / lib / cache.php
1 <?php
2
3 /*
4    This plugin can cache readily rendered pages either as files or in the
5    ewiki database, so they will show up much faster when accessed a second
6    time.
7
8    Right now it only supports storing the fully rendered page (_CACHE_FULL).
9    Storing of page plugins output (_CACHE_ALL) should be kept disabled,
10    because "UpdatedPages" and so on cannot be verified to not have updated
11    since the cache entry was written.
12    Also you may want to disallow caching of the "links" action, because
13    there is no change tracking with it.
14 */
15
16 #-- how and which pages to store
17 define("EWIKI_CACHE_FULL", 1);   # including control links line?
18 define("EWIKI_CACHE_ALL", 0);    # also virtual pages?
19 define("EWIKI_CACHE_VTIME", 1000);  # time to keep page plugins` output, NYI
20
21 #-- where to store the pre-rendered pages (DB or files) - unset one of them:
22 define("EWIKI_CACHE_DIR", "./var/cache");    # preferred over db storage
23 define("EWIKI_CACHE_DB", "system/cache/");   # only has effect, if _DIR undefined
24
25 #-- when to store rendered pages?
26 $ewiki_config["cache.actions"] = array(
27    "view", "info",
28    "links", 
29 );
30
31
32 #-- plugin glue
33 if (EWIKI_CACHE_FULL) {
34    $ewiki_plugins["handler"][] = "ewiki_handler_cache_full";
35    $ewiki_plugins["page_final"][] = "ewiki_store_cache_full";
36 }
37 else {
38    die("unsupported ewiki caching guideline setting");
39 }
40
41
42 #-- init
43 if (defined("EWIKI_CACHE_DIR") && !file_exists(EWIKI_CACHE_DIR)) {
44    @mkdir(dirname(EWIKI_CACHE_DIR));
45    mkdir(EWIKI_CACHE_DIR);
46 }
47
48
49 #-- fetch cache entry for page
50 function ewiki_get_cache($action, $id) {
51    $row = array();
52    if (defined("EWIKI_CACHE_DIR") && EWIKI_CACHE_DIR) {
53       $file = EWIKI_CACHE_DIR . "/" . $action . "," . urlencode($id);
54       if (file_exists($file)) {
55          $f = gzopen($file, "r");
56          if ($f) {
57             $content = gzread($f, 1<<17);
58             fclose($f);
59             if ($content) {
60                $row = array(
61                   "id" => $id,
62                   "version" => 1,
63                   "flags" => EWIKI_DB_F_BINARY¦EWIKI_DB_F_TEXT|EWIKI_DB_F_HTML,
64                   "created" => filectime($file),
65                   "lastmodified" => filemtime($file),
66                   "content" => $content,
67                   "meta" => array("class"=>"cache"),
68                );
69             }
70          }
71       }
72    }
73    elseif (defined("EWIKI_CACHE_DB") && (EWIKI_CACHE_DB)) {
74       $id = EWIKI_CACHE_DB."$action/$id";
75       $row = ewiki_db::GET($id);
76    }
77    return($row);
78 }
79
80
81 #-- return rendered page
82 function ewiki_handler_cache_full($id, &$data, $action) {
83    global $ewiki_config;
84    if (in_array($action, $ewiki_config["cache.actions"]) && ($cache = ewiki_get_cache($action,$id))) {
85       if ($cache["lastmodified"] >= $data["lastmodified"]) {
86          $data = &$cache;
87          ewiki_http_headers($data["content"], $id, $cache, $action);
88          return($data["content"]);
89       }
90    }
91 }
92
93
94 #-- if we get here, we should store the rendered page
95 function ewiki_store_cache_full(&$o, $id, &$data, $action) {
96    global $ewiki_config;
97    if (in_array($action, $ewiki_config["cache.actions"]) && ($data["version"] || EWIKI_CACHE_ALL) && ($_SERVER["REQUEST_METHOD"]=="GET")) {
98
99       #-- only store, if we got just a few QueryString parameters
100       if (count($_GET) <= 2) {
101          ewiki_put_cache($action, $id, $o);
102       }
103    }
104 }
105
106
107 #-- real save function
108 function ewiki_put_cache($action, $id, &$o) {
109    #-- save into cache dir
110    if (defined("EWIKI_CACHE_DIR") && EWIKI_CACHE_DIR) {
111       $file = EWIKI_CACHE_DIR . "/" . $action . "," . urlencode($id);
112       $f = gzopen($file, "w9");
113       if ($f) {
114          gzwrite($f, $o);
115          fclose($f);
116       }
117    }
118    #-- store in ewiki database
119    elseif (defined("EWIKI_CACHE_DB") && (EWIKI_CACHE_DB)) {
120       $id = EWIKI_CACHE_DB."$action/$id";
121       $save = array(
122          "id" => $id,
123          "version" => 1,
124          "flags" => EWIKI_DB_F_BINARY¦EWIKI_DB_F_TEXT|EWIKI_DB_F_HTML,
125          "created" => $data["lastmodified"],
126          "lastmodified" => time(),
127          "content" => &$o,
128          "meta" => array("class"=>"cache"),
129          "author" => ewiki_author("ewiki_cache"),
130       );
131       ewiki_db::WRITE($save, true);
132    }
133 }
134
135
136 ?>