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