Jun 27, 2014

Nginx proxy cache setting for Moin

MoinMoin is a nice wiki. I tried to cache it, and luckily succeed:

# Save this, e.g.: /etc/nginx/proxy_cache for reusable
proxy_cache zone-cache;

# Should add more if
proxy_cache_key "$host$request_uri";
proxy_cache_valid any 1d;
proxy_cache_methods GET HEAD;
proxy_ignore_headers Set-Cookie Cache-Control Expires;
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
proxy_http_version 1.1;

server {
     proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=zone-cache:4m max_size=1000m;    
     location @moin {
        include proxy_cache;
        proxy_pass http://127.0.0.1:8080;
        # each application need specify when to by pass cache
        set $no_cache 0;
        if ($query_string ~* "action=") {
            set $no_cache 1;
        }
        if ($http_cookie ~* "MOIN_SESSION_") {
            set $no_cache 1;
        }
        proxy_cache_bypass $no_cache;
    }
    location / {
        try_files $uri @moin;
    }
    location /moin_static197/ {
        alias /srv/moin/web/static/htdocs/;
    }
    # more here
}

Jun 23, 2014

Komodo Tips

Komodo Edit, an code editor based on Mozilla, has a nice features called "Macro", allows programming of UI. This applies to version 8.5 in MacOSX.
First, open Toolbox sidebar, right click / click on Setting button in top-right corner, we will have a list of customisation available (command, macro, snippet).

My favourite list:

1. View current file in Shift JIS

Japanese legacy code are largely written in Shift JIS encoding.
Adding this Javascript macro let you quickly switch:
komodo.assertMacroVersion(3);
var koDoc = ko.views.manager.currentView.koDoc;
if (koDoc.encoding.short_encoding_name != "Shift JIS") {
    koDoc.setEncodingFromEncodingName("Shift JIS");
}
I often assign Cmd + Shift + J shortcut to this.

2. Open GitX for this file

Git should be init beforehand (or you can add a command for init : )
The "%D" represents current folder of current file.
Add a command look like below, and set your own shortcut key:

Based on this, you can easily create a shortcut for executing PHP, Python script:

3. Move the current line up / down

The days before I works with Notepad++, above useful shortcuts are missing in Komodo.
However, we can "emulate" it using 2 macros.
Script below are taken from Komodo forums:
http://community.activestate.com/forum/move-line-move-line-down-macros

For move selection / current line down
// Move Line or Move Selection Down
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus() };
var ke = komodo.editor;

if( ke.lineFromPosition( ke.currentPos ) == ( ke.lineCount - 1 ) )
        return;

// Prevent Undo remember macro steps
ke.beginUndoAction();
   
var sel_start_a = ke.selectionStart;
var sel_end_a = ke.selectionEnd;

// Extend selection to beg of line at front and end of line at back
var selStartLine = ke.lineFromPosition(ke.selectionStart);
var selEndLine = ke.lineFromPosition(ke.selectionEnd);
var numLinesSelected = selEndLine - selStartLine;

var selStart = ke.positionFromLine( selStartLine );
var selEnd   = ke.getLineEndPosition( selEndLine );

ke.setSel( selStart, selEnd );

// Determine original selection position offset related to extended
var sel_start_b = ke.selectionStart;
var sel_end_b = ke.selectionEnd;
var offset_start = sel_start_a - sel_start_b;
var offset_end = sel_end_b - sel_end_a;  

// Copy the selected text and remove it
var text =  komodo.interpolate('%s');
komodo.doCommand('cmd_delete'); // This leaves a blank line in place of selection

// Move our selection to a new place
// First move our blank line up
komodo.doCommand('cmd_lineNext')
ke.lineTranspose();

// Insert our text
ke.insertText(ke.currentPos, text);

// Restore selection            
var newSelStartLine = ke.lineFromPosition( ke.currentPos );
var newSelEndLine   = newSelStartLine + numLinesSelected;

var newSelStart = ke.currentPos + offset_start;
var newSelEnd   = ke.getLineEndPosition(newSelEndLine) - offset_end;

ke.setSel(newSelStart, newSelEnd);

// End of prevent Undo
ke.endUndoAction();

For moving up:

// Move Line or Move Selection Up
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus() };
var ke = komodo.editor;

if( ke.lineFromPosition( ke.currentPos ) == 0 )
        return;

// Prevent Undo remember macro steps
ke.beginUndoAction();
   
var sel_start_a = ke.selectionStart;
var sel_end_a = ke.selectionEnd;

// Extend selection to beg of line at front and end of line at back
var selStartLine = ke.lineFromPosition(ke.selectionStart);
var selEndLine = ke.lineFromPosition(ke.selectionEnd);
var numLinesSelected = selEndLine - selStartLine;

