]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AppendText.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / AppendText.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 /**
24  * Append text to an existing page.
25  *
26  * @Author: Pascal Giard <evilynux@gmail.com>
27  *
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.
30  *
31  * Todo: multiple pages. e.g. AppendText s=~[CategoryINtime~] page=<!plugin TitleSearch intime !>
32  */
33 class WikiPlugin_AppendText
34 extends WikiPlugin
35 {
36     function getName() {
37         return _("AppendText");
38     }
39
40     function getDescription() {
41         return _("Append text to any page in this wiki.");
42     }
43
44     function getDefaultArguments() {
45         return array('page'     => '[pagename]',
46                      'pages'    => false,
47                      's'        => '',  // Text to append.
48                      'before'   => '',  // Add before (ignores after if defined)
49                      'after'    => '',  // Add after line beginning with this
50                      'redirect' => false // Redirect to modified page
51                      );
52     }
53
54     function _fallback($addtext, $oldtext, $notfound, &$message) {
55         $message->pushContent(sprintf(_("%s not found"), $notfound).". ".
56                               _("Appending at the end.")."\n");
57         return $oldtext . "\n" . $addtext;
58     }
59
60     function run($dbi, $argstr, &$request, $basepage) {
61
62         $args = $this->getArgs($argstr, $request);
63         if (!$args['pages'] or !$request->isPost()) {
64             return $this->_work($args['page'], $args, $dbi, $request);
65         } else {
66             $html = HTML();
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));
71             }
72             return $html;
73         }
74     }
75
76     function _work($pagename, $args, $dbi, &$request) {
77         if (empty($args['s'])) {
78             if ($request->isPost()) {
79                 if ($pagename != _("AppendText"))
80                     return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
81             }
82             return '';
83         }
84
85         $page = $dbi->getPage($pagename);
86         $message = HTML();
87
88         if (!$page->exists()) { // We might want to create it?
89             $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!\n"),
90                                             $pagename));
91             return $message;
92         }
93
94         $current = $page->getCurrentRevision();
95         $oldtext = $current->getPackedContent();
96         $text = $args['s'];
97
98         // If a "before" or "after" is specified but not found, we simply append text to the end.
99         if (!empty($args['before'])) {
100             $before = preg_quote($args['before'], "/");
101             // Insert before
102             $newtext = preg_match("/\n${before}/", $oldtext)
103                 ? preg_replace("/(\n${before})/",
104                                "\n" .  preg_quote($text, "/") . "\\1",
105                                $oldtext)
106                 : $this->_fallback($text, $oldtext, $args['before'], $message);
107         } elseif (!empty($args['after'])) {
108             // Insert after
109             $after = preg_quote($args['after'], "/");
110             $newtext = preg_match("/\n${after}/", $oldtext)
111                 ? preg_replace("/(\n${after})/",
112                                "\\1\n" .  preg_quote($text, "/"),
113                                $oldtext)
114                 : $this->_fallback($text, $oldtext, $args['after'], $message);
115         } else {
116             // Append at the end
117             $newtext = $oldtext .
118                 "\n" . $text;
119         }
120
121         require_once("lib/loadsave.php");
122         $meta = $current->_data;
123         $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
124         if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
125             $message->pushContent(HTML::p(array('class' => 'feedback'),
126                                           _("Page successfully updated.")));
127         }
128
129         // AppendText has been called from the same page that got modified
130         // so we directly show the page.
131         if ( $request->getArg($pagename) == $pagename ) {
132             // TODO: Just invalidate the cache, if AppendText didn't
133             // change anything before.
134             //
135             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
136
137         // The user asked to be redirected to the modified page
138         } elseif ($args['redirect']) {
139             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
140
141         } else {
142             $link = HTML::em(WikiLink($pagename));
143             $message->pushContent(HTML::Raw(sprintf(_("Go to %s."), $link->asXml())));
144         }
145
146         return $message;
147     }
148 };
149
150 // Local Variables:
151 // mode: php
152 // tab-width: 8
153 // c-basic-offset: 4
154 // c-hanging-comment-ender-p: nil
155 // indent-tabs-mode: nil
156 // End:
157 ?>