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 public 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 run($dbi, $argstr, &$request, $basepage)
71 $action = $request->getArg('action');
72 if ($action != 'browse' and $action != 'rename'
73 and $action != _("PhpWikiAdministration") . "/" . _("Rename")
75 return $this->disabled(_("Plugin not run: not in browse mode"));
78 if ($action == 'rename') {
79 // We rename a single page.
80 // No need to display "Regex?" and "Case insensitive?" boxes
87 $args = $this->getArgs($argstr, $request);
89 $this->preSelectS($args, $request);
91 $p = $request->getArg('p');
92 if (!$p) $p = $this->_list;
93 $post_args = $request->getArg('admin_rename');
94 $next_action = 'select';
96 if ($p && !$request->isPost())
98 if ($p && $request->isPost() &&
99 !empty($post_args['rename']) && empty($post_args['cancel'])
101 // without individual PagePermissions:
102 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
103 $request->_notAuthorized(WIKIAUTH_ADMIN);
104 $this->disabled("! user->isAdmin");
106 // DONE: error message if not allowed.
107 if ($post_args['action'] == 'verify') {
109 return $this->renamePages($dbi, $request, array_keys($p),
110 $post_args['from'], $post_args['to'],
111 !empty($post_args['updatelinks']),
112 !empty($post_args['createredirect']));
115 if ($post_args['action'] == 'select') {
116 if (!empty($post_args['from']))
117 $next_action = 'verify';
118 foreach ($p as $name => $c) {
122 if ($next_action == 'select' and empty($pages)) {
123 // List all pages to select from.
124 $pages = $this->collectPages($pages, $dbi, $args['sortby'],
125 $args['limit'], $args['exclude']);
127 /*if ($next_action == 'verify') {
128 $args['info'] = "checkbox,pagename,renamed_pagename";
130 $pagelist = new PageList_Selectable
132 $args['info'], $args['exclude'],
134 array('renamed_pagename'
135 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
137 $pagelist->addPageList($pages);
139 $header = HTML::div();
140 if ($next_action == 'verify') {
141 $button_label = _("Yes");
142 $header->pushContent(
143 HTML::p(HTML::strong(
144 _("Are you sure you want to rename the selected pages?"))));
145 $header = $this->renameForm($header, $post_args, $singlepage);
147 if ($singlepage === true) {
148 $button_label = _("Rename Page");
150 $button_label = _("Rename selected pages");
152 if (!$post_args and count($pages) == 1) {
153 list($post_args['from'],) = array_keys($pages);
154 $post_args['to'] = $post_args['from'];
156 $header = $this->renameForm($header, $post_args, $singlepage);
157 if ($singlepage === false) {
158 $header->pushContent(HTML::p(_("Select the pages to rename:")));
163 (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
164 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
166 if ($singlepage === false) {
167 $list = $pagelist->getContent();
171 return HTML::form(array('action' => $request->getPostURL(),
174 HTML::legend(_("Rename Page")),
178 HiddenInputs($request->getArgs(),
180 array('admin_rename')),
181 HiddenInputs(array('admin_rename[action]' => $next_action)),
184 : HiddenInputs(array('require_authority_for_post'
185 => WIKIAUTH_ADMIN))));
188 private function checkBox(&$post_args, $name, $msg)
190 $id = 'admin_rename-' . $name;
191 $checkbox = HTML::input(array('type' => 'checkbox',
192 'name' => 'admin_rename[' . $name . ']',
195 if (!empty($post_args[$name]))
196 $checkbox->setAttr('checked', 'checked');
197 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
200 private function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks = false,
201 $createredirect = false)
203 $result = HTML::div();
206 $post_args = $request->getArg('admin_rename');
208 array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
209 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
210 foreach ($pages as $name) {
211 if (($newname = $this->renameHelper($name, $from, $to, $options))
212 and $newname != $name
214 if (strlen($newname) > MAX_PAGENAME_LENGTH)
215 $ul->pushContent(HTML::li(_("Cannot rename. New page name too long.")));
216 elseif ($dbi->isWikiPage($newname))
217 $ul->pushContent(HTML::li(fmt("Page “%s” already exists. Ignored.",
218 WikiLink($newname)))); elseif (!mayAccessPage('edit', $name))
219 $ul->pushContent(HTML::li(fmt("Access denied to rename page “%s”.",
220 WikiLink($name)))); elseif ($dbi->renamePage($name, $newname, $updatelinks)) {
221 /* not yet implemented for all backends */
222 $page = $dbi->getPage($newname);
223 $current = $page->getCurrentRevision();
224 $version = $current->getVersion();
225 $meta = $current->_data;
226 $text = $current->getPackedContent();
227 $meta['summary'] = sprintf(_("Renamed page from “%s” to “%s”."), $name, $newname);
228 $meta['is_minor_edit'] = 1;
229 $meta['author'] = $request->_user->UserName();
230 unset($meta['mtime']); // force new date
231 $page->save($text, $version + 1, $meta);
232 if ($createredirect) {
233 $page = $dbi->getPage($name);
234 $text = "<<RedirectTo page=\"" . $newname . "\">>";
236 sprintf(_("Renaming created redirect page from “%s” to “%s”"),
238 $meta['is_minor_edit'] = 0;
239 $meta['author'] = $request->_user->UserName();
240 $page->save($text, 1, $meta);
242 $ul->pushContent(HTML::li(fmt("Renamed page from “%s” to “%s”.",
243 $name, WikiLink($newname))));
246 $ul->pushContent(HTML::li(fmt("Couldn't rename page “%s” to “%s”.",
250 $ul->pushContent(HTML::li(fmt("Couldn't rename page “%s” to “%s”.",
256 $result->setAttr('class', 'feedback');
258 $result->pushContent(HTML::p(
259 _("One page has been renamed:")));
261 $result->pushContent(HTML::p(
262 fmt("%d pages have been renamed:", $count)));
264 $result->pushContent($ul);
267 $result->setAttr('class', 'error');
268 $result->pushContent(HTML::p(fmt("No pages renamed.")));
269 $result->pushContent($ul);
274 private 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