PEAK XOOPS - Improving resource.db.php by hooking XoopsTpl on XCL2.1 in englishin japanese

Archive | RSS |
XOOPS
XOOPS : Improving resource.db.php by hooking XoopsTpl on XCL2.1
Poster : GIJOE on 2008-01-10 04:53:50 (16322 reads)

in englishin japanese
resource.(type).php is a smarty plugin controlling:
- which template source is adopted
- how new is the template

Since default template resource of XOOPS is "db:", resource.db.php is the most important file.

Default resource.db.php of XCL2.1 has problems like:
(1) With "default" theme, DB templates will be broken easily
(2) Templates under theme will stay alive even after the theme is changed
(3) Paths of templates under theme should be flat because "db:" is flat.
(4) Directory name of "template under theme" is not unified. (templates or modules)
(5) A template file under theme is not recognized its deletion
(6) A template file under theme is not recognized its addition if the compile cache is newer than the file

I've made a new resource.db.php solving the problems (1),(2),(3), and (4).

Of course, it is not a hack.
Just using "XoopsTpl Hooking" by preload, we can put the file under XOOPS_TRUST_PATH/libs/smartyplugins/

You can test it right now.
Just try hodajuku distribution of nightly build (not a preview version).
The theme of "skel_flex" has many templates.
The tempaltes will be adopted with the theme, hence they will never adopted after switching the theme like "chic_black".


<?php
/*
 * Smarty plugin
 * ------------------------------------------------------------- 
 * File:     resource.db.php
 * Type:     resource
 * Name:     db
 * Purpose:  Fetches templates from a database
 * -------------------------------------------------------------
 */

function smarty_resource_db_systemTpl($tpl_name)
{
    // Replace Legacy System Template name to Legacy Module Template name
    static $patterns = null;
    static $replacements = null;
    if (!$patterns) {
        $root=&XCube_Root::getSingleton();
        $systemTemplates = explode(',',$root->getSiteConfig('Legacy_RenderSystem','SystemTemplate',''));
        $prefix = $root->getSiteConfig('Legacy_RenderSystem','SystemTemplatePrefix','legacy');
        $patterns = preg_replace('/^\s*([^\s]*)\s*$/e', '"/".preg_quote("\1","/")."/"', $systemTemplates);
        $replacements = preg_replace('/^\s*system_([^\s]*)\s*/', $prefix.'_\1', $systemTemplates);
    }
    if ($patterns) {
        $tpl_name = preg_replace($patterns, $replacements,$tpl_name);
    }
    return $tpl_name;
}

function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
{
	$tpl_name = smarty_resource_db_systemTpl($tpl_name);
	if ( !$tpl = smarty_resource_db_tplinfo( $tpl_name ) ) {
		return false;
	}
	if ( is_object( $tpl ) ) {
		$tpl_source = $tpl->getVar( 'tpl_source', 'n' );
	} else {
		$tpl_source = file_get_contents( $tpl ) ;
	}
	return true;
}

function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
	$tpl_name = smarty_resource_db_systemTpl($tpl_name);
	if ( !$tpl = smarty_resource_db_tplinfo( $tpl_name ) ) {
		return false;
	}
	if ( is_object( $tpl ) ) {
		$tpl_timestamp = $tpl->getVar( 'tpl_lastmodified', 'n' );
	} else {
		$tpl_timestamp = filemtime( $tpl );
	}
	return true;
}

function smarty_resource_db_secure($tpl_name, &$smarty)
{
    // assume all templates are secure
    return true;
}

function smarty_resource_db_trusted($tpl_name, &$smarty)
{
    // not used for templates
}

// return object(XoopsTplfile) or string(filepath)
function smarty_resource_db_tplinfo( $tpl_name )
{
	static $cache = array();
	global $xoopsConfig;

	// First, check the cache
	if ( isset( $cache[$tpl_name] ) ) {
		return $cache[$tpl_name];
	}

	$tplset = isset( $xoopsConfig['template_set'] ) ? $xoopsConfig['template_set']: 'default' ;
	$theme = isset( $xoopsConfig['theme_set'] ) ? $xoopsConfig['theme_set'] : 'default';

	// Second, check templates under themes/(theme)/templates/ (file template)
	$filepath = XOOPS_THEME_PATH . '/' . $theme . '/templates/' . $tpl_name ;
	if ( file_exists( $filepath ) ) {
		return $cache[$tpl_name] = $filepath ;
	}

	// Third, find a DB template of the selected tplset
	$tplfile_handler =& xoops_gethandler('tplfile');
	$tplobj = $tplfile_handler->find( $tplset, null, null, null, $tpl_name, true);	if ( empty( $tplobj ) ) {
		// Forth, find a DB template in default tplset
		$tplobj = $tplfile_handler->find( 'default', null, null, null, $tpl_name, true);
		if( empty( $tplobj ) ) return false ;
	}
	return $cache[$tpl_name] = $tplobj[0];
}
?>


