AppendExpiryDataToFile - v1


13/10/04
 

AppendExpiryDataToFile - v1

I'm replacing the eregi check (in version 1) that the URL includes "www.lsbu.ac.uk" by a substr check on the beginning of the string, because I still occasionaly get false hits from Google's cached pages, such as this one:
http://216.239.59.104/search?q=cache:ehnyNCmS6VkJ:www.lsbu.ac.uk/bcim/intranet/
students/technical-support/faraday-wing.shtml+%22faraday+wing%22&hl=en
Here is my first attempt at this function...
 
function AppendExpiryDataToFile($theURL, $theOwner, $theExpiryDate, 
$theMessage, $theFile) {

  // if (theURL is a valid LSBU www address) then
  if ((substr($theURL, 0, 23) == "http://myweb.lsbu.ac.uk")) {
  
    // open and lock the expiry data file
    $theFile_fp = fopen($theFile, "r+");
    $lock = flock($theFile_fp, 2);

    // continue when lock is obtained
    if ($lock) {

      // read contents of file
      $db = readDatabase($theFile);

      $found = FALSE;
      // Pop each ExpiredPage object and see if the URL matches
      // *** ADD "and not found"!
      while ($poppedExpiredPage = (array_pop($db))):
        if ($poppedExpiredPage->url == $theURL) {
          $found = TRUE;
        }
      endwhile;
    
      // if there's no entry for the web page then append to file
      if ($found == FALSE) {

        // go to end of file, back two lines (i.e. 15 places)
        fseek($theFile_fp, -15, SEEK_END);

        // now append...
        fwrite($theFile_fp, "<expiredPage>\n");
        fwrite($theFile_fp, "  <url>");
        fwrite($theFile_fp, $theURL);
        fwrite($theFile_fp, "</url>\n");
        fwrite($theFile_fp, "  <owner>");
        fwrite($theFile_fp, $theOwner);
        fwrite($theFile_fp, "</owner>\n");
        fwrite($theFile_fp, "  <expired>");
        fwrite($theFile_fp, $theExpiryDate);
        fwrite($theFile_fp, "</expired>\n");
        fwrite($theFile_fp, "  <message>");
        fwrite($theFile_fp, $theMessage);
        fwrite($theFile_fp, "</message>\n");
        fwrite($theFile_fp, "</expiredPage>\n\n");
        fwrite($theFile_fp, "</expiredPages>");
      }

    }  // unlock and close reminders file
    $lock = flock($theFile_fp, 3);
    fclose($theFile_fp);

  }

}
 

Works fine (after some fiddling) - here's the full test program:
 
<?php

//====================================================================

/* This section is from "Parsing the ExpiryData file" */

//--------------------------------------------------------------------
class ExpiredPage {
    var $url;  
    var $owner; 
    var $expired;  
    var $message;  
//--------------------------------------------------------------------
function ExpiredPage ($aa) {
        foreach ($aa as $k=>$v)
            $this->$k = $aa[$k];
    }
}
//--------------------------------------------------------------------
function readDatabase($fileurl) {
    // read the xml database of ExpiredPages
    $data = implode("",file($fileurl));
    $parser = xml_parser_create();
    xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
    xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
    xml_parse_into_struct($parser,$data,$values,$tags);
    xml_parser_free($parser);

    // loop through the structures
    foreach ($tags as $key=>$val) {
        if ($key == "expiredPage") {
            $molranges = $val;
            // each contiguous pair of array entries are the 
            // lower and upper range for each expiredPage definition
            for ($i=0; $i < count($molranges); $i+=2) {
                    $offset = $molranges[$i] + 1;
                $len = $molranges[$i + 1] - $offset;
                $tdb[] = parseMol(array_slice($values, $offset, $len));
            }
        } else {
            continue;
        }
    }
    return $tdb;
}
//--------------------------------------------------------------------
function parseMol($mvalues) {
    for ($i=0; $i < count($mvalues); $i++)
        $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
    return new ExpiredPage($mol);
}

//--------------------------------------------------------------------

/* End of section from "Parsing the ExpiryData file" */

//====================================================================



function AppendExpiryDataToFile($theURL, $theOwner, $theExpiryDate, 
$theMessage, $theFile) {

  // if (theURL is a valid LSBU www address) then
  if ((substr($theURL, 0, 23) == "http://myweb.lsbu.ac.uk")) {
  
    // open and lock the expiry data file
    $theFile_fp = fopen($theFile, "r+");
    $lock = flock($theFile_fp, 2);

    // continue when lock is obtained
    if ($lock) {

      // read contents of file
      $db = readDatabase($theFile);

      $found = FALSE;
      // Pop each ExpiredPage object and see if the URL matches
      // *** ADD "and not found"!
      while ($poppedExpiredPage = (array_pop($db))):
        if ($poppedExpiredPage->url == $theURL) {
          $found = TRUE;
        }
      endwhile;
    
      // if there's no entry for the web page then append to file
      if ($found == FALSE) {

        // go to end of file, back two lines (i.e. 15 places)
        fseek($theFile_fp, -15, SEEK_END);

        // now append...
        fwrite($theFile_fp, "<expiredPage>\n");
        fwrite($theFile_fp, "  <url>");
        fwrite($theFile_fp, $theURL);
        fwrite($theFile_fp, "</url>\n");
        fwrite($theFile_fp, "  <owner>");
        fwrite($theFile_fp, $theOwner);
        fwrite($theFile_fp, "</owner>\n");
        fwrite($theFile_fp, "  <expired>");
        fwrite($theFile_fp, $theExpiryDate);
        fwrite($theFile_fp, "</expired>\n");
        fwrite($theFile_fp, "  <message>");
        fwrite($theFile_fp, $theMessage);
        fwrite($theFile_fp, "</message>\n");
        fwrite($theFile_fp, "</expiredPage>\n\n");
        fwrite($theFile_fp, "</expiredPages>");
      }

    }  // unlock and close reminders file
    $lock = flock($theFile_fp, 3);
    fclose($theFile_fp);

  }

}




/*
// MAIN PROGRAM
*/

$fileurl = "ExpiryData.xml";

AppendExpiryDataToFile("http://myweb.lsbu.ac.uk/blah.blah.blah", 
"martin.bush@lsbu.ac.uk", "2004-10-31", "testMessage", $fileurl);

?>
 

 
 

<<contents ^top^