4 Copyright 2004,2005,2007 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Usage: <?plugin WikiAdminRename ?> or called via WikiAdminSelect
25 * @author: Reini Urban <rurban@x-ray.at>
30 require_once('lib/PageList.php');
31 require_once('lib/plugin/WikiAdminSelect.php');
33 class WikiPlugin_WikiAdminRename
34 extends WikiPlugin_WikiAdminSelect
37 return _("WikiAdminRename");
40 function getDescription() {
41 return _("Rename selected pages");
44 function getVersion() {
45 return preg_replace("/[Revision: $]/", '',
49 function getDefaultArguments() {
52 PageList::supportedArgs(),
55 /* Columns to include in listing */
56 'info' => 'pagename,mtime',
61 function renameHelper($name, $from, $to, $options = false) {
62 if ($options['regex'])
63 return preg_replace('/'.$from.'/'.($options['icase']?'i':''), $to, $name);
64 elseif ($options['icase'])
65 return str_ireplace($from, $to, $name);
67 return str_replace($from, $to, $name);
70 function renamePages(&$dbi, &$request, $pages, $from, $to, $updatelinks=false) {
73 $post_args = $request->getArg('admin_rename');
74 $options = array('regex' => @$post_args['regex'],
75 'icase' => @$post_args['icase']);
76 foreach ($pages as $name) {
77 if ( ($newname = $this->renameHelper($name, $from, $to, $options))
78 and $newname != $name )
80 if ($dbi->isWikiPage($newname))
81 $ul->pushContent(HTML::li(fmt("Page %s already exists. Ignored.",
82 WikiLink($newname))));
83 elseif (! mayAccessPage('edit', $name))
84 $ul->pushContent(HTML::li(fmt("Access denied to rename page '%s'.",
86 elseif ( $dbi->renamePage($name, $newname, $updatelinks)) {
87 /* not yet implemented for all backends */
88 $ul->pushContent(HTML::li(fmt("Renamed page '%s' to '%s'.",
89 $name, WikiLink($newname))));
92 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
96 $ul->pushContent(HTML::li(fmt("Couldn't rename page '%s' to '%s'.",
102 return HTML($ul, HTML::p(fmt("%s pages have been permanently renamed.",
105 return HTML($ul, HTML::p(fmt("No pages renamed.")));
109 function run($dbi, $argstr, &$request, $basepage) {
110 $action = $request->getArg('action');
111 if ($action != 'browse' and $action != 'rename'
112 and $action != _("PhpWikiAdministration")."/"._("Rename"))
113 return $this->disabled("(action != 'browse')");
115 $args = $this->getArgs($argstr, $request);
116 $this->_args = $args;
117 $this->preSelectS($args, $request);
119 $p = $request->getArg('p');
120 if (!$p) $p = $this->_list;
121 $post_args = $request->getArg('admin_rename');
122 $next_action = 'select';
124 if ($p && !$request->isPost())
126 if ($p && $request->isPost() &&
127 !empty($post_args['rename']) && empty($post_args['cancel'])) {
128 // without individual PagePermissions:
129 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
130 $request->_notAuthorized(WIKIAUTH_ADMIN);
131 $this->disabled("! user->isAdmin");
133 // DONE: error message if not allowed.
134 if ($post_args['action'] == 'verify') {
136 return $this->renamePages($dbi, $request, array_keys($p),
137 $post_args['from'], $post_args['to'],
138 !empty($post_args['updatelinks']));
140 if ($post_args['action'] == 'select') {
141 if (!empty($post_args['from']))
142 $next_action = 'verify';
143 foreach ($p as $name => $c) {
148 if ($next_action == 'select' and empty($pages)) {
149 // List all pages to select from.
150 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
152 /*if ($next_action == 'verify') {
153 $args['info'] = "checkbox,pagename,renamed_pagename";
155 $pagelist = new PageList_Selectable
157 $args['info'], $args['exclude'],
159 array('renamed_pagename'
160 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
162 $pagelist->addPageList($pages);
164 $header = HTML::div();
165 if ($next_action == 'verify') {
166 $button_label = _("Yes");
167 $header->pushContent(
168 HTML::p(HTML::strong(
169 _("Are you sure you want to permanently rename the selected pages?"))));
170 $header = $this->renameForm($header, $post_args);
173 $button_label = _("Rename selected pages");
174 if (!$post_args and count($pages) == 1) {
175 list($post_args['from'],) = array_keys($pages);
176 $post_args['to'] = $post_args['from'];
178 $header = $this->renameForm($header, $post_args);
179 $header->pushContent(HTML::p(_("Select the pages to rename:")));
182 $buttons = HTML::p(Button('submit:admin_rename[rename]', $button_label, 'wikiadmin'),
183 HTML::Raw(' '),
184 Button('submit:admin_rename[cancel]', _("Cancel"), 'button'));
186 return HTML::form(array('action' => $request->getPostURL(),
189 $pagelist->getContent(),
190 HiddenInputs($request->getArgs(),
192 array('admin_rename')),
193 HiddenInputs(array('admin_rename[action]' => $next_action)),
196 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
200 function checkBox (&$post_args, $name, $msg) {
201 $id = 'admin_rename-'.$name;
202 $checkbox = HTML::input(array('type' => 'checkbox',
203 'name' => 'admin_rename['.$name.']',
206 if (!empty($post_args[$name]))
207 $checkbox->setAttr('checked', 'checked');
208 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
211 function renameForm(&$header, $post_args) {
212 $table = HTML::table();
213 $this->_tablePush($table, _("Rename"). " ". _("from").': ',
214 HTML::input(array('name' => 'admin_rename[from]',
216 'value' => $post_args['from'])));
217 $this->_tablePush($table, _("to").': ',
218 HTML::input(array('name' => 'admin_rename[to]',
220 'value' => $post_args['to'])));
221 $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
222 $this->_tablePush($table, '', $this->checkBox($post_args, 'icase', _("Case insensitive?")));
223 if (DEBUG) // not yet stable
224 $this->_tablePush($table, '', $this->checkBox($post_args, 'updatelinks',
225 _("Change pagename in all linked pages also?")));
226 $header->pushContent($table);
231 // TODO: grey out unchangeble pages, even in the initial list also?
232 // TODO: autoselect by matching name javascript in admin_rename[from]
233 // TODO: update rename[] fields when case-sensitive and regex is changed
235 // moved from lib/PageList.php
236 class _PageList_Column_renamed_pagename extends _PageList_Column {
237 function _getValue ($page_handle, &$revision_handle) {
239 $post_args = $request->getArg('admin_rename');
240 $options = array('regex' => @$post_args['regex'],
241 'icase' => @$post_args['icase']);
243 $value = $post_args ? WikiPlugin_WikiAdminRename::renameHelper($page_handle->getName(),
244 $post_args['from'], $post_args['to'],
246 : $page_handle->getName();
247 $div = HTML::div(" => ",HTML::input(array('type' => 'text',
248 'name' => 'rename[]',
249 'value' => $value)));
250 $new_page = $request->getPage($value);
251 if ($new_page->exists()) {
252 $div->setAttr('class','error');
253 $div->setAttr('title',_("This page already exists"));
263 // c-hanging-comment-ender-p: nil
264 // indent-tabs-mode: nil