Related articles
Printer friendly page Send this story to a friend

Comments list

hi-debow  Posted on 2008/1/21 21:54
GIJOEさん、毎度ありがとうございます。

Quote:
preload/HdXoopsTplHook.class.phpとresource.db.phpの2ファイルですけどね

そうだったのですか? 前の記事をすっかり忘れてしまっておりました・・・

ちなみにpreload/HdXoopsTplHook.class.phpは入れてませんでした。

ですが無い時は無い時で面白い動作に気が付きましたので良かったです。
なんせテーマ内テンプレートを配布するには最適に近く、新しいものしか読み込まなくなりますから。(でもaltsys等で編集されるとだめですけど)
GIJOE  Posted on 2008/1/21 4:30
hi-debowさん、こんにちは。

Quote:

今回のような1ファイル添付のみが良いです。
preload/HdXoopsTplHook.class.phpとresource.db.phpの2ファイルですけどね

Quote:
テンプレート配布に関してテストしておりますが、配布用のテーマ内テンプレートファイル保存時期が現在使用中のDBテンプレートより古いと反映されませんでした。

それが、本記事の(6)ですね。
確かに(5)と(6)まで解決してナンボという部分はあるので、処理速度と使い勝手を両立できる、何かうまい方法がないか考え中です。
(常に強制コンパイルってのだけは避けたい)
hi-debow  Posted on 2008/1/17 14:53 | Last modified
GIJOEさん毎度ありがとうございます。

Quote:
おそらくは、ローカルの編集環境の問題だと思いますよ。
確認いたしましたら、デフォルトの保存形式がShift_JISへ変更したままになっておりました。
もちろん保存形式をEUCへ再設定して日本語を記入しても文字化けしませんでした。

こちらの確認不足でした、すみません。

Quote:
今回のはX2がもともと持っていた機能の改善に過ぎないのですが、同じtrustdirname同士の共通テンプレートのクラス化、となれば、それなりに大規模な工事が必要です。

そうですか・・・大規模なものはダメですね。色々なミスを招きそうですし。
今回のような1ファイル添付のみが良いです。
シンプルかつ効果絶大って所がインパクトありました。

テンプレート配布に関してテストしておりますが、配布用のテーマ内テンプレートファイル保存時期が現在使用中のDBテンプレートより古いと反映されませんでした。
GIJOE  Posted on 2008/1/17 5:22
hi-debow さん、こんにちは。

Quote:

ローカルでX.C.Lを導入してテストをしましたが、theme内DBテンプレートファイルへ日本語を記入している場合は文字化けします。
おそらくは、ローカルの編集環境の問題だと思いますよ。
登録時には、エンコーディング変換は一切かかりません。
オリジナルテンプレートには日本語がないので、デフォルトのShift_JISとして開かれて、そのままセーブされちゃっている、という流れが一番ありそうです。

Quote:
また、当初の話なのですがroot内のd3forumを例えばQandAへ変えている場合と、同じd3モジュールを複数設置している場合はそれらを指定し処理を分ける事はできないものでしょうか?
それはまた別の仕掛けが必要ですね。

今回のはX2がもともと持っていた機能の改善に過ぎないのですが、同じtrustdirname同士の共通テンプレートのクラス化、となれば、それなりに大規模な工事が必要です。
hi-debow  Posted on 2008/1/14 20:58 | Last modified
GIJOEさん、いつもありがとうございます。
コメントで何処までの説明をつけるかは本当に難しいですね。

さて本題なのですが

ローカルでX.C.Lを導入してテストをしましたが、theme内DBテンプレートファイルへ日本語を記入している場合は文字化けします。

altsysを使ってDBテンプレートカスタマイズした場合はモチロン大丈夫なのですが、これは仕方のない事なのでしょうか?

また、当初の話なのですがroot内のd3forumを例えばQandAへ変えている場合と、同じd3モジュールを複数設置している場合はそれらを指定し処理を分ける事はできないものでしょうか?

自分のサイトでのカスタマイズなら今のままで十分すごいものですが、テンプレート作成&配布をするという事を考えると(私だけかもしれませんが・・・)、非常に自由なものになるのではないかと思いました。
GIJOE  Posted on 2008/1/13 5:08
hi-debowさん、こんにちは。

Quote:

