
sbutler
By: a guest on Jan 21st, 2009 | syntax:
Perl | size: 0.96 KB | hits: 135 | expires: Never
#!/usr/bin/perl -w
use strict;
use DBI;
use HTML::Parser;
my $dbh = DBI->connect( 'DBI:mysql:database=mydb;host=myhost.example.org', 'myuser', 'mypass', { RaiseError => 1 } )
|| die "Cannot connect to database: $!";
my $sth_update = $dbh->prepare( 'UPDATE `mytable` SET `body` = ? WHERE `id` = ?' );
my $sth_select = $dbh->prepare( 'SELECT `id`, `body` FROM `mytable`' );
my ($id, $body);
$sth_select->execute;
$sth_select->bind_columns( \$id, \$body );
while ($sth_select->fetch) {
my $tree = HTML::TreeBuilder->new;
$tree->parse( $body );
$tree->eof;
$tree = $tree->elementify;
# skip if this already has a src PARAM
next if $tree->look_down( '_tag' => 'param', 'name' => 'src' );
my $embed = $tree->look_down( '_tag' => 'embed' );
next unless $embed;
my $param = HTML::Element->new( 'param', 'name' => 'src', 'value' => $embed->attr( 'src' ) );
$tree->unshift_content( $param );
$sth_update->execute( $tree->as_HTML );
}