2 rcs_id('$Id: WikiAdminSearchReplace.php,v 1.12 2004-06-08 10:05:12 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: $]/", '',
48 "\$Revision: 1.12 $");
51 function getDefaultArguments() {
54 /* Pages to exclude */
56 /* Columns to include in listing */
59 'sortby' => 'pagename',
64 function replaceHelper(&$dbi, $pagename, $from, $to, $caseexact = true) {
65 $page = $dbi->getPage($pagename);
66 if ($page->exists()) {// don't replace default contents
67 $current = $page->getCurrentRevision();
68 $version = $current->getVersion();
69 $text = $current->getPackedContent();
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);
80 if ($text != $newtext) {
81 $meta = $current->_data;
82 $meta['summary'] = sprintf(_("WikiAdminSearchReplace %s by %s"),$from,$to);
83 return $page->save($newtext, $version + 1, $meta);
89 function searchReplacePages(&$dbi, &$request, $pages, $from, $to) {
90 if (empty($from)) return HTML::p(HTML::strong(fmt("Error: Empty search string.")));
93 $post_args = $request->getArg('admin_replace');
94 $caseexact = !empty($post_args['caseexact']);
95 foreach ($pages as $pagename) {
96 if (($result = $this->replaceHelper(&$dbi,$pagename,$from,$to,$caseexact))) {
97 $ul->pushContent(HTML::li(fmt("Replaced '%s' with '%s' in page '%s'.", $from, $to, WikiLink($pagename))));
100 $ul->pushContent(HTML::li(fmt("Search string '%s' not found in page '%s'.", $from, $to, WikiLink($pagename))));
106 HTML::p(fmt("%s pages changed.",$count)));
109 HTML::p(fmt("No pages changed.")));
113 function run($dbi, $argstr, &$request, $basepage) {
114 // no action=replace support yet
115 if ($request->getArg('action') != 'browse')
116 return $this->disabled("(action != 'browse')");
118 $args = $this->getArgs($argstr, $request);
119 $this->_args = $args;
120 if (!empty($args['exclude']))
121 $exclude = explodePageList($args['exclude']);
124 $this->preSelectS(&$args, &$request);
126 $p = $request->getArg('p');
127 if (!$p) $p = $this->_list;
128 $post_args = $request->getArg('admin_replace');
129 $next_action = 'select';
131 if ($p && !$request->isPost())
133 if ($p && $request->isPost() &&
134 empty($post_args['cancel'])) {
135 // without individual PagePermissions:
136 if (!ENABLE_PAGEPERM and !$request->_user->isAdmin()) {
137 $request->_notAuthorized(WIKIAUTH_ADMIN);
138 $this->disabled("! user->isAdmin");
141 if ($post_args['action'] == 'verify' and !empty($post_args['from'])) {
143 return $this->searchReplacePages($dbi, $request, array_keys($p), $post_args['from'], $post_args['to']);
145 if ($post_args['action'] == 'select') {
146 if (!empty($post_args['from']))
147 $next_action = 'verify';
148 foreach ($p as $name => $c) {
153 if ($next_action == 'select' and empty($pages)) {
154 // List all pages to select from.
155 $pages = $this->collectPages($pages, $dbi, $args['sortby'], $args['limit']);
158 if ($next_action == 'verify') {
159 $args['info'] = "checkbox,pagename,hi_content";
161 $pagelist = new PageList_Selectable($args['info'], $exclude,
162 array('types' => array(
163 'hi_content' // with highlighted search for SearchReplace
164 => new _PageList_Column_content('rev:hi_content', _("Content")))));
166 $pagelist->addPageList($pages);
169 if (empty($post_args['from']))
170 $header->pushContent(
171 HTML::p(HTML::em(_("Warning: The search string cannot be empty!"))));
172 if ($next_action == 'verify') {
173 $button_label = _("Yes");
174 $header->pushContent(
175 HTML::p(HTML::strong(
176 _("Are you sure you want to permanently search & replace text in the selected files?"))));
177 $this->replaceForm(&$header, $post_args);
180 $button_label = _("Search & Replace");
181 $this->replaceForm(&$header, $post_args);
182 $header->pushContent(HTML::p(_("Select the pages to search:")));
186 $buttons = HTML::p(Button('submit:admin_replace[rename]', $button_label, 'wikiadmin'),
187 Button('submit:admin_replace[cancel]', _("Cancel"), 'button'));
189 return HTML::form(array('action' => $request->getPostURL(),
192 $pagelist->getContent(),
193 HiddenInputs($request->getArgs(),
195 array('admin_replace')),
196 HiddenInputs(array('admin_replace[action]' => $next_action)),
199 : HiddenInputs(array('require_authority_for_post' => WIKIAUTH_ADMIN)),
203 function replaceForm(&$header, $post_args) {
204 $header->pushContent(_("Replace: "));
205 $header->pushContent(HTML::input(array('name' => 'admin_replace[from]',
206 'value' => $post_args['from'])));
207 $header->pushContent(' '._("by").': ');
208 $header->pushContent(HTML::input(array('name' => 'admin_replace[to]',
209 'value' => $post_args['to'])));
210 $header->pushContent(' '._("(no regex) Case-exact: "));
211 $checkbox = HTML::input(array('type' => 'checkbox',
212 'name' => 'admin_replace[caseexact]',
214 if (!empty($post_args['caseexact']))
215 $checkbox->setAttr('checked','checked');
216 $header->pushContent($checkbox);
217 $header->pushContent(HTML::br());
222 function stri_replace($find,$replace,$string) {
223 if (!is_array($find)) $find = array($find);
224 if (!is_array($replace)) {
225 if (!is_array($find))
226 $replace = array($replace);
228 // this will duplicate the string into an array the size of $find
232 for ($i = 0; $i < $c; $i++) {
233 $replace[$i] = $rString;
237 foreach ($find as $fKey => $fItem) {
238 $between = explode(strtolower($fItem),strtolower($string));
240 foreach($between as $bKey => $bItem) {
241 $between[$bKey] = substr($string,$pos,strlen($bItem));
242 $pos += strlen($bItem) + strlen($fItem);
244 $string = implode($replace[$fKey],$between);
249 // $Log: not supported by cvs2svn $
250 // Revision 1.11 2004/06/04 20:32:54 rurban
251 // Several locale related improvements suggested by Pierrick Meignen
252 // LDAP fix by John Cole
253 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
255 // Revision 1.10 2004/06/03 22:24:48 rurban
256 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
258 // Revision 1.9 2004/04/07 23:13:19 rurban
259 // fixed pear/File_Passwd for Windows
260 // fixed FilePassUser sessions (filehandle revive) and password update
262 // Revision 1.8 2004/03/17 20:23:44 rurban
263 // fixed p[] pagehash passing from WikiAdminSelect, fixed problem removing pages with [] in the pagename
265 // Revision 1.7 2004/03/12 13:31:43 rurban
266 // enforce PagePermissions, errormsg if not Admin
268 // Revision 1.6 2004/02/24 15:20:07 rurban
269 // fixed minor warnings: unchecked args, POST => Get urls for sortby e.g.
271 // Revision 1.5 2004/02/17 12:11:36 rurban
272 // 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, ...)
274 // Revision 1.4 2004/02/15 21:34:37 rurban
275 // PageList enhanced and improved.
276 // fixed new WikiAdmin... plugins
277 // editpage, Theme with exp. htmlarea framework
278 // (htmlarea yet committed, this is really questionable)
279 // WikiUser... code with better session handling for prefs
280 // enhanced UserPreferences (again)
281 // RecentChanges for show_deleted: how should pages be deleted then?
283 // Revision 1.3 2004/02/12 17:05:39 rurban
285 // added "Change pagename in all linked pages also"
287 // added javascript toggle for Select
288 // WikiAdminSearchReplace:
289 // fixed another typo
291 // Revision 1.2 2004/02/12 11:47:51 rurban
294 // Revision 1.1 2004/02/12 11:25:53 rurban
295 // new WikiAdminSearchReplace plugin (requires currently Admin)
296 // removed dead comments from WikiDB
304 // c-hanging-comment-ender-p: nil
305 // indent-tabs-mode: nil