このReachOutスクリプトにはRSS配信を自動生成する機能は今のところ無い。
RSS配信を自動生成するには、どうすれば良いか考えた。
findコマンドはコンテンツ数が増えたら遅くなりそうなので却下。
データベース機能は、機能てんこ盛りの入口になってしまう気がするので却下。
なるべく楽したいと思い、gitを利用することにした。
スクリプトのバージョン管理に git を使っているので、これを利用することを思いつく。
ルートディレクトリ ─┬─ index.php │ ├─ top.php │ ├─ git.sh (1) │ ├─ layout ─── default.php │ ├─ css_df ─── default.css │ └─ feed ─┬─ index.php (4) │ ├─ update.txt (2) │ └─ layout ─── default.php (3)
レンタルサーバに git が無ければインストールする。
Bluehostはgitがインストールされていた。
(1) git.sh
#!/bin/sh git add -A git commit -m "ReachOut `date -R`" git log --name-status | grep ^A[[:space:]] | head -n 100 > feed/update.txt
ファイル属性を 700 にするのを忘れずに!
例では feed というディレクトリを作成し、その配下にレイアウトファイル用の layout ディレクトリを作成。
レンタルサーバにシェルでログインして、git.sh を実行する。
$ cd public_html $ ./git.sh
必ずReachOutスクリプトのあるルートディレクトリで実行する。
git.sh を実行すると、以下のようなファイルができる。
追加されたファイルの一覧である。
(2) update.txt
A documentation/manual/contents.html A documentation/manual/css.html A documentation/manual/header.html A documentation/manual/index.html A documentation/manual/layout.html A downloads/index.html
このテキストファイルをPHPのスクリプトで読んで、RSSに変換する。
rss 2.0 の xml のテンプレートを作成。
(3) default.php
<?php
header("Content-Type: text/xml; charset=" . $charset);
echo '<?xml version="1.0" encoding="' . $charset . '"?>' . "\n";
?>
<rss version="2.0">
<channel>
<title><?php echo $siteName;?></title>
<link><?php echo $host . $homeUrl;?></link>
<description>ウェブページを作るためのPHPスクリプト。ヘッダ、フッタを付加してHTMLを出力する。低機狽セけど軽快なスクリプト!</description>
<lastBuildDate><?php echo date("r");?></lastBuildDate>
<language>ja</language>
<?php echo $contents;?>
</channel>
</rss>
これをディレクトリ layout に配置
$contents に <item> タグを埋め込むことで rss 2.0 の xml 文書を作る
<item>
<title>タイトル</title>
<link>http://www.hogehoge.com/exsample.html</link>
<description>ここに要約</description>
<pubDate>Thu, 20 Dec 2011 16:01:07 +0900</pubDate>
</item>
(4) index.php
<?php
// RSS配信を除外するディレクトリ
$rssDir = array("tmp","lib");
// RSS元データ
$rssUpdate = "/virtual/hogehoge/public_html/feed/update.txt";
// RSS配信最大アイテム数
$rssMax = 10;
// 更新ファイル
$rssFiles = array();
// RSS元データを読み込んで処理
if (is_readable($rssUpdate)) {
$fp = fopen ($rssUpdate, "r");
$ln= 0;
while ((($line = fgets($fp)) and ($ln < $rssMax))) {
$keywords = preg_split("/[s]+/", $line);
$item = $keywords[1];
if ($item == "index.php") continue;
// 除外するディレクトリ
$hit = false;
foreach ($rssDir as $val) {
$pattern = "@^" . $val . "/@";
if (preg_match($pattern, $item, $matches)) {
$hit = true;
}
}
if ($hit) continue;
// 既に追加されているか
if (array_search($item, $rssFiles) !== false) continue;
// アイテム出力
$file = $homeRealPath . DS . $item;
if (!is_readable($file)) continue;
$res = get_config(file_get_contents($file));
if (!$res['rss']) continue;
$url = $host . $homeDir . DS . $item;
$mtime = date ("r", filemtime($file));
echo "<item>";
echo "<title>{$res['title']}</title>";
echo "<link>{$url}</link>";
echo "<description>";
if (!$res['description']) {
ob_start();
include($file);
$res['description'] = ob_get_clean();
}
echo htmlspecialchars($res['description'], ENT_QUOTES, $charset);
echo "</description>";
echo "<pubDate>{$mtime}</pubDate>";
echo "</item>";
$ln++;
$rssFiles[] = $item;
}
fclose ($fp);
}
// ファイルから設定値を取得
function get_config($contents) {
$res['title'] = "";
// タイトル
if (preg_match('/config_title:"([^"]+)"/', $contents, $matches)) {
$res['title'] = $matches[1];
}
// 要約
$res['description'] = "";
if (preg_match('/config_rss_description:"([^"]+)"/', $contents, $matches)) {
$res['description'] = $matches[1];
}
// RSSフラグ
$res['rss'] = false;
if (preg_match('/config_rss:"([^"]+)"/', $contents, $matches)) {
if ($matches[1] == "yes") $res['rss'] = true;
}
return $res;
}
?>
ダウンロード:index.php
テキストファイルのフルパスを $rssUpdate に 指定。
すべてのファイルがRSS配信されては、こまるので以下のコメントが書かれているファイルだけをRSSのアイテムとする
<!-- config_rss:"yes" -->
<!-- config_rss_description:"
<h2>RSS配信は手動</h2>
<h3>RSSを出力する機能が欲しい</h3>
<p>このReachOutスクリプトにはRSS配信を自動生成する機能は今のところ無い。</p>
<p>RSS配信を自動生成するには、どうすれば良いか考えた。</p>
<ul>
<li>OSのfindコマンドで更新されたファイルを探す?</li>
<li>コンテンツの更新を覚える機能を付ける(データベース?)</li>
</ul>
<p>findコマンドはコンテンツ数が増えたら遅くなりそうなので却下。</p>
<p>データベース機能、機能てんこ盛りの入口になってしまう気がするので却下。</p>
<p>なるべく楽したいと思い、gitを利用することにした。</p>
" -->
<!-- config_rss:"yes" --> と書けばRSS配信される。
config_rss_description は短く要約したい時使う。このコメントは書かなくても良い。書かない場合は、コンテンツの全文が description としてアイテムになる。
後は、コンテンツに追加があった都度、(1) git.sh を実行する。
gitコマンドをコミットするという手動は残ったが、簡単にRSS配信ができるようになったので良しとする。(^_^;
git を使わない場合、もしくはレンタルサーバでシェルが使えない場合は、以下の方法で作成可能