Re: RSS First try
| From: | Adolfo Garcia Veytia | Date: | Tue, 26 Mar 2002 16:44:30 +0000 |
| Subject: | Re: RSS First try | ||
| References: | 1 | Groups: | php.mirrors |
| Request: | Send a blank email to php-mirrors+get-9275@lists.php.net to get a copy of this message | ||
Of course goba, I was just waiting till it was finished :)
I'm attaching the parser here. My only concern is that
parsing the home page is not very reliable, because if
something changes it will break.
I tried to make it as modular as possible so that the
data feed can be changed easily and the rss functions
reused. It includes the striptags() call as well.
Please take a look at it and let me know. Remember that
it is built to be run against the index.php source, not
the processed output.
I think that we should provide some code explaining
how to use this before the first howto e-mail arrives.
I think I'll write one as well.
Adolfo Garcia
uServers
At 17:32 3/26/2002 +0100, you wrote:
> > OK, this is the first version of the RSS news feed.
> > Please check it out and let me know what you think
> > of it. It gets sent as XML so if you don't see anything
> > use the "show source" feature of your browser:
> >
> > http://www.ulabs.uservers.net/proyectos/php_rdf/parser.php
>
>Seems OK. It would be nice to put up the output
>to phpweb (with some cron script), so mirrors can
>host the static output of this (quicker response
>times). And let's see what others think about it...
>
>Goba
Adolfo Garcia
Investigacion y Desarrollo - Urbano
PGP Fingerprint 804C 5DCB 4772 4A87 36EC 117A 761E 34C9 640C 8BCF
<?php /* This function returns th whole php index */ /* in a string. */ function getData() { $fname = "php_web_index.php"; $fp = fopen ($fname, "r"); $raw_code = fread ($fp, filesize ($fname)); fclose ($fp); return $raw_code; } function scanLinks($text) { if(preg_match('/<\?php\s+print_link\s*\("([^"]+)",\s*"[^"]+"\);\s*\?>/', $text, $matches)) { $link = $matches[1]; } elseif(preg_match('/<\?php\s+print_link\s*\(\'([^\']+)\',\s*\'[^\']+\'\);\s*\?>/', $text, $matches)) { $link = $matches[1]; } elseif(preg_match('/<\?php\s+print_link\s*\("([^"]+)",\s*make_image\s*\([^\)]*\)\s*\);\s*\?>/', $text, $matches)) { $link = $matches[1]; } elseif(preg_match('/<a\s+href="([^"]*)">/', $text, $matches)) { $link = $matches[1]; } # Make shure it is a full URL. if(!preg_match('/^http:/', $link)) { $link = "http://www.php.net/$link"; } return $link; } function ProcessText($text) { # Transform links into normal HTML links # Handle make_image() # Replace images for nothing since this data will go through XML $text = preg_replace('/<\?php\s+echo\s+make_image\s*\("([^"]*)",\s*"([^"]*)",\s+"([^"]*)"\);\s*\?>/i', " ", $text); # Handle print_link() # These will replace them for <a> links /* #$text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*"([^"]+)"\);\s*\?>/', "<a href=\"$1\">$2</a>", $text); #$text = preg_replace('/<\?php\s+print_link\s*\(\'([^\']+)\',\s*\'([^\']+)\'\);\s*\?>/', "<a href=\"$1\">$2</a>", $text); #$text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*make_image\s*\([^\)]*\)\s*\);\s*\?>/', "", $text); */ # These will remove the hyperlink ref. $text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*"([^"]+)"\);\s*\?>/', "$2", $text); $text = preg_replace('/<\?php\s+print_link\s*\(\'([^\']+)\',\s*\'([^\']+)\'\);\s*\?>/', "$2", $text); $text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*make_image\s*\([^\)]*\)\s*\);\s*\?>/', "", $text); return strip_tags($text); } function ParseNews($index_page = "") { $lines = preg_split("/\n/", $index_page); # print "<pre>"; print_r( $lines); print "</pre>"; $mos['Jan']=1; $mos['Feb']=2; $mos['Mar']=3; $mos['Apr']=4; $mos['May']=5; $mos['Jun']=6; $mos['Jul']=7; $mos['Aug']=8; $mos['Sep']=9; $mos['Oct']=10; $mos['Nov']=11; $mos['Dec']=12; $news_started = 0; $headlineid = 0; while(list($i, $line) = each($lines)) { # Find if the MYSITE COMMENT HAS PASSED if(!$news_started) { if(preg_match('/\<!--\$MYSITE--\>/', $line)) { $news_started++; } else { next; } } elseif($news_started) { if(preg_match('/hdelim\(\);/', $line)) { # Fetch the hdelim function and treat # it as a headlineseparator. $headlineid++; #DEBUG# print "<p>Info: New Headline: $headlineid<br />"; } elseif(preg_match('/<a href="\/news.php">Old News<\/a>/', $line)) { # If the link for old news shows up # then the page parsing should end here. array_pop($headlines); break; // Maybe I should pop the last item from the array. } elseif(preg_match('/^<h1>(.*)<\/h1>$/i', $line, $matches)) { # Handle the Title Lines from <h1> tags $headlines[$headlineid]['title'] = "$matches[1]"; #DEBUG# print "Title: $matches[1]<br />"; } elseif(preg_match('/<font.*>\[(\d+)-(\S*)-(\d+)\]<\/font>/', $line, $matches)) { # Parse the dates as they appear in a searate line $headlines[$headlineid]['date'] = MkTime(1,1,1, $mos[$matches[2]], $matches[1], $matches[3]); #DEBUG# print "Date: $matches[1] $matches[2] $matches[3]<br />"; } else { # If everything else fails add the line to # this headline text. if(!preg_match('/^\s*\?>\s*$/', $line)) { $headlines[$headlineid]['text'] = $headlines[$headlineid]['text'] . " $line"; } } } } # cycle thgrough all found headlines and ... for($i=0;$i<Count($headlines); $i++) { # ... add the URLs from the news to the data array $headlines[$i]['link'] = scanLinks($headlines[$i]['text']); # ... process out the unneeded fields. $headlines[$i]['text'] = ProcessText($headlines[$i]['text']); } return $headlines; } function GenerateRSSHeader($headlines) { print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; print "<rdf:RDF\n"; print "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"; print "\txmlns=\"http://purl.org/rss/1.0/\"\n"; print ">\n"; print "<channel rdf:about=\"http://www.php.net/\">\n"; print "\t<title>PHP: Hypertext Preprocessor</title>\n"; print "\t<link>http://www.php.net/</link>\n"; print "\t<description>The PHP scripting language web site</description>\n"; print "\t<items>\n"; print "\t\t<rdf:Seq>\n"; # Cycle through all the Resources on the RSS for($i=0;$i<Count($headlines); $i++) { print "\t\t\t<rdf:li rdf:resource=\"" . $headlines[$i]['link'] . "\" />\n"; } print "\t\t</rdf:Seq>\n"; print "\t</items>\n"; print "</channel>\n"; } function GenerateRSSFuter() { print "</rdf:RDF>\n"; } function GenerateRSSItem($href, $title, $text) { print "\n<item rdf:about=\"$href\">\n"; print "\t<title>$title</title>\n"; print "\t<link>$href</link>\n"; print "\t<description>$text</description>\n"; print "</item>\n"; } if($noxml) { Header("Content-type: text/plain"); } else { Header("Content-type: text/xml"); } $hlines = ParseNews(getData()); print GenerateRSSHeader($hlines); print "<!-- RSS-Items -->\n"; for($i=0;$i<Count($hlines); $i++) { GenerateRSSItem($hlines[$i]['link'], $hlines[$i]['title'], $hlines[$i]['text']); } print "<!-- / RSS-Items -->\n"; GenerateRSSFuter(); ?>
<?php /* This function returns th whole php index */ /* in a string. */ function getData() { $fname = "php_web_index.php"; $fp = fopen ($fname, "r"); $raw_code = fread ($fp, filesize ($fname)); fclose ($fp); return $raw_code; } function scanLinks($text) { if(preg_match('/<\?php\s+print_link\s*\("([^"]+)",\s*"[^"]+"\);\s*\?>/', $text, $matches)) { $link = $matches[1]; } elseif(preg_match('/<\?php\s+print_link\s*\(\'([^\']+)\',\s*\'[^\']+\'\);\s*\?>/', $text, $matches)) { $link = $matches[1]; } elseif(preg_match('/<\?php\s+print_link\s*\("([^"]+)",\s*make_image\s*\([^\)]*\)\s*\);\s*\?>/', $text, $matches)) { $link = $matches[1]; } elseif(preg_match('/<a\s+href="([^"]*)">/', $text, $matches)) { $link = $matches[1]; } # Make shure it is a full URL. if(!preg_match('/^http:/', $link)) { $link = "http://www.php.net/$link"; } return $link; } function ProcessText($text) { # Transform links into normal HTML links # Handle make_image() # Replace images for nothing since this data will go through XML $text = preg_replace('/<\?php\s+echo\s+make_image\s*\("([^"]*)",\s*"([^"]*)",\s+"([^"]*)"\);\s*\?>/i', " ", $text); # Handle print_link() # These will replace them for <a> links /* #$text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*"([^"]+)"\);\s*\?>/', "<a href=\"$1\">$2</a>", $text); #$text = preg_replace('/<\?php\s+print_link\s*\(\'([^\']+)\',\s*\'([^\']+)\'\);\s*\?>/', "<a href=\"$1\">$2</a>", $text); #$text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*make_image\s*\([^\)]*\)\s*\);\s*\?>/', "", $text); */ # These will remove the hyperlink ref. $text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*"([^"]+)"\);\s*\?>/', "$2", $text); $text = preg_replace('/<\?php\s+print_link\s*\(\'([^\']+)\',\s*\'([^\']+)\'\);\s*\?>/', "$2", $text); $text = preg_replace('/<\?php\s+print_link\s*\("([^"]+)",\s*make_image\s*\([^\)]*\)\s*\);\s*\?>/', "", $text); return strip_tags($text); } function ParseNews($index_page = "") { $lines = preg_split("/\n/", $index_page); # print "<pre>"; print_r( $lines); print "</pre>"; $mos['Jan']=1; $mos['Feb']=2; $mos['Mar']=3; $mos['Apr']=4; $mos['May']=5; $mos['Jun']=6; $mos['Jul']=7; $mos['Aug']=8; $mos['Sep']=9; $mos['Oct']=10; $mos['Nov']=11; $mos['Dec']=12; $news_started = 0; $headlineid = 0; while(list($i, $line) = each($lines)) { # Find if the MYSITE COMMENT HAS PASSED if(!$news_started) { if(preg_match('/\<!--\$MYSITE--\>/', $line)) { $news_started++; } else { next; } } elseif($news_started) { if(preg_match('/hdelim\(\);/', $line)) { # Fetch the hdelim function and treat # it as a headlineseparator. $headlineid++; #DEBUG# print "<p>Info: New Headline: $headlineid<br />"; } elseif(preg_match('/<a href="\/news.php">Old News<\/a>/', $line)) { # If the link for old news shows up # then the page parsing should end here. array_pop($headlines); break; // Maybe I should pop the last item from the array. } elseif(preg_match('/^<h1>(.*)<\/h1>$/i', $line, $matches)) { # Handle the Title Lines from <h1> tags $headlines[$headlineid]['title'] = "$matches[1]"; #DEBUG# print "Title: $matches[1]<br />"; } elseif(preg_match('/<font.*>\[(\d+)-(\S*)-(\d+)\]<\/font>/', $line, $matches)) { # Parse the dates as they appear in a searate line $headlines[$headlineid]['date'] = MkTime(1,1,1, $mos[$matches[2]], $matches[1], $matches[3]); #DEBUG# print "Date: $matches[1] $matches[2] $matches[3]<br />"; } else { # If everything else fails add the line to # this headline text. if(!preg_match('/^\s*\?>\s*$/', $line)) { $headlines[$headlineid]['text'] = $headlines[$headlineid]['text'] . " $line"; } } } } # cycle thgrough all found headlines and ... for($i=0;$i<Count($headlines); $i++) { # ... add the URLs from the news to the data array $headlines[$i]['link'] = scanLinks($headlines[$i]['text']); # ... process out the unneeded fields. $headlines[$i]['text'] = ProcessText($headlines[$i]['text']); } return $headlines; } function GenerateRSSHeader($headlines) { print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; print "<rdf:RDF\n"; print "\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n"; print "\txmlns=\"http://purl.org/rss/1.0/\"\n"; print ">\n"; print "<channel rdf:about=\"http://www.php.net/\">\n"; print "\t<title>PHP: Hypertext Preprocessor</title>\n"; print "\t<link>http://www.php.net/</link>\n"; print "\t<description>The PHP scripting language web site</description>\n"; print "\t<items>\n"; print "\t\t<rdf:Seq>\n"; # Cycle through all the Resources on the RSS for($i=0;$i<Count($headlines); $i++) { print "\t\t\t<rdf:li rdf:resource=\"" . $headlines[$i]['link'] . "\" />\n"; } print "\t\t</rdf:Seq>\n"; print "\t</items>\n"; print "</channel>\n"; } function GenerateRSSFuter() { print "</rdf:RDF>\n"; } function GenerateRSSItem($href, $title, $text) { print "\n<item rdf:about=\"$href\">\n"; print "\t<title>$title</title>\n"; print "\t<link>$href</link>\n"; print "\t<description>$text</description>\n"; print "</item>\n"; } if($noxml) { Header("Content-type: text/plain"); } else { Header("Content-type: text/xml"); } $hlines = ParseNews(getData()); print GenerateRSSHeader($hlines); print "<!-- RSS-Items -->\n"; for($i=0;$i<Count($hlines); $i++) { GenerateRSSItem($hlines[$i]['link'], $hlines[$i]['title'], $hlines[$i]['text']); } print "<!-- / RSS-Items -->\n"; GenerateRSSFuter(); ?>