先日ダウンロードしたhd_nightlyの中に入っていたskel_flexテーマを参考に疑問点をコメントさせていただきました。

skel_flex/templates/pico/ 以下にはinc_breadcrumbs.htmlが入っています。
skel_flex/ 以下にはpico_inc_breadcrumbs.htmlが入っています。

おお、よく見てますね!
そこまで見た上での質問に対する回答としては、確かに失礼でした。

ただこれは、チェックアウトしたタイミングによる、Nightlyならではの問題です。
すでに修正されています。

もちろん、
skel_flex/templates/pico_inc_bredcrumbs.html
が正しいオーバーライドテンプレートです。


そしてこれは、

- altsysからダウンロードしたアーカイブをそのまま展開したもの
- altsysのテーマ内テンプレートのDB自動取り込み機能のパス
- altsysへアップロードするテンプレートのアーカイブ

これらの既存機能のいずれとも統一されています。
hi-debow  Posted on 2008/1/12 12:02
GIJOEさん、うまく説明できずにすみませんでした。
Quote:
D3モジュールのtemplates/ 下にあるファイルはあくまで雛形です。
たまたま中身が書き換わっていないだけで、XOOPSのDBテンプレートそのものとは本来別物です。

逆に、テーマ下テンプレートとして置けるのはDBテンプレートと同じレベルのテンプレートです。
ありがとうございます。
これは理解していました。

先日ダウンロードしたhd_nightlyの中に入っていたskel_flexテーマを参考に疑問点をコメントさせていただきました。

skel_flex/templates/pico/ 以下にはinc_breadcrumbs.htmlが入っています。
skel_flex/ 以下にはpico_inc_breadcrumbs.htmlが入っています。

inc_breadcrumbs.htmlでしたらtrust側に入っているテンプレートの雛形ファイル名と同じであり、
pico_inc_breadcrumbs.htmlでしたら、root側のモジュールファイル名をpicoとした場合のDBテンプレート名であると思いました。

従って、テーマファイル以下に入れるテンプレートファイル名はどちらを付けた物を入れるのか知りたかったのです。
GIJOE  Posted on 2008/1/12 4:50
hi-debowさん、こんにちは。

Quote:

1) d3モジュールのテンプレートをテーマファイルへ入れる場合にテンプレート名はtrust側dirnameをxoops_trust_path側に入っているテンプレート名の前へつけるのでしょうか?

2) それともtrust側dirnameをxoops_trust_path側に入っているテンプレート名そのままで良いのでしょうか?

3) またroot側dirnameを変更している場合はroot側dirnameをxoops_trust_path側に入っているテンプレート名の前へつけるのですか?
う〜ん。XOOPSテンプレートの基本的な部分で勘違いしているように見えます。
D3モジュールのtemplates/ 下にあるファイルはあくまで雛形です。
たまたま中身が書き換わっていないだけで、XOOPSのDBテンプレートそのものとは本来別物です。

逆に、テーマ下テンプレートとして置けるのはDBテンプレートと同じレベルのテンプレートです。
そしてそれらは、altsysからダウンロードします。(ダウンロードそのものは、X2やLegacyRenderにもあります)

altsysからダウンロードする以上、テンプレートの前にdirnameをつける云々、という議論は存在しません。

Quote:
後、使用していないモジュールのテンプレートを入れておいても特に問題は無いのでしょうか?
問題ありません。単に使われないだけです。
ファイル数が1,000単位になってくると、さすがにファイルシステムのオーバーヘッドも出てくるでしょうけど。
hi-debow  Posted on 2008/1/10 19:15
GIJOEさん、こんにちは。

すごく興味のある内容でしたので早速参考の為にhd_nightly.zipをダウンロードしました。
私のローカル環境はXOOPS2.0.16a JPですので実際のテストは出来ていませんが、付属のテーマファイルを拝見しました。

そこで質問があります。

1) d3モジュールのテンプレートをテーマファイルへ入れる場合にテンプレート名はtrust側dirnameをxoops_trust_path側に入っているテンプレート名の前へつけるのでしょうか?

2) それともtrust側dirnameをxoops_trust_path側に入っているテンプレート名そのままで良いのでしょうか?

3) またroot側dirnameを変更している場合はroot側dirnameをxoops_trust_path側に入っているテンプレート名の前へつけるのですか?

3) の場合はテーマユーザにテンプレート名を変更してもらわないといけませんのでちょっと面倒になるかなと。


後、使用していないモジュールのテンプレートを入れておいても特に問題は無いのでしょうか?
Login
Username or e-mail:

Password:

Remember Me

Lost Password?

Register now!