2 rcs_id('$Id: WikiAdminSearchReplace.php,v 1.17 2004-09-17 14:24:06 rurban Exp $');
4 Copyright 2004 $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 WikiAdminSearchReplace ?> or called via WikiAdminSelect
25 * Author: Reini Urban <rurban@x-ray.at>
28 * Requires PHP 4.2 so far.
30 require_once('lib/PageList.php');
31 require_once('lib/plugin/WikiAdminSelect.php');
33 class WikiPlugin_WikiAdminSearchReplace
34 extends WikiPlugin_WikiAdminSelect
37 return _("WikiAdminSearchReplace");
40 function getDescription() {
41 return _("Search and replace text in selected wiki pages.");
44 function getVersion() {
45 return preg_replace("/[Revision: $]/", '',
46 "\$Revision: 1.17 $");
49 function getDefaultArguments() {
52 PageList::supportedArgs(),
55 /* Columns to include in listing */
60 function replaceHelper(&$dbi, $pagename, $from, $to, $caseexact = true, $regex = false) {
61 $page = $dbi->getPage($pagename);
62 if ($page->exists()) {// don't replace default contents
63 $current = $page->getCurrentRevision();
64 $version = $current->getVersion();
65 $text = $current->getPackedContent();
67 $newtext = preg_replace("/".$from."/".($caseexact?'':'i'), $to, $text);
70 $newtext = str_replace($from, $to, $text);
72 //not all PHP have this enabled. use a workaround
73 if (function_exists('str_ireplace'))
74 $newtext = str_ireplace($from, $to, $text);
76 $newtext = stri_replace($from, $to, $text);
80 if ($text != $newtext) {
81 $meta = $current->_data;
82 $meta['summary'] = sprintf(_("WikiAdminSearchReplace %s by %s"),$from,$to);
83 return $page->save($newtext, $version + 1, $meta);
89 function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
90 if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
93 $post_args = $request->getArg('admin_replace');
94 $caseexact = !empty($post_args['caseexact']);
95 $regex = !empty($post_args['regex']);
96 foreach ($pages as $pagename) {
97 if (!mayAccessPage('edit',$pagename)) {
98 $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",$pagename)));
99 } elseif (($result = $this->replaceHelper($dbi, $pagename, $from, $to, $caseexact, $regex))) {
100 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", $from, $to, WikiLink($pagename))));
103 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in content of page '%s'.",
104 $from, WikiLink($pagename))));
110 HTML::p(fmt("%s pages changed.",$count)));
113 HTML::p(fmt("No pages changed.")));
117 function run($dbi, $argstr, &$request, $basepage) {
118 // no action=replace support yet
119 if ($request->getArg('action') != 'browse')
120 return $this->disabled("(action != 'browse')");
122 $args = $this->getArgs($argstr, $request);
123 $this->_args = $args;
124 if (!empty($args['exclude']))
125 $exclude = is_string($args['exclude']) ? explodePageList($args['exclude'])
126 : $args['exclude']; // <! plugin-list !>
130 //TODO: support p from <!plugin-list !>
131 $this->preSelectS($args, $request);
133 $p = $request->getArg('p');
134 if (!$p) $p = $this->_list;
135 $post_args = $request->getArg('admin_replace');
136 $next_action = 'select';
138 if ($p && !$request->isPost())
140 if ($p && $request->isPost() &&
141 empty($post_args['cancel'])) {
142 // without individual PagePermissions:
143 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
144 $request->_notAuthorized(WIKIAUTH_ADMIN);
145 $this->disabled("! user->isAdmin");
148 if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
150 return $this->searchReplacePages($dbi, $request, array_keys($p), $post_args['from'], $post_args['to']);
152 if ($post_args['action'] == 'select') {
153 if (!empty($post_args['from']))
154 $next_action = 'verify';
155 foreach ($p as $name => $c) {
160 if ($next_action == 'select' and empty($pages)) {
161 // List all pages to select from.
162 //TODO: check for permissions and list only the allowed
163 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
166 if ($next_action == 'verify') {
167 $args['info'] = "checkbox,pagename,hi_content";
169 $pagelist = new PageList_Selectable($args['info'], $exclude,
173 array('types' => array
175 'hi_content' // with highlighted search for SearchReplace
176 => new _PageList_Column_content('rev:hi_content', _("Content"))))));
178 $pagelist->addPageList($pages);
181 if (empty($post_args['from']))
182 $header->pushContent(
183 HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
184 if ($next_action == 'verify') {
185 $button_label = _("Yes");
186 $header->pushContent(
187 HTML::p(HTML::strong(
188 _("Are you sure you want to permanently search & replace text in the selected files?"))));
189 $this->replaceForm($header, $post_args);
192 $button_label = _("Search & Replace");
193 $this->replaceForm($header, $post_args);
194 $header->pushContent(HTML::p(_("Select the pages to search:")));
198 $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
199 Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
201 return HTML::form(array('action' => $request->getPostURL(),
204 $pagelist->getContent(),
205 HiddenInputs($request->getArgs(),
207 array('admin_replace')),
208 HiddenInputs(array('admin_replace[action]' => $next_action)),
211 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
215 function replaceForm(&$header, $post_args) {
216 $header->pushContent(HTML::div(array('class'=>'hint'),
217 _("Replace all occurences of the given string in the content of all pages.")),
219 $header->pushContent(_("Replace: "));
220 $header->pushContent(HTML::input(array('name' => 'admin_replace[from]',
221 'value' => $post_args['from'])));
222 $header->pushContent(' '._("by").': ');
223 $header->pushContent(HTML::input(array('name' => 'admin_replace[to]',
224 'value' => $post_args['to'])));
225 $checkbox = HTML::input(array('type' => 'checkbox',
226 'name' => 'admin_replace[caseexact]',
228 if (!empty($post_args['caseexact']))
229 $checkbox->setAttr('checked','checked');
230 $header->pushContent(HTML::br(),$checkbox," ",_("case-exact"));
231 $checkbox_re = HTML::input(array('type' => 'checkbox',
232 'name' => 'admin_replace[regex]',
233 //'disabled' => 'disabled',
235 if (!empty($post_args['regex']))
236 $checkbox_re->setAttr('checked','checked');
237 $header->pushContent(HTML::br(),HTML::span(//array('style'=>'color: #aaa'),
238 $checkbox_re," ",_("regex")));
239 $header->pushContent(HTML::br());
244 function stri_replace($find,$replace,$string) {
245 if (!is_array($find)) $find = array($find);
246 if (!is_array($replace)) {
247 if (!is_array($find))
248 $replace = array($replace);
250 // this will duplicate the string into an array the size of $find
254 for ($i = 0; $i < $c; $i++) {
255 $replace[$i] = $rString;
259 foreach ($find as $fKey => $fItem) {
260 $between = explode(strtolower($fItem),strtolower($string));
262 foreach($between as $bKey => $bItem) {
263 $between[$bKey] = substr($string,$pos,strlen($bItem));
264 $pos += strlen($bItem) + strlen($fItem);
266 $string = implode($replace[$fKey],$between);
271 // $Log: not supported by cvs2svn $
272 // Revision 1.16 2004/06/16 10:38:59 rurban
273 // Disallow refernces in calls if the declaration is a reference
274 // ("allow_call_time_pass_reference clean").
275 // PhpWiki is now allow_call_time_pass_reference = Off clean,
276 // but several external libraries may not.
277 // In detail these libs look to be affected (not tested):
281 // Revision 1.15 2004/06/14 11:31:39 rurban
282 // renamed global $Theme to $WikiTheme (gforge nameclash)
283 // inherit PageList default options from PageList
284 // default sortby=pagename
285 // use options in PageList_Selectable (limit, sortby, ...)
286 // added action revert, with button at action=diff
287 // added option regex to WikiAdminSearchReplace
289 // Revision 1.14 2004/06/13 15:33:20 rurban
290 // new support for arguments owner, author, creator in most relevant
291 // PageList plugins. in WikiAdmin* via preSelectS()
293 // Revision 1.13 2004/06/13 14:30:26 rurban
294 // security fix: check permissions in SearchReplace
296 // Revision 1.12 2004/06/08 10:05:12 rurban
297 // simplified admin action shortcuts
299 // Revision 1.11 2004/06/04 20:32:54 rurban
300 // Several locale related improvements suggested by Pierrick Meignen
301 // LDAP fix by John Cole
302 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
304 // Revision 1.10 2004/06/03 22:24:48 rurban
305 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
307 // Revision 1.9 2004/04/07 23:13:19 rurban
308 // fixed pear/File_Passwd for Windows
309 // fixed FilePassUser sessions (filehandle revive) and password update
311 // Revision 1.8 2004/03/17 20:23:44 rurban
312 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
314 // Revision 1.7 2004/03/12 13:31:43 rurban
315 // enforce PagePermissions, errormsg if not Admin
317 // Revision 1.6 2004/02/24 15:20:07 rurban
318 // fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
320 // Revision 1.5 2004/02/17 12:11:36 rurban
321 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
323 // Revision 1.4 2004/02/15 21:34:37 rurban
324 // PageList enhanced and improved.
325 // fixed new WikiAdmin... plugins
326 // editpage, Theme with exp. htmlarea framework
327 // (htmlarea yet committed, this is really questionable)
328 // WikiUser... code with better session handling for prefs
329 // enhanced UserPreferences (again)
330 // RecentChanges for show_deleted: how should pages be deleted then?
332 // Revision 1.3 2004/02/12 17:05:39 rurban
334 // added "Change pagename in all linked pages also"
336 // added javascript toggle for Select
337 // WikiAdminSearchReplace:
338 // fixed another typo
340 // Revision 1.2 2004/02/12 11:47:51 rurban
343 // Revision 1.1 2004/02/12 11:25:53 rurban
344 // new WikiAdminSearchReplace plugin (requires currently Admin)
345 // removed dead comments from WikiDB
353 // c-hanging-comment-ender-p: nil
354 // indent-tabs-mode: nil