3 * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Append text to an existing page.
25 * @Author: Pascal Giard <evilynux@gmail.com>
27 * See http://sourceforge.net/mailarchive/message.php?msg_id=10141823
28 * why not to use "text" as parameter. Nasty mozilla bug with mult. radio rows.
30 * Todo: multiple pages. e.g. AppendText s=~[CategoryINtime~] page=<!plugin TitleSearch intime !>
32 class WikiPlugin_AppendText
36 return _("AppendText");
39 function getDescription() {
40 return _("Append text to any page in this wiki.");
43 function getDefaultArguments() {
44 return array('page' => '[pagename]',
46 's' => '', // Text to append.
47 'before' => '', // Add before (ignores after if defined)
48 'after' => '', // Add after line beginning with this
49 'redirect' => false // Redirect to modified page
53 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) {
61 $args = $this->getArgs($argstr, $request);
62 if (!$args['pages'] or !$request->isPost()) {
63 return $this->_work($args['page'], $args, $dbi, $request);
66 if ($args['page'] != $basepage)
67 $html->pushContent("pages argument overrides page argument. ignored.",HTML::br());
68 foreach ($args['pages'] as $pagename) {
69 $html->pushContent($this->_work($pagename, $args, $dbi, $request));
75 function _work($pagename, $args, $dbi, &$request) {
76 if (empty($args['s'])) {
77 if ($request->isPost()) {
78 if ($pagename != _("AppendText"))
79 return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
84 $page = $dbi->getPage($pagename);
87 if (!$page->exists()) { // We might want to create it?
88 $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!"),
93 $current = $page->getCurrentRevision();
94 $oldtext = $current->getPackedContent();
97 // If a "before" or "after" is specified but not found, we simply append text to the end.
98 if (!empty($args['before'])) {
99 $before = preg_quote($args['before'], "/");
101 $newtext = preg_match("/\n${before}/", $oldtext)
102 ? preg_replace("/(\n${before})/",
103 "\n" . preg_quote($text, "/") . "\\1",
105 : $this->_fallback($text, $oldtext, $args['before'], $message);
106 } elseif (!empty($args['after'])) {
108 $after = preg_quote($args['after'], "/");
109 $newtext = preg_match("/\n${after}/", $oldtext)
110 ? preg_replace("/(\n${after})/",
111 "\\1\n" . preg_quote($text, "/"),
113 : $this->_fallback($text, $oldtext, $args['after'], $message);
116 $newtext = $oldtext .
120 require_once 'lib/loadsave.php';
121 $meta = $current->_data;
122 $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
123 if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
124 $message->pushContent(HTML::p(array('class' => 'feedback'),
125 _("Page successfully updated.")));
128 // AppendText has been called from the same page that got modified
129 // so we directly show the page.
130 if ( $request->getArg($pagename) == $pagename ) {
131 // TODO: Just invalidate the cache, if AppendText didn't
132 // change anything before.
134 return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
136 // The user asked to be redirected to the modified page
137 } elseif ($args['redirect']) {
138 return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
141 $link = HTML::em(WikiLink($pagename));
142 $message->pushContent(HTML::Raw(sprintf(_("Go to %s."), $link->asXml())));
153 // c-hanging-comment-ender-p: nil
154 // indent-tabs-mode: nil