]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSearchReplace.php
Put buttons at the top so that they are always visible
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSearchReplace.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4  Copyright 2004,2007 $ThePhpWikiProgrammingTeam
5  Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
6
7  This file is part of PhpWiki.
8
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.
13
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.
18
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
22  */
23
24 /**
25  * Usage:   <?plugin WikiAdminSearchReplace ?> or called via WikiAdminSelect
26  * Author:  Reini Urban <rurban@x-ray.at>
27  *
28  * KNOWN ISSUES:
29  *   Requires PHP 4.2 so far.
30  */
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
33
34 class WikiPlugin_WikiAdminSearchReplace
35 extends WikiPlugin_WikiAdminSelect
36 {
37     function getName() {
38         return _("WikiAdminSearchReplace");
39     }
40
41     function getDescription() {
42         return _("Search and replace text in selected wiki pages.");
43     }
44
45     function getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision$");
48     }
49
50     function getDefaultArguments() {
51         return array_merge
52             (
53              PageList::supportedArgs(),
54              array(
55                    's'  => false,
56                    /* Columns to include in listing */
57                    'info'     => 'some',
58                    ));
59     }
60
61     function replaceHelper(&$dbi, $pagename, $from, $to, $case_exact=true, $regex=false) {
62         $page = $dbi->getPage($pagename);
63         if ($page->exists()) {// don't replace default contents
64             $current = $page->getCurrentRevision();
65             $version = $current->getVersion();
66             $text = $current->getPackedContent();
67             if ($regex) {
68                 $newtext = preg_replace("/".$from."/".($case_exact?'':'i'), $to, $text);
69             } else {
70                 if ($case_exact) {
71                     $newtext = str_replace($from, $to, $text);
72                 } else {
73                     //not all PHP have this enabled. use a workaround
74                     if (function_exists('str_ireplace'))
75                         $newtext = str_ireplace($from, $to, $text);
76                     else { // see eof
77                         $newtext = stri_replace($from, $to, $text);
78                     }
79                 }
80             }
81             if ($text != $newtext) {
82                 $meta = $current->_data;
83                 $meta['summary'] = sprintf(_("WikiAdminSearchReplace %s by %s"),$from,$to);
84                 return $page->save($newtext, $version + 1, $meta);
85             }
86         }
87         return false;
88     }
89
90     function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
91         if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
92         $ul = HTML::ul();
93         $count = 0;
94         $post_args = $request->getArg('admin_replace');
95         $case_exact = !empty($post_args['case_exact']);
96         $regex = !empty($post_args['regex']);
97         foreach ($pages as $pagename) {
98             if (!mayAccessPage('edit', $pagename)) {
99                 $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",$pagename)));
100             } elseif (($result = $this->replaceHelper($dbi, $pagename, $from, $to, 
101                                                       $case_exact, $regex))) 
102             {
103                 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", 
104                                               $from, $to, WikiLink($pagename))));
105                 $count++;
106             } else {
107                 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in content of page '%s'.", 
108                                               $from, WikiLink($pagename))));
109             }
110         }
111         if ($count) {
112             $dbi->touch();
113             return HTML($ul,
114                         HTML::p(fmt("%s pages changed.",$count)));
115         } else {
116             return HTML($ul,
117                         HTML::p(fmt("No pages changed.")));
118         }
119     }
120     
121     function run($dbi, $argstr, &$request, $basepage) {
122         // no action=replace support yet
123         if ($request->getArg('action') != 'browse')
124             return $this->disabled("(action != 'browse')");
125         
126         $args = $this->getArgs($argstr, $request);
127         $this->_args = $args;
128             
129         //TODO: support p from <!plugin-list !>
130         $this->preSelectS($args, $request);
131
132         $p = $request->getArg('p');
133         if (!$p) $p = $this->_list;
134         $post_args = $request->getArg('admin_replace');
135         $next_action = 'select';
136         $pages = array();
137         if ($p && !$request->isPost())
138             $pages = $p;
139         if ($p && $request->isPost() &&
140             empty($post_args['cancel'])) {
141             // without individual PagePermissions:
142             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
143                 $request->_notAuthorized(WIKIAUTH_ADMIN);
144                 $this->disabled("! user->isAdmin");
145             }
146
147             if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
148                 // Real action
149                 return $this->searchReplacePages($dbi, $request, array_keys($p), 
150                                                  $post_args['from'], $post_args['to']);
151             }
152             if ($post_args['action'] == 'select') {
153                 if (!empty($post_args['from']))
154                     $next_action = 'verify';
155                 foreach ($p as $name => $c) {
156                     $pages[$name] = 1;
157                 }
158             }
159         }
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'], 
164                                          $args['limit'], $args['exclude']);
165         }
166
167         if ($next_action == 'verify') {
168             $args['info'] = "checkbox,pagename,hi_content";
169         }
170         $pagelist = new PageList_Selectable
171             ($args['info'], $args['exclude'],
172              array_merge
173              (
174               $args,
175               array('types' => array
176                     (
177                      'hi_content' // with highlighted search for SearchReplace
178                      => new _PageList_Column_content('rev:hi_content', _("Content"))))));
179
180         $pagelist->addPageList($pages);
181
182         $header = HTML::p();
183         if (empty($post_args['from']))
184             $header->pushContent(
185               HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
186         if ($next_action == 'verify') {
187             $button_label = _("Yes");
188             $header->pushContent(
189               HTML::p(HTML::strong(
190                                    _("Are you sure you want to permanently search & replace text in the selected files?"))));
191             $this->replaceForm($header, $post_args);
192         }
193         else {
194             $button_label = _("Search & Replace");
195             $this->replaceForm($header, $post_args);
196             $header->pushContent(HTML::p(_("Select the pages to search:")));
197         }
198
199
200         $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
201                            Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
202
203         return HTML::form(array('action' => $request->getPostURL(),
204                                 'method' => 'post'),
205                           $header,
206                           $buttons,
207                           $pagelist->getContent(),
208                           HiddenInputs($request->getArgs(),
209                                         false,
210                                         array('admin_replace')),
211                           HiddenInputs(array('admin_replace[action]' => $next_action)),
212                           ENABLE_PAGEPERM
213                           ? ''
214                           : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
215     }
216
217     function checkBox (&$post_args, $name, $msg) {
218         $id = 'admin_replace-'.$name;
219         $checkbox = HTML::input(array('type' => 'checkbox',
220                                       'name' => 'admin_replace['.$name.']',
221                                       'id'   => $id,
222                                       'value' => 1));
223         if (!empty($post_args[$name]))
224             $checkbox->setAttr('checked', 'checked');
225         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
226     }
227
228     function replaceForm(&$header, $post_args) {
229         $header->pushContent(HTML::div(array('class'=>'hint'),
230                                        _("Replace all occurences of the given string in the content of all pages.")),
231                              HTML::br());
232         $table = HTML::table();
233         $this->_tablePush($table, _("Replace").": ",
234                           HTML::input(array('name' => 'admin_replace[from]',
235                                             'size' => 90,
236                                             'value' => $post_args['from'])));
237         $this->_tablePush($table, _("by").': ',
238                           HTML::input(array('name' => 'admin_replace[to]',
239                                             'size' => 90,
240                                             'value' => $post_args['to'])));
241         $this->_tablePush($table, '', $this->checkBox($post_args, 'case_exact', _("Case exact?")));
242         $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
243         $header->pushContent($table);
244         $header->pushContent(HTML::br());
245         return $header;
246     }
247 }
248
249 function stri_replace($find,$replace,$string) {
250     if (!is_array($find)) $find = array($find);
251     if (!is_array($replace))  {
252         if (!is_array($find)) 
253             $replace = array($replace);
254         else {
255             // this will duplicate the string into an array the size of $find
256             $c = count($find);
257             $rString = $replace;
258             unset($replace);
259             for ($i = 0; $i < $c; $i++) {
260                 $replace[$i] = $rString;
261             }
262         }
263     }
264     foreach ($find as $fKey => $fItem) {
265         $between = explode(strtolower($fItem),strtolower($string));
266         $pos = 0;
267         foreach($between as $bKey => $bItem) {
268             $between[$bKey] = substr($string,$pos,strlen($bItem));
269             $pos += strlen($bItem) + strlen($fItem);
270         }
271         $string = implode($replace[$fKey],$between);
272     }
273     return $string;
274 }
275
276 // Local Variables:
277 // mode: php
278 // tab-width: 8
279 // c-basic-offset: 4
280 // c-hanging-comment-ender-p: nil
281 // indent-tabs-mode: nil
282 // End:
283 ?>