Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
lam_search_replace
download link : lam_search_replace v0.4
changelog:
v0.4 – > completely forgot about url_title (and have thus added the option to change it along with the title as well)
Search and replace. Pretty basic functionality. This plugin allows you to search and replace any occurrence of text in your articles (title, body, excerpt areas)
example article:
= = = =
The man went shopping in Manhattan.
= = = =
By default, if you searched for “man” and intend to replace the term with “cat”, your new article will look like this:
= = = =
The cat went shopping in cathattan.
= = = =
Depending on your needs, you can utilize the "Match whole words only?"
checkbox to avoid instances like “cathattan” occurring.
The "Case-sensitive?"
checkbox can be utilized for hopefully obvious purposes but for the sake of being thorough, if it’s checked and you search for the occurrence of “the” (as opposed to “The”) in the example article, the plugin will not search/replace it.
- not optional: You must select either Title, Body, and/or Excerpt of your articles.
- optional: You also have the optional choice to select only a specific section and/or category of articles. Please note that if you do not select a section and/or category, then this plugin will perform the replacement within ALL of your articles.
Warning:
This plugin searches and replaces via your database. Please remember to back up your db before any major search & replaces are made. I take no responsibility for any mishaps/hiccups/aneurysms that, though unlikely, may occur.
Install, then head over to Admin -> Extensions -> Search & Replace tab to use it.
—-
This plugin probably isn’t helpful to those well versed in sql. I’m also quite positive the way its written is far from efficient but from my own testing it seems to work just fine. Since this plugin plays with your database I wouldn’t run this on a fully live, article-filled site yet just in case some bugs aren’t ironed out. But please feel free to test on a test site to see if it works for you. I’d also appreciate it if any security-minded folk would walk through the code and point out any holes as this was really a hack and slash job in general and since it affects the database and all..
Last edited by iblastoff (2007-12-09 02:19:04)
Offline
Re: lam_search_replace
I see some room for improvement. Both in security and performance:
- if you’re not selecting on field (like category), don’t do
category LIKE '%'
, but leave it out of the query. - in query parts like
"RLIKE '$lam_sr_orig'"
, usedoSlash
on the variable:"RLIKE '".doSlash($lam_sr_orig)."'"
to avoid SQL injection security bugs. - use add_privs() and require_privs() functions to limit the use of this plugin only to users with a high enough privilege level. The fact that they can’t see the extensions tab doesn’t mean they can’t call it directly.
Offline
Re: lam_search_replace
ruud wrote:
I see some room for improvement. Both in security and performance:
- if you’re not selecting on field (like category), don’t do
category LIKE '%'
, but leave it out of the query.- in query parts like
"RLIKE '$lam_sr_orig'"
, usedoSlash
on the variable:"RLIKE '".doSlash($lam_sr_orig)."'"
to avoid SQL injection security bugs.- use add_privs() and require_privs() functions to limit the use of this plugin only to users with a high enough privilege level. The fact that they can’t see the extensions tab doesn’t mean they can’t call it directly.
to be honest i didnt even think the category LIKE '%'
would even work as i’ve read you’re not allowed wildcards to select fields like that.
i’ve tried incorporating as many build-in txp functions as i thought i could but i know i’m probably missing a bunch i could utilize.
but yes, stuff like this is what i need to know! thanks ruud. will get on it. still learning the basics of php/mysql..
Last edited by iblastoff (2007-12-02 23:22:43)
Offline
Re: lam_search_replace
updated to v0.3
should reflect ruuds recommendations now.
Offline
Re: lam_search_replace
sections and categories are selected from a menu, not entered, so you can do an exact match instead of LIKE. It’s faster. Also, the quoting is not correct, I think, so this:
if(gps('lam_sr_section')) {
$lam_sr_section = gps('lam_sr_section');
$lam_sr_and_section = ' AND section LIKE ' . '\'' . doslash($lam_sr_section) . '\'';
} else {
$lam_sr_and_section = '';
}
if(gps('lam_sr_category')) {
$lam_sr_category = gps('lam_sr_category');
$lam_sr_and_category = ' AND (category1 LIKE ' . '\'' . doslash($lam_sr_category) . '\'' . ' OR category2 LIKE ' . '\'' . doslash($lam_sr_category) . '\')';
} else {
$lam_sr_and_category = '';
}
Becomes:
if(gps('lam_sr_section')) {
$lam_sr_and_section = " AND section = '".doslash(gps('lam_sr_section'))."'";
} else {
$lam_sr_and_section = '';
}
if(gps('lam_sr_category')) {
$lam_sr_and_category = " AND (category1 = '".doslash(gps('lam_sr_category'))."' OR category2 = '".doslash(gps('lam_sr_category'))."'";
} else {
$lam_sr_and_category = '';
}
or (and you can probably put all the WHERE parts of the query in qparts to make it even simpler):
$qparts = array();
if(gps('lam_sr_section'))
{
$qparts[] = "section = '".doslash(gps('lam_sr_section'))."'";
}
if(gps('lam_sr_category'))
{
$qparts[] = "(category1 = '".doslash(gps('lam_sr_category'))."' OR category2 = '".doslash(gps('lam_sr_category'))."')";
}
$query = 'SELECT .... FROM ... WHERE ...'.($qparts ? ' AND '.join(' AND ', $qparts));
Textpattern has lots of useful database functions that also help deal with table prefixes, so this:
$lam_sr_query = "SELECT id, $lam_sr_selected FROM `textpattern` WHERE $lam_sr_selected_or $lam_sr_match $lam_sr_and_section $lam_sr_and_category";
$lam_sr_result = safe_query($lam_sr_query);
$lam_num_results = mysql_num_rows($lam_sr_result);
if($lam_num_results > 0) {
$i = 0;
while($row = mysql_fetch_array($lam_sr_result)) {
...
$i++;
}
becomes:
$rows = safe_rows(
'id, '.$lam_sr_selected,
'textpattern',
$lam_sr_selected_or.' '.$lam_sr_match.' '.$lam_sr_and_section.' '.$lam_sr_and_category
);
if ($rows)
{
foreach($rows as $row)
{
...
}
$i = count($rows);
Offline
Re: lam_search_replace
ahh thanks again ruud! much appreciated.
i don’t think i’m approaching the $i count correctly though..its returning the # of articles affected rather than the number of actual replacements made. i know WHY its not working (since its only incrementing $i upon each new row) but i’ve read that preg_replace only supports counting the number of changes in php 5 and up. i’m sure theres an easy way i can do this but i haven’t thought of implementing the right logic yet.
also how do i properly include the require_privs function? i couldn’t find another plugin offhand that utilizes it so i wasn’t sure how to properly write it in. for example at the top i have
if (@txpinterface == 'admin') {
add_privs('lam_search_replace','1,2');
register_tab("extensions", "lam_search_replace", "Search & Replace");
register_callback("lam_search_replace", "lam_search_replace");
}
do i just add something like require_privs('1,2');
to that? i tried to find another plugin that utilized it but couldn’t find one to quickly reference.
Offline
Re: lam_search_replace
At this at the beginning of your lam_search_replace
function:
require_privs('lam_search_replace');
Offline
Re: lam_search_replace
Hey Steve,
How about extending this function to “forms” and “pages” (css is encoded in some whack way base64, otherwise I’d suggest that too)?
Offline
#9 2008-10-13 16:19:26
- Mattress
- New Member
- Registered: 2005-08-29
- Posts: 8
Re: lam_search_replace
I just installed this plugin, ran it and got this error:
“Warning: preg_replace() [function.preg-replace]: Unknown modifier ‘g’ in /home/spaanem2/public_html/freesand/textpattern/lib/txplib_misc.php(574) : eval()’d code on line 65”
It also said:
“Success! All occurrences of ‘/glass/’ have been replaced by ‘/’ within 1 articles (body, excerpt, excerpt_html)”
except I know that it exists in way more than just one place.
Any insight/help would be appreciated I really do not want to manually go through all of my articles to change this…
Last edited by Mattress (2008-10-13 16:20:55)
Offline
Re: lam_search_replace
If can set [ limit=“number” ] replace words?
If can do it,so better!
thanks for your plug-ins for TXP
Offline
Re: lam_search_replace
@Steve: I just installed lam_search_replace 0.4 in my TXP 4.0.7 because I changed my identi.ca user name from markusmerz
to m1
(Silly, I know)
So I wanted to change all URLs/mentions in articles, excerpts and title + case sensitive.
The plug-in did not find any markusmerz
and ‘article search’ still showed the same list.
TXP admin article search had no problems to find:
- markusmerz
- /markusmerz
I had to change all articles by S&R in a text editor.
Update
Tested S&R Test1426
with Test_succes
in title + case sensitive
Interesting error – see double table prefix ‘tpstgi2’
Warning: Table ‘dbxxx.tpstgi2tpstgi2textpattern’ doesn’t exist update tpstgi2tpstgi2textpattern set title = ‘Test_success’ where id = xxx in …/textpattern/lib/txplib_db.php on line 82
Could not connect to database
Last edited by merz1 (2009-02-06 13:32:17)
Get all online mentions of Textpattern via OPML subscription: TXP Info Sources: Textpattern RSS feeds as dynamic OPML
Offline
Re: lam_search_replace
Saved my database. Ran the plugin. No changes.
Error says it couldn’t connect to database.
here is the entire error message:
Warning: preg_replace() [function.preg-replace]: Unknown modifier ‘a’ in /usr/home/drivein/public_html/crestar/crestar/textpattern/lib/txplib_misc.php(594) : eval()‘d code on line 65
Warning: Table ‘drivein_txtp.drivein_drivein_textpattern’ doesn’t exist update drivein_drivein_textpattern set body = ‘’ where id = 257 in /usr/home/drivein/public_html/crestar/crestar/textpattern/lib/txplib_db.php on line 85
Could not connect to database
end error message.
first I tried replacing archives with articles, no change.
then I tried /archives/ and /articles/ then got the above error message.
Suggestions ?
Offline