]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSearchReplace.php
replaceHelper is public
[SourceForge/phpwiki.git] / lib / plugin / WikiAdminSearchReplace.php
1 <?php
2
3 /*
4  * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
5  * Copyright 2008-2009 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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 /**
25  * Usage:   <<WikiAdminSearchReplace >> or called via WikiAdminSelect
26  * Author:  Reini Urban <rurban@x-ray.at>
27  *
28  */
29 require_once 'lib/PageList.php';
30 require_once 'lib/plugin/WikiAdminSelect.php';
31
32 class WikiPlugin_WikiAdminSearchReplace
33     extends WikiPlugin_WikiAdminSelect
34 {
35     function getDescription()
36     {
37         return _("Search and replace text in selected wiki pages.");
38     }
39
40     function getDefaultArguments()
41     {
42         return array_merge
43         (
44             parent::getDefaultArguments(),
45             array(
46                 /* Columns to include in listing */
47                 'info' => 'some',
48             ));
49     }
50
51     function run($dbi, $argstr, &$request, $basepage)
52     {
53         // no action=replace support yet
54         if ($request->getArg('action') != 'browse') {
55             return $this->disabled(_("Plugin not run: not in browse mode"));
56         }
57
58         $args = $this->getArgs($argstr, $request);
59         $this->_args = $args;
60
61         //TODO: support p from <!plugin-list !>
62         $this->preSelectS($args, $request);
63
64         $p = $request->getArg('p');
65         if (!$p) {
66             $p = $this->_list;
67         }
68         $post_args = $request->getArg('admin_replace');
69         $next_action = 'select';
70         $pages = array();
71         if ($p && !$request->isPost()) {
72             $pages = $p;
73         }
74         if ($p && $request->isPost() && empty($post_args['cancel'])) {
75             // without individual PagePermissions:
76             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
77                 $request->_notAuthorized(WIKIAUTH_ADMIN);
78                 $this->disabled(_("You must be an administrator to use this plugin."));
79             }
80
81             if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
82                 // Real action
83                 return $this->searchReplacePages($dbi, $request, array_keys($p),
84                     $post_args['from'], $post_args['to']);
85             }
86             if ($post_args['action'] == 'select') {
87                 if (!empty($post_args['from']))
88                     $next_action = 'verify';
89                 foreach ($p as $name => $c) {
90                     $pages[$name] = 1;
91                 }
92             }
93         }
94         $result = HTML::div();
95         if ($request->isPost() and empty($post_args['from'])) {
96             $result->pushContent(HTML::p(array('class' => 'warning'),
97                                          _("Warning: The search string cannot be empty!")));
98         }
99         if ($next_action == 'select' and empty($pages)) {
100             // List all pages to select from.
101             //TODO: check for permissions and list only the allowed
102             $pages = $this->collectPages($pages, $dbi, $args['sortby'],
103                 $args['limit'], $args['exclude']);
104         }
105
106         $args['info'] = "checkbox,pagename,mtime,author";
107         if ($next_action == 'select') {
108             $columns = $args;
109         } else {
110            $columns = array_merge($args,
111                                   // with highlighted search for SearchReplace
112                                   array('types' => array('hi_content' 
113                     => new _PageList_Column_content('rev:hi_content', _("Content")))));
114         }
115
116         $header = HTML::fieldset();
117         $header->pushContent(HTML::legend(_("Select the pages to search and replace")));
118         if ($next_action == 'verify') {
119             $args['info'] = "pagename,mtime,author";
120             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $columns);
121             $pagelist->addPageList($pages);
122             $button_label = _("Replace");
123             $header->pushContent(
124                 HTML::p(HTML::strong(
125                     _("Are you sure you want to replace text in the selected files?"))));
126             $this->replaceForm($header, $post_args);
127         } else {
128             $pagelist = new PageList_Selectable($args['info'], $args['exclude'], $columns);
129             $pagelist->addPageList($pages);
130             $button_label = _("Search");
131             $this->replaceForm($header, $post_args);
132         }
133
134         $buttons = HTML::p(Button('submit:admin_replace[replace]', $button_label, 'wikiadmin'),
135             Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
136         $header->pushContent($buttons);
137
138         $result->pushContent(HTML::form(array('action' => $request->getPostURL(),
139                 'method' => 'post'),
140             $header,
141             $pagelist->getContent(),
142             HiddenInputs($request->getArgs(),
143                 false,
144                 array('admin_replace')),
145             HiddenInputs(array('admin_replace[action]' => $next_action)),
146             ENABLE_PAGEPERM
147                 ? ''
148                 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN))));
149         return $result;
150     }
151
152     public function replaceHelper(&$dbi, &$request, $pagename, $from, $to, $case_exact = true, $regex = false)
153     {
154         $page = $dbi->getPage($pagename);
155         if ($page->exists()) { // don't replace default contents
156             $current = $page->getCurrentRevision();
157             $version = $current->getVersion();
158             $text = $current->getPackedContent();
159             if ($regex) {
160                 $newtext = preg_replace("/" . $from . "/" . ($case_exact ? '' : 'i'), $to, $text);
161             } else {
162                 if ($case_exact) {
163                     $newtext = str_replace($from, $to, $text);
164                 } else {
165                     $newtext = str_ireplace($from, $to, $text);
166                 }
167             }
168             if ($text != $newtext) {
169                 $meta = $current->_data;
170                 $meta['summary'] = sprintf(_("Replace “%s” by “%s”"), $from, $to);
171                 $meta['is_minor_edit'] = 0;
172                 $meta['author'] = $request->_user->UserName();
173                 unset($meta['mtime']); // force new date
174                 return $page->save($newtext, $version + 1, $meta);
175             }
176         }
177         return false;
178     }
179
180     private function searchReplacePages(&$dbi, &$request, $pages, $from, $to)
181     {
182         $result = HTML::div();
183         $ul = HTML::ul();
184         $count = 0;
185         $post_args = $request->getArg('admin_replace');
186         $case_exact = !empty($post_args['case_exact']);
187         $regex = !empty($post_args['regex']);
188         foreach ($pages as $pagename) {
189             if (!mayAccessPage('edit', $pagename)) {
190                 $ul->pushContent(HTML::li(fmt("Access denied to change page “%s”.", $pagename)));
191             } elseif ($this->replaceHelper($dbi, $request, $pagename, $from, $to, $case_exact, $regex)) {
192                 $ul->pushContent(HTML::li(fmt("Replaced “%s” with “%s” in page “%s”.",
193                     $from, $to, WikiLink($pagename))));
194                 $count++;
195             }
196         }
197         if ($count) {
198             $dbi->touch();
199             $result->setAttr('class', 'feedback');
200             if ($count == 1) {
201                 $result->pushContent(HTML::p(_("One page has been changed:")));
202             } else {
203                 $result->pushContent(HTML::p(fmt("%d pages have been changed:", $count)));
204             }
205             $result->pushContent($ul);
206         } else {
207             $result->setAttr('class', 'error');
208             $result->pushContent(HTML::p(_("No pages changed.")));
209         }
210         return $result;
211     }
212
213     private function checkBox(&$post_args, $name, $msg)
214     {
215         $id = 'admin_replace-' . $name;
216         $checkbox = HTML::input(array('type' => 'checkbox',
217             'name' => 'admin_replace[' . $name . ']',
218             'id' => $id,
219             'value' => 1));
220         if (!empty($post_args[$name])) {
221             $checkbox->setAttr('checked', 'checked');
222         }
223         return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
224     }
225
226     private function replaceForm(&$header, $post_args)
227     {
228         $header->pushContent(HTML::p(array('class' => 'hint'),
229                 _("Replace all occurences of the given string in the content of all selected pages.")));
230         $table = HTML::table();
231         $this->tablePush($table, _("Replace") . _(": "),
232             HTML::input(array('name' => 'admin_replace[from]',
233                 'size' => 90,
234                 'value' => $post_args['from'])));
235         $this->tablePush($table, _("by") . _(": "),
236             HTML::input(array('name' => 'admin_replace[to]',
237                 'size' => 90,
238                 'value' => $post_args['to'])));
239         $this->tablePush($table, '', $this->checkBox($post_args, 'case_exact', _("Case exact?")));
240         $this->tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
241         $header->pushContent($table);
242         return $header;
243     }
244 }
245
246 // Local Variables:
247 // mode: php
248 // tab-width: 8
249 // c-basic-offset: 4
250 // c-hanging-comment-ender-p: nil
251 // indent-tabs-mode: nil
252 // End: