Fix cache management of the wiki.
[platal.git] / include / wiki.engine.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 $wp = PlWikiPage::currentPage();
23 $n = $wp->path;
24 $feed = false;
25
26 // Check user perms
27 switch (Env::v('action')) {
28 case 'rss': case 'atom': case 'sdf': case 'dc':
29 $wp->prepareFeed();
30 $feed = true;
31 case '': case 'search':
32 break;
33
34 case 'edit':
35 $wp->applyWritePerms();
36 break;
37
38 default:
39 $wp->applyReadPerms();
40 break;
41 }
42
43 if ($p = Post::v('setrperms')) {
44 $wp->applyPerms('admin');
45 if ($wp->setPerms($p, $wp->writePerms())) {
46 Platal::page()->trigSuccess('Permissions mises à jour');
47 }
48 }
49
50 if ($p = Post::v('setwperms')) {
51 $wp->applyPerms('admin');
52 if ($wp->setPerms($wp->readPerms(), $p)) {
53 Platal::page()->trigSuccess('Permissions mises à jour');
54 }
55 }
56
57 // Generate cache even if we don't have access rights
58 $wiki_cache = $wp->cacheFilename();
59 $cache_exists = file_exists($wiki_cache);
60 if (Env::v('action') || !$cache_exists) {
61 if ($cache_exists && !$feed) {
62 $wp->removePageCache();
63 $cache_exists = false;
64 }
65
66 // we leave pmwiki do whatever it wants and store everything
67 ob_start();
68 require_once($globals->spoolroot . '/wiki/pmwiki.php');
69
70 $wikiAll = ob_get_clean();
71 pl_clear_errors();
72
73 // the pmwiki skin we are using (almost empty) has these keywords:
74 $i = strpos($wikiAll, "<!--/HeaderText-->");
75 $j = strpos($wikiAll, "<!--/PageLeftFmt-->", $i);
76 }
77
78 $wiki_exists = file_exists($wp->filename());
79
80 $page =& Platal::page();
81 $page->coreTpl('wiki.tpl');
82
83 if ($feed) {
84 $wikiAll = str_replace('dc:contributor', 'author', $wikiAll);
85 $wikiAll = preg_replace('!<author>.*?\..*?\.(\d{4})\|(.*?)</author>!u', '<author>$2 (X$1)</author>', $wikiAll);
86 $wikiAll = str_replace('<link>./', '<link>' . $globals->baseurl . '/' . $platal->ns, $wikiAll);
87 echo $wikiAll;
88 pl_clear_errors();
89 exit;
90 } elseif (Env::v('action')) {
91 $page->assign('pl_extra_header', substr($wikiAll, 0, $i));
92 $wikiAll = substr($wikiAll, $j);
93 } else {
94 if (!$cache_exists && $wiki_exists) {
95 $wikiAll = substr($wikiAll, $j);
96 file_put_contents($wiki_cache, $wikiAll);
97 } elseif ($cache_exists) {
98 $wikiAll = file_get_contents($wiki_cache);
99 } elseif (S::has_perms()) {
100 $wikiAll = "<p>La page de wiki $n n'existe pas. "
101 . "Il te suffit de <a href='" . str_replace('.', '/', $n) . "?action=edit'>l'éditer</a></p>";
102 } else {
103 global $platal;
104 $platal->error404();
105 }
106 }
107
108 // Check user perms
109 $wp->applyReadPerms();
110
111 $page->assign('perms', array($wp->readPerms(), $wp->writePerms()));
112 $page->assign('perms_opts', PlWikiPage::permOptions());
113
114 $page->assign('canedit', $wp->canWrite());
115 $page->assign('has_perms', S::has_perms());
116
117 $page->assign('wikipage', str_replace('.', '/', $n));
118 if (!$feed && $wp->writePerms() == 'admin' && !Env::v('action') && $wiki_exists) {
119 $page->assign('pmwiki_cache', $wiki_cache);
120 } else {
121 $page->assign('pmwiki', $wikiAll);
122 $page->assign('text', true);
123 }
124 $page->addCssLink('wiki.css');
125 $page->addJsLink('wiki.js');
126 if (!Env::v('action')) {
127 $url = '/' . str_replace('.', '/', $n) . '?action=rss';
128 if (S::logged()) {
129 if (S::user()) {
130 $url .= '&user=' . S::user()->login() . '&hash=' . S::v('core_rss_hash');
131 } else {
132 // TODO(vzanotti): trash that code when forlife support will be gone.
133 $url .= '&user=' . S::v('forlife') . '&hash=' . S::v('core_rss_hash');
134 }
135 }
136 $page->setRssLink($n, $url);
137 }
138
139 $page->run();
140
141 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
142 ?>