]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSearchReplace.php
better support for case_exact search (not caseexact for consistency),
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSearchReplace.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminSearchReplace.php,v 1.18 2004-11-23 15:17:20 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
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.
12
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.
17
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
21  */
22
23 /**
24  * Usage:   <?plugin WikiAdminSearchReplace ?> or called via WikiAdminSelect
25  * Author:  Reini Urban <rurban@x-ray.at>
26  *
27  * KNOWN ISSUES:
28  *   Requires PHP 4.2 so far.
29  */
30 require_once('lib/PageList.php');
31 require_once('lib/plugin/WikiAdminSelect.php');
32
33 class WikiPlugin_WikiAdminSearchReplace
34 extends WikiPlugin_WikiAdminSelect
35 {
36     function getName() {
37         return _("WikiAdminSearchReplace");
38     }
39
40     function getDescription() {
41         return _("Search and replace text in selected wiki pages.");
42     }
43
44     function getVersion() {
45         return preg_replace("/[Revision: $]/", '',
46                             "\$Revision: 1.18 $");
47     }
48
49     function getDefaultArguments() {
50         return array_merge
51             (
52              PageList::supportedArgs(),
53              array(
54                    's'  => false,
55                    /* Columns to include in listing */
56                    'info'     => 'some',
57                    ));
58     }
59
60     function replaceHelper(&$dbi, $pagename, $from, $to, $case_exact = 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();
66             if ($regex) {
67                 $newtext = preg_replace("/".$from."/".($case_exact?'':'i'), $to, $text);
68             } else {
69                 if ($case_exact) {
70                     $newtext = str_replace($from, $to, $text);
71                 } else {
72                     //not all PHP have this enabled. use a workaround
73                     if (function_exists('str_ireplace'))
74                         $newtext = str_ireplace($from, $to, $text);
75                     else { // see eof
76                         $newtext = stri_replace($from, $to, $text);
77                     }
78                 }
79             }
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);
84             }
85         }
86         return false;
87     }
88
89     function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
90         if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
91         $ul = HTML::ul();
92         $count = 0;
93         $post_args = $request->getArg('admin_replace');
94         $case_exact = !empty($post_args['case_exact']);
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, $case_exact, $regex))) {
100                 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", $from, $to, WikiLink($pagename))));
101                 $count++;
102             } else {
103                 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in content of page '%s'.", 
104                                               $from, WikiLink($pagename))));
105             }
106         }
107         if ($count) {
108             $dbi->touch();
109             return HTML($ul,
110                         HTML::p(fmt("%s pages changed.",$count)));
111         } else {
112             return HTML($ul,
113                         HTML::p(fmt("No pages changed.")));
114         }
115     }
116     
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')");
121         
122         $args = $this->getArgs($argstr, $request);
123         $this->_args = $args;
124             
125         //TODO: support p from <!plugin-list !>
126         $this->preSelectS($args, $request);
127
128         $p = $request->getArg('p');
129         if (!$p) $p = $this->_list;
130         $post_args = $request->getArg('admin_replace');
131         $next_action = 'select';
132         $pages = array();
133         if ($p && !$request->isPost())
134             $pages = $p;
135         if ($p && $request->isPost() &&
136             empty($post_args['cancel'])) {
137             // without individual PagePermissions:
138             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
139                 $request->_notAuthorized(WIKIAUTH_ADMIN);
140                 $this->disabled("! user->isAdmin");
141             }
142
143             if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
144                 // Real action
145                 return $this->searchReplacePages($dbi, $request, array_keys($p), $post_args['from'], $post_args['to']);
146             }
147             if ($post_args['action'] == 'select') {
148                 if (!empty($post_args['from']))
149                     $next_action = 'verify';
150                 foreach ($p as $name => $c) {
151                     $pages[$name] = 1;
152                 }
153             }
154         }
155         if ($next_action == 'select' and empty($pages)) {
156             // List all pages to select from.
157             //TODO: check for permissions and list only the allowed
158             $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit'], $args['exclude']);
159         }
160
161         if ($next_action == 'verify') {
162             $args['info'] = "checkbox,pagename,hi_content";
163         }
164         $pagelist = new PageList_Selectable($args['info'], $args['exclude'],
165                                             array_merge
166                                             (
167                                              $args,
168                                              array('types' => array
169                                                    (
170                                                     'hi_content' // with highlighted search for SearchReplace
171                                                     => new _PageList_Column_content('rev:hi_content', _("Content"))))));
172
173         $pagelist->addPageList($pages);
174
175         $header = HTML::p();
176         if (empty($post_args['from']))
177             $header->pushContent(
178               HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
179         if ($next_action == 'verify') {
180             $button_label = _("Yes");
181             $header->pushContent(
182               HTML::p(HTML::strong(
183                                    _("Are you sure you want to permanently search & replace text in the selected files?"))));
184             $this->replaceForm($header, $post_args);
185         }
186         else {
187             $button_label = _("Search & Replace");
188             $this->replaceForm($header, $post_args);
189             $header->pushContent(HTML::p(_("Select the pages to search:")));
190         }
191
192
193         $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
194                            Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
195
196         return HTML::form(array('action' => $request->getPostURL(),
197                                 'method' => 'post'),
198                           $header,
199                           $pagelist->getContent(),
200                           HiddenInputs($request->getArgs(),
201                                         false,
202                                         array('admin_replace')),
203                           HiddenInputs(array('admin_replace[action]' => $next_action)),
204                           ENABLE_PAGEPERM
205                           ? ''
206                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
207                           $buttons);
208     }
209
210     function replaceForm(&$header, $post_args) {
211         $header->pushContent(HTML::div(array('class'=>'hint'),
212                                        _("Replace all occurences of the given string in the content of all pages.")),
213                              HTML::br());
214         $header->pushContent(_("Replace: "));
215         $header->pushContent(HTML::input(array('name' => 'admin_replace[from]',
216                                                'value' => $post_args['from'])));
217         $header->pushContent(' '._("by").': ');
218         $header->pushContent(HTML::input(array('name' => 'admin_replace[to]',
219                                                'value' => $post_args['to'])));
220         $checkbox = HTML::input(array('type' => 'checkbox',
221                                       'name' => 'admin_replace[case_exact]',
222                                       'value' => 1));
223         if (!empty($post_args['case_exact']))
224             $checkbox->setAttr('checked','checked');
225         $header->pushContent(HTML::br(),$checkbox," ",_("case-exact"));
226         $checkbox_re = HTML::input(array('type' => 'checkbox',
227                                          'name' => 'admin_replace[regex]',
228                                          //'disabled' => 'disabled',
229                                          'value' => 1));
230         if (!empty($post_args['regex']))
231             $checkbox_re->setAttr('checked','checked');
232         $header->pushContent(HTML::br(),HTML::span(//array('style'=>'color: #aaa'),
233                                                    $checkbox_re," ",_("regex")));
234         $header->pushContent(HTML::br());
235         return $header;
236     }
237 }
238
239 function stri_replace($find,$replace,$string) {
240     if (!is_array($find)) $find = array($find);
241     if (!is_array($replace))  {
242         if (!is_array($find)) 
243             $replace = array($replace);
244         else {
245             // this will duplicate the string into an array the size of $find
246             $c = count($find);
247             $rString = $replace;
248             unset($replace);
249             for ($i = 0; $i < $c; $i++) {
250                 $replace[$i] = $rString;
251             }
252         }
253     }
254     foreach ($find as $fKey => $fItem) {
255         $between = explode(strtolower($fItem),strtolower($string));
256         $pos = 0;
257         foreach($between as $bKey => $bItem) {
258             $between[$bKey] = substr($string,$pos,strlen($bItem));
259             $pos += strlen($bItem) + strlen($fItem);
260         }
261         $string = implode($replace[$fKey],$between);
262     }
263     return $string;
264 }
265
266 // $Log: not supported by cvs2svn $
267 // Revision 1.17  2004/09/17 14:24:06  rurban
268 // support exclude=<!plugin-list !>, p not yet
269 //
270 // Revision 1.16  2004/06/16 10:38:59  rurban
271 // Disallow refernces in calls if the declaration is a reference
272 // ("allow_call_time_pass_reference clean").
273 //   PhpWiki is now allow_call_time_pass_reference = Off clean,
274 //   but several external libraries may not.
275 //   In detail these libs look to be affected (not tested):
276 //   * Pear_DB odbc
277 //   * adodb oracle
278 //
279 // Revision 1.15  2004/06/14 11:31:39  rurban
280 // renamed global $Theme to $WikiTheme (gforge nameclash)
281 // inherit PageList default options from PageList
282 //   default sortby=pagename
283 // use options in PageList_Selectable (limit, sortby, ...)
284 // added action revert, with button at action=diff
285 // added option regex to WikiAdminSearchReplace
286 //
287 // Revision 1.14  2004/06/13 15:33:20  rurban
288 // new support for arguments owner, author, creator in most relevant
289 // PageList plugins. in WikiAdmin* via preSelectS()
290 //
291 // Revision 1.13  2004/06/13 14:30:26  rurban
292 // security fix: check permissions in SearchReplace
293 //
294 // Revision 1.12  2004/06/08 10:05:12  rurban
295 // simplified admin action shortcuts
296 //
297 // Revision 1.11  2004/06/04 20:32:54  rurban
298 // Several locale related improvements suggested by Pierrick Meignen
299 // LDAP fix by John Cole
300 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
301 //
302 // Revision 1.10  2004/06/03 22:24:48  rurban
303 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
304 //
305 // Revision 1.9  2004/04/07 23:13:19  rurban
306 // fixed pear/File_Passwd for Windows
307 // fixed FilePassUser sessions (filehandle revive) and password update
308 //
309 // Revision 1.8  2004/03/17 20:23:44  rurban
310 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
311 //
312 // Revision 1.7  2004/03/12 13:31:43  rurban
313 // enforce PagePermissions, errormsg if not Admin
314 //
315 // Revision 1.6  2004/02/24 15:20:07  rurban
316 // fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
317 //
318 // Revision 1.5  2004/02/17 12:11:36  rurban
319 // 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, ...)
320 //
321 // Revision 1.4  2004/02/15 21:34:37  rurban
322 // PageList enhanced and improved.
323 // fixed new WikiAdmin... plugins
324 // editpage, Theme with exp. htmlarea framework
325 //   (htmlarea yet committed, this is really questionable)
326 // WikiUser... code with better session handling for prefs
327 // enhanced UserPreferences (again)
328 // RecentChanges for show_deleted: how should pages be deleted then?
329 //
330 // Revision 1.3  2004/02/12 17:05:39  rurban
331 // WikiAdminRename:
332 //   added "Change pagename in all linked pages also"
333 // PageList:
334 //   added javascript toggle for Select
335 // WikiAdminSearchReplace:
336 //   fixed another typo
337 //
338 // Revision 1.2  2004/02/12 11:47:51  rurban
339 // typo
340 //
341 // Revision 1.1  2004/02/12 11:25:53  rurban
342 // new WikiAdminSearchReplace plugin (requires currently Admin)
343 // removed dead comments from WikiDB
344 //
345 //
346
347 // Local Variables:
348 // mode: php
349 // tab-width: 8
350 // c-basic-offset: 4
351 // c-hanging-comment-ender-p: nil
352 // indent-tabs-mode: nil
353 // End:
354 ?>