2 rcs_id('$Id: WikiAdminSearchReplace.php,v 1.8 2004-03-17 20:23:44 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',
63 function replaceHelper(&$dbi, $pagename, $from, $to, $caseexact = true) {
64 $page = $dbi->getPage($pagename);
65 if ($page->exists()) {// don't replace default contents
66 $current = $page->getCurrentRevision();
67 $version = $current->getVersion();
68 $text = $current->getPackedContent();
70 $newtext = str_replace($from, $to, $text);
72 //not all PHP have this enabled. use a workaround
73 if (function_exists('str_ireplace'))
74 $newtext = str_ireplace($from, $to, $text);
76 $newtext = stri_replace($from, $to, $text);
79 if ($text != $newtext) {
80 $meta = $current->_data;
81 $meta['summary'] = sprintf(_("WikiAdminSearchReplace %s by %s"),$from,$to);
82 return $page->save($newtext, $version + 1, $meta);
88 function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
89 if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
92 $post_args = $request->getArg('admin_replace');
93 $caseexact = !empty($post_args['caseexact']);
94 foreach ($pages as $pagename) {
95 if (($result = $this->replaceHelper(&$dbi,$pagename,$from,$to,$caseexact))) {
96 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", $from, $to, WikiLink($pagename))));
99 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in page '%s'.", $from, $to, WikiLink($pagename))));
105 HTML::p(fmt("%s pages changed.",$count)));
108 HTML::p(fmt("No pages changed.")));
112 function run($dbi, $argstr, &$request, $basepage) {
113 if ($request->getArg('action') != 'browse')
114 return $this->disabled("(action != 'browse')");
116 $args = $this->getArgs($argstr, $request);
117 $this->_args = $args;
118 if (!empty($args['exclude']))
119 $exclude = explodePageList($args['exclude']);
124 $p = $request->getArg('p');
125 $post_args = $request->getArg('admin_replace');
126 $next_action = 'select';
128 if ($p && !$request->isPost())
130 if ($p && $request->isPost() &&
131 empty($post_args['cancel'])) {
133 // FIXME: check individual PagePermissions
134 if (!$request->_user->isAdmin()) {
135 $request->_notAuthorized(WIKIAUTH_ADMIN);
136 $this->disabled("! user->isAdmin");
139 if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
141 return $this->searchReplacePages($dbi, $request, array_keys($p), $post_args['from'], $post_args['to']);
143 if ($post_args['action'] == 'select') {
144 if (!empty($post_args['from']))
145 $next_action = 'verify';
146 foreach ($p as $name => $c) {
151 if ($next_action == 'select' and empty($pages)) {
152 // List all pages to select from.
153 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
156 if ($next_action == 'verify') {
157 $args['info'] = "checkbox,pagename,hi_content";
159 $pagelist = new PageList_Selectable($args['info'], $exclude);
160 $pagelist->addPageList($pages);
163 if (empty($post_args['from']))
164 $header->pushContent(
165 HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
166 if ($next_action == 'verify') {
167 $button_label = _("Yes");
168 $header->pushContent(
169 HTML::p(HTML::strong(
170 _("Are you sure you want to permanently search & replace text in the selected files?"))));
171 $this->replaceForm(&$header, $post_args);
174 $button_label = _("Search & Replace");
175 $this->replaceForm(&$header, $post_args);
176 $header->pushContent(HTML::p(_("Select the pages to search:")));
180 $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
181 Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
183 return HTML::form(array('action' => $request->getPostURL(),
186 $pagelist->getContent(),
187 HiddenInputs($request->getArgs(),
189 array('admin_replace')),
190 HiddenInputs(array('admin_replace[action]' => $next_action,
191 'require_authority_for_post' => WIKIAUTH_ADMIN)),
195 function replaceForm(&$header, $post_args) {
196 $header->pushContent(_("Replace: "));
197 $header->pushContent(HTML::input(array('name' => 'admin_replace[from]',
198 'value' => $post_args['from'])));
199 $header->pushContent(' '._("by").': ');
200 $header->pushContent(HTML::input(array('name' => 'admin_replace[to]',
201 'value' => $post_args['to'])));
202 $header->pushContent(' '._("(no regex) Case-exact: "));
203 $checkbox = HTML::input(array('type' => 'checkbox',
204 'name' => 'admin_replace[caseexact]',
206 if (!empty($post_args['caseexact']))
207 $checkbox->setAttr('checked','checked');
208 $header->pushContent($checkbox);
209 $header->pushContent(HTML::br());
214 function stri_replace($find,$replace,$string) {
215 if (!is_array($find)) $find = array($find);
216 if (!is_array($replace)) {
217 if (!is_array($find))
218 $replace = array($replace);
220 // this will duplicate the string into an array the size of $find
224 for ($i = 0; $i < $c; $i++) {
225 $replace[$i] = $rString;
229 foreach ($find as $fKey => $fItem) {
230 $between = explode(strtolower($fItem),strtolower($string));
232 foreach($between as $bKey => $bItem) {
233 $between[$bKey] = substr($string,$pos,strlen($bItem));
234 $pos += strlen($bItem) + strlen($fItem);
236 $string = implode($replace[$fKey],$between);
241 // $Log: not supported by cvs2svn $
242 // Revision 1.7 2004/03/12 13:31:43 rurban
243 // enforce PagePermissions, errormsg if not Admin
245 // Revision 1.6 2004/02/24 15:20:07 rurban
246 // fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
248 // Revision 1.5 2004/02/17 12:11:36 rurban
249 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
251 // Revision 1.4 2004/02/15 21:34:37 rurban
252 // PageList enhanced and improved.
253 // fixed new WikiAdmin... plugins
254 // editpage, Theme with exp. htmlarea framework
255 // (htmlarea yet committed, this is really questionable)
256 // WikiUser... code with better session handling for prefs
257 // enhanced UserPreferences (again)
258 // RecentChanges for show_deleted: how should pages be deleted then?
260 // Revision 1.3 2004/02/12 17:05:39 rurban
262 // added "Change pagename in all linked pages also"
264 // added javascript toggle for Select
265 // WikiAdminSearchReplace:
266 // fixed another typo
268 // Revision 1.2 2004/02/12 11:47:51 rurban
271 // Revision 1.1 2004/02/12 11:25:53 rurban
272 // new WikiAdminSearchReplace plugin (requires currently Admin)
273 // removed dead comments from WikiDB
281 // c-hanging-comment-ender-p: nil
282 // indent-tabs-mode: nil