Created
January 11, 2012 17:49
-
-
Save deltaepsilon/1595831 to your computer and use it in GitHub Desktop.
Find/replace text in Wordpress MySQL post table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Use mysql dump to back up the wp_posts table (mysqldump -u username -ppassword wp_databasename wp_posts > wp_posts.sql) | |
* Repair your damage with the wp_posts.sql file (mysql -u username -ppassword wp_databasename < wp_posts.sql ) | |
* Install lib_mysqludf found here: http://www.mysqludf.org/lib_mysqludf_preg/ | |
* Initialize lib_mysqludf functions | |
* Set target regex pattern | |
* Set replacement | |
* Test target regex pattern. Function returns 1 for success, 0 for an invalid pattern | |
* Test replacement. Function shows what the results will look like. | |
* Grit your teeth and run the replace. | |
*/ | |
CREATE FUNCTION preg_capture RETURNS STRING SONAME 'lib_mysqludf_preg.so'; | |
CREATE FUNCTION preg_check RETURNS INTEGER SONAME 'lib_mysqludf_preg.so'; | |
CREATE FUNCTION preg_position RETURNS INTEGER SONAME 'lib_mysqludf_preg.so'; | |
CREATE FUNCTION preg_replace RETURNS STRING SONAME 'lib_mysqludf_preg.so'; | |
CREATE FUNCTION preg_rlike RETURNS INTEGER SONAME 'lib_mysqludf_preg.so'; | |
CREATE FUNCTION lib_mysqludf_preg_info RETURNS STRING SONAME 'lib_mysqludf_preg.so' ; | |
/* | |
select LIB_MYSQLUDF_PREG_INFO() from dual; | |
*/ | |
/* | |
* Set pattern and replacement | |
* Mysql escape character is \, so you have to double backslash your | |
* regex backslashes | |
*/ | |
select @pattern := "/http:\\/\\/blog\\.oldblog\.com/"; | |
select @replacement := '/blog'; | |
/* | |
* Test pattern | |
*/ | |
select preg_check(@pattern); | |
/* | |
* Test replacement | |
*/ | |
select preg_replace(@pattern, @replacement, post_content) as post from wp_posts where preg_capture(@pattern, post_content) is not null; | |
/* | |
* Run replacement | |
*/ | |
/* | |
* Create temp table | |
*/ | |
DROP TABLE IF EXISTS wp_posts_archive; | |
create temporary table wp_posts_archive | |
select * from wp_posts; | |
/* | |
* Post_excerpt tends to be blank, so I like to put something there for | |
* the posts that I've affected. I'll go back later and clear it out. | |
* It's nice to have the visual confirmation that yes, I did replace | |
* what I meant to replace. | |
* Uncomment the next line to set post_excerpt to 'replace' | |
*/ | |
update wp_posts l | |
set | |
/* post_excerpt = 'replaced', */ | |
post_content = | |
( | |
select preg_replace(@pattern, @replacement, post_content) as post_content | |
from wp_posts_archive m | |
where preg_capture(@pattern, post_content) is not null | |
and l.id = m.id | |
) | |
where preg_capture(@pattern, post_content) is not null | |
; | |
/* | |
* Test result | |
*/ | |
select * from wp_posts; | |
select preg_capture(@pattern, post_content) as post from wp_posts where preg_capture(@pattern, post_content) is not null; | |
/* select * from wp_posts where post_excerpt = 'replaced'; */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment