2012年2月7日火曜日

Smmrty3 〜Smarty2との互換性がないアップグレード〜

原文ママ。その内訳すかも。newline at {if} tags は メールとかでここに改行を入れたく無いという時は個人的には邪魔です。


= Known incompatibilities with Smarty 2 =

Smarty 2 との良く知られている非互換性

== Syntax ==

Smarty 3 API has a new syntax. Much of the Smarty 2 syntax is supported
by a wrapper but deprecated. See the README that comes with Smarty 3 for more
information.

Smarty 3 API には 新しい シンタックスがある。

The {$array|@mod} syntax has always been a bit confusing, where an "@" is required
to apply a modifier to an array instead of the individual elements. Normally you
always want the modifier to apply to the variable regardless of its type. In Smarty 3,
{$array|mod} and {$array|@mod} behave identical. It is safe to drop the "@" and the
modifier will still apply to the array. If you really want the modifier to apply to
each array element, you must loop the array in-template, or use a custom modifier that
supports array iteration. Most smarty functions already escape values where necessary
such as {html_options}

== PHP Version ==
Smarty 3 is PHP 5 only. It will not work with PHP 4.

== {php} Tag ==
The {php} tag is disabled by default. The use of {php} tags is
deprecated. It can be enabled with $smarty->allow_php_tag=true.

But if you scatter PHP code which belongs together into several
{php} tags it may not work any longer.

== Delimiters and whitespace ==
Delimiters surrounded by whitespace are no longer treated as Smarty tags.
Therefore, { foo } will not compile as a tag, you must use {foo}. This change
Makes Javascript/CSS easier to work with, eliminating the need for {literal}.
This can be disabled by setting $smarty->auto_literal = false;

== Unquoted Strings ==
Smarty 2 was a bit more forgiving (and ambiguous) when it comes to unquoted strings
in parameters. Smarty3 is more restrictive. You can still pass strings without quotes
so long as they contain no special characters. (anything outside of A-Za-z0-9_)

For example filename strings must be quoted

{include file='path/foo.tpl'}


== Extending the Smarty class ==
Smarty 3 makes use of the __construct method for initialization. If you are extending
the Smarty class, its constructor is not called implicitly if the your child class defines
its own constructor. In order to run Smarty's constructor, a call to parent::__construct()
within your child constructor is required.


class MySmarty extends Smarty {
function __construct() {
parent::__construct();

// your initialization code goes here

}
}


== Autoloader ==
Smarty 3 does register its own autoloader with spl_autoload_register. If your code has
an existing __autoload function then this function must be explicitly registered on
the __autoload stack. See http://us3.php.net/manual/en/function.spl-autoload-register.php
for further details.

== Plugin Filenames ==
Smarty 3 optionally supports the PHP spl_autoloader. The autoloader requires filenames
to be lower case. Because of this, Smarty plugin file names must also be lowercase.
In Smarty 2, mixed case file names did work.

== Scope of Special Smarty Variables ==
In Smarty 2 the special Smarty variables $smarty.section... and $smarty.foreach...
had global scope. If you had loops with the same name in subtemplates you could accidentally
overwrite values of parent template.

In Smarty 3 these special Smarty variable have only local scope in the template which
is defining the loop. If you need their value in a subtemplate you have to pass them
as parameter.

{include file='path/foo.tpl' index=$smarty.section.foo.index}


== SMARTY_RESOURCE_CHAR_SET ==
Smarty 3 sets the constant SMARTY_RESOURCE_CHAR_SET to utf-8 as default template charset.
This is now used also on modifiers like escape as default charset. If your templates use
other charsets make sure that you define the constant accordingly. Otherwise you may not
get any output.

== newline at {if} tags ==
A \n was added to the compiled code of the {if},{else},{elseif},{/if} tags to get output of newlines as expected by the template source.
If one of the {if} tags is at the line end you will now get a newline in the HTML output.



== trigger_error() ==
The API function trigger_error() has been removed because it did just map to PHP trigger_error.
However it's still included in the Smarty2 API wrapper.

== Smarty constants ==
The constants
SMARTY_PHP_PASSTHRU
SMARTY_PHP_QUOTE
SMARTY_PHP_REMOVE
SMARTY_PHP_ALLOW
have been replaced with class constants
Smarty::PHP_PASSTHRU
Smarty::PHP_QUOTE
Smarty::PHP_REMOVE
Smarty::PHP_ALLOW
14:29 No comments

2012年1月26日木曜日

PDO

接続

文字コードの設定

try {
$pdo = new PDO($db_dsn,$db_user, $db_password,
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`"
)
);
} catch (PDOException $e) {
die($e->getMessage());
}

推奨されないらしい

$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);

持続的な接続

検討中。

データベースサーバへの持続的な接続による恩恵をこうむる web アプリケーションは多いでしょう。持続的な接続は、スクリプトが 終了しても閉じられずにキャッシュされ、他のスクリプトが同じ内容の 接続を要求してきた際にそれが再利用されます。持続的接続の キャッシュにより、スクリプトがデータベースを使用するたびに 新しい接続を確立するオーバーヘッドを避けることができます。 それにより、結果として web アプリケーションを高速化できるように なります。

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::ATTR_PERSISTENT => true
));

今の所の接続案

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`",
PDO::ATTR_PERSISTENT => true
));

参考

SET NAMESは禁止
8:49 2 comments

2012年1月21日土曜日

コマンド 備忘録

[Subversion] .svnファイルを一括削除する

rm -rf `find ./ -type d -name .svn ! -regex \.svn/. -print`

! -regex \.svn/. が ? いらないのでは。