var selStart = ke.positionFromLine(selStartLine);
var selEnd   = ke.getLineEndPosition(selEndLine);

ke.setSel(selStart, selEnd);

// Determine original selection position offset related to extended
var sel_start_b = ke.selectionStart;
var sel_end_b = ke.selectionEnd;
var offset_start = sel_start_a - sel_start_b;
var offset_end = sel_end_b - sel_end_a;  

// Copy the selected text and remove it
var text =  komodo.interpolate('%s');
komodo.doCommand('cmd_delete'); // This leaves a blank line in place of selection

// Move our selection to a new place
// First move our blank line up
ke.lineTranspose();
komodo.doCommand('cmd_linePrevious')

// Insert our text
ke.insertText(ke.currentPos, text);

// Restore selection
var newSelStartLine = ke.lineFromPosition( ke.currentPos );
var newSelEndLine   = newSelStartLine + numLinesSelected;

var newSelStart = ke.currentPos + offset_start;
var newSelEnd   = ke.getLineEndPosition(newSelEndLine) - offset_end;

ke.setSel(newSelStart, newSelEnd);

// End of prevent Undo
ke.endUndoAction();

4. Find and replace

The find window is often get lost because of Windows overlap.
A solution is clicking pin icon on the right down corner.
Also, set proper include and exclude pattern may help while searching on a directory.
My exclude pattern is:
vendor:.svn:.git:template_c:*~
The regex in replace box using notation \1, ..  \n for matched groups.

5. Emmet

Emmet is an add-on for typing HTML faster.
Watch how amazing it is at http://docs.emmet.io/
I often set key Shift - Enter for expanding emmet abbr.

6. Keybindings
Key with * are not set by default.

Cmd + Shift + K = Invoke tool
*Cmd + P = Quick open (like Sublime does, but we need to create a "project" beforehand)
*Cmd + | =  Browser preview split view: the page reload on save
*Cmd + M = Duplicate line or selection
*Cmd + G = Go to line
*F4 and Esc = Complete word
*Cmd + K = Set mark
*F10 = Show current file in places




Jun 20, 2014

MacOSX good apps list

Applications

* Komodo Edit: Main editor

* Textmate2: Additional Editor

* Homebrew: Build packages and software for OSX

* iterm2: Terminal, quickly copy / paste; window-division.

* SequelPro: For managing MySQL (local and remote). For Postgres, SQLite: adminer (PHP based) is a good choice.

http://ohmyz.sh/ improve shell

Visual Git, line by line staging.
For diff, using console-based colordiff is good.
Using sdiff for side-by-side diff

* Adium: Messenger for Facebook, Google, Yahoo combined. Messages/Facetime from Apple are good as well.

* FileZilla: Solid SFTP/FTP client or Cyberduck: many features

* SketchBook Express: freely and powerful drawing tool

* Seashore: bitmap image tool

* MacGDBp: Standalone PHP Deugger GUI

* VirtualBox: VM and testing

* LibreOffice

Utilities:

* Caffeine: Prevent sleep

* PCKeyboarHack, Keyremap4Macbook: change keymap

* Spectacle: Windows arrangement tool (change size/position faster)

* Clipmenu: clipboard history and snippet tools (great time saving)

* Google Japanese Input: better IME

* LICEcap: GIF screenshot win animation

* Developer color picker http://download.panic.com/picker/

http://sequentialx.com View bulk images

* ToyViewer: fast images viewer

* imageoptim Reduce images size (replaces)

* nvALT: notetaker http://brettterpstra.com/projects/nvalt/

Jun 16, 2014

MySQL script to generate a ASCII-table of a database schema

Run this in the command line client (interactive mysql program):
-- References: http://dev.mysql.com/doc/refman/5.0/en/columns-table.html
use information_schema;
select table_name 'Table', column_name 'Column', column_key 'Index', column_type 'Type',
if (IS_NULLABLE = 'NO', if(column_default IS NULL, '', column_default), 'NULL') 'Default', column_comment 'Comment'
from columns where table_schema = '$DB_NAME';
Replace $DB_NAME by your own name. Japanese version:
-- References: http://dev.mysql.com/doc/refman/5.0/en/columns-table.html
use information_schema;
select table_name 'フィール', column_name '論理名', column_key 'インデックス', column_type '型',
if (IS_NULLABLE = 'NO', if(column_default IS NULL, '', column_default), 'NULL') 'デフォルト', column_comment '備考'
from columns where table_schema = '$DB_NAME';

Jun 15, 2014

VPS Providers in Japan

Most providers requires credit cards, however there are some with flexible bank transfers as payment method:

http://www.cloudcore.jp
Backed by KDDI

Other choices:
http://dream.jp/vps/
http://www.kagoya.jp/cloud/vps/

Most plans start at 500 yen/month, fairly cheep with good config.