Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-12-02 22:12:19

iblastoff
Plugin Author
From: Toronto
Registered: 2006-06-11
Posts: 1,197
Website

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

#2 2007-12-02 23:00:18

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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'", use doSlash 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

#3 2007-12-02 23:16:55

iblastoff
Plugin Author
From: Toronto
Registered: 2006-06-11
Posts: 1,197
Website

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'", use doSlash 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

#4 2007-12-03 15:17:27

iblastoff
Plugin Author
From: Toronto
Registered: 2006-06-11
Posts: 1,197
Website

Re: lam_search_replace

updated to v0.3

should reflect ruuds recommendations now.

Offline

#5 2007-12-03 19:01:08

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#6 2007-12-03 21:13:55

iblastoff
Plugin Author
From: Toronto
Registered: 2006-06-11
Posts: 1,197
Website

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

#7 2007-12-03 21:39:33

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: lam_search_replace

At this at the beginning of your lam_search_replace function:

require_privs('lam_search_replace');

Offline

#8 2008-06-12 16:56:06

mrdale
Moderator
From: Walla Walla
Registered: 2004-11-19
Posts: 2,201
Website

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

#10 2008-11-29 12:11:05

the1design.cn
New Member
From: CHINA
Registered: 2008-11-28
Posts: 5
Website

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

Board footer

Powered by FluxBB