]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AppendText.php
renamed text to s because of nasty mozilla radio button bug
[SourceForge/phpwiki.git] / lib / plugin / AppendText.php
1 <?php // -*-php-*-
2 rcs_id('$Id: AppendText.php,v 1.3 2004-11-25 13:56:23 rurban Exp $');
3 /*
4 Copyright 2004 Pascal Giard <evilynux@gmail.com>
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  * See http://sourceforge.net/mailarchive/forum.php?thread_id=6028698&forum_id=4517 
27  * why not to use "text" as parameter. Nasty mozilla bug with mult. radio rows.
28  */
29 class WikiPlugin_AppendText
30 extends WikiPlugin
31 {
32     function getName() {
33         return _("AppendText");
34     }
35
36     function getDescription() {
37         return _("Append text to any page in this wiki.");
38     }
39
40     function getVersion() {
41         return preg_replace("/[Revision: $]/", '',
42                             "\$Revision: 1.3 $");
43     }
44
45     function getDefaultArguments() {
46         return array('page'     => '[pagename]',
47                      's'        => '',  // Text to append.
48                      'before'   => '',  // Add before (ignores after if defined)
49                      'after'    => '',  // Add after line beginning with this
50                      );
51     }
52
53     function _fallback($addtext, $oldtext, $notfound, &$message) {
54         $message->pushContent(sprintf(_("%s not found. Appending at the end.\n"), $notfound));
55         return $oldtext . "\n" . $addtext;
56     }
57
58     function run($dbi, $argstr, &$request, $basepage) {
59
60         $args = $this->getArgs($argstr, $request);
61         $pagename = $args['page'];
62
63         if (empty($args['s']))
64             if ($request->isPost() and $pagename != _("AppendText"))
65                 return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
66             else    
67                 return '';
68
69         $page = $dbi->getPage($pagename);
70         $message = HTML();
71
72         if (!$page->exists()) { // create it?
73             $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!\n",
74                                             $pagename)));
75             return $message;
76         }
77             
78         $current = $page->getCurrentRevision();
79         $oldtext = $current->getPackedContent();
80         $text = $args['s'];
81
82         // If a "before" or "after" is specified but not found, we simply append text to the end.
83         if (!empty($args['before'])) {
84             $before = preg_quote($args['before'], "/");
85             // Insert before
86             $newtext =
87                 ( preg_match("/\n${before}/", $oldtext) ) ?
88                 preg_replace("/(\n${before})/",
89                              "\n" .  preg_quote($text, "/") . "\\1",
90                              $oldtext) :
91                 $this->_fallback($text, $oldtext, $args['before'], &$message);
92
93         } elseif (!empty($args['after'])) {
94             // Insert after
95             $after = preg_quote($args['after'], "/");
96             $newtext = 
97                 ( preg_match("/\n${after}/", $oldtext) ) ?
98                 preg_replace("/(\n${after})/",
99                              "\\1\n" .  preg_quote($text, "/"),
100                              $oldtext) :
101                 $this->_fallback($text, $oldtext, $args['after'], &$message);
102
103         } else {
104             // Append at the end
105             $newtext = $oldtext .
106                 "\n" . $text;
107         }
108
109         require_once("lib/loadsave.php");
110         $meta = $current->_data;
111         $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
112         if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
113             $message->pushContent(_("Page successfully updated."), HTML::br());
114             $message->pushContent(_("Go to "));
115             $message->pushContent(HTML::em(WikiLink($pagename)));
116         }
117
118         return $message;
119     }
120 };
121
122 // $Log: not supported by cvs2svn $
123 // Revision 1.2  2004/11/25 08:29:43  rurban
124 // update from Pascal
125 //
126 // Revision 1.2  2004/11/24 11:22:30  Pascal Giard <evilynux@gmail.com>
127 // * Integrated rurban's modifications.
128 //
129 // Revision 1.1  2004/11/24 09:25:35  rurban
130 // simple plugin by Pascal Giard (QC/EMC)
131 //
132 // Revision 1.0  2004/11/23 09:43:35  epasgia
133 // * Initial version.
134 //
135
136 // Local Variables:
137 // mode: php
138 // tab-width: 8
139 // c-basic-offset: 4
140 // c-hanging-comment-ender-p: nil
141 // indent-tabs-mode: nil
142 // End:
143 ?>