- return realpath($globals->spoolroot.'htdocs/'.$globals->wiki->workdir);
-}
-
-function wiki_template($n) {
- global $wikisite;
- return $tpl = wiki_work_dir().'/cache_'.$wikisite.'_'.$n.'.tpl';
-}
-
-// several files are used for wiki :
-// - spool/wiki.d/PageName : the wiki page
-// - spool/wiki.d/cache_PageName.tpl : the template cache
-// - spool/templates_c/%%...%%cache_PageName.tpl.php : the PHP from Smarty
-function wiki_clear_cache($n) {
- global $page, $wikisite, $wikisites;
- $oldwikisite = $wikisite;
- foreach ($wikisites as $s) {
- $wikisite = $s;
- $tpl = wiki_template($n);
- @unlink($tpl);
- $page->clear_compiled_tpl($tpl);
- }
- $wikisite = $oldwikisite;
-}
-
-// editing pages are not static but used templates too, so we used
-// temp template files containing result from wiki
-function wiki_create_tmp($content) {
- $tmpfile = tempnam(wiki_work_dir(), "temp_");
- $f = fopen($tmpfile, 'w');
- fputs($f, $content);
- fclose($f);
- return $tmpfile;
-}
-
-function wiki_clean_tmp() {
- // clean old tmp files (more than one hour)
- $wiki_work_dir = wiki_work_dir();
- $dh = opendir(wiki_work_dir());
- $time = time();
- while (($file = readdir($dh)) !== false) {
- if (strpos($file, 'temp_') === 0) {
- $created = filectime($wiki_work_dir.'/'.$file);
- if ($time-$created > 60 * 60)
- @unlink($wiki_work_dir.'/'.$file);
+ $pagename_slashes = str_replace('.','/',$pagename);
+ $pagename_dots = str_replace('/','.',$pagename);
+ if (is_file(wiki_work_dir().'/cache_'.$pagename_dots.'.tpl')) {
+ return;
+ }
+ system('wget --no-check-certificate '. escapeshellarg($globals->baseurl.'/'.$pagename_slashes) . ' -O /dev/null');
+}
+
+function wiki_delete_page($pagename)
+{
+ $pagename_dots = str_replace('/','.',$pagename);
+ if (!strpos($pagename_dots, '.')) {
+ return false;
+ }
+ $file = wiki_work_dir().'/'.wiki_filename($pagename_dots);
+ $cachefile = wiki_work_dir().'/cache_'.$pagename_dots.'.tpl';
+ if (is_file($cachefile)) {
+ unlink($cachefile);
+ }
+ if (!is_file($file)) {
+ return false;
+ }
+ unlink($file);
+ return true;
+}
+
+function wiki_links_in_line($line, $groupname)
+{
+ $links = array();
+ if (preg_match_all('@\[\[([^~][^\]\|\?#]*)((\?|#)[^\]\|]+)?(\\|[^\]]+)?\]\]@', $line, $matches, PREG_OFFSET_CAPTURE)) {
+ foreach ($matches[1] as $j => $link) if (!preg_match('@http://@', $link[0])) {
+ $mylink = str_replace('/','.',trim($link[0]));
+ $sup = trim(substr($matches[2][$j][0],1));
+ $alt = trim(substr($matches[4][$j][0],1));
+ $newlink = str_replace(' ','',ucwords($mylink));
+ if (strpos($newlink,'.') === false) {
+ $newlink = $groupname.'.'.$newlink;
+ }
+ if (!$alt && $mylink != $newlink) {
+ $alt = trim($link[0]);
+ }
+ $links[] = array(
+ 'pos' => $matches[0][$j][1],
+ 'size' => strlen($matches[0][$j][0]),
+ 'href' => $newlink,
+ 'sup' => $sup,
+ 'alt' => $alt,
+ 'group' => substr($mylink, 0, strpos($mylink, '.')));
+ }
+ }
+ return $links;
+}
+
+function wiki_rename_page($pagename, $newname, $changeLinks = true)
+{
+ $pagename_dots = str_replace('/','.',$pagename);
+ $newname_dots = str_replace('/','.',$newname);
+ if (!strpos($pagename_dots, '.') || !strpos($newname_dots, '.')) {
+ return false;
+ }
+ $groupname = substr($pagename_dots, 0, strpos($pagename_dots,'.'));
+ $newgroupname = substr($newname_dots, 0, strpos($pagename_dots,'.'));
+
+ $file = wiki_work_dir().'/'.wiki_filename($pagename_dots);
+ $newfile = wiki_work_dir().'/'.wiki_filename($newname_dots);
+ if (!is_file($file)) {
+ // old page doesn't exist
+ return false;
+ }
+ if (!rename($file, $newfile)) {
+ // impossible to renama page
+ return false;
+ }
+
+ if (!$changeLinks) {
+ return true;
+ }
+
+ $changedLinks = 0;
+ // change name inside this folder and ingroup links if changing group
+ $lines = explode("\n", file_get_contents($newfile));
+ $changed = false;
+ foreach ($lines as $i => $line) {
+ list($k, $v) = explode('=', $line, 2);
+ if ($k == 'name' && $v == $pagename_dots) {
+ $lines[$i] = 'name='.$newname_dots;
+ $changed = true;
+ } else if ($groupname != $newgroupname) {
+ $links = wiki_links_in_line($line, $groupname);
+ $newline = ''; $last = 0;
+ foreach ($links as $link) if ($link['group'] == $groupname) {
+ $newline .= substr($line, $last, $link['pos']);
+ $newline .= '[['.$link['href'].$link['sup'].($link['alt']?(' |'.$link['alt']):'').']]';
+ $last = $link['pos']+$link['size'];
+ $changedLinks++;
+ }
+ if ($last != 0) {
+ $newline .= substr($line, $last);
+ $lines[$i] = $newline;
+ $changed = true;
+ }