Loading...

使用perl对wordpress数据库进行了整理

2007-11-24 16:21:20 发表于编程 本文链接: 使用perl对wordpress数据库进行了整理

原来不是很理解post_name与post_title之间的差别。到phpmyadmin看了看,发现,
post_title是文章标题;post_name相当于文件名。我发现自己的数据库的表wp_posts里存在以下问题:

  1. 有的guid是http://zhasm.com/archives/94.html格式的,有的是http://www.zhasm.com/archives/94格式的。不统一。
  2. post_name有的是英文,有的是%+数字格式的乱码。

为解决上述问题,我写了一段perl代码,使用正则式自动解决问题1,手动解决问题2。代码如下:


#!/usr/bin/perl -w
#
use DBI;
#connect to the database
my $dbh=DBI->connect(”DBI:mysql:database=XX;host=localhost”,”user”,”password”,{’RaiseError’=>1});
#use the correct char-set
$dbh->do(”set names utf8″);
#
my $sqr=$dbh->prepare(”SELECT * FROM wp_posts”);

$sqr->execute();

while(my $ref=$sqr->fetchrow_hashref())
{

print “————————\n\n”;
my $guid=$ref->{’guid’};
my $id=$ref->{’ID’};
my $title=$ref->{’post_title’};
my $name=$ref->{’post_name’};

$_=$guid;
if(/www\./)
{ #delete www. from each guid
s/www\.//;
$dbh->do(”update wp_posts set guid=’$_’ where ID=$id”);
}
if(/\d{2,})$/)
{ #add .html to each guid if needed.
s/(\d{2,})$/\1.html/;
$dbh->do(”update wp_posts set guid=’$_’ where ID=$id”);
}
## change the postname
print $title.”\n”;
print $name.”\n”;
print “do u want to translate by yourself?(y/n)”;
chomp($answer=);
if ($answer eq ‘y’)
{
chomp($newname=);
print “your translation is :\t”.$newname.”\n”;
$dbh->do(”update wp_posts set post_name=’$newname’ where ID=$id”);
print “new translation has been aplied.\n”;
}
}

发表于 2007-11-24 16:21:20 目录:编程 [RSS 2.0] 你可以发表评论, 或者从您的网站 trackback
feed url
上一篇: « 爆米花的回忆
下一篇: 深柳读书堂 »