]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/AppendText.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / plugin / AppendText.php
1 <?php // -*-php-*-
2 /*
3  * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
4  *
5  * This file is part of PhpWiki.
6  *
7  * PhpWiki is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * PhpWiki is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /**
23  * Append text to an existing page.
24  *
25  * @Author: Pascal Giard <evilynux@gmail.com>
26  *
27  * See http://sourceforge.net/mailarchive/message.php?msg_id=10141823
28  * why not to use "text" as parameter. Nasty mozilla bug with mult. radio rows.
29  *
30  * Todo: multiple pages. e.g. AppendText s=~[CategoryINtime~] page=<!plugin TitleSearch intime !>
31  */
32 class WikiPlugin_AppendText
33 extends WikiPlugin
34 {
35     function getName() {
36         return _("AppendText");
37     }
38
39     function getDescription() {
40         return _("Append text to any page in this wiki.");
41     }
42
43     function getDefaultArguments() {
44         return array('page'     => '[pagename]',
45                      'pages'    => false,
46                      's'        => '',  // Text to append.
47                      'before'   => '',  // Add before (ignores after if defined)
48                      'after'    => '',  // Add after line beginning with this
49                      'redirect' => false // Redirect to modified page
50                      );
51     }
52
53     function _fallback($addtext, $oldtext, $notfound, &$message) {
54         $message->pushContent(sprintf(_("%s not found"), $notfound).". ".
55                               _("Appending at the end.")."\n");
56         return $oldtext . "\n" . $addtext;
57     }
58
59     function run($dbi, $argstr, &$request, $basepage) {
60
61         $args = $this->getArgs($argstr, $request);
62         if (!$args['pages'] or !$request->isPost()) {
63             return $this->_work($args['page'], $args, $dbi, $request);
64         } else {
65             $html = HTML();
66             if ($args['page'] != $basepage)
67                 $html->pushContent("pages argument overrides page argument. ignored.",HTML::br());
68             foreach ($args['pages'] as $pagename) {
69                 $html->pushContent($this->_work($pagename, $args, $dbi, $request));
70             }
71             return $html;
72         }
73     }
74
75     function _work($pagename, $args, $dbi, &$request) {
76         if (empty($args['s'])) {
77             if ($request->isPost()) {
78                 if ($pagename != _("AppendText"))
79                     return HTML($request->redirect(WikiURL($pagename, false, 'absurl'), false));
80             }
81             return '';
82         }
83
84         $page = $dbi->getPage($pagename);
85         $message = HTML();
86
87         if (!$page->exists()) { // We might want to create it?
88             $message->pushContent(sprintf(_("Page could not be updated. %s doesn't exist!"),
89                                             $pagename));
90             return $message;
91         }
92
93         $current = $page->getCurrentRevision();
94         $oldtext = $current->getPackedContent();
95         $text = $args['s'];
96
97         // If a "before" or "after" is specified but not found, we simply append text to the end.
98         if (!empty($args['before'])) {
99             $before = preg_quote($args['before'], "/");
100             // Insert before
101             $newtext = preg_match("/\n${before}/", $oldtext)
102                 ? preg_replace("/(\n${before})/",
103                                "\n" .  preg_quote($text, "/") . "\\1",
104                                $oldtext)
105                 : $this->_fallback($text, $oldtext, $args['before'], $message);
106         } elseif (!empty($args['after'])) {
107             // Insert after
108             $after = preg_quote($args['after'], "/");
109             $newtext = preg_match("/\n${after}/", $oldtext)
110                 ? preg_replace("/(\n${after})/",
111                                "\\1\n" .  preg_quote($text, "/"),
112                                $oldtext)
113                 : $this->_fallback($text, $oldtext, $args['after'], $message);
114         } else {
115             // Append at the end
116             $newtext = $oldtext .
117                 "\n" . $text;
118         }
119
120         require_once 'lib/loadsave.php';
121         $meta = $current->_data;
122         $meta['summary'] = sprintf(_("AppendText to %s"), $pagename);
123         if ($page->save($newtext, $current->getVersion() + 1, $meta)) {
124             $message->pushContent(HTML::p(array('class' => 'feedback'),
125                                           _("Page successfully updated.")));
126         }
127
128         // AppendText has been called from the same page that got modified
129         // so we directly show the page.
130         if ( $request->getArg($pagename) == $pagename ) {
131             // TODO: Just invalidate the cache, if AppendText didn't
132             // change anything before.
133             //
134             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
135
136         // The user asked to be redirected to the modified page
137         } elseif ($args['redirect']) {
138             return $request->redirect(WikiURL($pagename, false, 'absurl'), false);
139
140         } else {
141             $link = HTML::em(WikiLink($pagename));
142             $message->pushContent(HTML::Raw(sprintf(_("Go to %s."), $link->asXml())));
143         }
144
145         return $message;
146     }
147 };
148
149 // Local Variables:
150 // mode: php
151 // tab-width: 8
152 // c-basic-offset: 4
153 // c-hanging-comment-ender-p: nil
154 // indent-tabs-mode: nil
155 // End: