]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WikiAdminSearchReplace.php
getName should not translate
[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             WikiPlugin_WikiAdminSelect::getDefaultArguments(),
45             array(
46                 /* Columns to include in listing */
47                 'info' => 'some',
48             ));
49     }
50
51     private function replaceHelper(&$dbi, &$request, $pagename, $from, $to, $case_exact = true, $regex = false)
52     {
53         $page = $dbi->getPage($pagename);
54         if ($page->exists()) { // don't replace default contents
55             $current = $page->getCurrentRevision();
56             $version = $current->getVersion();
57             $text = $current->getPackedContent();
58             if ($regex) {
59                 $newtext = preg_replace("/" . $from . "/" . ($case_exact ? '' : 'i'), $to, $text);
60             } else {
61                 if ($case_exact) {
62                     $newtext = str_replace($from, $to, $text);
63                 } else {
64                     $newtext = str_ireplace($from, $to, $text);
65                 }
66             }
67             if ($text != $newtext) {
68                 $meta = $current->_data;
69                 $meta['summary'] = sprintf(_("Replace “%s” by “%s”"), $from, $to);
70                 $meta['is_minor_edit'] = 0;
71                 $meta['author'] = $request->_user->UserName();
72                 unset($meta['mtime']); // force new date
73                 return $page->save($newtext, $version + 1, $meta);
74             }
75         }
76         return false;
77     }
78
79     private function searchReplacePages(&$dbi, &$request, $pages, $from, $to)
80     {
81         if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
82         $result = HTML::div();
83         $ul = HTML::ul();
84         $count = 0;
85         $post_args = $request->getArg('admin_replace');
86         $case_exact = !empty($post_args['case_exact']);
87         $regex = !empty($post_args['regex']);
88         foreach ($pages as $pagename) {
89             if (!mayAccessPage('edit', $pagename)) {
90                 $ul->pushContent(HTML::li(fmt("Access denied to change page “%s”.", $pagename)));
91             } elseif ($this->replaceHelper($dbi, $request, $pagename, $from, $to, $case_exact, $regex)) {
92                 $ul->pushContent(HTML::li(fmt("Replaced “%s” with “%s” in page “%s”.",
93                     $from, $to, WikiLink($pagename))));
94                 $count++;
95             }
96         }
97         if ($count) {
98             $dbi->touch();
99             $result->setAttr('class', 'feedback');
100             if ($count == 1) {
101                 $result->pushContent(HTML::p(_("One page has been changed:")));
102             } else {
103                 $result->pushContent(HTML::p(fmt("%d pages have been changed:", $count)));
104             }
105             $result->pushContent($ul);
106         } else {
107             $result->setAttr('class', 'error');
108             $result->pushContent(HTML::p(_("No pages changed.")));
109         }
110         return $result;
111     }
112
113     function run($dbi, $argstr, &$request, $basepage)
114     {
115         // no action=replace support yet
116         if ($request->getArg('action') != 'browse') {
117             return $this->disabled(_("Plugin not run: not in browse mode"));
118         }
119
120         $args = $this->getArgs($argstr, $request);
121         $this->_args = $args;
122
123         //TODO: support p from <!plugin-list !>
124         $this->preSelectS($args, $request);
125
126         $p = $request->getArg('p');
127         if (!$p) $p = $this->_list;
128         $post_args = $request->getArg('admin_replace');
129         $next_action = 'select';
130         $pages = array();
131         if ($p && !$request->isPost())
132             $pages = $p;
133         if ($p && $request->isPost() &&
134             empty($post_args['cancel'])
135         ) {
136             // without individual PagePermissions:
137             if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
138                 $request->_notAuthorized(WIKIAUTH_ADMIN);
139                 $this->disabled("! user->isAdmin");
140             }
141
142             if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
143                 // Real action
144                 return $this->searchReplacePages($dbi, $request, array_keys($p),
145                     $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'],
159                 $args['limit'], $args['exclude']);
160         }
161
162         if ($next_action == 'verify') {
163             $args['info'] = "checkbox,pagename";
164         } else {
165             // Avoid warning about "hi_content"
166             // $args['info'] = "checkbox,pagename,hi_content,mtime,author";
167             $args['info'] = "checkbox,pagename,mtime,author";
168         }
169         $pagelist = new PageList_Selectable
170         ($args['info'], $args['exclude'],
171             array_merge
172             (
173                 $args,
174                 array('types' => array
175                 (
176                     'hi_content' // with highlighted search for SearchReplace
177                     => new _PageList_Column_content('rev:hi_content', _("Content"))))));
178
179         $pagelist->addPageList($pages);
180
181         $header = HTML::fieldset();
182         if (empty($post_args['from']))
183             $header->pushContent(
184                 HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
185         if ($next_action == 'verify') {
186             $button_label = _("Yes");
187             $header->pushContent(
188                 HTML::p(HTML::strong(
189                     _("Are you sure you want to replace text in the selected files?"))));
190             $this->replaceForm($header, $post_args);
191         } else {
192             $button_label = _("Search & Replace");
193             $this->replaceForm($header, $post_args);
194             $header->pushContent(HTML::legend(_("Select the pages to search and replace")));
195         }
196
197         $buttons = HTML::p(Button('submit:admin_replace[replace]', $button_label, 'wikiadmin'),
198             Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
199         $header->pushContent($buttons);
200
201         return HTML::form(array('action' => $request->getPostURL(),
202                 'method' => 'post'),
203             $header,
204             $pagelist->getContent(),
205             HiddenInputs($request->getArgs(),
206                 false,
207                 array('admin_replace')),
208             HiddenInputs(array('admin_replace[action]' => $next_action)),
209             ENABLE_PAGEPERM
210                 ? ''
211                 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
212     }
213
214     private function checkBox(&$post_args, $name, $msg)
215     {
216         $id = 'admin_replace-' . $name;
217         $checkbox = HTML::input(array('type' => 'checkbox',
218             'name' => 'admin_replace[' . $name . ']',
219             'id' => $id,
220             'value' => 1));
221         if (!empty($post_args[$name]))
222             $checkbox->setAttr('checked', 'checked');
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::div(array('class' => 'hint'),
229                 _("Replace all occurences of the given string in the content of all pages.")),
230             HTML::br());
231         $table = HTML::table();
232         $this->tablePush($table, _("Replace") . _(": "),
233             HTML::input(array('name' => 'admin_replace[from]',
234                 'size' => 90,
235                 'value' => $post_args['from'])));
236         $this->tablePush($table, _("by") . _(": "),
237             HTML::input(array('name' => 'admin_replace[to]',
238                 'size' => 90,
239                 'value' => $post_args['to'])));
240         $this->tablePush($table, '', $this->checkBox($post_args, 'case_exact', _("Case exact?")));
241         $this->tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
242         $header->pushContent($table);
243         return $header;
244     }
245 }
246
247 // Local Variables:
248 // mode: php
249 // tab-width: 8
250 // c-basic-offset: 4
251 // c-hanging-comment-ender-p: nil
252 // indent-tabs-mode: nil
253 // End: