4 * Copyright 2004,2005,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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
25 * Usage: <?plugin WikiAdminRename ?> or called via WikiAdminSelect
26 * @author: Reini Urban <rurban@x-ray.at>
29 * Requires PHP 4.2 at least.
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
34 class WikiPlugin_WikiAdminRename
35 extends WikiPlugin_WikiAdminSelect
38 return _("WikiAdminRename");
41 function getDescription() {
42 return _("Rename selected pages");
45 function getDefaultArguments() {
48 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
50 /* Columns to include in listing */
51 'info' => 'pagename,mtime',
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);
63 return str_replace($from, $to, $name);
66 function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false,
67 $createredirect=false)
69 $result = HTML::div();
72 $post_args = $request->getArg('admin_rename');
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 )
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'.",
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 . "\">>";
102 sprintf(_("Renaming created redirect page from '%s' to '%s'"),
104 $meta['is_minor_edit'] = 0;
105 $meta['author'] = $request->_user->UserName();
106 $page->save($text, 1, $meta);
108 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
109 $name, WikiLink($newname))));
112 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
116 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
122 $result->setAttr('class', 'feedback');
124 $result->pushContent(HTML::p(
125 "One page has been permanently renamed:"));
127 $result->pushContent(HTML::p(
128 fmt("%s pages have been permanently renamed:", $count)));
130 $result->pushContent($ul);
133 $result->setAttr('class', 'error');
134 $result->pushContent(HTML::p(fmt("No pages renamed.")));
135 $result->pushContent($ul);
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')");
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
155 $args = $this->getArgs($argstr, $request);
156 $this->_args = $args;
157 $this->preSelectS($args, $request);
159 $p = $request->getArg('p');
160 if (!$p) $p = $this->_list;
161 $post_args = $request->getArg('admin_rename');
162 $next_action = 'select';
164 if ($p && !$request->isPost())
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");
173 // DONE: error message if not allowed.
174 if ($post_args['action'] == 'verify') {
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']));
182 if ($post_args['action'] == 'select') {
183 if (!empty($post_args['from']))
184 $next_action = 'verify';
185 foreach ($p as $name => $c) {
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']);
194 /*if ($next_action == 'verify') {
195 $args['info'] = "checkbox,pagename,renamed_pagename";
197 $pagelist = new PageList_Selectable
199 $args['info'], $args['exclude'],
201 array('renamed_pagename'
202 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
204 $pagelist->addPageList($pages);
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);
214 if ($singlepage === true) {
215 $button_label = _("Rename page");
217 $button_label = _("Rename selected pages");
219 if (!$post_args and count($pages) == 1) {
220 list($post_args['from'],) = array_keys($pages);
221 $post_args['to'] = $post_args['from'];
223 $header = $this->renameForm($header, $post_args, $singlepage);
224 if ($singlepage === false) {
225 $header->pushContent(HTML::p(_("Select the pages to rename:")));
230 (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
231 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
233 if ($singlepage === false) {
234 $list = $pagelist->getContent();
238 return HTML::form(array('action' => $request->getPostURL(),
241 HTML::legend("Rename page"),
245 HiddenInputs($request->getArgs(),
247 array('admin_rename')),
248 HiddenInputs(array('admin_rename[action]' => $next_action)),
251 : HiddenInputs(array('require_authority_for_post'
252 => WIKIAUTH_ADMIN))));
255 function checkBox (&$post_args, $name, $msg) {
256 $id = 'admin_rename-'.$name;
257 $checkbox = HTML::input(array('type' => 'checkbox',
258 'name' => 'admin_rename['.$name.']',
261 if (!empty($post_args[$name]))
262 $checkbox->setAttr('checked', 'checked');
263 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
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]',
271 'value' => $post_args['from'])));
272 $this->_tablePush($table, _("to").': ',
273 HTML::input(array('name' => 'admin_rename[to]',
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?")));
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);
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
298 // moved from lib/PageList.php
299 class _PageList_Column_renamed_pagename extends _PageList_Column {
300 function _getValue ($page_handle, &$revision_handle) {
302 $post_args = $request->getArg('admin_rename');
303 $options = array('regex' => @$post_args['regex'],
304 'icase' => @$post_args['icase']);
307 ? WikiPlugin_WikiAdminRename::renameHelper
308 ($page_handle->getName(),
309 $post_args['from'], $post_args['to'],
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);
324 // c-hanging-comment-ender-p: nil
325 // indent-tabs-mode: nil