| 1 |
<?php
|
| 2 |
// $Id: search_and_replace.module,v 1.3.4.1 2008/10/07 15:58:26 btopro Exp $
|
| 3 |
//ELMS: Search and Replace - Search and Replace Drupal content
|
| 4 |
//Copyright (C) 2008 The Pennsylvania State University
|
| 5 |
//
|
| 6 |
//Bryan Ollendyke
|
| 7 |
//bto108@psu.edu
|
| 8 |
//
|
| 9 |
//Keith D. Bailey
|
| 10 |
//kdb163@psu.edu
|
| 11 |
//
|
| 12 |
//12 Borland
|
| 13 |
//University Park, PA 16802
|
| 14 |
//
|
| 15 |
//This program is free software; you can redistribute it and/or modify
|
| 16 |
//it under the terms of the GNU General Public License as published by
|
| 17 |
//the Free Software Foundation; either version 2 of the License, or
|
| 18 |
//(at your option) any later version.
|
| 19 |
//
|
| 20 |
//This program is distributed in the hope that it will be useful,
|
| 21 |
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 22 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 23 |
//GNU General Public License for more details.
|
| 24 |
//
|
| 25 |
//You should have received a copy of the GNU General Public License along
|
| 26 |
//with this program; if not, write to the Free Software Foundation, Inc.,
|
| 27 |
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Implementation of hook_perm
|
| 31 |
*/
|
| 32 |
function search_and_replace_perm() {
|
| 33 |
return array('access search and replace');
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_help
|
| 38 |
*/
|
| 39 |
function search_and_replace_help($path, $arg) {
|
| 40 |
switch ($path) {
|
| 41 |
case 'admin/help#search_and_replace':
|
| 42 |
return t("Search and replace lets you perform the operation on your site's content.");
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Implementation of hook_menu
|
| 48 |
*/
|
| 49 |
function search_and_replace_menu() {
|
| 50 |
$items = array();
|
| 51 |
$items['search_and_replace'] = array(
|
| 52 |
'title' => 'Search and Replace',
|
| 53 |
'type' => MENU_NORMAL_ITEM,
|
| 54 |
'description' => 'Search and Replace',
|
| 55 |
'page callback' => '_search_and_replace_page',
|
| 56 |
'access arguments' => array('access search and replace'),
|
| 57 |
);
|
| 58 |
return $items;
|
| 59 |
}
|
| 60 |
|
| 61 |
function _search_and_replace_page() {
|
| 62 |
return drupal_get_form('_search_and_replace_form');
|
| 63 |
}
|
| 64 |
|
| 65 |
|
| 66 |
/**
|
| 67 |
* Implementation of hook_settings
|
| 68 |
*/
|
| 69 |
function _search_and_replace_form($form_state) {
|
| 70 |
$data = '$(document).ready(function() {
|
| 71 |
$("#edit-select-all-content-types").css("display", "").after("Select All Types");
|
| 72 |
});
|
| 73 |
|
| 74 |
function on_off_all(obj) {
|
| 75 |
$(":checkbox").attr("checked", obj.checked);
|
| 76 |
}';
|
| 77 |
drupal_add_js($data, 'inline');
|
| 78 |
$result = db_query("SELECT name, type FROM {node_type}");
|
| 79 |
while ($val = db_fetch_array($result)) {
|
| 80 |
$options[$val['type']] = $val['name'];
|
| 81 |
}
|
| 82 |
$form["case_sensitive"] = array(
|
| 83 |
'#type' => 'radios',
|
| 84 |
'#title' => t("Case Sensitive replace"),
|
| 85 |
'#required' => FALSE,
|
| 86 |
'#options' => array(
|
| 87 |
1 => "Yes",
|
| 88 |
2 => "No",
|
| 89 |
),
|
| 90 |
'#default_value' => 1,
|
| 91 |
);
|
| 92 |
$form["search_term"] = array(
|
| 93 |
'#type' => 'textfield',
|
| 94 |
'#description' => 'Must be at least 3 characters long',
|
| 95 |
'#title' => t("Search for"),
|
| 96 |
'#default_value' => '',
|
| 97 |
'#required' => TRUE,
|
| 98 |
);
|
| 99 |
$form["replace_term"] = array(
|
| 100 |
'#type' => 'textfield',
|
| 101 |
'#description' => 'To replace a string with nothing type %NULL%',
|
| 102 |
'#title' => t("Replace with"),
|
| 103 |
'#default_value' => '',
|
| 104 |
'#required' => FALSE,
|
| 105 |
);
|
| 106 |
$form["search_and_replace"] = array(
|
| 107 |
'#type' => 'radios',
|
| 108 |
'#title' => t("Search and Replace function"),
|
| 109 |
'#options' => array(
|
| 110 |
0 => "Search Body and Title Text",
|
| 111 |
1 => "Search Body Text",
|
| 112 |
2 => "Search Title Text",
|
| 113 |
3 => "Search and Replace Body and Title Text",
|
| 114 |
4 => "Search and Replace Body Text",
|
| 115 |
5 => "Search and Replace Title Text",
|
| 116 |
),
|
| 117 |
'#required' => TRUE,
|
| 118 |
);
|
| 119 |
$form["content_types"] = array(
|
| 120 |
'#type' => 'checkboxes',
|
| 121 |
'#options' => $options,
|
| 122 |
'#title' => t("Search in these content types"),
|
| 123 |
'#required' => TRUE,
|
| 124 |
);
|
| 125 |
$form["select_all_content_types"] = array(
|
| 126 |
'#type' => 'checkbox',
|
| 127 |
'#required' => FALSE,
|
| 128 |
'#attributes' => array('onclick' => 'on_off_all(this);', 'style' => 'display:none;'),
|
| 129 |
);
|
| 130 |
$form['submit'] = array(
|
| 131 |
'#type' => 'submit',
|
| 132 |
'#value' => t('Submit'),
|
| 133 |
);
|
| 134 |
return $form;
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Implementation of hook_settings_submit
|
| 139 |
*/
|
| 140 |
function _search_and_replace_form_submit($form, &$form_state) {
|
| 141 |
if (strlen($form_state['values']["search_term"]) < 3) {
|
| 142 |
form_set_error("search_term", "You can't do a replace of a string less then 3 in length.");
|
| 143 |
return FALSE;
|
| 144 |
}
|
| 145 |
$count = 0;
|
| 146 |
$all_replacements = 0;
|
| 147 |
$replacement_nodes = '';
|
| 148 |
$perform_replacement = TRUE;
|
| 149 |
$selections = ' WHERE ';
|
| 150 |
$case_sensitive = $form_state['values']["case_sensitive"];
|
| 151 |
foreach ($form_state['values']["content_types"] as $content_type) {
|
| 152 |
if ($content_type != '0') {
|
| 153 |
if ($count == 0) {
|
| 154 |
$selections.= "type='$content_type'";
|
| 155 |
}
|
| 156 |
else {
|
| 157 |
$selections.= " OR type='$content_type'";
|
| 158 |
}
|
| 159 |
$count++;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
$result = db_query("SELECT nid FROM {node}$selections");
|
| 163 |
|
| 164 |
//if this is a replace function then make sure the user REALLY wants to replace the string with nothing
|
| 165 |
//if nothing was typed then block the submission
|
| 166 |
//if %NULL% was typed then set replace_term to '' so that it will commit the something to nothing command
|
| 167 |
if ($form_state['values']["search_and_replace"] == 3 || $form_state['values']["search_and_replace"] == 4 || $form_state['values']["search_and_replace"] == 5) {
|
| 168 |
if ($form_state['values']["replace_term"] == '') {
|
| 169 |
$perform_replacement = FALSE;
|
| 170 |
}
|
| 171 |
else if ($form_state['values']["replace_term"] == '%NULL%') {
|
| 172 |
$form_state['values']["replace_term"] = '';
|
| 173 |
}
|
| 174 |
}
|
| 175 |
if ($perform_replacement) {
|
| 176 |
while ($val = db_fetch_array($result)) {
|
| 177 |
$replacements = 0;
|
| 178 |
$node = node_load($val['nid']);
|
| 179 |
//ONLY search (no replace) body and title text
|
| 180 |
if ($form_state['values']["search_and_replace"] == 0) {
|
| 181 |
_search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["search_term"], $node->body, $replacements, $case_sensitive);
|
| 182 |
$all_replacements+= $replacements;
|
| 183 |
_search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["search_term"], $node->title, $replacements, $case_sensitive);
|
| 184 |
$all_replacements+= $replacements;
|
| 185 |
$replace_text = 'occurrences found in the following node bodies and titles';
|
| 186 |
}
|
| 187 |
//ONLY search (no replace) body text
|
| 188 |
if ($form_state['values']["search_and_replace"] == 1) {
|
| 189 |
_search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["search_term"], $node->body, $replacements, $case_sensitive);
|
| 190 |
$all_replacements+= $replacements;
|
| 191 |
$replace_text = 'occurrences found in the following node bodies';
|
| 192 |
}
|
| 193 |
//ONLY search (no replace) title text
|
| 194 |
if ($form_state['values']["search_and_replace"] == 2) {
|
| 195 |
_search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["search_term"], $node->title, $replacements, $case_sensitive);
|
| 196 |
$all_replacements+= $replacements;
|
| 197 |
$replace_text = 'occurrences found in the following node titles';
|
| 198 |
}
|
| 199 |
|
| 200 |
//search and replace body text and title text
|
| 201 |
if ($form_state['values']["search_and_replace"] == 3) {
|
| 202 |
$node->body = _search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["replace_term"], $node->body, $replacements, $case_sensitive);
|
| 203 |
$all_replacements+= $replacements;
|
| 204 |
$node->title = _search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["replace_term"], $node->title, $replacements, $case_sensitive);
|
| 205 |
$all_replacements+= $replacements;
|
| 206 |
$replace_text = 'replacements made in the following node bodies and titles';
|
| 207 |
$node->revision = TRUE;
|
| 208 |
$node->log = 'Search and Replace: Replaced the title/body text "'. $form_state['values']["search_term"] .'" with "'. $form_state['values']["replace_term"] .'"';
|
| 209 |
}
|
| 210 |
//search and replace the body text
|
| 211 |
if ($form_state['values']["search_and_replace"] == 4) {
|
| 212 |
$node->body = _search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["replace_term"], $node->body, $replacements, $case_sensitive);
|
| 213 |
$all_replacements+= $replacements;
|
| 214 |
$replace_text = 'replacements made in the following node bodies';
|
| 215 |
$node->revision = TRUE;
|
| 216 |
$node->log = 'Search and Replace: Replaced the body text "'. $form_state['values']["search_term"] .'" with "'. $form_state['values']["replace_term"] .'"';
|
| 217 |
}
|
| 218 |
//search and replace the title text
|
| 219 |
if ($form_state['values']["search_and_replace"] == 5) {
|
| 220 |
$node->title = _search_and_replace_helper($form_state['values']["search_term"], $form_state['values']["replace_term"], $node->title, $replacements, $case_sensitive);
|
| 221 |
$all_replacements+= $replacements;
|
| 222 |
$replace_text = 'replacements made in the following node titles';
|
| 223 |
$node->revision = TRUE;
|
| 224 |
$node->log = 'Search and Replace: Replaced the title text "'. $form_state['values']["search_term"] .'" with "'. $form_state['values']["replace_term"] .'"';
|
| 225 |
}
|
| 226 |
node_save($node);
|
| 227 |
if ($replacements != 0) {
|
| 228 |
$replacement_nodes.= l(t($node->title), 'node/'. $node->nid) .' {'. $node->nid .'}<br />';
|
| 229 |
}
|
| 230 |
}
|
| 231 |
drupal_set_message($all_replacements .' '. $replace_text .':<br />'. $replacement_nodes);
|
| 232 |
}
|
| 233 |
else {
|
| 234 |
form_set_error("replace_term", "If you want to replace a string with nothing please type %NULL%. This message is to prevent accidental replacement of a search term with nothing.");
|
| 235 |
return FALSE;
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
function _search_and_replace_helper($search, $replace, $haystack, &$replacements, $case_sensitive) {
|
| 240 |
if ($case_sensitive == 2) {
|
| 241 |
return str_ireplace($search, $replace, $haystack, $replacements);
|
| 242 |
}
|
| 243 |
else {
|
| 244 |
return str_replace($search, $replace, $haystack, $replacements);
|
| 245 |
}
|
| 246 |
}
|