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