]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSearchReplace.php
PageList enhanced and improved.
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSearchReplace.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WikiAdminSearchReplace.php,v 1.4 2004-02-15 21:34:37 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  * Currently we must be Admin.
29  * Future versions will support PagePermissions.
30  * requires PHP 4.2 so far.
31  */
32 require_once('lib/PageList.php');
33 require_once('lib/plugin/WikiAdminSelect.php');
34
35 class WikiPlugin_WikiAdminSearchReplace
36 extends WikiPlugin_WikiAdminSelect
37 {
38     function getName() {
39         return _("WikiAdminSearchReplace");
40     }
41
42     function getDescription() {
43         return _("Search and replace text in selected wiki pages.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.4 $");
49     }
50
51     function getDefaultArguments() {
52         return array(
53                      /* Pages to exclude */
54                      'exclude'  => '.',
55                      /* Columns to include in listing */
56                      'info'     => 'some',
57                      /* How to sort */
58                      'sortby'   => 'pagename',
59                      );
60     }
61
62     function replaceHelper(&$dbi, $pagename, $from, $to, $caseexact = true) {
63         $page = $dbi->getPage($pagename);
64         if ($page->exists()) {// don't replace default contents
65             $current = $page->getCurrentRevision();
66             $version = $current->getVersion();
67             $text = $current->getPackedContent();
68             if ($caseexact) {
69                 $newtext = str_replace($from, $to, $text);
70             } else {
71                 //not all PHP have this enabled. use a workaround
72                 if (function_exists('str_ireplace'))
73                     $newtext = str_ireplace($from, $to, $text);
74                 else { // see eof
75                     $newtext = stri_replace($from, $to, $text);
76                 }
77             }
78             if ($text != $newtext) {
79                 $meta = $current->_data;
80                 $meta['summary'] = sprintf(_("WikiAdminSearchReplace %s by %s"),$from,$to);
81                 return $page->save($newtext, $version + 1, $meta);
82             }
83         }
84         return false;
85     }
86
87     function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
88         if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
89         $ul = HTML::ul();
90         $count = 0;
91         $post_args = $request->getArg('admin_replace');
92         $caseexact = $post_args['caseexact'] == 1;
93         foreach ($pages as $pagename) {
94             if (($result = $this->replaceHelper(&$dbi,$pagename,$from,$to,$caseexact))) {
95                 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", $from, $to, WikiLink($pagename))));
96                 $count++;
97             } else {
98                 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in page '%s'.", $from, $to, WikiLink($pagename))));
99             }
100         }
101         if ($count) {
102             $dbi->touch();
103             return HTML($ul,
104                         HTML::p(fmt("%s pages changed.",$count)));
105         } else {
106             return HTML($ul,
107                         HTML::p(fmt("No pages changed.")));
108         }
109     }
110     
111     function run($dbi, $argstr, $request) {
112         if ($request->getArg('action') != 'browse')
113             return $this->disabled("(action != 'browse')");
114         
115         $args = $this->getArgs($argstr, $request);
116         $this->_args = $args;
117         if (!empty($args['exclude']))
118             $exclude = explodePageList($args['exclude']);
119         else
120             $exclude = false;
121
122
123         $p = $request->getArg('p');
124         $post_args = $request->getArg('admin_replace');
125         $next_action = 'select';
126         $pages = array();
127         if ($p && !$request->isPost())
128             $pages = $p;
129         if ($p && $request->isPost() && $request->_user->isAdmin()
130             && empty($post_args['cancel'])) {
131             // FIXME: error message if not admin.
132             if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
133                 // Real action
134                 return $this->searchReplacePages($dbi, $request, $p, $post_args['from'], $post_args['to']);
135             }
136             if ($post_args['action'] == 'select') {
137                 if (!empty($post_args['from']))
138                     $next_action = 'verify';
139                 if (is_array($p[0])) {
140                   foreach ($p as $name) {
141                     $pages[$name] = 1;
142                   }
143                 } else {
144                   $pages = $p;
145                 }
146             }
147         }
148         if ($next_action == 'select' and empty($pages)) {
149             // List all pages to select from.
150             $pages = $this->collectPages($pages, $dbi, $args['sortby']);
151         }
152
153         if ($next_action == 'verify') {
154             $args['info'] = "checkbox,pagename,hi_content";
155         }
156         $pagelist = new PageList_Selectable($args['info'], $exclude);
157         $pagelist->addPageList($pages);
158
159         $header = HTML::p();
160         if (empty($post_args['from']))
161             $header->pushContent(
162               HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
163         if ($next_action == 'verify') {
164             $button_label = _("Yes");
165             $header->pushContent(
166               HTML::p(HTML::strong(
167                                    _("Are you sure you want to permanently search & replace text in the selected files?"))));
168             $this->replaceForm(&$header, $post_args);
169         }
170         else {
171             $button_label = _("Search & Replace");
172             $this->replaceForm(&$header, $post_args);
173             $header->pushContent(HTML::p(_("Select the pages to search:")));
174         }
175
176
177         $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
178                            Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
179
180         return HTML::form(array('action' => $request->getPostURL(),
181                                 'method' => 'post'),
182                           $header,
183                           $pagelist->getContent(),
184                           HiddenInputs($request->getArgs(),
185                                         false,
186                                         array('admin_replace')),
187                           HiddenInputs(array('admin_replace[action]' => $next_action,
188                                              'require_authority_for_post' => WIKIAUTH_ADMIN)),
189                           $buttons);
190     }
191
192     function replaceForm(&$header, $post_args) {
193         $header->pushContent(_("Replace: "));
194         $header->pushContent(HTML::input(array('name' => 'admin_replace[from]',
195                                                'value' => $post_args['from'])));
196         $header->pushContent(' '._("by").': ');
197         $header->pushContent(HTML::input(array('name' => 'admin_replace[to]',
198                                                'value' => $post_args['to'])));
199         $header->pushContent(' '._("(no regex) Case-exact: "));
200         $checkbox = HTML::input(array('type' => 'checkbox',
201                                       'name' => 'admin_replace[caseexact]',
202                                       'value' => 1));
203         if ($post_args['caseexact'])
204             $checkbox->setAttr('checked','checked');
205         $header->pushContent($checkbox);
206         $header->pushContent(HTML::br());
207         return $header;
208     }
209 }
210
211 function stri_replace($find,$replace,$string) {
212     if (!is_array($find)) $find = array($find);
213     if (!is_array($replace))  {
214         if (!is_array($find)) 
215             $replace = array($replace);
216         else {
217             // this will duplicate the string into an array the size of $find
218             $c = count($find);
219             $rString = $replace;
220             unset($replace);
221             for ($i = 0; $i < $c; $i++) {
222                 $replace[$i] = $rString;
223             }
224         }
225     }
226     foreach ($find as $fKey => $fItem) {
227         $between = explode(strtolower($fItem),strtolower($string));
228         $pos = 0;
229         foreach($between as $bKey => $bItem) {
230             $between[$bKey] = substr($string,$pos,strlen($bItem));
231             $pos += strlen($bItem) + strlen($fItem);
232         }
233         $string = implode($replace[$fKey],$between);
234     }
235     return $string;
236 }
237
238 // $Log: not supported by cvs2svn $
239 // Revision 1.3  2004/02/12 17:05:39  rurban
240 // WikiAdminRename:
241 //   added "Change pagename in all linked pages also"
242 // PageList:
243 //   added javascript toggle for Select
244 // WikiAdminSearchReplace:
245 //   fixed another typo
246 //
247 // Revision 1.2  2004/02/12 11:47:51  rurban
248 // typo
249 //
250 // Revision 1.1  2004/02/12 11:25:53  rurban
251 // new WikiAdminSearchReplace plugin (requires currently Admin)
252 // removed dead comments from WikiDB
253 //
254 //
255
256 // Local Variables:
257 // mode: php
258 // tab-width: 8
259 // c-basic-offset: 4
260 // c-hanging-comment-ender-p: nil
261 // indent-tabs-mode: nil
262 // End:
263 ?>