プラグイン:MultiCounterXの紹介2

 昨日に引き続きMultiCounterXの紹介記事です。勢いがあるうちに書き留めておかないと。。

4.MultiCounterXのディレクトリーツリー

 本プラグインのディレクトリーツリーは下記のとおりです。オレンジ色のファイルは5項で説明しているもの、ピンク色のファイルは5項で一部説明しているものです。

$MT_DIR/
|__ plugins/
|  |__ MultiCounterX/
|     |__ config.yaml
|     |__ mcounterx.cgi
|     |__ lib/
|     |  |__ MultiCounterX.pm
|     | |__ MultiCounterX/
|     |  | |__ L10N.pm
|     |  |  |__ Tags.pm
|     |  | |__ L10N/
|     |  |  |__ en_us.pm
|     |  |     |__ ja.pm
|     |  |__ logs/
|     |  |  |__ (記事毎のアクセス履歴ファイル群)
|     |  |__ countfiles/
|     |     |__ (記事毎のカウンター値保持ファイル群)
|     |__ tmpl/
|     |  |__ mc_setting_blog.tmpl

5.主要ファイルの説明

(1)config.yaml
本プラグインのレジストリ設定を記述したファイル

applications:
  multicounterx: --アプリケーションのID
    handler: MultiCounterX --perlパッケージ名
    script: $MultiCounterX::MultiCounterX::script
      --script名を返すcoderef。上記パッケージ内で実体定義
    cgi_path: $MultiCounterX::MultiCounterX::cgi_path
      --CGIのパスを返すcoderef。上記パッケージ内で実体定義
    methods:
      main0: $MultiCounterX::MultiCounterX::main0
      --プラグインとして呼ばれるメソッド名とcoderef
        上記パッケージ内で実体定義
blog_config_template: mc_setting_blog.tmpl

      --ブログレベルの設定画面を与えるテンプレートファイル指定
settings: --ブログレベル設定画面の設定項目毎の初期値・スコープ(=ブログ
    MCFontfile:
       default: /usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf
       scope: blog
    MCCGIURL:
       default: /mt/plugins/MultiCounterX/mcounterx.cgi
       scope: blog
    SESTIMEOUT:
        default: 900
        scope: blog
    UNAMEVAL:
        default: denden
        scope: blog
    XSIZE:
        default: 360
        scope: blog
    YSIZE:
        default: 51
        scope: blog
    FILLCOLOR:
        default: 255,255,255
        scope: blog
    BORDERCOLOR:
        default: 255,255,255
        scope: blog
    SIZE:
        default: 10
        scope: blog
    M1:
        default: Visits Today:%s Yesterday:%s Total:%s
        scope: blog
・・・・(省略)・・・・
    M3COLOR:
        default: 11,59,28
        scope: blog
    M3XPOS:
        default: 2
        scope: blog
    M3YPOS:
        default: 42
        scope: blog
tags:
    function:
        CounterMessagesX: $MultiCounterX::MultiCounterX::Tags::_hdlr_counter_output
   --ファンクションタグ名とタグへの出力関数へのcoderef
     Tag.pm内に実体定義される。
     ファンクションタグを利用するテンプレートを使用するドキュメントを
     再構築するたびに呼び出される。

(2)mc_setting_blog.tmpl
ブログレベル設定画面の設定項目のhtmlフォームを定義するテンプレートファイル

<mtapp:setting
 id="MCFontfile"
 label="<_trans phrase='MCXFont'>"> 
--ローカライゼーション用
 
   <input type="text" name="MCFontfile" id="MCFontfile" value="<mt:var name="MCFontfile" escape="html">" size="30" />
</mtapp:setting>
・・・(以下省略)・・・

(3)mcounterx.cgi
 MT::Bootstrapを利用するCGIファイル。コードはMT::BootstrapのAppメンバー変数にアプリケーションを登録するだけ。

#!/usr/bin/perl -w
use strict;
use lib $ENV{MTHOME} ? "$ENV{MTHOME}/lib" : 'lib';
use lib '../../lib';
use MT::Bootstrap App => 'MultiCounterX';

(4)MultiCounterX.pm
プラグイン本体perlパッケージ

# ===================================================================
# ★ MultiCounterX ★
# ===================================================================
package MultiCounterX;
#use strict;
use base 'MT::App'; --MT::Appを継承します
use GD; --GDモジュールのstringFT関数を使ってカウンターイメージを作ります。
use Jcode;
#use URI::Escape;
use File::Path;
# クラス変数定義
our($im);
our(
・・・(パッケージ変数定義)・・・
);
sub main0 {
 my $app = shift;
 --MT::Appオブジェクトreference
・・・(別項にて解説)・・・

(5)Tags.pm

package MultiCounterX::Tags;
use strict;
use URI::Escape;
use utf8; # 以後全ての文字がutf8(フラグ付き)となる
use Encode;
sub _hdlr_counter_output {
    my ($ctx, $args) = @_;
   --$ctxはMT::Template::Contextオブジェクトreference

    my $blog_id = 'blog:' . $ctx->stash('blog_id');
   --$ctxのstashメソッドブログID番号を取得
    my $plugin = MT->component('MultiCounterX');
   --MTのcomponentメソッドでプラグインオブジェクトreferenceを取得
my $MCFontfile = $plugin->get_config_value('MCFontfile', $blog_id);
my $CGIURL = $plugin->get_config_value('MCCGIURL', $blog_id);
・・・(省略)・・・
my $entry = $ctx->stash('entry')
        || $ctx->error(MT->translate('You used an tag outside of the proper context.'));
   --$ctx->stash('entry')で今処理をしているEntryオブジェクト
     (記事コンテキスト)を取得

my $kiziID = $entry->id() || return;

   --$entry->id()で今処理をしているEntryオブジェクトのID(記事ID)を取得
my $retst =
"
return '<img src = "' . $CGIURL .'?' . $retst .'"/>';
   --imgタグを出力。CGIのURLを指定しクエリーデータを付加

}

(6)L10N.pm,en_us.pm,ja.pm
 別項にて解説