echo rm -rf `find . -type d -name .svn`

find . -type d -name .svn -exec rm -rf {} \;

WEB制作者の苦悩

[Subversion] ブランチをスイッチしたもの(確認方法)

別のブランチの .svn をコピー したらこうなった。

svn stat | awk '$1 == "S"'

svn:ignore

svn propset svn:ignore target .
svn commit -m "Ignored target directory"

svn:ignoreを 再帰的に適用する


-R を使う。./tmp ディレクトリ以下の全てのファイルをバージョン管理から外す。

svn propset -R 'svn:ignore' '*' ./tmp/
15:50 No comments

2012年1月20日金曜日

twipic と Flicker を連携する

1. メールアドレスを入力する


2. Twitter のアカウント名を入力


3. 同期

Send all existing photos to flickr?

4.

12:54 No comments

2012年1月2日月曜日

Twitter/xAuth

序. コンソールアプリとの発言で。。。

XAuthを薦められました。

結局 Twitter 社の認識としては コンソールアプリでやるんだから パスワードを使うXAuth でよくね。
という話らしい。その後食い下がってみたみたけど、返信が来ないのは話は終わりましたよという事か。
そう言う訳で XAutを使ってみました。

何を作ったかは後ほど

XAUth

xAuth provides a way for desktop and mobile applications to exchange a username and password for an OAuth access token. Once the access token is retrieved, xAuth-enabled developers should dispose of the login and password corresponding to the user.

xAuth access is restricted to approved applications. If your application is a desktop or mobile application and the standard web OAuth flow or PIN-code out-of-band flow is not right for you, send a detailed message to api@twitter.com to request xAuth privileges. Include the name of your application, the consumer key, the application ID (if available), and a summary of how xAuth is best-suited for your application.

11:05 1 comment

2011年6月19日日曜日

Smartyの継承について

PHPの開発を受けたり、お守り(運用)をしたりしているとcakePHP等のMVCモデルまではいかなくとも SmartyのViewだけでもあった方が良いなと思う事が多いです。直接PHPファイルに書いたり preg_replace を使って自前のテンプレートエンジンを使われたりすると、肝のソースコードに到達するまでに余計な時間が掛かります。

デザイン(マークアップ言語)は外に出そうよと強く思います。V(View) が別管理されているだけでも違います。

さて、Smartyの継承です。Smartyは各プロダクトで共通の設定をする方が設定が散逸せずに管理が楽です。

継承して使いたいと考え 先人を探して googleすると下記が引っかかりました。

Smartyの継承クラスを用意する

require_once("Smarty.class.php"); // smarty.class.phpの指定。環境によって異なります。
class xxxxSmarty extends Smarty{ // Smartyクラスを継承したxxxxSmaryクラスを定義します。
public function __construct(){    // __construct()はPH5以上です。
$this->Smarty();
$this->left_delimiter = "{!";    // xoopsと同じにする。(xoopsに慣れてるので)
$this->right_delimiter = "}";
$this->template_dir = "xxxxxxxx/templates";
$this->compile_dir = "xxxxxxxx/templates_c";
}
}

Uncaught exception 'SmartyException' with message 'Please use parent::__construct() to call parent constuctor'

のエラーが出た。parent::__construct() 親のコンストラクタを先頭で呼んで下さい。

色々あったのねという事で 下記に修正しました。(抜粋)

class SpnsSmarty extends Smarty{ 
    public function __construct(){
        parent::__construct();
        //$this->Smarty();
        $this->left_delimiter = "{{";    
        $this->right_delimiter = "}}";

テンプレートの継承も奇麗にヘッダーやフッターを分割出来るので是非機会があれば使ってみて下さい。本家のサイトが詳しいです。

テンプレートの継承
3:42 No comments

2011年4月18日月曜日

FTPの自動化あれこれ

1. シェルスクリプト 方式


(1) ftp -n

-n ユーザ名とパスワードを聞かれないようにする。
⇒ シェルスクリプトで実行出来るようにする。

$ vi ftp.sh
下記を入力する

---------
ftp -n hostname << ECHO
user userid password
bin
cd www
ls
ascii
bye
ECHO
---------
$ chmod 755 ftp.sh (2) echo "" | ftp -n
#!/bin/sh
echo "user userid password
ls
" | ftp -n hostname

2. wget (ダウンロードのみ) 方式

$ wget ftp://userid:password@ hostname/pass

3. ncftp 方式

ncftpget ncftp + get : ダウンロード ncftpget ncftp + put : アップロード インストール: yum install ncftp (1) ncftpget Usages:
  ncftpget [flags] remote-host local-dir remote-path-names...      (mode 1)
  ncftpget -f login.cfg [flags] local-dir remote-path-names...     (mode 2) ← 推奨?
  ncftpget [flags] ftp://url.style.host/path/name                  (mode 3)
  ncftpget -c [flags] remote-host remote-path-name > stdout        (mode 4)
  ncftpget -C [flags] remote-host remote-path-name local-path-name (mode 5)
  ncftpget -c [flags] ftp://url.style.host/path/name > stdout      (mode 6)
(2) ncftpput Usages:
  ncftpput [flags] remote-host remote-dir local-files...   (mode 1)
  ncftpput -f login.cfg [flags] remote-dir local-files...  (mode 2) ← 推奨?
  ncftpput -c remote-host remote-path-name < stdin         (mode 3)
  ncftpput -C remote-host local-path-name remote-path-name (mode 4)

注意:flags の -u -p はセキュリティ的に使用は推奨されない

(3) login.cfg
2:01 No comments