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
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
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 »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()
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
Monospaced Fonts for Programming
Posted: September 29th, 2011 | Author: Vincent | Filed under: Development | No Comments »Some nice monospaced fonts.
- Consolas.ttf
- Mensch.ttf
- Terminus.ttf
- Inconsolata.ttf
- Monaco.ttf
Managing apache folder
Posted: September 8th, 2011 | Author: Vincent | Filed under: Development, Linux | No Comments »The goal here is to allow apache process as well as multiple developers to have read and write permission to all apache files.
What I’m currently doing is:
- Add a new group “dev”
addgroup dev
- Add www-data (or whatever your apache runner user is) to the group “dev”
adduser www-data dev
- Add all developers to the group “dev”
adduser vincent dev adduser anotherdev dev
- Change apache user “www-data” and all developers’ default group to “dev”
vim /etc/passwd # each line is a user: # username:x:userid:groupid:userinfo:script # now change groupid from the default user group to the groupid of "dev"
- Change umask to 002 (all developers on ssh, all developers on sftp, and www-data)
vim /etc/profile # change umask to 002 vim /etc/ssh/sshd_config # change Subsystem to: Subsystem sftp /usr/lib/openssh/sftp-server -u 0002 vim /etc/apache2/envvars # add umask 002
- Change all apache folders to www-data:dev, 775 and files to www-data:dev, 664
chown www-data:dev /home/www-data -R
chmod 775 /home/www-data
find /home/www-data/* -type d -exec chmod 775 {} \;
find /home/www-data/* -type f -exec chmod 664 {} \;
CSS3 based jQuery Gallery
Posted: September 5th, 2011 | Author: Vincent | Filed under: CSS, JavaScript | No Comments »YUI Compressor for JavaScript
Posted: September 2nd, 2011 | Author: Vincent | Filed under: JavaScript | No Comments »# $1 input file # $2 output file java -jar yuicompressor.jar $1 -o $2
yuicompressor.jar is attached.
source: http://yuilibrary.com/download/yuicompressor/
Google event tracking
Posted: August 30th, 2011 | Author: Vincent | Filed under: JavaScript | No Comments »
$(document).ready( function() {
// add an event to all link for google analytics
$('a').click(function () {
// tell analytics to save event
try {
var identifier=$(this).attr('id') ;
var href=$(this).attr('href')
var label="";
if ( typeof( identifier ) != 'undefined' ) {
label=label+'[id]:'+identifier
category='JSLink'
}
if ( typeof( href ) != 'undefined' ) {
label=label+' [href]:'+href
if ( href[0] == '#' ) {
category='Anchor';
} else {
category='Link';
}
}
_gaq.push(['_trackEvent', category, 'clicked', label]);
// console.log('[tracked]: ' + category + ' ; clicked ; ' + label );
}
catch (err) {
// console.log(err);
}
// pause to allow google script to run
var date = new Date();
var curDate = null;
do {
curDate = new Date();
} while (curDate-date < 300);
});
});
Source: External blog
Make JavaScript calls within ActionScript 3
Posted: August 30th, 2011 | Author: Vincent | Filed under: JavaScript | No Comments »
import flash.external.ExternalInterface;
var returnedValue:int = ExternalInterface.call("foobar()", "argument");
Learn Vim Progressively
Posted: August 30th, 2011 | Author: Vincent | Filed under: Development, Linux | No Comments »Best practice for loading jquery, and other JavaScript libs
Posted: August 4th, 2011 | Author: Vincent | Filed under: Development, JavaScript | No Comments »
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/VERSION.NUMBER/jquery.min.js"></script>
<script type="text/javascript">window.jQuery || document.write('<script src="js/jquery.js" type="text/javascript">\x3C/script>')</script>