]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AppendText.php
HTML::Raw --> HTML::raw
[SourceForge/phpwiki.git] / lib / plugin / AppendText.php
1 <?php
2
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     {
38         return _("AppendText");
39     }
40
41     function getDescription()
42     {
43         return _("Append text to any page in this wiki.");
44     }
45
46     function getDefaultArguments()
47     {
48         return array('page' => '[pagename]',
49             'pages' => false,
50             's' => '', // Text to append.
51             'before' => '', // Add before (ignores after if defined)
52             'after' => '', // Add after line beginning with this
53             'redirect' => false // Redirect to modified page
54         );
55     }
56
57     private function fallback($addtext, $oldtext, $notfound, &$message)
58     {
59         $message->pushContent(sprintf(_("%s not found"), $notfound) . ". " .
60             _("Appending at the end.") . "\n");
61         return $oldtext . "\n" . $addtext;
62     }
63
64     function run($dbi, $argstr, &$request, $basepage)
65     {
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     private function work($pagename, $args, $dbi, &$request)
82     {
83         if (empty($args['s'])) {
84             if ($request->isPost()) {
85                 if ($pagename != _("AppendText"))
86                     return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
87             }
88             return HTML();
89         }
90
91         $page = $dbi->getPage($pagename);
92         $message = HTML();
93
94         if (!$page->exists()) { // We might want to create it?
95             $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!"),
96                 $pagename));
97             return $message;
98         }
99
100         $current = $page->getCurrentRevision();
101         $oldtext = $current->getPackedContent();
102         $text = $args['s'];
103
104         // If a "before" or "after" is specified but not found, we simply append text to the end.
105         if (!empty($args['before'])) {
106             $before = preg_quote($args['before'], "/");
107             // Insert before
108             $newtext = preg_match("/\n${before}/", $oldtext)
109                 ? preg_replace("/(\n${before})/",
110                     "\n" . preg_quote($text, "/") . "\\1",
111                     $oldtext)
112                 : $this->fallback($text, $oldtext, $args['before'], $message);
113         } elseif (!empty($args['after'])) {
114             // Insert after
115             $after = preg_quote($args['after'], "/");
116             $newtext = preg_match("/\n${after}/", $oldtext)
117                 ? preg_replace("/(\n${after})/",
118                     "\\1\n" . preg_quote($text, "/"),
119                     $oldtext)
120                 : $this->fallback($text, $oldtext, $args['after'], $message);
121         } else {
122             // Append at the end
123             $newtext = $oldtext .
124                 "\n" . $text;
125         }
126
127         require_once 'lib/loadsave.php';
128         $meta = $current->_data;
129         $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
130         if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
131             $message->pushContent(HTML::p(array('class' => 'feedback'),
132                 _("Page successfully updated.")));
133         }
134
135         // AppendText has been called from the same page that got modified
136         // so we directly show the page.
137         if ($request->getArg($pagename) == $pagename) {
138             // TODO: Just invalidate the cache, if AppendText didn't
139             // change anything before.
140             //
141             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
142
143             // The user asked to be redirected to the modified page
144         } elseif ($args['redirect']) {
145             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
146
147         } else {
148             $link = HTML::em(WikiLink($pagename));
149             $message->pushContent(HTML::raw(sprintf(_("Go to %s."), $link->asXml())));
150         }
151
152         return $message;
153     }
154 }
155
156 // Local Variables:
157 // mode: php
158 // tab-width: 8
159 // c-basic-offset: 4
160 // c-hanging-comment-ender-p: nil
161 // indent-tabs-mode: nil
162 // End: