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 getVersion() {
46 return preg_replace("/[Revision: $]/", '',
50 function getDefaultArguments() {
53 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
55 /* Columns to include in listing */
56 'info' => 'pagename,mtime',
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);
68 return str_replace($from, $to, $name);
71 function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false,
72 $createredirect=false)
74 $result = HTML::div();
77 $post_args = $request->getArg('admin_rename');
79 array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
80 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
81 foreach ($pages as $name) {
82 if ( ($newname = $this->renameHelper($name, $from, $to, $options))
83 and $newname != $name )
85 if ($dbi->isWikiPage($newname))
86 $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
87 WikiLink($newname))));
88 elseif (! mayAccessPage('edit', $name))
89 $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
91 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
92 /* not yet implemented for all backends */
93 $page = $dbi->getPage($newname);
94 $current = $page->getCurrentRevision();
95 $version = $current->getVersion();
96 $meta = $current->_data;
97 $text = $current->getPackedContent();
98 $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
99 $meta['is_minor_edit'] = 1;
100 $meta['author'] = $request->_user->UserName();
101 unset($meta['mtime']); // force new date
102 $page->save($text, $version + 1, $meta);
103 if ($createredirect) {
104 $page = $dbi->getPage($name);
105 $text = "<<RedirectTo page=\"" . $newname . "\">>";
107 sprintf(_("Renaming created redirect page from '%s' to '%s'"),
109 $meta['is_minor_edit'] = 0;
110 $meta['author'] = $request->_user->UserName();
111 $page->save($text, 1, $meta);
113 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
114 $name, WikiLink($newname))));
117 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
121 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
127 $result->setAttr('class', 'feedback');
129 $result->pushContent(HTML::p(
130 "One page has been permanently renamed:"));
132 $result->pushContent(HTML::p(
133 fmt("%s pages have been permanently renamed:", $count)));
135 $result->pushContent($ul);
138 $result->setAttr('class', 'error');
139 $result->pushContent(HTML::p(fmt("No pages renamed.")));
140 $result->pushContent($ul);
145 function run($dbi, $argstr, &$request, $basepage) {
146 $action = $request->getArg('action');
147 if ($action != 'browse' and $action != 'rename'
148 and $action != _("PhpWikiAdministration")."/"._("Rename"))
149 return $this->disabled("(action != 'browse')");
151 if ($action == 'rename') {
152 // We rename a single page.
153 // No need to display "Regex?" and "Case insensitive?" boxes
154 // No need to confirm
160 $args = $this->getArgs($argstr, $request);
161 $this->_args = $args;
162 $this->preSelectS($args, $request);
164 $p = $request->getArg('p');
165 if (!$p) $p = $this->_list;
166 $post_args = $request->getArg('admin_rename');
167 $next_action = 'select';
169 if ($p && !$request->isPost())
171 if ($p && $request->isPost() &&
172 !empty($post_args['rename']) && empty($post_args['cancel'])) {
173 // without individual PagePermissions:
174 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
175 $request->_notAuthorized(WIKIAUTH_ADMIN);
176 $this->disabled("! user->isAdmin");
178 // DONE: error message if not allowed.
179 if ($post_args['action'] == 'verify') {
181 return $this->renamePages($dbi, $request, array_keys($p),
182 $post_args['from'], $post_args['to'],
183 !empty($post_args['updatelinks']),
184 !empty($post_args['createredirect']));
187 if ($post_args['action'] == 'select') {
188 if (!empty($post_args['from']))
189 $next_action = 'verify';
190 foreach ($p as $name => $c) {
194 if ($next_action == 'select' and empty($pages)) {
195 // List all pages to select from.
196 $pages = $this->collectPages($pages, $dbi, $args['sortby'],
197 $args['limit'], $args['exclude']);
199 /*if ($next_action == 'verify') {
200 $args['info'] = "checkbox,pagename,renamed_pagename";
202 $pagelist = new PageList_Selectable
204 $args['info'], $args['exclude'],
206 array('renamed_pagename'
207 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
209 $pagelist->addPageList($pages);
211 $header = HTML::div();
212 if ($next_action == 'verify') {
213 $button_label = _("Yes");
214 $header->pushContent(
215 HTML::p(HTML::strong(
216 _("Are you sure you want to permanently rename the selected pages?"))));
217 $header = $this->renameForm($header, $post_args, $singlepage);
219 if ($singlepage === true) {
220 $button_label = _("Rename page");
222 $button_label = _("Rename selected pages");
224 if (!$post_args and count($pages) == 1) {
225 list($post_args['from'],) = array_keys($pages);
226 $post_args['to'] = $post_args['from'];
228 $header = $this->renameForm($header, $post_args, $singlepage);
229 if ($singlepage === false) {
230 $header->pushContent(HTML::p(_("Select the pages to rename:")));
235 (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
236 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
238 if ($singlepage === false) {
239 $list = $pagelist->getContent();
243 return HTML::form(array('action' => $request->getPostURL(),
246 HTML::legend("Rename page"),
250 HiddenInputs($request->getArgs(),
252 array('admin_rename')),
253 HiddenInputs(array('admin_rename[action]' => $next_action)),
256 : HiddenInputs(array('require_authority_for_post'
257 => WIKIAUTH_ADMIN))));
260 function checkBox (&$post_args, $name, $msg) {
261 $id = 'admin_rename-'.$name;
262 $checkbox = HTML::input(array('type' => 'checkbox',
263 'name' => 'admin_rename['.$name.']',
266 if (!empty($post_args[$name]))
267 $checkbox->setAttr('checked', 'checked');
268 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
271 function renameForm(&$header, $post_args, $singlepage) {
272 $table = HTML::table();
273 $this->_tablePush($table, _("Rename"). " ". _("from").': ',
274 HTML::input(array('name' => 'admin_rename[from]',
276 'value' => $post_args['from'])));
277 $this->_tablePush($table, _("to").': ',
278 HTML::input(array('name' => 'admin_rename[to]',
280 'value' => $post_args['to'])));
281 if ($singlepage === false) {
282 $this->_tablePush($table, '',
283 $this->checkBox($post_args, 'regex', _("Regex?")));
284 $this->_tablePush($table, '',
285 $this->checkBox($post_args, 'icase', _("Case insensitive?")));
287 if (defined('EXPERIMENTAL') and EXPERIMENTAL) // not yet stable
288 $this->_tablePush($table, '',
289 $this->checkBox($post_args, 'updatelinks',
290 _("Change pagename in all linked pages also?")));
291 $this->_tablePush($table, '',
292 $this->checkBox($post_args, 'createredirect',
293 _("Create redirect from old to new name?")));
294 $header->pushContent($table);
299 // TODO: grey out unchangeble pages, even in the initial list also?
300 // TODO: autoselect by matching name javascript in admin_rename[from]
301 // TODO: update rename[] fields when case-sensitive and regex is changed
303 // moved from lib/PageList.php
304 class _PageList_Column_renamed_pagename extends _PageList_Column {
305 function _getValue ($page_handle, &$revision_handle) {
307 $post_args = $request->getArg('admin_rename');
308 $options = array('regex' => @$post_args['regex'],
309 'icase' => @$post_args['icase']);
312 ? WikiPlugin_WikiAdminRename::renameHelper
313 ($page_handle->getName(),
314 $post_args['from'], $post_args['to'],
316 : $page_handle->getName();
317 $div = HTML::div(" => ",HTML::input(array('type' => 'text',
318 'name' => 'rename[]',
319 'value' => $value)));
320 $new_page = $request->getPage($value);
329 // c-hanging-comment-ender-p: nil
330 // indent-tabs-mode: nil