find -> rss

車輪の再発明かもしれないが、最近更新されたファイルをRSSで出力してみた。本当はファイル名をURIにしたりしないといけないけど、以下では省略。後で考えると、RSSの出力部分はPEARにありそう。

<?php
/**
 * findからRSSを作成する。
 */

$rss_url = '';
$description = 'newly-arrived files';

/** find の基底ディレクトリ */
$base_dir = '';
$mtime = 7;

$cmd = "find $base_dir -mtime -$mtime";

exec($cmd, $output);

$docarray = array();

foreach($output as $newfile)
{
    if (is_dir($newfile))
    {
        continue;
    }
    $modtime = filemtime($newfile);
    $docarray[$newfile] = $modtime;
}
ksort($docarray);

// これ以降で出力
header('Content-Type: application/xml;charset=EUC-JP');

// <? が PHP の開始とされてしまうshort_open_tag=onの環境だったので echo で
echo '<?xml version="1.0" encoding="EUC-JP"?>' . "\n";
echo '<?xml-stylesheet href="rss.xsl" type="text/xsl" media="screen"?>' . "\n";
?>
<!-- generator="newly-arrived files RSS generator" -->
<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
>
<channel rdf:about="<?php echo htmlspecialchars($rss_url); ?>">
    <title><?php echo $description; ?></title>
    <link><?php echo htmlspecialchars($rss_url); ?></link>
    <description><?php echo $description; ?></description>
    <dc:language>ja</dc:language>
    <dc:date><?php echo date("Y-m-d\TH:i:s+09:00"); ?></dc:date>
    <items>
        <rdf:Seq>
<?php
    foreach ($docarray as $fname => $mtime)
    {
        $uri = htmlspecialchars($fname);
        echo "<rdf:li rdf:resource=\"$uri\" />\n";
    }
?>
        </rdf:Seq>
    </items>
</channel>
<?php
    foreach ($docarray as $fname => $mtime)
    {
        $uri = htmlspecialchars($fname);
        $title = htmlspecialchars($fname);
        $link = htmlspecialchars($fname);
        $date = date("Y-m-d\TH:i:s+09:00", $mtime);
        $creator = '';
        echo "<item rdf:about=\"$uri\"><title>$title</title><link>$link</link><dc:date>$date</dc:date><dc:creator>$creator</dc:creator></item>\n";
    }
?>
</rdf:RDF>