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: <<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
36 return _("WikiAdminRename");
39 function getDescription() {
40 return _("Rename selected pages");
43 function getDefaultArguments() {
46 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
48 /* Columns to include in listing */
49 'info' => 'pagename,mtime',
55 function renameHelper($name, $from, $to, $options = false) {
56 if ($options['regex'])
57 return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
58 elseif ($options['icase'])
59 return str_ireplace($from, $to, $name);
61 return str_replace($from, $to, $name);
64 function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false,
65 $createredirect=false)
67 $result = HTML::div();
70 $post_args = $request->getArg('admin_rename');
72 array('regex' => isset($post_args['regex']) ? $post_args['regex'] : null,
73 'icase' => isset($post_args['icase']) ? $post_args['icase'] : null);
74 foreach ($pages as $name) {
75 if ( ($newname = $this->renameHelper($name, $from, $to, $options))
76 and $newname != $name )
78 if ($dbi->isWikiPage($newname))
79 $ul->pushContent(HTML::li(fmt("Page '%s' already exists. Ignored.",
80 WikiLink($newname))));
81 elseif (! mayAccessPage('edit', $name))
82 $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
84 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
85 /* not yet implemented for all backends */
86 $page = $dbi->getPage($newname);
87 $current = $page->getCurrentRevision();
88 $version = $current->getVersion();
89 $meta = $current->_data;
90 $text = $current->getPackedContent();
91 $meta['summary'] = sprintf(_("Renamed page from '%s' to '%s'"), $name, $newname);
92 $meta['is_minor_edit'] = 1;
93 $meta['author'] = $request->_user->UserName();
94 unset($meta['mtime']); // force new date
95 $page->save($text, $version + 1, $meta);
96 if ($createredirect) {
97 $page = $dbi->getPage($name);
98 $text = "<<RedirectTo page=\"" . $newname . "\">>";
100 sprintf(_("Renaming created redirect page from '%s' to '%s'"),
102 $meta['is_minor_edit'] = 0;
103 $meta['author'] = $request->_user->UserName();
104 $page->save($text, 1, $meta);
106 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
107 $name, WikiLink($newname))));
110 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
114 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
120 $result->setAttr('class', 'feedback');
122 $result->pushContent(HTML::p(
123 "One page has been permanently renamed:"));
125 $result->pushContent(HTML::p(
126 fmt("%s pages have been permanently renamed:", $count)));
128 $result->pushContent($ul);
131 $result->setAttr('class', 'error');
132 $result->pushContent(HTML::p(fmt("No pages renamed.")));
133 $result->pushContent($ul);
138 function run($dbi, $argstr, &$request, $basepage) {
139 $action = $request->getArg('action');
140 if ($action != 'browse' and $action != 'rename'
141 and $action != _("PhpWikiAdministration")."/"._("Rename"))
142 return $this->disabled("(action != 'browse')");
144 if ($action == 'rename') {
145 // We rename a single page.
146 // No need to display "Regex?" and "Case insensitive?" boxes
147 // No need to confirm
153 $args = $this->getArgs($argstr, $request);
154 $this->_args = $args;
155 $this->preSelectS($args, $request);
157 $p = $request->getArg('p');
158 if (!$p) $p = $this->_list;
159 $post_args = $request->getArg('admin_rename');
160 $next_action = 'select';
162 if ($p && !$request->isPost())
164 if ($p && $request->isPost() &&
165 !empty($post_args['rename']) && empty($post_args['cancel'])) {
166 // without individual PagePermissions:
167 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
168 $request->_notAuthorized(WIKIAUTH_ADMIN);
169 $this->disabled("! user->isAdmin");
171 // DONE: error message if not allowed.
172 if ($post_args['action'] == 'verify') {
174 return $this->renamePages($dbi, $request, array_keys($p),
175 $post_args['from'], $post_args['to'],
176 !empty($post_args['updatelinks']),
177 !empty($post_args['createredirect']));
180 if ($post_args['action'] == 'select') {
181 if (!empty($post_args['from']))
182 $next_action = 'verify';
183 foreach ($p as $name => $c) {
187 if ($next_action == 'select' and empty($pages)) {
188 // List all pages to select from.
189 $pages = $this->collectPages($pages, $dbi, $args['sortby'],
190 $args['limit'], $args['exclude']);
192 /*if ($next_action == 'verify') {
193 $args['info'] = "checkbox,pagename,renamed_pagename";
195 $pagelist = new PageList_Selectable
197 $args['info'], $args['exclude'],
199 array('renamed_pagename'
200 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
202 $pagelist->addPageList($pages);
204 $header = HTML::div();
205 if ($next_action == 'verify') {
206 $button_label = _("Yes");
207 $header->pushContent(
208 HTML::p(HTML::strong(
209 _("Are you sure you want to permanently rename the selected pages?"))));
210 $header = $this->renameForm($header, $post_args, $singlepage);
212 if ($singlepage === true) {
213 $button_label = _("Rename page");
215 $button_label = _("Rename selected pages");
217 if (!$post_args and count($pages) == 1) {
218 list($post_args['from'],) = array_keys($pages);
219 $post_args['to'] = $post_args['from'];
221 $header = $this->renameForm($header, $post_args, $singlepage);
222 if ($singlepage === false) {
223 $header->pushContent(HTML::p(_("Select the pages to rename:")));
228 (Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
229 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
231 if ($singlepage === false) {
232 $list = $pagelist->getContent();
236 return HTML::form(array('action' => $request->getPostURL(),
239 HTML::legend("Rename page"),
243 HiddenInputs($request->getArgs(),
245 array('admin_rename')),
246 HiddenInputs(array('admin_rename[action]' => $next_action)),
249 : HiddenInputs(array('require_authority_for_post'
250 => WIKIAUTH_ADMIN))));
253 function checkBox (&$post_args, $name, $msg) {
254 $id = 'admin_rename-'.$name;
255 $checkbox = HTML::input(array('type' => 'checkbox',
256 'name' => 'admin_rename['.$name.']',
259 if (!empty($post_args[$name]))
260 $checkbox->setAttr('checked', 'checked');
261 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
264 function renameForm(&$header, $post_args, $singlepage) {
265 $table = HTML::table();
266 $this->_tablePush($table, _("Rename"). " ". _("from").': ',
267 HTML::input(array('name' => 'admin_rename[from]',
269 'value' => $post_args['from'])));
270 $this->_tablePush($table, _("to").': ',
271 HTML::input(array('name' => 'admin_rename[to]',
273 'value' => $post_args['to'])));
274 if ($singlepage === false) {
275 $this->_tablePush($table, '',
276 $this->checkBox($post_args, 'regex', _("Regex?")));
277 $this->_tablePush($table, '',
278 $this->checkBox($post_args, 'icase', _("Case insensitive?")));
280 if (defined('EXPERIMENTAL') and EXPERIMENTAL) // not yet stable
281 $this->_tablePush($table, '',
282 $this->checkBox($post_args, 'updatelinks',
283 _("Change pagename in all linked pages also?")));
284 $this->_tablePush($table, '',
285 $this->checkBox($post_args, 'createredirect',
286 _("Create redirect from old to new name?")));
287 $header->pushContent($table);
292 // TODO: grey out unchangeble pages, even in the initial list also?
293 // TODO: autoselect by matching name javascript in admin_rename[from]
294 // TODO: update rename[] fields when case-sensitive and regex is changed
296 // moved from lib/PageList.php
297 class _PageList_Column_renamed_pagename extends _PageList_Column {
298 function _getValue ($page_handle, &$revision_handle) {
300 $post_args = $request->getArg('admin_rename');
301 $options = array('regex' => @$post_args['regex'],
302 'icase' => @$post_args['icase']);
305 ? WikiPlugin_WikiAdminRename::renameHelper
306 ($page_handle->getName(),
307 $post_args['from'], $post_args['to'],
309 : $page_handle->getName();
310 $div = HTML::div(" => ",HTML::input(array('type' => 'text',
311 'name' => 'rename[]',
312 'value' => $value)));
313 $new_page = $request->getPage($value);
322 // c-hanging-comment-ender-p: nil
323 // indent-tabs-mode: nil