kikukawa's diary

都内で活動するシステムエンジニアが書いてます。 興味を持った技術やハマったポイント、自分用メモをつけてます。 最近はweb中心

cakephpでsjisのデータベースを使う

データベースの文字コードsjisで、ソースコードはUTF8という状況です。
基本的にはdatabase.phpの設定でsjisの設定をするだけで使えます。

        'default' => array(
            'datasource' => 'Mysql'
            'persistent' => false,
            'host' => 'host',
            'login' => 'login',
            'password' => 'password',
            'database' => 'database',
            'prefix' => '',
            'encoding' => 'sjis',
        ),

ただし、このままだとphp5C問題に引っ掛かります。
特定の文字で文字化けを起こします。
回避するためにMysql.phpのconnectメソッドをオーバーライドします。
PDOを作る時のデータベース接続文字列にエンコードの指定をしてあげます。
継承するクラスは下記にあります。

Cake\Model\Datasource\Database\Mysql.php

phpは5.3,cakeは2.2というバージョンで動作確認していますが、
2系の最新のcakeでもMysql.phpのソースが変っていなかったので同じだと思います。

下記の場所にクラスを作って、database.phpのdatasourceで作成したクラスを指定します。
必要があれば、App::buildでパスを追加して下さい。
私の場合は、かなりディレクトリ構成を変えていたため、追加する必要がありました。

App\Model\Datasource\Database\MysqlCharaSet.php
<?php

App::uses('DboSource', 'Model/Datasource');
App::uses('Mysql', 'Model/Datasource/Database');

class MysqlCharaSet extends Mysql {

    public function connect() {

        $config = $this->config;
        $this->connected = false;
        try {
            $flags = array(
                PDO::ATTR_PERSISTENT => $config['persistent'],
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            );
            if (!empty($config['encoding'])) {
                $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
            }
            if (empty($config['unix_socket'])) {
                $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
            } else {
                $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
            }
            if (!empty($config['encoding'])) {
                $dsn .= ';charset='.$config['encoding'];
            }
            $this->_connection = new PDO(
                    $dsn, $config['login'], $config['password'], $flags
            );
            $this->connected = true;
        } catch (PDOException $e) {
            throw new MissingConnectionException(array('class' => $e->getMessage()));
        }

        $this->_useAlias = (bool) version_compare($this->getVersion(), "4.1", ">=");

        return $this->connected;
    }

}

この部分が追加した処理です。 PDO自体に使用する文字コードを教えてあげることで適切な処理をエスケープをしてくれるようになります。

            if (!empty($config['encoding'])) {
                $dsn .= ';charset='.$config['encoding'];
            }

あとは、通常通りModelから操作するだけで大丈夫なはずです。

特定のディレクトリから画像だけを階層を維持しつつrsync

自分用メモ
引っ張るパターン
実行するときはnオプション外すこと

コマンド

rsync -avmCn --include='*/' --include-from=/path/to/include_list --exclude='*' src-server:/path/to/src/dir/* /path/to/dst/dir

mは空ディレクトリを作らないオプション

include_listの中身

*.bmp
*.gif
*.ico
*.jpeg
*.jpg
*.JPG
*.png

Gitでsubmoduleのurlを変更する

自分用メモ

サブモジュール内で下記のようになっているものを

bash-3.2$ git remote -v
origin  git:/path/to/repo.git (fetch)
origin  git:/path/to/repo.git (push)

下記に変更したい場合

bash-3.2$ git remote -v
origin  ssh://git/path/to/repo.git (fetch)
origin  ssh://git/path/to/repo.git (push)

親となっているリポジトリで、.gitmodulesを編集
urlの部分を新しくしたいurlに変更する

vi .gitmodules

このままでは、親リポジトリのほうで認識できていないのでsyncで教えてあげる。
すると、親リポジトリの .git/configの中も変わる。

git submodule sync

参考
http://stackoverflow.com/questions/913701/changing-remote-repository-for-a-git-submodule

nodebrewのインストールが失敗する

ドキュメントにも書いてありますが、自分用メモ

ワンライナーでやろうとしたら下記のようにエラーが発生しました。

$ curl -L git.io/nodebrew | perl - setup
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
139   279  139   279    0     0    152      0  0:00:01  0:00:01 --:--:-- 69750
Bareword found where operator expected at - line 3, near "400 Bad"
        (Missing operator before Bad?)
Bareword found where operator expected at - line 6, near "<p>Your"
        (Missing operator before Your?)
syntax error at - line 2, near "html>"
Execution of - aborted due to compilation errors.

その場合は、wgetとsetupを分けるとうまくいくことがあります。

$ wget git.io/nodebrew
$ perl nodebrew setup 
fetching nodebrew...
install nodebrew in $HOME/.nodebrew

========================================
Add path:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================

https://github.com/hokaccha/nodebrew

CKEditorの画像アップロード時のプレビュー文字を変える

CKEditorを使用して、画像アップロードを行おうとすると、プレビューが表示されます。
その中に下記のような文言があります。

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas feugiat consequat diam. ...

これはダミーテキストとしてよく使われるものだそうです。
日本人にはまったく馴染みが無いので変更します。
下記のようにconfigで文言の指定が可能でした。

var config = {image_previewText:'プレビュー'};
CKEDITOR.replace( 'editor1' ,config);

サンプル