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