気の利かないPHPの論理和

PHPで以下のようなコードを書いてバグに気付かずしばらく悩んでしまった。


$var1 = '';
$var2 = 'aaa';

$hoge = $var1 || $var2;

echo $hoge; // 'aaa'を期待

実際は、$hogeの値は1になる。
rubyなら当然'aaa'になる。
Javaのように型に厳格な言語ならまだしも、PHPは'aaa'を返して欲しかった。


if ($var1) {
$hoge = $var1;
} elseif ($var2) {
$hoge = $var2;
}
こう書くしかないのかな?

rails.vimのインストール

http://www.vim.org/scripts/script.php?script_id=1567からrails.zipをダウンロードする。
インストール


$ unzip rails.zip
Archive: rails.zip
inflating: autoload/rails.vim
inflating: plugin/rails.vim
inflating: doc/rails.txt
$ mv autoload/rails.vim ~/.vim/autoload/
$ mv plugin/rails.vim ~/.vim/plugin/
$ mv doc/rails.txt ~/.vim/doc/
ヘルプ機能を有効にするために、vimを起動して以下を実行する。

:helptags ~/.vim/doc

日本語の変換を楽にする

参考http://idak604.blog39.fc2.com/blog-entry-2.html
vimのモードとIM変換が混じると鬱陶しいので少しでも楽にするために、入力モードを抜ける時にIMを自動的にオフにする設定。
xvkbdが必要らしいのでRPM Searchからインストールする。


$ wget ftp://ftp.pbone.net/mirror/ftp.freshrpms.net/pub/freshrpms/pub/dag/redhat/9/en/i386/dag/RPMS/xvkbd-2.8-1.rh9.rf.i386.rpm
$ sudo rpm -Uvh xvkbd-2.8-1.rh9.rf.i386.rpm
IMの設定をする。
SCIMを終了するホットキーにControl+Shift+spaceを割り当てる。
CentOS5では、システム > 設定 > 他の個人設定 > SCIM入力メソッドの設定 > フロントエンド > 全体設定 から設定できた。
.vimrcに以下を記述する。丸写しです。

inoremap :call ForceImeOff()
function! ForceImeOff()
let imeoff = system('xvkbd -text "\[Control]\[Shift]\[space]" > /dev/null 2>&1')
endfunction

vim7.2のインストール

