Convert MySQL content from one encoding to another
Posted: February 14th, 2012 | Author: Vincent | Filed under: Development | 1 Comment »This is an example to convert from latin-1 to utf-8. I used this to convert the content of old wordpress mysql database to true utf-8 content.
# Dump the old database in its original encoding (latin1 in this example) mysqldump --default-character-set=latin1 SOURCE_DATABASE > db.sql # Replace sql code to reflect new encoding (utf8 in this example) sed -e 's/SET NAMES latin1/SET NAMES utf8/g' -i db.sql sed -e 's/CHARSET=latin1/CHARSET=utf8 COLLATE=utf8_unicode_ci/g' -i db.sql # Import the db.sql into the database (which must be created with the correct new encoding) mysql --default-character-set=utf8 -u root -p TARGET_DATABASE < db.sql
Noty looks good
Posted: February 14th, 2012 | Author: Vincent | Filed under: Development, JavaScript | No Comments »http://needim.github.com/noty/
ACF is awesomeone
Posted: February 1st, 2012 | Author: Vincent | Filed under: PHP | No Comments »http://www.advancedcustomfields.com
Permanently remove Mac’s “Downloaded from Internet” warning
Posted: January 29th, 2012 | Author: Vincent | Filed under: OS X | No Comments »defaults write com.apple.LaunchServices LSQuarantine -bool NO
Of course, you should know what you are doing.
Apache VirtualDocumentRoot for wildcard virtualhosts
Posted: January 27th, 2012 | Author: Vincent | Filed under: Development | No Comments »
<VirtualHost *:80>
UseCanonicalName Off
VirtualDocumentRoot /mnt/projects/%1/public
ServerName subdomains.starfall.local
ServerAlias *.starfall.local
</VirtualHost>
<Directory /mnt/projects/*/public>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Now you might run into infinite redirect issue if use together with mod_rewrite. Add this line in the mod_rewrite .htaccess file for each folder under /mnt/projects to stop this.
RewriteBase /
Update:
The similar effect can be achieved via mod_rewrite. Put this into /etc/apache2/httpd.conf, or apache2.conf but it needs to be loaded first before virtualhost/conf.d. The point here is to let virtualhost config to override this wildcard.
<VirtualHost *:80>
UseCanonicalName Off
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{HTTP_HOST} ^(.*)\.starfall.local$
RewriteRule ^/(.*)$ /mnt/projects/%1/public/$1
</VirtualHost>
You may still need to add “RewriteBase /” in case of infinite redirect.
From what I tested this new method handles:
- allow wildcard request to point to /mnt/projects/SUBDOMAIN_NAME/public
- it doesn’t not interfere with subsequent rewrite rule usually present in .htaccess
Achieve wildcard custom domain via Automatic Proxy Configuration
Posted: January 27th, 2012 | Author: Vincent | Filed under: System | No Comments »Update: This method seriously screws up apache server variables, namely, REQUEST_URI and DOCUMENT_ROOT. It is recommmended to only use this pac method when you know you don’t need correct REQUEST_URI and DOCUMENT_ROOT.
This allows to point multiple wildcard domain names (canonical or not) to a particular ip or hostname.
First create a Automatic Proxy Configuration file say wildcard.pac, which is basically a javascript file:
function FindProxyForURL(url, host)
{
if (dnsDomainIs(host, ".customhost.local")) {
return "PROXY customhost";
}
return "DIRECT";
}
Note:
“.customhost.local” is basically the pattern to detect
The “customhost” in “return “PROXY customhost”;” is whatever solvable hostname you want, or it can be an ip.
You can use this to config multiple local fake domain names for development purpose, pointing them (e.g. project1.myhost.local, project2.myhost.local to an apache server at 192.168.1.2).
Now to load this pac file, each browser/os is slightly different.
Safari/IE/Chrome: use system network setting, just find the Automatic Proxy Configuration and load the pac file just created.
Firefox/Opera: use their own network config in Preferences
Config Mac OSX Lion’s Samba share
Posted: January 27th, 2012 | Author: Vincent | Filed under: OS X | No Comments »smb.conf no longer work. The actual configuration is stored at /System/Library/LaunchDaemons/com.apple.smbd.plist
To make Lion’s samba share follow symbolic link, you need to modify “ProgramArguments”
from:
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/smbd</string>
</array>
</code>
to:
<key>ProgramArguments</key>
<array>
<string>/usr/sbin/smbd</string>
<string>--no-symlinks</string>
<string>false</string>
</array>
vimrc smart indent and tab with 4 spaces
Posted: January 26th, 2012 | Author: Vincent | Filed under: System | No Comments »set smartindent
set tabstop=4
set shiftwidth=4
set expandtab ts=4 sw=4 ai
Push local git repo to a remote server
Posted: January 23rd, 2012 | Author: Vincent | Filed under: Development | No Comments »So this is how to put local repository onto a remote “central” version control repository:
On remote server (note the “–bare” is important otherwise you won’t be able to push):
mkdir /home/vincent/projects/repo.git cd /home/vincent/projects/repo.git/ git init --bare
On local, cd into the local repo directory first
git remote add origin ssh://username@remoteserver/home/vincent/projects/repo.git git push origin master
In future you can just clone:
git clone ssh://username@remoteserver/home/vincent/projects/repo.git
jQuery Performance Tips
Posted: December 15th, 2011 | Author: Vincent | Filed under: JavaScript | No Comments »http://dumitruglavan.com/jquery-performance-tips-cheat-sheet/
Some notes from reddit:
#6 There is little to no performance difference between a cached selector and chaining. The ONLY difference would be shorter code (less bandwidth used?).
#9 This is just plain wrong. DOM manipulation can be extremely fast if you know what you are doing. He uses this example
The faster way to do this would be
frag = $('<ul id="menu"></ul>');
listItem = $('<li></li>')
for (var i = 1; i < 100; i++) {
frag.append(listItem.clone().text(i));
}
$('#header').prepend(frag);
#14 Use jQuery’s utility functions
“.each(), for example, relies on anonymous function calls. This can be REALLY slow”
#15 not really much of a performance tip
#16 is REALLY finicky and hard to profile.
Git deploy script
Posted: November 25th, 2011 | Author: Vincent | Filed under: Development | No Comments »The script grab new files (after “git add”) and modified files and copy them to a new directory “deploy”.
git status | grep "modified\|new file" | awk '{print $3}' | while read file; do mkdir -p `dirname deploy/"$file"`; cp $file deploy/$file; done
Or, use this to create the deploy folder for commits done in the last 2 days: (the code may generate some error output saying certain files don’t exist, because it’s trying to copy the files you deleted in commits, they are harmless just too lazy to put check in)
git whatchanged --since "2 days ago" | grep ^: | awk '{print $6}' | sort | uniq -c | awk '{print $2}' | while read file; do mkdir -p `dirname deploy/"$file"`; cp $file deploy/$file; done
Two interesting jQuery plugins
Posted: November 21st, 2011 | Author: Vincent | Filed under: JavaScript | No Comments »How to time machine
Posted: November 11th, 2011 | Author: Vincent | Filed under: OS X | No Comments »http://blog.interlinked.org/tutorials/rsync_time_machine.html
Django production server
Posted: November 10th, 2011 | Author: Vincent | Filed under: Development, Python | 1 Comment »Install
sudo apt-get install python-django libapache2-mod-wsgi python-mysqldb python-setuptools
Virtualhost
<VirtualHost *:80>
ServerName tang.local
WSGIDaemonProcess snake display-name=%{GROUP}
WSGIProcessGroup snake
WSGIScriptAlias / /mnt/host/snake/apache/django.wsgi
Alias /static /mnt/host/snake/static
</VirtualHost>
WSGI script
import os
import sys
sys.path.append('/mnt/host')
sys.path.append('/mnt/host/snake')
os.environ['DJANGO_SETTINGS_MODULE'] = 'snake.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Sftp upload with lftp/mirror
Posted: November 2nd, 2011 | Author: Vincent | Filed under: Linux | No Comments »lftp -c "open -u USERNAME,PASSWORD sftp://REMOTE_HOST/PATH/TO/ ; mirror -R /LOCAL/PATH/TO/."
Generate git change log
Posted: October 31st, 2011 | Author: Vincent | Filed under: Development | No Comments »
git whatchanged --since "2 month ago" | grep ^: | awk '{print $6}' | sort | uniq -c | awk '{print $2}' > change.log
Mount samba share on Mac Lion
Posted: October 18th, 2011 | Author: Vincent | Filed under: System | 1 Comment »mount.cifs //griffith/vincent/Projects /mnt/projects -o user=vincent,password=1234,nounix,noserverino,sec=ntlmssp,uid=33,gid=1001,file_mode=0664,dir_mode=0775
Note: only need sec=ntlmssp for Mac
phpMyAdmin config.inc.php
Posted: October 15th, 2011 | Author: Vincent | Filed under: Linux | No Comments »
<?php
// Load secret generated on postinst
include('/var/lib/phpmyadmin/blowfish_secret.inc.php');
// Load autoconf local config
include('/var/lib/phpmyadmin/config.inc.php');
$i = 0;
$i++;
if (is_readable('/etc/phpmyadmin/config-db.php')) {
require('/etc/phpmyadmin/config-db.php');
} else {
error_log('phpmyadmin: Failed to load /etc/phpmyadmin/config-db.php.'
. ' Check group www-data has read access.');
}
if (!empty($dbname)) {
$cfg['Servers'][$i]['auth_type'] = 'cookie';
if (empty($dbserver)) $dbserver = 'localhost';
$cfg['Servers'][$i]['host'] = $dbserver;
if (!empty($dbport) || $dbserver != 'localhost') {
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['port'] = $dbport;
}
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['controluser'] = $dbuser;
$cfg['Servers'][$i]['controlpass'] = $dbpass;
$cfg['Servers'][$i]['pmadb'] = $dbname;
$cfg['Servers'][$i]['bookmarktable'] = 'pma_bookmark';
$cfg['Servers'][$i]['relation'] = 'pma_relation';
$cfg['Servers'][$i]['table_info'] = 'pma_table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma_table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma_pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma_column_info';
$cfg['Servers'][$i]['history'] = 'pma_history';
$cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords';
$cfg['Servers'][$i]['tracking'] = 'pma_tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma_userconfig';
$cfg['Servers'][$i]['hide_db'] = '^information_schema|mysql|phpmyadmin$';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Servers'][$i]['CountTables'] = true;
$i++;
}
$cfg['DefaultLang'] = 'en';
$cfg['DisplayDatabasesList'] = 1;
$cfg['ServerDefault'] = 1;
$cfg['GZipDump'] = false;
$cfg['BZipDump'] = false;
$cfg['AllowUserDropDatabase'] = true;
$cfg['AllowArbitraryServer'] = true;
$cfg['LoginCookieValidity'] = 60000;
$cfg['LeftFrameDBSeparator'] = '';
$cfg['LeftDisplayTableFilterMinimum'] = 100;
$cfg['UserprefsDisallow'] = array('LeftFrameDBSeparator');
$cfg['ShowPhpInfo'] = true;
$cfg['ShowAll'] = true;
$cfg['MaxRows'] = 100;
$cfg['Order'] = 'ASC';
$cfg['LimitChars'] = 30;
$cfg['PropertiesIconic'] = true;
$cfg['Import']['charset'] = 'utf-8';
$cfg['Import']['ods_col_names'] = true;
$cfg['Export']['quick_export_onserver'] = true;
$cfg['Export']['quick_export_onserver_overwrite'] = true;
$cfg['Export']['charset'] = 'utf-8';
$cfg['Export']['sql_include_comments'] = false;
$cfg['Export']['sql_drop_table'] = true;
$cfg['Export']['sql_utc_time'] = false;
$cfg['Export']['csv_columns'] = true;
$cfg['Export']['excel_columns'] = true;
$cfg['Export']['htmlword_columns'] = true;
$cfg['Export']['ods_columns'] = true;
$cfg['Export']['texytext_columns'] = true;