]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AppendText.php
Activated Id substitution for Subversion
[SourceForge/phpwiki.git] / lib / plugin / AppendText.php
1 <?php // -*-php-*-
2 rcs_id('$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
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 getVersion() {
45         return preg_replace("/[Revision: $]/", '',
46                             "\$Revision: 1.8 $");
47     }
48
49     function getDefaultArguments() {
50         return array('page'     => '[pagename]',
51                      'pages'    => false,
52                      's'        => '',  // Text to append.
53                      'before'   => '',  // Add before (ignores after if defined)
54                      'after'    => '',  // Add after line beginning with this
55                      'redirect' => false // Redirect to modified page
56                      );
57     }
58
59     function _fallback($addtext, $oldtext, $notfound, &$message) {
60         $message->pushContent(sprintf(_("%s not found"), $notfound).". ".
61                               _("Appending at the end.")."\n");
62         return $oldtext . "\n" . $addtext;
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66
67         $args = $this->getArgs($argstr, $request);
68         if (!$args['pages'] or !$request->isPost()) {
69             return $this->_work($args['page'], $args, $dbi, $request);
70         } else {
71             $html = HTML();
72             if ($args['page'] != $basepage)
73                 $html->pushContent("pages argument overrides page argument. ignored.",HTML::br());
74             foreach ($args['pages'] as $pagename) {
75                 $html->pushContent($this->_work($pagename, $args, $dbi, $request));
76             }
77             return $html;
78         }
79     }
80
81     function _work($pagename, $args, $dbi, &$request) {
82         if (empty($args['s'])) {
83             if ($request->isPost()) {
84                 if ($pagename != _("AppendText"))
85                     return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
86             }
87             return '';
88         }
89
90         $page = $dbi->getPage($pagename);
91         $message = HTML();
92
93         if (!$page->exists()) { // We might want to create it?
94             $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!\n"),
95                                             $pagename));
96             return $message;
97         }
98             
99         $current = $page->getCurrentRevision();
100         $oldtext = $current->getPackedContent();
101         $text = $args['s'];
102
103         // If a "before" or "after" is specified but not found, we simply append text to the end.
104         if (!empty($args['before'])) {
105             $before = preg_quote($args['before'], "/");
106             // Insert before
107             $newtext = preg_match("/\n${before}/", $oldtext) 
108                 ? preg_replace("/(\n${before})/",
109                                "\n" .  preg_quote($text, "/") . "\\1",
110                                $oldtext) 
111                 : $this->_fallback($text, $oldtext, $args['before'], $message);
112         } elseif (!empty($args['after'])) {
113             // Insert after
114             $after = preg_quote($args['after'], "/");
115             $newtext = preg_match("/\n${after}/", $oldtext) 
116                 ? preg_replace("/(\n${after})/",
117                                "\\1\n" .  preg_quote($text, "/"),
118                                $oldtext)
119                 : $this->_fallback($text, $oldtext, $args['after'], $message);
120         } else {
121             // Append at the end
122             $newtext = $oldtext .
123                 "\n" . $text;
124         }
125
126         require_once("lib/loadsave.php");
127         $meta = $current->_data;
128         $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
129         if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
130             $message->pushContent(_("Page successfully updated."), HTML::br());
131         }
132         
133         // AppendText has been called from the same page that got modified
134         // so we directly show the page.
135         if ( $request->getArg($pagename) == $pagename ) {
136             // TODO: Just invalidate the cache, if AppendText didn't 
137             // change anything before.
138             // 
139             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
140
141         // The user asked to be redirected to the modified page
142         } elseif ($args['redirect']) {
143             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
144             
145         } else {
146             $link = HTML::em(WikiLink($pagename));
147             $message->pushContent(HTML::Raw(sprintf(_("Go to %s."), $link->asXml())));
148         }
149
150         return $message;
151     }
152 };
153
154 // $Log: not supported by cvs2svn $
155 // Revision 1.7  2005/04/02 03:05:43  uckelman
156 // Removed & from vars passed by reference (not needed, causes PHP to complain).
157 //
158 // Revision 1.6  2005/02/12 17:24:23  rurban
159 // locale update: missing . : fixed. unified strings
160 // proper linebreaks
161 //
162 // Revision 1.5  2004/11/26 18:39:02  rurban
163 // new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
164 //
165 // Revision ext-1.4  2004/11/25 15:39:40  Pascal Giard <evilynux@gmail.com>
166 // * Directly including modified page when AppendText got called from 
167 //   the page to be modified.
168 // * Translatable link to page.
169 //
170 // Revision ext-1.3  2004/11/25  9:44:45  Pascal Giard <evilynux@gmail.com>
171 // * text modified to s to workaround mozilla bug.
172 // * Added redirect parameter allowing you to be redirected to the modified page.
173 //
174 // Revision 1.4  2004/11/25 17:20:52  rurban
175 // and again a couple of more native db args: backlinks
176 //
177 // Revision 1.3  2004/11/25 13:56:23  rurban
178 // renamed text to s because of nasty mozilla radio button bug
179 //
180 // Revision 1.2  2004/11/25 08:29:43  rurban
181 // update from Pascal
182 //
183 // Revision ext-1.2  2004/11/24 11:22:30  Pascal Giard <evilynux@gmail.com>
184 // * Integrated rurban's modifications.
185 //
186 // Revision 1.1  2004/11/24 09:25:35  rurban
187 // simple plugin by Pascal Giard (QC/EMC)
188 //
189 // Revision 1.0  2004/11/23 09:43:35  epasgia
190 // * Initial version.
191 //
192
193 // Local Variables:
194 // mode: php
195 // tab-width: 8
196 // c-basic-offset: 4
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil
199 // End:
200 ?>