2 rcs_id('$Id: WikiAdminSearchReplace.php,v 1.3 2004-02-12 17:05:39 rurban Exp $');
4 Copyright 2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Usage: <?plugin WikiAdminSearchReplace ?> or called via WikiAdminSelect
25 * Author: Reini Urban <rurban@x-ray.at>
28 * Currently we must be Admin.
29 * Future versions will support PagePermissions.
30 * requires PHP 4.2 so far.
32 require_once('lib/PageList.php');
33 require_once('lib/plugin/WikiAdminSelect.php');
35 class WikiPlugin_WikiAdminSearchReplace
36 extends WikiPlugin_WikiAdminSelect
39 return _("WikiAdminSearchReplace");
42 function getDescription() {
43 return _("Search and replace text in selected wiki pages.");
46 function getVersion() {
47 return preg_replace("/[Revision: $]/", '',
51 function getDefaultArguments() {
53 /* Pages to exclude */
55 /* Columns to include in listing */
58 'sortby' => 'pagename',
62 function replaceHelper(&$dbi, $pagename, $from, $to, $caseexact = true) {
63 $page = $dbi->getPage($pagename);
64 if ($page->exists()) {// don't replace default contents
65 $current = $page->getCurrentRevision();
66 $version = $current->getVersion();
67 $text = $current->getPackedContent();
69 $newtext = str_replace($from, $to, $text);
71 //not all PHP have this enabled. use a workaround
72 if (function_exists('str_ireplace'))
73 $newtext = str_ireplace($from, $to, $text);
75 $newtext = stri_replace($from, $to, $text);
78 if ($text != $newtext) {
79 $meta = $current->_data;
80 $meta['summary'] = sprintf(_("WikiAdminSearchReplace %s by %s"),$from,$to);
81 return $page->save($newtext, $version + 1, $meta);
87 function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
88 if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
91 $post_args = $request->getArg('admin_replace');
92 $caseexact = $post_args['caseexact'] == 1;
93 foreach ($pages as $pagename) {
94 if (($result = $this->replaceHelper(&$dbi,$pagename,$from,$to,$caseexact))) {
95 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", $from, $to, WikiLink($pagename))));
98 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in page '%s'.", $from, $to, WikiLink($pagename))));
104 HTML::p(fmt("%s pages changed.",$count)));
107 HTML::p(fmt("No pages changed.")));
111 function run($dbi, $argstr, $request) {
112 if ($request->getArg('action') != 'browse')
113 return $this->disabled("(action != 'browse')");
115 $args = $this->getArgs($argstr, $request);
116 $this->_args = $args;
117 if (!empty($args['exclude']))
118 $exclude = explodePageList($args['exclude']);
123 $p = $request->getArg('p');
124 $post_args = $request->getArg('admin_replace');
125 $next_action = 'select';
128 if ($p && $request->isPost() && $request->_user->isAdmin()
129 && !empty($post_args['rename']) && empty($post_args['cancel'])) {
130 // FIXME: error message if not admin.
131 if ($post_args['action'] == 'verify') {
133 return $this->searchReplacePages($dbi, $request, $p, $post_args['from'], $post_args['to']);
136 if ($post_args['action'] == 'select') {
137 if (!empty($post_args['from']))
138 $next_action = 'verify';
139 foreach ($p as $name) {
144 if ($next_action == 'select') {
145 // List all pages to select from.
146 $list = $this->collectPages($pages, $dbi, $args['sortby']);
152 $info .= "," . $args['info'];
153 if ($next_action == 'verify') {
154 $info = "checkbox,pagename,content";
156 $pagelist = new PageList_Selectable($info, $exclude);
157 $pagelist->addPageList($pages);
160 if (empty($post_args['from']))
161 $header->pushContent(
162 HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
163 if ($next_action == 'verify') {
164 $button_label = _("Yes");
165 $header->pushContent(
166 HTML::p(HTML::strong(
167 _("Are you sure you want to permanently search & replace text in the selected files?"))));
168 $this->replaceForm(&$header, $post_args);
171 $button_label = _("Search & Replace");
172 $this->replaceForm(&$header, $post_args);
173 $header->pushContent(HTML::p(_("Select the pages to search:")));
177 $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
178 Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
180 return HTML::form(array('action' => $request->getPostURL(),
183 $pagelist->getContent(),
184 HiddenInputs($request->getArgs(),
186 array('admin_replace')),
187 HiddenInputs(array('admin_replace[action]' => $next_action,
188 'require_authority_for_post' => WIKIAUTH_ADMIN)),
192 function replaceForm(&$header, $post_args) {
193 $header->pushContent(_("Replace: "));
194 $header->pushContent(HTML::input(array('name' => 'admin_replace[from]',
195 'value' => $post_args['from'])));
196 $header->pushContent(' '._("by").': ');
197 $header->pushContent(HTML::input(array('name' => 'admin_replace[to]',
198 'value' => $post_args['to'])));
199 $header->pushContent(' '._("(no regex) Case-exact: "));
200 $checkbox = HTML::input(array('type' => 'checkbox',
201 'name' => 'admin_replace[caseexact]',
203 if ($post_args['caseexact'])
204 $checkbox->setAttr('checked','checked');
205 $header->pushContent($checkbox);
206 $header->pushContent(HTML::br());
211 function stri_replace($find,$replace,$string) {
212 if (!is_array($find)) $find = array($find);
213 if (!is_array($replace)) {
214 if (!is_array($find))
215 $replace = array($replace);
217 // this will duplicate the string into an array the size of $find
221 for ($i = 0; $i < $c; $i++) {
222 $replace[$i] = $rString;
226 foreach ($find as $fKey => $fItem) {
227 $between = explode(strtolower($fItem),strtolower($string));
229 foreach($between as $bKey => $bItem) {
230 $between[$bKey] = substr($string,$pos,strlen($bItem));
231 $pos += strlen($bItem) + strlen($fItem);
233 $string = implode($replace[$fKey],$between);
238 // $Log: not supported by cvs2svn $
239 // Revision 1.2 2004/02/12 11:47:51 rurban
242 // Revision 1.1 2004/02/12 11:25:53 rurban
243 // new WikiAdminSearchReplace plugin (requires currently Admin)
244 // removed dead comments from WikiDB
252 // c-hanging-comment-ender-p: nil
253 // indent-tabs-mode: nil