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>
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 PageList::supportedArgs(),
56 /* Columns to include in listing */
57 'info' => 'pagename,mtime',
63 function renameHelper($name, $from, $to, $options = false) {
64 if ($options['regex'])
65 return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
66 elseif ($options['icase'])
67 return str_ireplace($from, $to, $name);
69 return str_replace($from, $to, $name);
72 function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false, $createredirect=false) {
73 $result = HTML::div();
76 $post_args = $request->getArg('admin_rename');
77 $options = array('regex' => @$post_args['regex'],
78 'icase' => @$post_args['icase']);
79 foreach ($pages as $name) {
80 if ( ($newname = $this->renameHelper($name, $from, $to, $options))
81 and $newname != $name )
83 if ($dbi->isWikiPage($newname))
84 $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
85 WikiLink($newname))));
86 elseif (! mayAccessPage('edit', $name))
87 $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
89 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'] = 0;
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 . "\">>";
104 $meta['summary'] = sprintf(_("Renaming created redirect page from '%s' to '%s'"), $name, $newname);
105 $meta['is_minor_edit'] = 0;
106 $meta['author'] = $request->_user->UserName();
107 $page->save($text, 1, $meta);
109 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
110 $name, WikiLink($newname))));
113 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
117 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
123 $result->setAttr('class', 'feedback');
124 $result->pushContent(HTML::p(fmt("%s pages have been permanently renamed:", $count)));
125 $result->pushContent($ul);
128 $result->setAttr('class', 'error');
129 $result->pushContent(HTML::p(fmt("No pages renamed:")));
130 $result->pushContent($ul);
135 function run($dbi, $argstr, &$request, $basepage) {
136 $action = $request->getArg('action');
137 if ($action != 'browse' and $action != 'rename'
138 and $action != _("PhpWikiAdministration")."/"._("Rename"))
139 return $this->disabled("(action != 'browse')");
141 if ($action == 'rename') {
142 // We rename a single page.
143 // No need to display "Regex?" and "Case insensitive?" boxes
144 // No need to confirm
150 $args = $this->getArgs($argstr, $request);
151 $this->_args = $args;
152 $this->preSelectS($args, $request);
154 $p = $request->getArg('p');
155 if (!$p) $p = $this->_list;
156 $post_args = $request->getArg('admin_rename');
157 $next_action = 'select';
159 if ($p && !$request->isPost())
161 if ($p && $request->isPost() &&
162 !empty($post_args['rename']) && empty($post_args['cancel'])) {
163 // without individual PagePermissions:
164 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
165 $request->_notAuthorized(WIKIAUTH_ADMIN);
166 $this->disabled("! user->isAdmin");
168 // DONE: error message if not allowed.
169 if ($post_args['action'] == 'verify') {
171 return $this->renamePages($dbi, $request, array_keys($p),
172 $post_args['from'], $post_args['to'],
173 !empty($post_args['updatelinks']),
174 !empty($post_args['createredirect']));
176 if ($post_args['action'] == 'select') {
177 if (!empty($post_args['from']))
178 $next_action = 'verify';
179 foreach ($p as $name => $c) {
184 if ($next_action == 'select' and empty($pages)) {
185 // List all pages to select from.
186 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
188 /*if ($next_action == 'verify') {
189 $args['info'] = "checkbox,pagename,renamed_pagename";
191 $pagelist = new PageList_Selectable
193 $args['info'], $args['exclude'],
195 array('renamed_pagename'
196 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
198 $pagelist->addPageList($pages);
200 $header = HTML::div();
201 if ($next_action == 'verify') {
202 $button_label = _("Yes");
203 $header->pushContent(
204 HTML::p(HTML::strong(
205 _("Are you sure you want to permanently rename the selected pages?"))));
206 $header = $this->renameForm($header, $post_args, $singlepage);
208 if ($singlepage === true) {
209 $button_label = _("Rename page");
211 $button_label = _("Rename selected pages");
213 if (!$post_args and count($pages) == 1) {
214 list($post_args['from'],) = array_keys($pages);
215 $post_args['to'] = $post_args['from'];
217 $header = $this->renameForm($header, $post_args, $singlepage);
218 if ($singlepage === false) {
219 $header->pushContent(HTML::p(_("Select the pages to rename:")));
223 $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
224 HTML::Raw(' '),
225 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
227 if ($singlepage === false) {
228 $list = $pagelist->getContent();
232 return HTML::form(array('action' => $request->getPostURL(),
237 HiddenInputs($request->getArgs(),
239 array('admin_rename')),
240 HiddenInputs(array('admin_rename[action]' => $next_action)),
243 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
246 function checkBox (&$post_args, $name, $msg) {
247 $id = 'admin_rename-'.$name;
248 $checkbox = HTML::input(array('type' => 'checkbox',
249 'name' => 'admin_rename['.$name.']',
252 if (!empty($post_args[$name]))
253 $checkbox->setAttr('checked', 'checked');
254 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
257 function renameForm(&$header, $post_args, $singlepage) {
258 $table = HTML::table();
259 $this->_tablePush($table, _("Rename"). " ". _("from").': ',
260 HTML::input(array('name' => 'admin_rename[from]',
262 'value' => $post_args['from'])));
263 $this->_tablePush($table, _("to").': ',
264 HTML::input(array('name' => 'admin_rename[to]',
266 'value' => $post_args['to'])));
267 if ($singlepage === false) {
268 $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
269 $this->_tablePush($table, '', $this->checkBox($post_args, 'icase', _("Case insensitive?")));
271 if (DEBUG) // not yet stable
272 $this->_tablePush($table, '', $this->checkBox($post_args, 'updatelinks',
273 _("Change pagename in all linked pages also?")));
274 $this->_tablePush($table, '', $this->checkBox($post_args, 'createredirect',
275 _("Create redirect from old to new name?")));
276 $header->pushContent($table);
281 // TODO: grey out unchangeble pages, even in the initial list also?
282 // TODO: autoselect by matching name javascript in admin_rename[from]
283 // TODO: update rename[] fields when case-sensitive and regex is changed
285 // moved from lib/PageList.php
286 class _PageList_Column_renamed_pagename extends _PageList_Column {
287 function _getValue ($page_handle, &$revision_handle) {
289 $post_args = $request->getArg('admin_rename');
290 $options = array('regex' => @$post_args['regex'],
291 'icase' => @$post_args['icase']);
293 $value = $post_args ? WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(),
294 $post_args['from'], $post_args['to'],
296 : $page_handle->getName();
297 $div = HTML::div(" => ",HTML::input(array('type' => 'text',
298 'name' => 'rename[]',
299 'value' => $value)));
300 $new_page = $request->getPage($value);
309 // c-hanging-comment-ender-p: nil
310 // indent-tabs-mode: nil