]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AppendText.php
simple plugin by Pascal Giard (QC/EMC)
[SourceForge/phpwiki.git] / lib / plugin / AppendText.php
1 <?php // -*-php-*-
2 rcs_id('$Id: AppendText.php,v 1.1 2004-11-24 09:25:35 rurban Exp $');
3 /*
4 Copyright 2004 Pascal Giard (QC/EMC)
5
6 This file is not (yet) 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  * TODO: support for lbound and hbound.
26  */
27 class WikiPlugin_AppendText
28 extends WikiPlugin
29 {
30     function getName() {
31         return _("AppendText");
32     }
33
34     function getDescription() {
35         return _("Append text to any page in this wiki.");
36     }
37
38     function getVersion() {
39         return preg_replace("/[Revision: $]/", '',
40                             "\$Revision: 1.1 $");
41     }
42
43     function getDefaultArguments() {
44         return array('page'     => '[pagename]',
45                      'text'     => '',  // Text to append
46                      'before'   => '',  // Add before (ignores after if defined)
47                      'after'    => '',  // Add after line beginning with this
48                      );
49     }
50
51     function run($dbi, $argstr, &$request, $basepage) {
52
53         $args = $this->getArgs($argstr, $request);
54         $pagename = $args['page'];
55
56         if (empty($args['text']))
57             if ($request->isPost() and $pagename != _("AppendText"))
58                 return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
59             else    
60                 return '';
61
62         $page = $dbi->getPage($pagename);
63         $message = HTML();
64
65         if (!$page->exists()) { // create it?
66             $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!\n",
67                                             $pagename)));
68             return $message;
69         }
70             
71         $current = $page->getCurrentRevision();
72         $oldtext = $current->getPackedContent();
73         $text = $args['text'];
74
75         if (!empty($args['before'])) {
76             $before = preg_quote($args['before']);
77             if (preg_match("/\n${before}/", $oldtext)) {
78                 $newtext = preg_replace("/(\n${before})/",
79                                         "\n${text}\\1",
80                                         $oldtext);
81             } else {
82                 $message->pushContent(sprintf(_("%s not found. Appending at the end.\n",
83                                                 $args['before'])));
84                 $newtext = $oldtext . "\n" . $text;
85             }
86         } elseif (!empty($args['after'])) {
87             $after = preg_quote($args['after']);
88             if (preg_match("/\n${after}/", $oldtext)) {
89                 $newtext = preg_replace("/(\n${after})/",
90                                         "\\1\n${text}",
91                                         $oldtext);
92             } else {
93                 $message->pushContent(sprintf(_("%s not found. Appending at the end.\n",
94                                                 $args['after'])));
95                 $newtext = $oldtext . "\n" . $text;
96             }
97         } else {
98             // Append at the end
99             $newtext = $oldtext .
100                 "\n" . $text;
101         }
102         require_once("lib/loadsave.php");
103         $meta = $current->_data;
104         $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
105         if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
106             $message->pushContent(_("Page successfully updated."), HTML::br());
107             $message->pushContent(_("Go to "));
108             $message->pushContent(HTML::em(WikiLink($pagename)));
109         }
110
111         return $message;
112     }
113 };
114
115 // $Log: not supported by cvs2svn $
116 // Revision 1.0  2004/11/23 09:43:35  epasgia
117 // * Initial version.
118 //
119
120 // Local Variables:
121 // mode: php
122 // tab-width: 8
123 // c-basic-offset: 4
124 // c-hanging-comment-ender-p: nil
125 // indent-tabs-mode: nil
126 // End:
127 ?>