2 // $Id: WikiAdminSearchReplace.php 7955 2011-03-03 16:41:35Z vargenau $
4 * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Usage: <<WikiAdminSearchReplace >> or called via WikiAdminSelect
26 * Author: Reini Urban <rurban@x-ray.at>
29 require_once('lib/PageList.php');
30 require_once('lib/plugin/WikiAdminSelect.php');
32 class WikiPlugin_WikiAdminSearchReplace
33 extends WikiPlugin_WikiAdminSelect
36 return _("WikiAdminSearchReplace");
39 function getDescription() {
40 return _("Search and replace text in selected wiki pages.");
43 function getDefaultArguments() {
46 WikiPlugin_WikiAdminSelect::getDefaultArguments(),
48 /* Columns to include in listing */
53 function replaceHelper(&$dbi, &$request, $pagename, $from, $to, $case_exact=true, $regex=false) {
54 $page = $dbi->getPage($pagename);
55 if ($page->exists()) {// don't replace default contents
56 $current = $page->getCurrentRevision();
57 $version = $current->getVersion();
58 $text = $current->getPackedContent();
60 $newtext = preg_replace("/".$from."/".($case_exact?'':'i'), $to, $text);
63 $newtext = str_replace($from, $to, $text);
65 //not all PHP have this enabled. use a workaround
66 if (function_exists('str_ireplace'))
67 $newtext = str_ireplace($from, $to, $text);
69 $newtext = stri_replace($from, $to, $text);
73 if ($text != $newtext) {
74 $meta = $current->_data;
75 $meta['summary'] = sprintf(_("Replace '%s' by '%s'"), $from, $to);
76 $meta['is_minor_edit'] = 0;
77 $meta['author'] = $request->_user->UserName();
78 unset($meta['mtime']); // force new date
79 return $page->save($newtext, $version + 1, $meta);
85 function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
86 if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
87 $result = HTML::div();
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))));
104 $result->setAttr('class', 'feedback');
106 $result->pushContent(HTML::p(_("One page has been changed:")));
108 $result->pushContent(HTML::p(fmt("%d pages have been changed:", $count)));
110 $result->pushContent($ul);
112 $result->setAttr('class', 'error');
113 $result->pushContent(HTML::p(_("No pages changed.")));
118 function run($dbi, $argstr, &$request, $basepage) {
119 // no action=replace support yet
120 if ($request->getArg('action') != 'browse')
121 return $this->disabled("(action != 'browse')");
123 $args = $this->getArgs($argstr, $request);
124 $this->_args = $args;
126 //TODO: support p from <!plugin-list !>
127 $this->preSelectS($args, $request);
129 $p = $request->getArg('p');
130 if (!$p) $p = $this->_list;
131 $post_args = $request->getArg('admin_replace');
132 $next_action = 'select';
134 if ($p && !$request->isPost())
136 if ($p && $request->isPost() &&
137 empty($post_args['cancel'])) {
138 // without individual PagePermissions:
139 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
140 $request->_notAuthorized(WIKIAUTH_ADMIN);
141 $this->disabled("! user->isAdmin");
144 if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
146 return $this->searchReplacePages($dbi, $request, array_keys($p),
147 $post_args['from'], $post_args['to']);
149 if ($post_args['action'] == 'select') {
150 if (!empty($post_args['from']))
151 $next_action = 'verify';
152 foreach ($p as $name => $c) {
157 if ($next_action == 'select' and empty($pages)) {
158 // List all pages to select from.
159 //TODO: check for permissions and list only the allowed
160 $pages = $this->collectPages($pages, $dbi, $args['sortby'],
161 $args['limit'], $args['exclude']);
164 if ($next_action == 'verify') {
165 $args['info'] = "checkbox,pagename";
167 $args['info'] = "checkbox,pagename,hi_content,mtime,author";
169 $pagelist = new PageList_Selectable
170 ($args['info'], $args['exclude'],
174 array('types' => array
176 'hi_content' // with highlighted search for SearchReplace
177 => new _PageList_Column_content('rev:hi_content', _("Content"))))));
179 $pagelist->addPageList($pages);
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);
192 $button_label = _("Search & Replace");
193 $this->replaceForm($header, $post_args);
194 $header->pushContent(HTML::legend(_("Select the pages to search and replace")));
197 $buttons = HTML::p(Button('submit:admin_replace[replace]', $button_label, 'wikiadmin'),
198 Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
199 $header->pushContent($buttons);
201 return HTML::form(array('action' => $request->getPostURL(),
204 $pagelist->getContent(),
205 HiddenInputs($request->getArgs(),
207 array('admin_replace')),
208 HiddenInputs(array('admin_replace[action]' => $next_action)),
211 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
214 function checkBox (&$post_args, $name, $msg) {
215 $id = 'admin_replace-'.$name;
216 $checkbox = HTML::input(array('type' => 'checkbox',
217 'name' => 'admin_replace['.$name.']',
220 if (!empty($post_args[$name]))
221 $checkbox->setAttr('checked', 'checked');
222 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
225 function replaceForm(&$header, $post_args) {
226 $header->pushContent(HTML::div(array('class'=>'hint'),
227 _("Replace all occurences of the given string in the content of all pages.")),
229 $table = HTML::table();
230 $this->_tablePush($table, _("Replace")._(": "),
231 HTML::input(array('name' => 'admin_replace[from]',
233 'value' => $post_args['from'])));
234 $this->_tablePush($table, _("by")._(": "),
235 HTML::input(array('name' => 'admin_replace[to]',
237 'value' => $post_args['to'])));
238 $this->_tablePush($table, '', $this->checkBox($post_args, 'case_exact', _("Case exact?")));
239 $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
240 $header->pushContent($table);
245 function stri_replace($find,$replace,$string) {
246 if (!is_array($find)) $find = array($find);
247 if (!is_array($replace)) {
248 if (!is_array($find))
249 $replace = array($replace);
251 // this will duplicate the string into an array the size of $find
255 for ($i = 0; $i < $c; $i++) {
256 $replace[$i] = $rString;
260 foreach ($find as $fKey => $fItem) {
261 $between = explode(strtolower($fItem),strtolower($string));
263 foreach($between as $bKey => $bItem) {
264 $between[$bKey] = substr($string,$pos,strlen($bItem));
265 $pos += strlen($bItem) + strlen($fItem);
267 $string = implode($replace[$fKey],$between);
276 // c-hanging-comment-ender-p: nil
277 // indent-tabs-mode: nil