Photography by E.W. Faircloth

Bridgeville DE

E.W. Faircloth Photography

Email: wayne@faircloth.org

Bridgeville DE

The original post for picture(s) done on 2011-06-12 by E.W. Faircloth can be found at

https://faircloth.org/blog1/?p=4166

http://faircloth.org/blog1/?p=4166

http://faircloth.org/blog1/?p=4166

Tags: I see it this way! Heritage Shores Bridgville De E.W. Faircloth photography photographer perl computer code computer programming Google open source free software software

To me, the following lines of perl code are about as pretty as a picture.  The computer code changes my blog posts into pages to be easier found by Google. Hello world!   ------------ --------------------------------- ------------- #!/usr/bin/perl use Net::SMTP; use DBI; use Net::FTP; # Needing to get more ranking in Google searches for images around or near Bridgeville DE # I wrote this script to get previous WordPress blog posts and extract content to make # regular HTML pages.  This script only took 2 day around September 25, 2010. # WordPress keeps track of blog information in a database full of tables. Some table # keep user name, some keep comments, and the list goes on.  I discovered that if a post # was published then the field "post_status" of table "wp_posts" would contain the word "publish" # after searching around. # To find tags on blog you need to get information from two(2) tables.  One table lists the # post where tags were used and another lists the tag id.  Space must be saved that way because # a tag is saved only once in a tag dictionary. Any usage after that refers to the original tag. # **********  ******************* Main Section *********** ********* # Here's what the script does: First it connects(ftp) to my Network Solutions hosting site and changes # to a directory I've created manually to place web pages which this script will create. # Then it connects to the database which Wordpress uses on my server. # It then goes throught database looking for "published" Wordpress posts, extracts the information and copies it to # my local hardrive as a html page. # Once done script reports how many pages were built and location of file on my hardrive. # I then manually ftp the files to a folder on my website so Google can see my post as normal pages. # This greatly ups my image ranking on Google. # One day, I'll get around to automating all this, with no manual work as that's what computers are for.  If anyone would like help changing code for their use, contact me at wayne@faircloth.org #   my $hostname="localhost"; my $hostname="yourHostIpNumber"; my $port="3306"; my $database="yourWordpressDb"; my $begin=4001;    # begin building pages with published posts of this number my $end=5000; # end building pages with published posts of this number # 1-1000    -> blog_pages0 -- done # 1001-2000 -> blog_pages1 -- done # 2001-3000 -> blog_pages2 -- done # 3001-4000 -> blog_pages3 -- done # 4001-5000 -> blog_pages4 -- started June 12th 2011 # 3001-4000 -> blog_pages5 # 4001-5000 -> blog_pages6 # pages can be found in "/home/thecloth/scripts/a_html # you'll have to manually move them to server, so far! my $pass=0; my $ftp=Net::FTP->new("yourFTPipAddress"); if($ftp){$pass=1;}else {err("Connection refused from yourFTPipAddress server\n");} if($pass){$ftp->login("ftpUsername","ftpPassword") or err("Could not login to yourFTPipAddress server\n");} # Don't forget to change ftp->cwd("/htdocs/blog_pagesXX") if($pass){$ftp->cwd("/htdocs/blog_pages4")or  print"Could not change to - /htdocs/blog_pages4 - directory.\n"} if($pass){$ftp->binary;} my $range; my $page=""; my $i, $counter, $pages; my $sw1 = $sw2 = $sw3, $pn =1; my @tag_list; my $key, $value, $tag, %tags; my @term_id; my $sth; my $dsn="DBI:mysql:database=$database;host=$hostname;port=$port"; my $dbh=DBI->connect($dsn,"DdUsername","Dbpassword"); if(!defined($dbh)){ die "Can not connect to database\n";} $range=$end-$begin; $index=$begin; $counter=$pages=0; # every 1000 pages create sequential blog_pagesXX folder manually :( # first we build hash table of key/tag values to only call once for optimization my $sth = $dbh->prepare('select term_id, name from wp_terms') or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() or die "Couldn't execute statement: " . $sth->errstr; while (@term_id = $sth->fetchrow_array()){ $key=$term_id[0]; $value=$term_id[1]; $tags{$key}="$value"; # print "$key -- $tags{$key}\n"; # get key/value pairs for tags } # ------------------------------------------- LET'S GET IT GOING ------------------- open (DIR,"/home/thecloth/scripts/a_html") or die "Can not open /home/thecloth/scripts/a_html ";    # open DIR to write html pages chdir ("/home/thecloth/scripts/a_html") or die "Can not change to /home/thecloth/scripts/a_html ";    # open DIR to write html pages while($index<=$end){    # beginning of while loop #1 # exit(0); #    Check database to see if this post was published my $sth = $dbh->prepare('select post_status from wp_posts where id ='."$index") or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() or die "Couldn't execute statement: " . $sth->errstr; $post_status = $sth->fetchrow(); if($post_status eq "publish") {    # begin if #1 $filename="blog_$index"; # ---------------------- let's get the title and date of post ---------------- my $sth = $dbh->prepare('select post_title, post_date from wp_posts where id ='."$index") or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() or die "Couldn't execute statement: " . $sth->errstr; @post_title = $sth->fetchrow(); $post_title=$post_title[0]; $post_date=$post_title[1]; # print"\nID $index: $post_title -- $post_date\n"; # --------------- let's get the image links out of data ---------- my $sth = $dbh->prepare('select ID, post_content from wp_posts where id ='."$index") or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() or die "Couldn't execute statement: " . $sth->errstr; $page=newPage($index,$post_title,$post_date); while (@data = $sth->fetchrow_array()) { my $id = $data[0];    # ID in not needed but was used in beginning tests.  Change above query and remove my $data = $data[1]; # print "\t$id: $data\n";    # test # if ($data=~/(.+)(<img.+\/>)/ ) {print"\n$2\n";} if (@img=($data=~/(<img.+\/>)/g )) {        # regex out just image tag <img ...> $img=@img;    #img contains number of images on page $pn=0; while($pn<=$img){ $page=$page.$img[$pn];    #    add img tag from data $page=$page."<p><p><a HREF=\"http:\/\/faircloth.org\/blog1\/?p=$index\">http://faircloth.org/blog1/?p=$index</a><p><p>"; $pn++; #     Add link to page with actual blog post link } } #  We use @myArray to get all matches returned from global regex. } # ------------------- let's get the tags from page ----------------- $page=$page."<p></CENTER><p><p>Tags: "; my $sth = $dbh->prepare('select term_taxonomy_id  from wp_term_relationships where object_id='."$index") or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() or die "Couldn't execute statement: " . $sth->errstr; while (@term_id = $sth->fetchrow_array()){ # print"\n$term_id[$i]\n"; $tag=$term_id[$i]; $page=$page." $tags{$tag}"; # get key/value pairs for tags } # We're done so put end of page in html $page=$page."<p></body></html>"; open FILE, ">", "$filename.html" or die $!; print FILE $page; $pages++; close(FILE); if($pass){$ftp->put("$filename.html") or print"Could not ftp $filename.html\n";} # print"\n$page\n"; # print"\n------------------- ----------------------------- ----------------------------\n"; if(($counter >(.25 *$range)) && ($sw1==1)){print"\nWe processed over 25% of $range files\n";$sw1=0;} if(($counter >(.50 *$range)) && ($sw2==1)){print"\nWe processed over 50% of $range files\n";$sw2=0;} if(($counter >(.75 *$range)) && ($sw3==1)){print"\nWe processed over 75% of $range files\n";$sw3=0;} }    # end if #1 $index++; $counter++;    # up the number of files written }    # end of while loop #1 closedir(DIR);    # Finished writing files, so close DIR chdir ("/home/thecloth/scripts");    # Go back to original directory -- not needed, however $sth->finish if defined ($sth);     # Not always needed.  I needed it fot the unfinished $sth-.fetchrow() statement if($pass){$ftp->quit;}    # close the ftp connection $dbh->disconnect if defined ($dbh); print "\nWe created a total of $pages new html pages.\n\n"; print 'They can be found in "/home/thecloth/scripts/a_html"'."\n\n"; sub newPage{ my ($id,$title,$date)=@_; my $page; $date=~/(\d\d\d\d-\d\d-\d\d)/; $date=$1; $page='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'; $page=$page."<title> $title </title><body><CENTER><h2>Photography by E.W. Faircloth</h2><p>Bridgeville DE<p> <a HREF=\"http://faircloth.org/faircloth\">E.W. Faircloth Photography</a><p>Email: wayne\@faircloth.org<p>Bridgeville DE<p>"; $page=$page."<p>The original post for picture(s) done on $date by E.W. Faircloth can be found at<p><a HREF=\"http:\/\/faircloth.org\/blog1\/?p=$index\">http://faircloth.org/blog1/?p=$index</a><p><p>"; return $page; }