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