前幾天看到了 TweetBacks 這個新 idea ,實用性可能很低,但相當有趣,因此今天趁著有空寫了給 Habari 用的版本。目前完成度還不高,不過 TweetBacks 的功能都有了。
為了讓此 Plugin 得以運作,我修改了 Habari Core ,請參閱安裝前須知。目前 Habari 暫時還沒有自訂 Comment 內容型態的功能, Post 則有,這個功能未來計畫會加入,因此現在只能用這種克難作法。
Things to Do Before Installation
請先修改 comment.php, comments.php 兩個檔案,方能使用此 Plugin 。
To use this plugin, please modify your comment.php and comments.php as below.
comment.php
const COMMENT = 0; const PINGBACK = 1; const TRACKBACK = 2; const TWEETBACK = 3; // Add this linepublic static function list_comment_types( $refresh = false ) { if ( ( ! $refresh ) && ( ! empty( self::$comment_type_list ) ) ) { return self::$comment_type_list; } self::$comment_type_list = array( self::COMMENT => 'comment', self::PINGBACK => 'pingback', self::TRACKBACK => 'trackback', self::TWEETBACK => 'tweetback', // Add this line ); return self::$comment_type_list; }comments.php
private function sort_comments() { $type_sort = array( Comment::COMMENT => 'comments', Comment::PINGBACK => 'pingbacks', Comment::TRACKBACK => 'trackbacks', Comment::TWEETBACK => 'tweetbacks', // Add this line ); ...public function __get( $name ) { switch ( $name ) { case 'count': return count( $this ); case 'approved': case 'unapproved': case 'moderated': case 'comments': case 'pingbacks': case 'trackbacks': case 'tweetbacks': // Add this line return new Comments( $this->only( $name ) ); } }
Get it!
目前只能透過 SVN 取得。
You can get TweetSuite via SVN.
Known Issues
- 有時(或者該說是經常?)資料庫會儲存到重複的 Tweetbacks ,但這種情況是一次就出現很多重複的 Tweetbacks。
- 有些短網址是大小寫有別的,而 Twitter Search 不區分大小寫,於是可能會抓到一些無關的 Tweetbacks 。(尤其是 bit.ly 的短網址超容易發生這種事)
Notes
此 Plugin 的運作流程依序為:
- 使用者讀取單篇文章(Post)
- 讀取完畢時, TweetSuite 才開始執行,這有兩層考量:在讀取文章前執行可能導致讀取變得非常慢、讀取文章前還沒有 Post 物件能用 XD 但這種作法的缺點就是最新的 TweetBacks 會在第二次載入時才出現。
- TweetSuite 首先會讀取這篇文章的短網址資訊,如果資訊不存在,便會到各個短網址服務產生這篇文章的短網址,然後儲存到資料庫。如果是有缺漏,便只會重新嘗試產生遺漏的短網址。(寫這個的時候我發現 tr.im 這個服務,程式寫得不太好…… 因為它不會檢查資料庫中是否已經存在重複的網址,每次都將給你一個新產生的網址)
- 得到這些短網址後, TweetSuite 會將它們串接起來丟到 Twitter Search 作聯集搜尋,最後將得到的搜尋結果存入
comments資料表。其實在執行 Twitter Search 之前, TweetSuite 會先取得這篇文章最近一次的 TweetBacks 的 ID ,然後就能加入 Twitter Search 的參數中,讓 Twitter Search 只回傳較新的 Tweets ,因此資料庫中的 TweetBacks 不會重複。 - TweetSuite 會紀錄這一次執行的時間,接下來一個小時內同樣的文章將不會重複執行。

