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