From 9162f4ed1e2f9567805f6465511d69460c83d766 Mon Sep 17 00:00:00 2001 From: x2001corpet Date: Sun, 25 Mar 2007 00:44:11 +0000 Subject: [PATCH] admin wiki, delete pages or rename pages git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1611 839d8a87-29fc-0310-9880-83ba4fa771e5 --- include/wiki.inc.php | 140 +++++++++++++++++++++++++++++++++++++++++++++++ modules/admin.php | 22 +++++++- templates/admin/wiki.tpl | 30 ++++++---- 3 files changed, 181 insertions(+), 11 deletions(-) diff --git a/include/wiki.inc.php b/include/wiki.inc.php index c0a8945..de810be 100644 --- a/include/wiki.inc.php +++ b/include/wiki.inc.php @@ -174,5 +174,145 @@ function wiki_require_page($pagename) system('wget '.$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; + } + } + } + wiki_putfile($newfile, join("\n", $lines)); + + // change wiki links in all wiki pages + $endname = substr($pagename_dots, strpos($pagename_dots,'.')+1); + $pages = array(); + exec("grep ".$endname." ".wiki_work_dir()."/* -sc", $pages); + foreach($pages as $line) { + if (preg_match('%/([^/:]+):([0-9]+)$%', $line, $vals) && $vals[2] > 0) { + $inpage = $vals[1]; + $lines = explode("\n", file_get_contents(wiki_work_dir().'/'.$inpage)); + $changed = false; + // find all wiki links in page and change if linked to this page + foreach ($lines as $i => $line) { + $links = wiki_links_in_line($line, substr($inpage, 0, strpos($inpage, '.'))); + $newline = ''; $last = 0; + foreach ($links as $link) { + if ($link['href'] == $pagename_dots) { + $newline .= substr($line, $last, $link['pos']); + $newline .= '[['.$newname_dots.$link['sup'].($link['alt']?(' |'.$link['alt']):'').']]'; + $last = $link['pos']+$link['size']; + $changedLinks++; + } + } + if ($last != 0) { + $newline .= substr($line, $last); + $lines[$i] = $newline; + $changed = true; + } + } + if ($changed) + { + wiki_putfile(wiki_work_dir().'/'.$inpage, join("\n", $lines)); + } + } + } + if ($changedLinks > 0) { + return $changedLinks; + } + return true; +} + +function wiki_rename_folder($pagename, $newname, $changeLinks = true) +{ +} + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?> diff --git a/modules/admin.php b/modules/admin.php index 1b13c58..e33f27b 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -892,7 +892,7 @@ class AdminModule extends PLModule $table_editor->apply($page, $action, $id); } - function handler_wiki(&$page, $action='list') + function handler_wiki(&$page, $action='list', $wikipage='', $wikipage2='') { require_once 'wiki.inc.php'; @@ -913,6 +913,26 @@ class AdminModule extends PLModule } } } + + if ($action == 'delete' && $wikipage != '') { + if (wiki_delete_page($wikipage)) { + $page->trig("La page ".$wikipage." a été supprimée."); + } else { + $page->trig("Impossible de supprimer la page ".$wikipage."."); + } + } + + if ($action == 'rename' && $wikipage != '' && $wikipage2 != '' && $wikipage != $wikipage2) { + if ($changedLinks = wiki_rename_page($wikipage, $wikipage2)) { + $s = 'La page '.$wikipage.' a été déplacée en '.$wikipage2.'.'; + if (is_numeric($changedLinks)) { + $s .= $changedLinks.' lien'.(($changedLinks>1)?'s ont été modifiés.':' a été modifié.'); + } + $page->trig($s); + } else { + $page->trig("Impossible de déplacer la page ".$wikipage); + } + } $perms = wiki_perms_options(); diff --git a/templates/admin/wiki.tpl b/templates/admin/wiki.tpl index e6ec181..149f294 100644 --- a/templates/admin/wiki.tpl +++ b/templates/admin/wiki.tpl @@ -36,24 +36,32 @@ return false; } var toggle = 0; - function replie() { + function replie(me, cat) { if (toggle == 1) return; toggle = 2; - var cat=$.trim($(this).parent().text().replace(/(.*)\([0-9]+\)/, "$1")); $("tr[@id^=row/"+cat+"/]").hide(); - $(this).attr('src', 'images/k1.gif').unbind("click", replie).click(deplie); - setTimeout("toggle = 0;", 10); + $(me).attr('src', 'images/k1.gif'); } - function deplie(image) { + function deplie(me, cat) { if (toggle == 2) return; toggle = 1; - var cat=$.trim($(this).parent().text().replace(/(.*)\([0-9]+\)/, "$1")); $("tr[@id^=row/"+cat+"/]").show(); - $(this).attr('src', 'images/k2.gif').unbind("click", deplie).click(replie); + $(me).attr('src', 'images/k2.gif'); + } + function toggle_folder() { + me = this; + if ($(this).attr("class") == "wiki_category") + me = $("../img.wiki_root", me)[0]; + var cat=$.trim($(me).parent().text().replace(/(.*)\([0-9]+\)/, "$1")); + if ($(me).attr('src') == "images/k1.gif") { + deplie(me, cat); + } + replie(me, cat); setTimeout("toggle = 0;", 10); } $(document).ready(function() { - $("tr.pair img[@alt=-]").css("cursor","pointer").each(replie); + $("tr.pair img[@alt=-]").css("cursor","pointer").click(toggle_folder).each(toggle_folder); + $(".wiki_category").css("cursor","pointer").click(toggle_folder); }); // --> @@ -82,8 +90,8 @@ {foreach from=$wiki_pages key=cat item=pages} - - - {$cat} ({$pages|@count}) {icon name=magnifier title="Changements récents"} + - + {$cat} ({$pages|@count}) {icon name=magnifier title="Changements récents"} {foreach from=$pages item=perm key=page name=pages} @@ -103,6 +111,8 @@ {$perm.edit} + {icon name=book_next title='déplacer'} + {icon name=delete title='supprimer'} -- 2.1.4