User:Fl/scripts/achewoodscriptc.php

This is an old revision of this page, as edited by Fl (talk | contribs) at 03:38, 2 November 2008 (Copied script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
<?php
  // Achewood script c, converts previously made date based references to [Cartoon name], Date, Achewood. References
require_once 		"pwiki.class.php";
$pw			= new PWiki;

function source_code ($url) {
  if (function_exists ('curl_init')) {
    $curl = curl_init ($url);
    curl_setopt ($curl, CURLOPT_HEADER, FALSE);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt ($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    $source = curl_exec ($curl);
    curl_close ($curl);
    return $source;
  } else {
    return file_get_contents ($url);
  }
}


$page = $pw->getPage('Achewood');
preg_match_all("/<ref( name=(.*?)>|>)\[(http:\/\/(www\.a|a)chewood\.com\/(index.php\?|\?)date=([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])) (.*?)\]\. ''Achewood''<\/ref>/i", $page, $matches);
$i = 0;

$runs = count($matches[0]);
foreach($matches[0] as $match) {
  echo "Run ".($i+1)." of $runs started\n";
  $link = $matches[3][$i];
  $date = $matches[7][$i];
  $refend = $matches[1][$i];
  $achewood = source_code($link);
  preg_match_all('/<p id="comic_body">\s*<a href="(.*?)">(.*?)<\/p>/si', $achewood, $titlelink);
  $othersite = html_entity_decode($titlelink[1][0]);
  $othersitesource = source_code($othersite);
  preg_match_all('/<title>(.*?)<\/title>/si', $othersitesource, $titlearray);
  $title = $titlearray[1][0];
  $matchnew = "<ref".$refend."[".$link." ".$title."], ".$date.", ''Achewood''</ref>";
  $find[$i] = $match;
  $replace[$i] = $matchnew;
  $i++;
  echo "Run $i of $runs complete\n";
}

$far = "AchewoodFindAndReplace.txt";
$fh = fopen($far, 'w') or die("can't open file");
fwrite($fh, print_r($find)."\n\n".print_r($replace));
fclose($fh);

$newpage = str_replace($find, $replace, $page);

$wiki = "AchewoodWikitext.txt";
$fh = fopen($wiki, 'w') or die("can't open file");
fwrite($fh, $newpage);
fclose($fh);

$pw->_exit("End of script.");
?>