]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Use braces
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminRename.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2004,2005,2007 $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:   <<WikiAdminRename >> 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_WikiAdminRename
33 extends WikiPlugin_WikiAdminSelect
34 {
35     function getName() {
36         return _("WikiAdminRename");
37     }
38
39     function getDescription() {
40         return _("Rename selected pages");
41     }
42
43     function getDefaultArguments() {
44         return array_merge
45             (
46              WikiPlugin_WikiAdminSelect::getDefaultArguments(),
47              array(
48                    /* Columns to include in listing */
49                    'info'     => 'pagename,mtime',
50                    'updatelinks' => 0,
51                    'createredirect' => 0
52                    ));
53     }
54
55     function renameHelper($name, $from, $to, $options = false) {
56             if ($options['regex'])
57                 return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
58             elseif ($options['icase'])
59                 return str_ireplace($from, $to, $name);
60             else
61             return str_replace($from, $to, $name);
62     }
63
64     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false,
65                          $createredirect=false)
66     {
67         $result = HTML::div();
68         $ul = HTML::ul();
69         $count = 0;
70         $post_args = $request->getArg('admin_rename');
71         $options =
72           array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
73                 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
74         foreach ($pages as $name) {
75             if ( ($newname = $this->renameHelper($name, $from, $to, $options))
76                  and $newname != $name )
77             {
78                 if ($dbi->isWikiPage($newname))
79                     $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
80                                                   WikiLink($newname))));
81                 elseif (! mayAccessPage('edit', $name))
82                     $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
83                                                   WikiLink($name))));
84                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
85                     /* not yet implemented for all backends */
86                     $page = $dbi->getPage($newname);
87                     $current = $page->getCurrentRevision();
88                     $version = $current->getVersion();
89                     $meta = $current->_data;
90                     $text = $current->getPackedContent();
91                     $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
92                     $meta['is_minor_edit'] = 1;
93                     $meta['author'] = $request->_user->UserName();
94                     unset($meta['mtime']); // force new date
95                     $page->save($text, $version + 1, $meta);
96                     if ($createredirect) {
97                         $page = $dbi->getPage($name);
98                         $text = "<<RedirectTo page=\"" . $newname . "\">>";
99                         $meta['summary'] =
100                             sprintf(_("Renaming created redirect page from '%s' to '%s'"),
101                                     $name, $newname);
102                         $meta['is_minor_edit'] = 0;
103                         $meta['author'] = $request->_user->UserName();
104                         $page->save($text, 1, $meta);
105                     }
106                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
107                                                   $name, WikiLink($newname))));
108                     $count++;
109                 } else {
110                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
111                                                   $name, $newname)));
112                 }
113             } else {
114                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
115                                               $name, $newname)));
116             }
117         }
118         if ($count) {
119             $dbi->touch();
120             $result->setAttr('class', 'feedback');
121             if ($count == 1) {
122                 $result->pushContent(HTML::p(
123                   _("One page has been renamed:")));
124             } else {
125                 $result->pushContent(HTML::p(
126                   fmt("%d pages have been renamed:", $count)));
127             }
128             $result->pushContent($ul);
129             return $result;
130         } else {
131             $result->setAttr('class', 'error');
132             $result->pushContent(HTML::p(fmt("No pages renamed.")));
133             $result->pushContent($ul);
134             return $result;
135         }
136     }
137
138     function run($dbi, $argstr, &$request, $basepage) {
139         $action = $request->getArg('action');
140         if ($action != 'browse' and $action != 'rename'
141                                 and $action != _("PhpWikiAdministration")."/"._("Rename")) {
142             return $this->disabled("(action != 'browse')");
143         }
144
145         if ($action == 'rename') {
146             // We rename a single page.
147             // No need to display "Regex?" and "Case insensitive?" boxes
148             // No need to confirm
149            $singlepage = true;
150         } else {
151            $singlepage = false;
152         }
153
154         $args = $this->getArgs($argstr, $request);
155         $this->_args = $args;
156         $this->preSelectS($args, $request);
157
158         $p = $request->getArg('p');
159         if (!$p) $p = $this->_list;
160         $post_args = $request->getArg('admin_rename');
161         $next_action = 'select';
162         $pages = array();
163         if ($p && !$request->isPost())
164             $pages = $p;
165         if ($p && $request->isPost() &&
166             !empty($post_args['rename']) && empty($post_args['cancel'])) {
167             // without individual PagePermissions:
168             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
169                 $request->_notAuthorized(WIKIAUTH_ADMIN);
170                 $this->disabled("! user->isAdmin");
171             }
172             // DONE: error message if not allowed.
173             if ($post_args['action'] == 'verify') {
174                 // Real action
175                 return $this->renamePages($dbi, $request, array_keys($p),
176                                           $post_args['from'], $post_args['to'],
177                                           !empty($post_args['updatelinks']),
178                                           !empty($post_args['createredirect']));
179             }
180         }
181         if ($post_args['action'] == 'select') {
182             if (!empty($post_args['from']))
183                 $next_action = 'verify';
184             foreach ($p as $name => $c) {
185                 $pages[$name] = 1;
186             }
187         }
188         if ($next_action == 'select' and empty($pages)) {
189             // List all pages to select from.
190             $pages = $this->collectPages($pages, $dbi, $args['sortby'],
191                                          $args['limit'], $args['exclude']);
192         }
193         /*if ($next_action == 'verify') {
194             $args['info'] = "checkbox,pagename,renamed_pagename";
195         }*/
196         $pagelist = new PageList_Selectable
197             (
198              $args['info'], $args['exclude'],
199              array('types' =>
200                    array('renamed_pagename'
201                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
202                          )));
203         $pagelist->addPageList($pages);
204
205         $header = HTML::div();
206         if ($next_action == 'verify') {
207             $button_label = _("Yes");
208             $header->pushContent(
209               HTML::p(HTML::strong(
210                 _("Are you sure you want to rename the selected pages?"))));
211             $header = $this->renameForm($header, $post_args, $singlepage);
212         } else {
213             if ($singlepage === true) {
214                 $button_label = _("Rename page");
215             } else {
216                 $button_label = _("Rename selected pages");
217             }
218             if (!$post_args and count($pages) == 1) {
219                 list($post_args['from'],) = array_keys($pages);
220                 $post_args['to'] = $post_args['from'];
221             }
222             $header = $this->renameForm($header, $post_args, $singlepage);
223             if ($singlepage === false) {
224                 $header->pushContent(HTML::p(_("Select the pages to rename:")));
225             }
226         }
227
228         $buttons = HTML::p
229             (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
230              Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
231
232         if ($singlepage === false) {
233             $list = $pagelist->getContent();
234         } else {
235             $list = "";
236         }
237         return HTML::form(array('action' => $request->getPostURL(),
238                                 'method' => 'post'),
239                           HTML::fieldset(
240                               HTML::legend(_("Rename page")),
241                               $header,
242                               $buttons,
243                               $list,
244                               HiddenInputs($request->getArgs(),
245                                             false,
246                                             array('admin_rename')),
247                               HiddenInputs(array('admin_rename[action]' => $next_action)),
248                               ENABLE_PAGEPERM
249                               ? ''
250                               : HiddenInputs(array('require_authority_for_post'
251                                                    => WIKIAUTH_ADMIN))));
252     }
253
254     function checkBox (&$post_args, $name, $msg) {
255             $id = 'admin_rename-'.$name;
256             $checkbox = HTML::input(array('type' => 'checkbox',
257                                       'name' => 'admin_rename['.$name.']',
258                                       'id'   => $id,
259                                       'value' => 1));
260         if (!empty($post_args[$name]))
261             $checkbox->setAttr('checked', 'checked');
262         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
263     }
264
265     function renameForm(&$header, $post_args, $singlepage) {
266         $table = HTML::table();
267         $this->_tablePush($table, _("Rename"). " ". _("from")._(": "),
268                           HTML::input(array('name' => 'admin_rename[from]',
269                                             'size' => 90,
270                                             'value' => $post_args['from'])));
271         $this->_tablePush($table, _("to")._(": "),
272                           HTML::input(array('name' => 'admin_rename[to]',
273                                             'size' => 90,
274                                             'value' => $post_args['to'])));
275         if ($singlepage === false) {
276             $this->_tablePush($table, '',
277                               $this->checkBox($post_args, 'regex', _("Regex?")));
278             $this->_tablePush($table, '',
279                               $this->checkBox($post_args, 'icase', _("Case insensitive?")));
280         }
281         if (defined('EXPERIMENTAL') and EXPERIMENTAL) // not yet stable
282             $this->_tablePush($table, '',
283                               $this->checkBox($post_args, 'updatelinks',
284                                 _("Change pagename in all linked pages also?")));
285         $this->_tablePush($table, '',
286                           $this->checkBox($post_args, 'createredirect',
287                                           _("Create redirect from old to new name?")));
288         $header->pushContent($table);
289         return $header;
290     }
291 }
292
293 // TODO: grey out unchangeble pages, even in the initial list also?
294 // TODO: autoselect by matching name javascript in admin_rename[from]
295 // TODO: update rename[] fields when case-sensitive and regex is changed
296
297 // moved from lib/PageList.php
298 class _PageList_Column_renamed_pagename extends _PageList_Column {
299     function _getValue ($page_handle, &$revision_handle) {
300         global $request;
301         $post_args = $request->getArg('admin_rename');
302         $options = array('regex' => @$post_args['regex'],
303                          'icase' => @$post_args['icase']);
304
305         $value = $post_args
306             ? WikiPlugin_WikiAdminRename::renameHelper
307                 ($page_handle->getName(),
308                  $post_args['from'], $post_args['to'],
309                  $options)
310             : $page_handle->getName();
311         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
312                                                   'name' => 'rename[]',
313                                                   'value' => $value)));
314         $new_page = $request->getPage($value);
315         return $div;
316     }
317 };
318
319 // Local Variables:
320 // mode: php
321 // tab-width: 8
322 // c-basic-offset: 4
323 // c-hanging-comment-ender-p: nil
324 // indent-tabs-mode: nil
325 // End:
326 ?>