ソースからvimをインストールする。
目的は最新のvimを入れることと、vim-rubyruby開発できるようにすること。
paco(http://paco.sourceforge.net/)というツールを使うことにする。
pacoを使うとソースからインストールしたソフトウェアを管理できるようになる。


pacoのインストール


$ wget http://heanet.dl.sourceforge.net/sourceforge/paco/paco-2.0.7.tar.bz2
$ tar jxvf paco-2.0.7.tar.bz2
$ cd paco-2.0.7
$ ./configure --disable-gpaco --enable-scripts
$ make
# make install
# make logme
vimのインストール

$ svn co https://vim.svn.sourceforge.net/svnroot/vim/vim7
$ cd vim7
$ ./configure
--with-features=huge
--enable-multibyte
--enable-fontset
--enable-rubyinterp
$ make
$ sudo paco -D make install
configureの最後のオプションの--enable-rubyinterpはvim-rubyを使うために必要。
この指定が無いとvim-rubyの機能をフルに使えないらしい。
pythonを使うなら--enable-pythoninterpを指定する。



pacoでちゃんと管理できているか確認してみる。


$ paco -a
>paco-2.0.7 vim7
vim-rubyを入れる。

$ sudo gem install vim-ruby
$ /usr/lib/ruby/gems/1.8/gems/vim-ruby-2007.05.07/bin/vim-ruby-install.rb
~/.vim以下にたくさん設定ファイルが作成された。


これでRubyのソースファイル中でC-x C-oと打つとオムニ補完ができる。たとえば関数名を途中まで入力してC-x C-oと打つと、文法上その場所に挿入し得る関数名をプルダウンで表示してくれる。
オムニ補完に対応している言語は、:help compl-omni-filetypesで確認できる。
上の通りインストールすると、C/CSS/HTML/XHTML/JAVASCRIPT/PHP/RUBY/SQL/XMLに対応していた。
オムニ補完とは別に、emacsの動的略称機展開の機能も備わっており、C-nと打つと編集中ファイル内の単語を補完してくれる。
emacsyasnippetに当たる機能もsnippetsEmusnipMateといったプラグインで実現できるようだ。




(追記)
Ruby Enterprise Editionを使ってるマシンで上記の方法でVimをインストールすると、make時にlruby-staticが見つからないというエラーが出た。
これは標準とは異なる場所にrubyをインストールしていたため。
以下のコマンドを実行した後、configureからやり直すと正常にインストールできた。

$ export LDFLAGS="-L/opt/ruby-enterprise-1.8.7-2010.01/lib -lruby-static"

Railsのバージョンアップ

gemのRailsをバージョンアップする。


$ rails --version 現在のバージョンを確認する。
Rails 2.3.2

$ sudo gem update --system gemのバージョンアップをする。
パスワード:
Updating RubyGems
Updating rubygems-update
Successfully installed rubygems-update-1.3.5
:0:Warning: Gem::SourceIndex#search support for String patterns is deprecated
Updating RubyGems to 1.3.5
Installing RubyGems 1.3.5
RubyGems 1.3.5 installed

=== 1.3.5 / 2009-07-21

Bug fixes:

* Fix use of prerelease gems.
* Gem.bin_path no longer escapes path with spaces. Bug #25935 and #26458.

Deprecation Notices:

* Bulk index update is no longer supported (the code currently remains, but not
the tests)
* Gem::manage_gems was removed in 1.3.3.
* Time::today was removed in 1.3.3.


------------------------------------------------------------------------------

RubyGems installed the following executables:
/usr/bin/gem

$ sudo gem update rails Railsのバージョンアップをする。
パスワード:
Updating installed gems
Updating rails
Successfully installed activesupport-2.3.5
Successfully installed activerecord-2.3.5
Successfully installed rack-1.0.1
Successfully installed actionpack-2.3.5
Successfully installed actionmailer-2.3.5
Successfully installed activeresource-2.3.5
Successfully installed rails-2.3.5
Gems updated: activesupport, activerecord, rack, actionpack, actionmailer, activeresource, rails
Installing ri documentation for activesupport-2.3.5...
Installing ri documentation for activerecord-2.3.5...
Installing ri documentation for rack-1.0.1...
Installing ri documentation for actionpack-2.3.5...
Installing ri documentation for actionmailer-2.3.5...
Installing ri documentation for activeresource-2.3.5...
Installing ri documentation for rails-2.3.5...
Installing RDoc documentation for activesupport-2.3.5...
Installing RDoc documentation for activerecord-2.3.5...
Installing RDoc documentation for rack-1.0.1...
Installing RDoc documentation for actionpack-2.3.5...
Installing RDoc documentation for actionmailer-2.3.5...
Installing RDoc documentation for activeresource-2.3.5...
Installing RDoc documentation for rails-2.3.5...

$ rails --version バージョンを確認する。
Rails 2.3.5

gemのupdateは既存のライブラリをバージョンアップするのではなく最新バージョンを新規にインストールするだけなので、update後は複数のRailsが入っている。


$ gem list rails gemにインストールされているRailsの一覧を表示する。

*** LOCAL GEMS ***

rails (2.3.5, 2.3.2)

Subversionの導入

インストール

ここからsubversionのソースをダウンロードしてインストールする。
subversionをインストールする前にApacheのインストールをしておく。
またINSTALLファイルを読んで依存ライブラリを確認しておく。
Apacheと連携して、ブラウザからHTTPプロトコルリポジトリにアクセスするためにはneon(バージョン0.25以降)というライブラリが必要。またその際にSSL機能を使うのであればopensslも必要となる。

自分の環境にはopensslはインストール済みなのでneonのみインストールする。


# wget http://www.webdav.org/neon/neon-0.28.6.tar.gz
# tar xzvf neon-0.28.6.tar.gz
# cd neon-0.28.6
# ./configure --prefix=/usr/local/neon-0.28.6 --with-ssl
# make
# make install

subversionをインストールする。


# wget http://subversion.tigris.org/downloads/subversion-1.6.6.tar.gz
# tar xzvf subversion-1.6.6.tar.gz
# cd subversion-1.6.6
# ./configure
# --prefix=/usr/local/subversion-1.6.6
# --with-apxs=/usr/local/httpd/bin/apxs
# --with-apr=/usr/local/apr-httpd
# --with-apr-util=/usr/local/apr-util-httpd
# --with-neon=/usr/local/neon-0.28.6
# --with-ssl
configureを実行すると以下のエラーが出た。

An appropriate version of sqlite could not be found. We recommmend
3.6.13, but require at least 3.4.0.
Please either install a newer sqlite on this system

or

get the sqlite 3.6.13 amalgamation from:
http://www.sqlite.org/sqlite-amalgamation-3.6.13.tar.gz
unpack the archive using tar/gunzip and copy sqlite3.c from the
resulting directory to:
/usr/local/src/subversion-1.6.6/sqlite-amalgamation/sqlite3.c
This file also ships as part of the subversion-deps distribution.

configure: error: Subversion requires SQLite

エラーメッセージに従ってsqlite3.cを取得してコピーする。

$ wget http://www.sqlite.org/sqlite-amalgamation-3.6.13.tar.gz
$ tar xzvf sqlite-amalgamation-3.6.13.tar.gz
# mkdir /usr/local/src/subversion-1.6.6/sqlite-amalgamation
# cp sqlite-3.6.13/sqlite3.c /usr/local/src/subversion-1.6.6/sqlite-amalgamation/
再びconfigureを実行すると無事終了したが以下のようにberkeley-dbが無いという警告が出た。

configure: WARNING: we have configured without BDB filesystem support


You don't seem to have Berkeley DB version 4.0.14 or newer
installed and linked to APR-UTIL. We have created Makefiles which
will build without the Berkeley DB back-end; your repositories will
use FSFS as the default back-end. You can find the latest version of
Berkeley DB here:
http://www.oracle.com/technology/software/products/berkeley-db/index.html

berkeley-dbは使わないので警告は無視して続行する。

# make
ここで今度は以下のエラー。

/usr/bin/ld: cannot find -lexpat
expatをインストールする。

# wget http://sourceforge.net/projects/expat/files/expat/2.0.1/expat-2.0.1.tar.gz/download
# tar expat-2.0.1.tar.gz
# cd expat-2.0.1
# ./configure --prefix=/usr/local/expat-2.0.1
# make
# make install
再びconfigure&makeしても上のエラーが消えないので
LD_LIBRARY_PATH環境変数にexpatライブラリのパスを指定するとmakeが通った。
最終的には以下のように実行。

# cd /usr/local/src/subversion-1.6.6
# make clean
# export LD_LIBRARY_PATH=/usr/local/export-2.0.1/lib/
# ./configure
# --prefix=/usr/local/subversion-1.6.6
# --with-apxs=/usr/local/httpd/bin/apxs
# --with-apr=/usr/local/apr-httpd
# --with-apr-util=/usr/local/apr-util-httpd
# --with-neon=/usr/local/neon-0.28.6
# --with-ssl
# make
# make install

設定

シンボリックリンクを張る。


# ln -s subversion-1.6.6 subversion
環境変数の設定。~/.bash_profileに以下を追加。

export PATH=$PATH:/usr/local/subversion/bin
export SVN_EDITOR=/usr/bin/vim
Apache連携用のモジュールを確認してみる。

$ ls -lt /usr/local/httpd/modules
mod_authz_svn.so
mod_dav_svn.so
libphp5.so
mod_dav_fs.so
mod_rewrite.so
mod_dav.so
mod_proxy_ajp.so
mod_proxy_balancer.so
mod_ssl.so
mod_proxy.so
mod_proxy_connect.so
mod_proxy_ftp.so
mod_proxy_http.so
mod_authz_svn.soとmod_dav_svn.soというモジュールが作成されている。
また、svn --versionとコマンド打つと組み込まれた機能を確認できる。

以下のリポジトリアクセス (RA) モジュールが利用できます:

* ra_neon : Neon を利用して WebDAV (DeltaV) プロトコルリポジトリにアクセスするモジュール。
- 'http' スキームを操作します
- 'https' スキームを操作します
* ra_svn : svn ネットワークプロトコルを使ってリポジトリにアクセスするモジュール。
- 'svn' スキームを操作します
* ra_local : ローカルディスク上のリポジトリにアクセスするモジュール。
- 'file' スキームを操作します

/usr/local/httpd/conf/extra/httpd-svn.confを作成して以下を記述。


DAV svn
SVNPath /var/svn/repo
AuthType Basic
AuthName "Subversion repository"
AuthUserFile /usr/local/httpd-2.2.9/conf/.webdav_passwd
Require valid-user
BrowserMatch "MSIE" AuthDigestEnableQueryStringHack = On

httpd.confに以下を記述してhttpd-svn.confを読み込ませる。

# Subversion
Include conf/extra/httpd-svn.conf

インポートとチェックアウト

subversionリポジトリ用のディレクトリを作成する。


# mkdir /var/svn/
# svnadmin create --fs-type fsfs /var/svn/repo
# chown -R httpd:httpd /var/svn/repo
--fs-typeはリポジトリのフォーマットを指定する。
省略するとデフォルトが適用される。以前はBerkely DBがデフォルトだったが、バージョン1.2からFSFSがデフォルトになった。


初期インポートする。

$ svn import myapp/ http://localhost/svn/myapp/trunk
実行すると以下のような注意が出た。

                                                                                                                                            • -

ATTENTION! Your password for authentication realm:

http://localhost:80 Subversion repository

can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/home/totemn/.subversion/servers'.

                                                                                                                                            • -

暗号化されていないパスワードを保存しますか (yes/no)?

./subversion/serversに以下の行を加える。

store-plaintext-passwords=no
リポジトリの中身を確認してみる。

# svnlook tree /var/svn/repo/
> myapp/
> trunk/
data1
data2
チェックアウトする。

# svn checkout http://localhost/svn/myapp

コマンドに与えるパスについて

svnコマンドに与えるパスは、

  • file:/// (ローカル)
  • http:// (リモート)

のいずれかのURL形式で与える。
しかし、svnadminやsvnlookは上のような指定はできない。
単純に、/var/svn/repoのようにファイルシステム形式で指定する。

mmm-modeのインストール

ここからmmm-mode-0.4.8をダウンロード&解凍し、以下を実行。


# ./configure
# make
# make install
すると、/usr/local/info/と/usr/local/share/emacs/site-lisp/が作られ、
前者にマニュアル、後者にelisp一式が格納された。
自分の環境では/usr/local/share/emacs/site-lispにはパスが通ってないのでelispを移動する。

$ mkdir ~/.emacs.d/plugins/mmm-mode
$ cp /usr/local/share/emacs/site-lisp/* ~/.emacs.d/plugins/mmm-mode
.emacsに以下を記述する。

;;;; mmm-mode
(add-to-list 'load-path "~/.emacs.d/plugins/mmm-mode")
(require 'mmm-mode)
(setq mmm-global-mode 'maybe)
(set-face-background 'mmm-default-submode-face nil)
;; for php-mode in html-helper-mode
(mmm-add-mode-ext-class nil "\\.php?\\'" 'html-php)
(mmm-add-classes
'((html-php
:submode php-mode
:front "<\\?\\(php\\)?"
:back "\\?>")))
(add-to-list 'auto-mode-alist '("\\.php?\\'" . html-helper-mode))

これで.php拡張子が付くファイルを開くとhtml-helperモードになり、
<?phpと?>で囲まれた範囲にカーソルがあるときだけPHPモードに変わる。


参考 http://bluestar.s32.xrea.com/text/php-mode.php
詳しいドキュメント http://www.xemacs.org/Documentation/packages/html/mmm.html