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
20 along with PhpWiki; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Usage: <?plugin WikiAdminSearchReplace ?> or called via WikiAdminSelect
26 * Author: Reini Urban <rurban@x-ray.at>
29 * Requires PHP 4.2 so far.
31 require_once('lib/PageList.php');
32 require_once('lib/plugin/WikiAdminSelect.php');
34 class WikiPlugin_WikiAdminSearchReplace
35 extends WikiPlugin_WikiAdminSelect
38 return _("WikiAdminSearchReplace");
41 function getDescription() {
42 return _("Search and replace text in selected wiki pages.");
45 function getVersion() {
46 return preg_replace("/[Revision: $]/", '',
50 function getDefaultArguments() {
53 PageList::supportedArgs(),
56 /* Columns to include in listing */
61 function replaceHelper(&$dbi, &$request, $pagename, $from, $to, $case_exact=true, $regex=false) {
62 $page = $dbi->getPage($pagename);
63 if ($page->exists()) {// don't replace default contents
64 $current = $page->getCurrentRevision();
65 $version = $current->getVersion();
66 $text = $current->getPackedContent();
68 $newtext = preg_replace("/".$from."/".($case_exact?'':'i'), $to, $text);
71 $newtext = str_replace($from, $to, $text);
73 //not all PHP have this enabled. use a workaround
74 if (function_exists('str_ireplace'))
75 $newtext = str_ireplace($from, $to, $text);
77 $newtext = stri_replace($from, $to, $text);
81 if ($text != $newtext) {
82 $meta = $current->_data;
83 $meta['summary'] = sprintf(_("Replace '%s' by '%s'"), $from, $to);
84 $meta['is_minor_edit'] = 0;
85 $meta['author'] = $request->_user->UserName();
86 unset($meta['mtime']); // force new date
87 return $page->save($newtext, $version + 1, $meta);
93 function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
94 if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
97 $post_args = $request->getArg('admin_replace');
98 $case_exact = !empty($post_args['case_exact']);
99 $regex = !empty($post_args['regex']);
100 foreach ($pages as $pagename) {
101 if (!mayAccessPage('edit', $pagename)) {
102 $ul->pushContent(HTML::li(fmt("Access denied to change page '%s'.",$pagename)));
103 } elseif (($result = $this->replaceHelper($dbi, $request, $pagename, $from, $to,
104 $case_exact, $regex)))
106 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.",
107 $from, $to, WikiLink($pagename))));
110 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in content of page '%s'.",
111 $from, WikiLink($pagename))));
117 HTML::p(fmt("%s pages changed.",$count)));
120 HTML::p(fmt("No pages changed.")));
124 function run($dbi, $argstr, &$request, $basepage) {
125 // no action=replace support yet
126 if ($request->getArg('action') != 'browse')
127 return $this->disabled("(action != 'browse')");
129 $args = $this->getArgs($argstr, $request);
130 $this->_args = $args;
132 //TODO: support p from <!plugin-list !>
133 $this->preSelectS($args, $request);
135 $p = $request->getArg('p');
136 if (!$p) $p = $this->_list;
137 $post_args = $request->getArg('admin_replace');
138 $next_action = 'select';
140 if ($p && !$request->isPost())
142 if ($p && $request->isPost() &&
143 empty($post_args['cancel'])) {
144 // without individual PagePermissions:
145 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
146 $request->_notAuthorized(WIKIAUTH_ADMIN);
147 $this->disabled("! user->isAdmin");
150 if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
152 return $this->searchReplacePages($dbi, $request, array_keys($p),
153 $post_args['from'], $post_args['to']);
155 if ($post_args['action'] == 'select') {
156 if (!empty($post_args['from']))
157 $next_action = 'verify';
158 foreach ($p as $name => $c) {
163 if ($next_action == 'select' and empty($pages)) {
164 // List all pages to select from.
165 //TODO: check for permissions and list only the allowed
166 $pages = $this->collectPages($pages, $dbi, $args['sortby'],
167 $args['limit'], $args['exclude']);
170 if ($next_action == 'verify') {
171 $args['info'] = "checkbox,pagename,hi_content";
173 $pagelist = new PageList_Selectable
174 ($args['info'], $args['exclude'],
178 array('types' => array
180 'hi_content' // with highlighted search for SearchReplace
181 => new _PageList_Column_content('rev:hi_content', _("Content"))))));
183 $pagelist->addPageList($pages);
185 $header = HTML::div();
186 if (empty($post_args['from']))
187 $header->pushContent(
188 HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
189 if ($next_action == 'verify') {
190 $button_label = _("Yes");
191 $header->pushContent(
192 HTML::p(HTML::strong(
193 _("Are you sure you want to permanently search & replace text in the selected files?"))));
194 $this->replaceForm($header, $post_args);
197 $button_label = _("Search & Replace");
198 $this->replaceForm($header, $post_args);
199 $header->pushContent(HTML::p(_("Select the pages to search:")));
203 $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
204 Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
206 return HTML::form(array('action' => $request->getPostURL(),
210 $pagelist->getContent(),
211 HiddenInputs($request->getArgs(),
213 array('admin_replace')),
214 HiddenInputs(array('admin_replace[action]' => $next_action)),
217 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)));
220 function checkBox (&$post_args, $name, $msg) {
221 $id = 'admin_replace-'.$name;
222 $checkbox = HTML::input(array('type' => 'checkbox',
223 'name' => 'admin_replace['.$name.']',
226 if (!empty($post_args[$name]))
227 $checkbox->setAttr('checked', 'checked');
228 return HTML::div($checkbox, ' ', HTML::label(array('for' => $id), $msg));
231 function replaceForm(&$header, $post_args) {
232 $header->pushContent(HTML::div(array('class'=>'hint'),
233 _("Replace all occurences of the given string in the content of all pages.")),
235 $table = HTML::table();
236 $this->_tablePush($table, _("Replace").": ",
237 HTML::input(array('name' => 'admin_replace[from]',
239 'value' => $post_args['from'])));
240 $this->_tablePush($table, _("by").': ',
241 HTML::input(array('name' => 'admin_replace[to]',
243 'value' => $post_args['to'])));
244 $this->_tablePush($table, '', $this->checkBox($post_args, 'case_exact', _("Case exact?")));
245 $this->_tablePush($table, '', $this->checkBox($post_args, 'regex', _("Regex?")));
246 $header->pushContent($table);
247 $header->pushContent(HTML::br());
252 function stri_replace($find,$replace,$string) {
253 if (!is_array($find)) $find = array($find);
254 if (!is_array($replace)) {
255 if (!is_array($find))
256 $replace = array($replace);
258 // this will duplicate the string into an array the size of $find
262 for ($i = 0; $i < $c; $i++) {
263 $replace[$i] = $rString;
267 foreach ($find as $fKey => $fItem) {
268 $between = explode(strtolower($fItem),strtolower($string));
270 foreach($between as $bKey => $bItem) {
271 $between[$bKey] = substr($string,$pos,strlen($bItem));
272 $pos += strlen($bItem) + strlen($fItem);
274 $string = implode($replace[$fKey],$between);
283 // c-hanging-comment-ender-p: nil
284 // indent-tabs-mode: nil