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