]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminMarkup.php
Better user interface and feedback messages
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminMarkup.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2005 $ThePhpWikiProgrammingTeam
5  Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * Usage:   <?plugin WikiAdminMarkup s||=* ?> or called via WikiAdminSelect
26  * @author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  * Requires PHP 4.2 so far.
30  */
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
33
34 class WikiPlugin_WikiAdminMarkup
35 extends WikiPlugin_WikiAdminSelect
36 {
37     function getName() {
38         return _("WikiAdminMarkup");
39     }
40
41     function getDescription() {
42         return _("Change the markup type of selected pages.");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array_merge 
52             (
53              PageList::supportedArgs(),
54              array(
55                    's'          => false,
56                    'markup'     => 2,
57                    /* Columns to include in listing */
58                    'info'     => 'pagename,markup,mtime',
59                    ));
60     }
61
62     function chmarkupPages(&$dbi, &$request, $pages, $newmarkup) {
63         $result = HTML::div();
64         $ul = HTML::ul();
65         $count = 0;
66         foreach ($pages as $name) {
67             $page = $dbi->getPage($name);
68             $current = $page->getCurrentRevision();
69             $markup = $current->get('markup');
70             if ( !$markup or $newmarkup != $markup ) {
71                 if (!mayAccessPage('change', $name)) {
72                     $result->setAttr('class', 'error');
73                     $result->pushContent(HTML::p(fmt("Access denied to change page '%s'.",
74                                                   WikiLink($name))));
75                 } else {
76                     $version = $current->getVersion();
77                     $meta = $current->_data;
78                     $meta['markup'] = $newmarkup;
79                     // convert text?
80                     $text = $current->getPackedContent();
81                     $meta['summary'] = sprintf(_("Change markup type from %s to %s"), $markup, $newmarkup);
82                     $meta['is_minor_edit'] = 0;
83                     $meta['author'] =  $request->_user->UserName();
84                     unset($meta['mtime']); // force new date
85                     $page->save($text, $version + 1, $meta);
86                     $current = $page->getCurrentRevision();
87                     if ($current->get('markup') === $newmarkup) {
88                         $ul->pushContent(HTML::li(fmt("change page '%s' to markup type '%s'.",
89                                                       WikiLink($name), $newmarkup)));
90                         $count++;
91                     } else {
92                         $ul->pushContent(HTML::li(fmt("Couldn't change page '%s' to markup type '%s'.", 
93                                                       WikiLink($name), $newmarkup)));
94                     }
95                 }
96             }
97         }
98         if ($count) {
99             $dbi->touch();
100             $result->setAttr('class', 'feedback');
101             if ($count == 1) {
102                 $result->pushContent(HTML::p("One page has been permanently changed:"));
103             } else {
104                 $result->pushContent(HTML::p(fmt("%s pages have been permanently changed:", $count)));
105             }
106             $result->pushContent($ul);
107             return $result;
108         } else {
109             $result->setAttr('class', 'error');
110             $result->pushContent(HTML::p("No pages changed."));
111             return $result;
112         }
113     }
114     
115     function run($dbi, $argstr, &$request, $basepage) {
116         if ($request->getArg('action') != 'browse')
117             if (!$request->getArg('action') == _("PhpWikiAdministration/Markup"))
118                 return $this->disabled("(action != 'browse')");
119         
120         $args = $this->getArgs($argstr, $request);
121         $this->_args = $args;
122         $this->preSelectS($args, $request);
123
124         $p = $request->getArg('p');
125         if (!$p) $p = $this->_list;
126         $post_args = $request->getArg('admin_markup');
127         if (!$request->isPost() and empty($post_args['markup']))
128             $post_args['markup'] = $args['markup'];
129         $next_action = 'select';
130         $pages = array();
131         if ($p && !$request->isPost())
132             $pages = $p;
133         if ($p && $request->isPost() &&
134             !empty($post_args['button']) && empty($post_args['cancel'])) {
135             // without individual PagePermissions:
136             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
137                 $request->_notAuthorized(WIKIAUTH_ADMIN);
138                 $this->disabled("! user->isAdmin");
139             }
140             // DONE: error message if not allowed.
141             if ($post_args['action'] == 'verify') {
142                 // Real action
143                 return $this->chmarkupPages($dbi, $request, array_keys($p), 
144                                             $post_args['markup']);
145             }
146             if ($post_args['action'] == 'select') {
147                 if (!empty($post_args['markup']))
148                     $next_action = 'verify';
149                 foreach ($p as $name => $c) {
150                     $pages[$name] = 1;
151                 }
152             }
153         }
154         if ($next_action == 'select' and empty($pages)) {
155             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], 
156                                          $args['exclude']);
157         }
158     
159         if ($next_action == 'select') {
160             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
161         } else {
162             $pagelist = new PageList_Unselectable($args['info'], $args['exclude'], $args);
163         }
164         $pagelist->addPageList($pages);
165
166         $header = HTML::fieldset();
167         if ($next_action == 'verify') {
168             $button_label = _("Yes");
169             $header->pushContent(
170               HTML::p(HTML::strong(
171                 _("Are you sure you want to permanently change the markup type of the selected files?"))));
172             $header = $this->chmarkupForm($header, $post_args);
173         }
174         else {
175             $button_label = _("Change markup type");
176             $header->pushContent(HTML::legend(_("Select the pages to change the markup type")));
177             $header = $this->chmarkupForm($header, $post_args);
178         }
179
180         $buttons = HTML::p(Button('submit:admin_markup[button]', $button_label, 'wikiadmin'),
181                            Button('submit:admin_markup[cancel]', _("Cancel"), 'button'));
182         $header->pushContent($buttons);
183
184         return HTML::form(array('action' => $request->getPostURL(),
185                                 'method' => 'post'),
186                           $header,
187                           $pagelist->getContent(),
188                           HiddenInputs($request->getArgs(),
189                                         false,
190                                         array('admin_markup')),
191                           HiddenInputs(array('admin_markup[action]' => $next_action)),
192                           ENABLE_PAGEPERM
193                           ? ''
194                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
195     }
196
197     function chmarkupForm(&$header, $post_args) {
198         $header->pushContent(_("Change markup")." ");
199         $header->pushContent(' '._("to").': ');
200         $header->pushContent(HTML::input(array('name' => 'admin_markup[markup]',
201                                                'value' => $post_args['markup'])));
202         return $header;
203     }
204 }
205
206 // Local Variables:
207 // mode: php
208 // tab-width: 8
209 // c-basic-offset: 4
210 // c-hanging-comment-ender-p: nil
211 // indent-tabs-mode: nil
212 // End:
213 ?>