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 along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Usage: <<WikiAdminRename >> or called via WikiAdminSelect
26 * @author: Reini Urban <rurban@x-ray.at>
29 require_once 'lib/PageList.php';
30 require_once 'lib/plugin/WikiAdminSelect.php';
32 class WikiPlugin_WikiAdminRename
33 extends WikiPlugin_WikiAdminSelect
37 return _("WikiAdminRename");
40 function getDescription()
42 return _("Rename selected pages");
45 function getDefaultArguments()
49 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
51 /* Columns to include in listing */
52 'info' => 'pagename,mtime',
58 function renameHelper($name, $from, $to, $options = false)
60 if (isset($options['regex'])) {
61 return preg_replace('/' . $from . '/' . (isset($options['icase']) ? 'i' : ''), $to, $name);
62 } elseif (isset($options['icase'])) {
63 return str_ireplace($from, $to, $name);
65 return str_replace($from, $to, $name);
69 function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks = false,
70 $createredirect = false)
72 $result = HTML::div();
75 $post_args = $request->getArg('admin_rename');
77 array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
78 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
79 foreach ($pages as $name) {
80 if (($newname = $this->renameHelper($name, $from, $to, $options))
83 if (strlen($newname) > MAX_PAGENAME_LENGTH)
84 $ul->pushContent(HTML::li(_("Cannot rename. New page name too long.")));
85 elseif ($dbi->isWikiPage($newname))
86 $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
87 WikiLink($newname)))); elseif (!mayAccessPage('edit', $name))
88 $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
89 WikiLink($name)))); elseif ($dbi->renamePage($name, $newname, $updatelinks)) {
90 /* not yet implemented for all backends */
91 $page = $dbi->getPage($newname);
92 $current = $page->getCurrentRevision();
93 $version = $current->getVersion();
94 $meta = $current->_data;
95 $text = $current->getPackedContent();
96 $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'."), $name, $newname);
97 $meta['is_minor_edit'] = 1;
98 $meta['author'] = $request->_user->UserName();
99 unset($meta['mtime']); // force new date
100 $page->save($text, $version + 1, $meta);
101 if ($createredirect) {
102 $page = $dbi->getPage($name);
103 $text = "<<RedirectTo page=\"" . $newname . "\">>";
105 sprintf(_("Renaming created redirect page from '%s' to '%s'"),
107 $meta['is_minor_edit'] = 0;
108 $meta['author'] = $request->_user->UserName();
109 $page->save($text, 1, $meta);
111 $ul->pushContent(HTML::li(fmt("Renamed page from '%s' to '%s'.",
112 $name, WikiLink($newname))));
115 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
119 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
125 $result->setAttr('class', 'feedback');
127 $result->pushContent(HTML::p(
128 _("One page has been renamed:")));
130 $result->pushContent(HTML::p(
131 fmt("%d pages have been renamed:", $count)));
133 $result->pushContent($ul);
136 $result->setAttr('class', 'error');
137 $result->pushContent(HTML::p(fmt("No pages renamed.")));
138 $result->pushContent($ul);
143 function run($dbi, $argstr, &$request, $basepage)
145 $action = $request->getArg('action');
146 if ($action != 'browse' and $action != 'rename'
147 and $action != _("PhpWikiAdministration") . "/" . _("Rename")
149 return $this->disabled(_("Plugin not run: not in browse mode"));
152 if ($action == 'rename') {
153 // We rename a single page.
154 // No need to display "Regex?" and "Case insensitive?" boxes
155 // No need to confirm
161 $args = $this->getArgs($argstr, $request);
162 $this->_args = $args;
163 $this->preSelectS($args, $request);
165 $p = $request->getArg('p');
166 if (!$p) $p = $this->_list;
167 $post_args = $request->getArg('admin_rename');
168 $next_action = 'select';
170 if ($p && !$request->isPost())
172 if ($p && $request->isPost() &&
173 !empty($post_args['rename']) && empty($post_args['cancel'])
175 // without individual PagePermissions:
176 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
177 $request->_notAuthorized(WIKIAUTH_ADMIN);
178 $this->disabled("! user->isAdmin");
180 // DONE: error message if not allowed.
181 if ($post_args['action'] == 'verify') {
183 return $this->renamePages($dbi, $request, array_keys($p),
184 $post_args['from'], $post_args['to'],
185 !empty($post_args['updatelinks']),
186 !empty($post_args['createredirect']));
189 if ($post_args['action'] == 'select') {
190 if (!empty($post_args['from']))
191 $next_action = 'verify';
192 foreach ($p as $name => $c) {
196 if ($next_action == 'select' and empty($pages)) {
197 // List all pages to select from.
198 $pages = $this->collectPages($pages, $dbi, $args['sortby'],
199 $args['limit'], $args['exclude']);
201 /*if ($next_action == 'verify') {
202 $args['info'] = "checkbox,pagename,renamed_pagename";
204 $pagelist = new PageList_Selectable
206 $args['info'], $args['exclude'],
208 array('renamed_pagename'
209 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
211 $pagelist->addPageList($pages);
213 $header = HTML::div();
214 if ($next_action == 'verify') {
215 $button_label = _("Yes");
216 $header->pushContent(
217 HTML::p(HTML::strong(
218 _("Are you sure you want to rename the selected pages?"))));
219 $header = $this->renameForm($header, $post_args, $singlepage);
221 if ($singlepage === true) {
222 $button_label = _("Rename Page");
224 $button_label = _("Rename selected pages");
226 if (!$post_args and count($pages) == 1) {
227 list($post_args['from'],) = array_keys($pages);
228 $post_args['to'] = $post_args['from'];
230 $header = $this->renameForm($header, $post_args, $singlepage);
231 if ($singlepage === false) {
232 $header->pushContent(HTML::p(_("Select the pages to rename:")));
237 (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
238 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
240 if ($singlepage === false) {
241 $list = $pagelist->getContent();
245 return HTML::form(array('action' => $request->getPostURL(),
248 HTML::legend(_("Rename Page")),
252 HiddenInputs($request->getArgs(),
254 array('admin_rename')),
255 HiddenInputs(array('admin_rename[action]' => $next_action)),
258 : HiddenInputs(array('require_authority_for_post'
259 => WIKIAUTH_ADMIN))));
262 function checkBox(&$post_args, $name, $msg)
264 $id = 'admin_rename-' . $name;
265 $checkbox = HTML::input(array('type' => 'checkbox',
266 'name' => 'admin_rename[' . $name . ']',
269 if (!empty($post_args[$name]))
270 $checkbox->setAttr('checked', 'checked');
271 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
274 function renameForm(&$header, $post_args, $singlepage)
276 $table = HTML::table();
277 $this->_tablePush($table, _("Rename") . " " . _("from") . _(": "),
278 HTML::input(array('name' => 'admin_rename[from]',
279 'size' => MAX_PAGENAME_LENGTH,
280 'maxlength' => MAX_PAGENAME_LENGTH,
281 'readonly' => 'readonly',
282 'value' => $post_args['from'])));
283 $this->_tablePush($table, _("to") . _(": "),
284 HTML::input(array('name' => 'admin_rename[to]',
285 'size' => MAX_PAGENAME_LENGTH,
286 'maxlength' => MAX_PAGENAME_LENGTH,
287 'value' => $post_args['to'])));
288 if ($singlepage === false) {
289 $this->_tablePush($table, '',
290 $this->checkBox($post_args, 'regex', _("Regex?")));
291 $this->_tablePush($table, '',
292 $this->checkBox($post_args, 'icase', _("Case insensitive?")));
294 if (defined('EXPERIMENTAL') and EXPERIMENTAL) // not yet stable
295 $this->_tablePush($table, '',
296 $this->checkBox($post_args, 'updatelinks',
297 _("Change pagename in all linked pages also?")));
298 $this->_tablePush($table, '',
299 $this->checkBox($post_args, 'createredirect',
300 _("Create redirect from old to new name?")));
301 $header->pushContent($table);
306 // TODO: grey out unchangeble pages, even in the initial list also?
307 // TODO: autoselect by matching name javascript in admin_rename[from]
308 // TODO: update rename[] fields when case-sensitive and regex is changed
310 // moved from lib/PageList.php
311 class _PageList_Column_renamed_pagename extends _PageList_Column
313 function _getValue($page_handle, &$revision_handle)
316 $post_args = $request->getArg('admin_rename');
318 array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
319 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
321 ? WikiPlugin_WikiAdminRename::renameHelper
322 ($page_handle->getName(),
323 $post_args['from'], $post_args['to'],
325 : $page_handle->getName();
326 $div = HTML::div(" => ", HTML::input(array('type' => 'text',
327 'name' => 'rename[]',
328 'value' => $value)));
329 $new_page = $request->getPage($value);
338 // c-hanging-comment-ender-p: nil
339 // indent-tabs-mode: nil