]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminMarkup.php
Replace tabs by spaces; remove EOL spaces
[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              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
54              array(
55                    'markup'         => 2,
56                    /* Columns to include in listing */
57                    'info'     => 'pagename,markup,mtime',
58                    ));
59     }
60
61     function chmarkupPages(&$dbi, &$request, $pages, $newmarkup) {
62         $result = HTML::div();
63         $ul = HTML::ul();
64         $count = 0;
65         foreach ($pages as $name) {
66             $page = $dbi->getPage($name);
67             $current = $page->getCurrentRevision();
68             $markup = $current->get('markup');
69             if ( !$markup or $newmarkup != $markup ) {
70                 if (!mayAccessPage('change', $name)) {
71                     $result->setAttr('class', 'error');
72                     $result->pushContent(HTML::p(fmt("Access denied to change page '%s'.",
73                                                   WikiLink($name))));
74                 } else {
75                     $version = $current->getVersion();
76                     $meta = $current->_data;
77                     $meta['markup'] = $newmarkup;
78                     // convert text?
79                     $text = $current->getPackedContent();
80                     $meta['summary'] = sprintf(_("Change markup type from %s to %s"), $markup, $newmarkup);
81                     $meta['is_minor_edit'] = 1;
82                     $meta['author'] =  $request->_user->UserName();
83                     unset($meta['mtime']); // force new date
84                     $page->save($text, $version + 1, $meta);
85                     $current = $page->getCurrentRevision();
86                     if ($current->get('markup') === $newmarkup) {
87                         $ul->pushContent(HTML::li(fmt("change page '%s' to markup type '%s'.",
88                                                       WikiLink($name), $newmarkup)));
89                         $count++;
90                     } else {
91                         $ul->pushContent(HTML::li(fmt("Couldn't change page '%s' to markup type '%s'.",
92                                                       WikiLink($name), $newmarkup)));
93                     }
94                 }
95             }
96         }
97         if ($count) {
98             $dbi->touch();
99             $result->setAttr('class', 'feedback');
100             if ($count == 1) {
101                 $result->pushContent(HTML::p("One page has been permanently changed:"));
102             } else {
103                 $result->pushContent(HTML::p(fmt("%s pages have been permanently changed:", $count)));
104             }
105             $result->pushContent($ul);
106             return $result;
107         } else {
108             $result->setAttr('class', 'error');
109             $result->pushContent(HTML::p("No pages changed."));
110             return $result;
111         }
112     }
113
114     function run($dbi, $argstr, &$request, $basepage) {
115         if ($request->getArg('action') != 'browse')
116             if (!$request->getArg('action') == _("PhpWikiAdministration/Markup"))
117                 return $this->disabled("(action != 'browse')");
118
119         $args = $this->getArgs($argstr, $request);
120         $this->_args = $args;
121         $this->preSelectS($args, $request);
122
123         $p = $request->getArg('p');
124         if (!$p) $p = $this->_list;
125         $post_args = $request->getArg('admin_markup');
126         if (!$request->isPost() and empty($post_args['markup']))
127             $post_args['markup'] = $args['markup'];
128         $next_action = 'select';
129         $pages = array();
130         if ($p && !$request->isPost())
131             $pages = $p;
132         if ($p && $request->isPost() &&
133             !empty($post_args['button']) && empty($post_args['cancel'])) {
134             // without individual PagePermissions:
135             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
136                 $request->_notAuthorized(WIKIAUTH_ADMIN);
137                 $this->disabled("! user->isAdmin");
138             }
139             // DONE: error message if not allowed.
140             if ($post_args['action'] == 'verify') {
141                 // Real action
142                 return $this->chmarkupPages($dbi, $request, array_keys($p),
143                                             $post_args['markup']);
144             }
145             if ($post_args['action'] == 'select') {
146                 if (!empty($post_args['markup']))
147                     $next_action = 'verify';
148                 foreach ($p as $name => $c) {
149                     $pages[$name] = 1;
150                 }
151             }
152         }
153         if ($next_action == 'select' and empty($pages)) {
154             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'],
155                                          $args['exclude']);
156         }
157
158         if ($next_action == 'select') {
159             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $args);
160         } else {
161             $pagelist = new PageList_Unselectable($args['info'], $args['exclude'], $args);
162         }
163         $pagelist->addPageList($pages);
164
165         $header = HTML::fieldset();
166         if ($next_action == 'verify') {
167             $button_label = _("Yes");
168             $header->pushContent(
169               HTML::p(HTML::strong(
170                 _("Are you sure you want to permanently change the markup type of the selected files?"))));
171             $header = $this->chmarkupForm($header, $post_args);
172         }
173         else {
174             $button_label = _("Change markup type");
175             $header->pushContent(HTML::legend(_("Select the pages to change the markup type")));
176             $header = $this->chmarkupForm($header, $post_args);
177         }
178
179         $buttons = HTML::p(Button('submit:admin_markup[button]', $button_label, 'wikiadmin'),
180                            Button('submit:admin_markup[cancel]', _("Cancel"), 'button'));
181         $header->pushContent($buttons);
182
183         return HTML::form(array('action' => $request->getPostURL(),
184                                 'method' => 'post'),
185                           $header,
186                           $pagelist->getContent(),
187                           HiddenInputs($request->getArgs(),
188                                         false,
189                                         array('admin_markup')),
190                           HiddenInputs(array('admin_markup[action]' => $next_action)),
191                           ENABLE_PAGEPERM
192                           ? ''
193                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
194     }
195
196     function chmarkupForm(&$header, $post_args) {
197         $header->pushContent(_("Change markup")." ");
198         $header->pushContent(' '._("to").': ');
199         $header->pushContent(HTML::input(array('name' => 'admin_markup[markup]',
200                                                'value' => $post_args['markup'])));
201         return $header;
202     }
203 }
204
205 // Local Variables:
206 // mode: php
207 // tab-width: 8
208 // c-basic-offset: 4
209 // c-hanging-comment-ender-p: nil
210 // indent-tabs-mode: nil
211 // End:
212 ?>