4 * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Append text to an existing page.
26 * @Author: Pascal Giard <evilynux@gmail.com>
28 * See http://sourceforge.net/mailarchive/message.php?msg_id=10141823
29 * why not to use "text" as parameter. Nasty mozilla bug with mult. radio rows.
31 * Todo: multiple pages. e.g. AppendText s=~[CategoryINtime~] page=<!plugin TitleSearch intime !>
33 class WikiPlugin_AppendText
36 function getDescription()
38 return _("Append text to any page in this wiki.");
41 function getDefaultArguments()
43 return array('page' => '[pagename]',
45 's' => '', // Text to append.
46 'before' => '', // Add before (ignores after if defined)
47 'after' => '', // Add after line beginning with this
48 'redirect' => false // Redirect to modified page
52 private function fallback($addtext, $oldtext, $notfound, &$message)
54 $message->pushContent(sprintf(_("%s not found"), $notfound) . ". " .
55 _("Appending at the end.") . "\n");
56 return $oldtext . "\n" . $addtext;
59 function run($dbi, $argstr, &$request, $basepage)
62 $args = $this->getArgs($argstr, $request);
63 if (!$args['pages'] or !$request->isPost()) {
64 return $this->work($args['page'], $args, $dbi, $request);
67 if ($args['page'] != $basepage)
68 $html->pushContent("pages argument overrides page argument. ignored.", HTML::br());
69 foreach ($args['pages'] as $pagename) {
70 $html->pushContent($this->work($pagename, $args, $dbi, $request));
76 private function work($pagename, $args, $dbi, &$request)
78 if (empty($args['s'])) {
79 if ($request->isPost()) {
80 if ($pagename != _("AppendText"))
81 return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
86 $page = $dbi->getPage($pagename);
89 if (!$page->exists()) { // We might want to create it?
90 $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!"),
95 $current = $page->getCurrentRevision();
96 $oldtext = $current->getPackedContent();
99 // If a "before" or "after" is specified but not found, we simply append text to the end.
100 if (!empty($args['before'])) {
101 $before = preg_quote($args['before'], "/");
103 $newtext = preg_match("/\n${before}/", $oldtext)
104 ? preg_replace("/(\n${before})/",
105 "\n" . preg_quote($text, "/") . "\\1",
107 : $this->fallback($text, $oldtext, $args['before'], $message);
108 } elseif (!empty($args['after'])) {
110 $after = preg_quote($args['after'], "/");
111 $newtext = preg_match("/\n${after}/", $oldtext)
112 ? preg_replace("/(\n${after})/",
113 "\\1\n" . preg_quote($text, "/"),
115 : $this->fallback($text, $oldtext, $args['after'], $message);
118 $newtext = $oldtext .
122 require_once 'lib/loadsave.php';
123 $meta = $current->_data;
124 $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
125 if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
126 $message->pushContent(HTML::p(array('class' => 'feedback'),
127 _("Page successfully updated.")));
130 // AppendText has been called from the same page that got modified
131 // so we directly show the page.
132 if ($request->getArg($pagename) == $pagename) {
133 // TODO: Just invalidate the cache, if AppendText didn't
134 // change anything before.
136 return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
138 // The user asked to be redirected to the modified page
139 } elseif ($args['redirect']) {
140 return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
143 $link = HTML::em(WikiLink($pagename));
144 $message->pushContent(HTML::raw(sprintf(_("Go to %s."), $link->asXml())));
155 // c-hanging-comment-ender-p: nil
156 // indent-tabs-mode: nil