RSS配信は手動

RSSを出力する機能が欲しい

このReachOutスクリプトにはRSS配信を自動生成する機能は今のところ無い。

RSS配信を自動生成するには、どうすれば良いか考えた。

  • OSのfindコマンドで更新されたファイルを探す?
  • コンテンツの更新を覚える機能付ける(データベース?)

findコマンドはコンテンツ数が増えたら遅くなりそうなので却下。

データベース機能は、機能てんこ盛りの入口になってしまう気がするので却下。

なるべく楽したいと思い、gitを利用することにした。

構想

スクリプトのバージョン管理に git を使っているので、これを利用することを思いつく。

  1. gitで更新されたファイルを取得
  2. 更新されたファイルをRSSに変換

作成手順

全体図

ルートディレクトリ ─┬─ 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)

1.gitをインストール

レンタルサーバに git が無ければインストールする。

参考:coreserverでgitを使う

Bluehostはgitがインストールされていた。

2.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 にするのを忘れずに!

3.RSS配信スクリプトのディレクトリを作成

例では feed というディレクトリを作成し、その配下にレイアウトファイル用の layout ディレクトリを作成。

4.gitを実行するスクリプトを実行

レンタルサーバにシェルでログインして、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に変換する。

5.レイアウトファイルを作成

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 例.

<item>
<title>タイトル</title>
<link>http://www.hogehoge.com/exsample.html</link>
<description>ここに要約</description>
<pubDate>Thu, 20 Dec 2011 16:01:07 +0900</pubDate>
</item>

6.テキストファイルを読んで 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");
    
$ln0;
    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 に 指定。

7.コンテンツに以下のコメントを追加

すべてのファイルが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 としてアイテムになる。

以上でRSS配信完成!

後は、コンテンツに追加があった都度、(1) git.sh を実行する。

gitコマンドをコミットするという手動は残ったが、簡単にRSS配信ができるようになったので良しとする。(^_^;

git を使わない場合、もしくはレンタルサーバでシェルが使えない場合は、以下の方法で作成可能

  • (2) update.txt に ファイル名を直接書く
  • テキストファイルは使わず、(4) index.php に <item> タグを直接書く