]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminRename.php
Page renaming now recordered in history
[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.
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 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                    /* Columns to include in listing */
57                    'info'     => 'pagename,mtime',
58                    'updatelinks' => 0
59                    ));
60     }
61
62     function renameHelper($name, $from, $to, $options = false) {
63         if ($options['regex'])
64             return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
65         elseif ($options['icase'])
66             return str_ireplace($from, $to, $name);
67         else
68             return str_replace($from, $to, $name);
69     }
70
71     function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false) {
72         $ul = HTML::ul();
73         $count = 0;
74         $post_args = $request->getArg('admin_rename');
75         $options = array('regex' => @$post_args['regex'],
76                          'icase' => @$post_args['icase']);
77         foreach ($pages as $name) {
78             if ( ($newname = $this->renameHelper($name, $from, $to, $options)) 
79                  and $newname != $name )
80             {
81                 if ($dbi->isWikiPage($newname))
82                     $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
83                                                   WikiLink($newname))));
84                 elseif (! mayAccessPage('edit', $name))
85                     $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
86                                                   WikiLink($name))));
87                 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
88                     /* not yet implemented for all backends */
89                     $page = $dbi->getPage($newname);
90                     $current = $page->getCurrentRevision();
91                     $version = $current->getVersion();
92                     $meta = $current->_data;
93                     $text = $current->getPackedContent();
94                     $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
95                     $page->save($text, $version + 1, $meta);
96                     $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
97                                                   $name, WikiLink($newname))));
98                     $count++;
99                 } else {
100                     $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
101                                                   $name, $newname)));
102                 }
103             } else {
104                 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.", 
105                                               $name, $newname)));
106             }
107         }
108         if ($count) {
109             $dbi->touch();
110             return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",
111                                          $count)));
112         } else {
113             return HTML($ul, HTML::p(fmt("No pages renamed.")));
114         }
115     }
116     
117     function run($dbi, $argstr, &$request, $basepage) {
118         $action = $request->getArg('action');
119         if ($action != 'browse' and $action != 'rename' 
120                                 and $action != _("PhpWikiAdministration")."/"._("Rename"))
121             return $this->disabled("(action != 'browse')");
122         
123         $args = $this->getArgs($argstr, $request);
124         $this->_args = $args;
125         $this->preSelectS($args, $request);
126
127         $p = $request->getArg('p');
128         if (!$p) $p = $this->_list;
129         $post_args = $request->getArg('admin_rename');
130         $next_action = 'select';
131         $pages = array();
132         if ($p && !$request->isPost())
133             $pages = $p;
134         if ($p && $request->isPost() &&
135             !empty($post_args['rename']) && empty($post_args['cancel'])) {
136             // without individual PagePermissions:
137             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
138                 $request->_notAuthorized(WIKIAUTH_ADMIN);
139                 $this->disabled("! user->isAdmin");
140             }
141             // DONE: error message if not allowed.
142             if ($post_args['action'] == 'verify') {
143                 // Real action
144                 return $this->renamePages($dbi, $request, array_keys($p), 
145                                           $post_args['from'], $post_args['to'], 
146                                           !empty($post_args['updatelinks']));
147             }
148             if ($post_args['action'] == 'select') {
149                 if (!empty($post_args['from']))
150                     $next_action = 'verify';
151                 foreach ($p as $name => $c) {
152                     $pages[$name] = 1;
153                 }
154             }
155         }
156         if ($next_action == 'select' and empty($pages)) {
157             // List all pages to select from.
158             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
159         }
160         /*if ($next_action == 'verify') {
161             $args['info'] = "checkbox,pagename,renamed_pagename";
162         }*/
163         $pagelist = new PageList_Selectable
164             (
165              $args['info'], $args['exclude'],
166              array('types' => 
167                    array('renamed_pagename'
168                          => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
169                          )));
170         $pagelist->addPageList($pages);
171
172         $header = HTML::div();
173         if ($next_action == 'verify') {
174             $button_label = _("Yes");
175             $header->pushContent(
176               HTML::p(HTML::strong(
177                 _("Are you sure you want to permanently rename the selected pages?"))));
178             $header = $this->renameForm($header, $post_args);
179         }
180         else {
181             $button_label = _("Rename selected pages");
182             if (!$post_args and count($pages) == 1) {
183                 list($post_args['from'],) = array_keys($pages);
184                 $post_args['to'] = $post_args['from'];
185             }
186             $header = $this->renameForm($header, $post_args);
187             $header->pushContent(HTML::p(_("Select the pages to rename:")));
188         }
189
190         $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
191                            HTML::Raw('&nbsp;&nbsp;'),
192                            Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
193
194         return HTML::form(array('action' => $request->getPostURL(),
195                                 'method' => 'post'),
196                           $header,
197                           $buttons,
198                           $pagelist->getContent(),
199                           HiddenInputs($request->getArgs(),
200                                         false,
201                                         array('admin_rename')),
202                           HiddenInputs(array('admin_rename[action]' => $next_action)),
203                           ENABLE_PAGEPERM
204                           ? ''
205                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
206     }
207
208     function checkBox (&$post_args, $name, $msg) {
209         $id = 'admin_rename-'.$name;
210         $checkbox = HTML::input(array('type' => 'checkbox',
211                                       'name' => 'admin_rename['.$name.']',
212                                       'id'   => $id,
213                                       'value' => 1));
214         if (!empty($post_args[$name]))
215             $checkbox->setAttr('checked', 'checked');
216         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
217     }
218
219     function renameForm(&$header, $post_args) {
220         $table = HTML::table();
221         $this->_tablePush($table, _("Rename"). " ". _("from").': ',
222                           HTML::input(array('name' => 'admin_rename[from]',
223                                             'size' => 90,
224                                             'value' => $post_args['from'])));
225         $this->_tablePush($table, _("to").': ',
226                           HTML::input(array('name' => 'admin_rename[to]',
227                                             'size' => 90,
228                                             'value' => $post_args['to'])));
229         $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
230         $this->_tablePush($table, '', $this->checkBox($post_args, 'icase', _("Case insensitive?")));
231         if (DEBUG) // not yet stable
232             $this->_tablePush($table, '', $this->checkBox($post_args, 'updatelinks', 
233                                                       _("Change pagename in all linked pages also?")));
234         $header->pushContent($table);
235         return $header;
236     }
237 }
238
239 // TODO: grey out unchangeble pages, even in the initial list also?
240 // TODO: autoselect by matching name javascript in admin_rename[from]
241 // TODO: update rename[] fields when case-sensitive and regex is changed
242
243 // moved from lib/PageList.php
244 class _PageList_Column_renamed_pagename extends _PageList_Column {
245     function _getValue ($page_handle, &$revision_handle) {
246         global $request;
247         $post_args = $request->getArg('admin_rename');
248         $options = array('regex' => @$post_args['regex'],
249                          'icase' => @$post_args['icase']);
250                          
251         $value = $post_args ? WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(), 
252                                                           $post_args['from'], $post_args['to'],
253                                                           $options)
254                             : $page_handle->getName();                              
255         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
256                                                   'name' => 'rename[]',
257                                                   'value' => $value)));
258         $new_page = $request->getPage($value);
259         return $div;
260     }
261 };
262
263 // Local Variables:
264 // mode: php
265 // tab-width: 8
266 // c-basic-offset: 4
267 // c-hanging-comment-ender-p: nil
268 // indent-tabs-mode: nil
269 // End:
270 ?>