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