Pages

Wednesday, August 1, 2012

MySQL: Change Collation on an Existing Table

I recently ran across an issue where a table's collation was set to latin1_swedish_ci and it really should have been utf8_unicode_ci. Just changing the collation didn't seem to be doing the trick because some characters were still messed up. That's when I realized you need to convert the current character set to UTF8. After doing this everything worked as expected.

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci

Wednesday, August 24, 2011

Configure PhpStorm to Auto-complete CakePHP Models, Views, and Controllers

After playing around a bit today I finally figured out how to get PhpStorm to auto-complete methods for models and controllers. Here's what you need to do.

Removing Multiple Definitions


First, let's tackle the multiple definitions problem that we see below.

Multiple Definitions


There are multiple places defining AppController. We need to remove the ones that are included in the following locations from our 'Directories' in the project's settings.

The two locations are:
  • $CAKEHOME/cake/console
  • $CAKEHOME/cake/tests
Exclude Directories


Next we need to mark the following file as plain text.
  • $CAKEHOME/cake/libs/controllers/app_controller.php
Mark as Plain Text


You should now see that PhpStorm is no longer complaining about multiple definitions. If it is you may want to check your plugins/components to see if they're mucking it up. If they are, just mark the file with the definition as plain text.

Multiple Definitions Resolved


Auto-completion should now work for the controller. However, it's still not working correctly on our model.

Autocomplete works, but not on the model.


Adding the Model


To fix the model we need to add a magic property to the class.

 /**  
 *@property ModelName $ModelName  
 */  

Here's an example from the controller we've been working in.

@property ModelName $ModelName


 We can now auto-complete on our models in the controller.

Auto-complete on the model.


Defining Model Relationships


Lastly, we need to add magic properties to our models to define its relationships with other models. Basically, for each "belongs to" relationship defined in the model's file you need to add the magic property comment.

Define belongs to relationships


We can now auto-complete these relationships.

Auto-complete model relationships


Setting Up Helper Auto-completion in Views


To get auto-completion working in views we need to include a file created by junichi11 over at GitHub.

Download this file and save it in a directory somewhere outside of your current project. I did this so I could use the same file on multiple projects.
Now add that directory to your current project.

Open a view file and add the following variable definition.

 /**  
 *@var $this View  
 */  


You should now be able to auto-complete helpers in your view!

Auto-completion of helpers


Core Component Auto-Completion


Add the following to your app_controller.php file and this will add component auto-completion.

 /**  
  * CakePHP Component & Model Code Completion  
  * @author junichi11  
  *  
  * ==============================================  
  * CakePHP Core Components  
  * ==============================================  
  * @property AuthComponent $Auth  
  * @property AclComponent $Acl  
  * @property CookieComponent $Cookie  
  * @property EmailComponent $Email  
  * @property RequestHandlerComponent $RequestHandler  
  * @property SecurityComponent $Security  
  * @property SessionComponent $Session  
  */  

Wednesday, April 21, 2010

SABnzbd URL Bookmarklet (NZBMatrix)

Here's a simple bookmarklet that will take the URL of the current page you're on and tell SABnzbd to download it. It works great if you're using NZBMatrix and on the page for the post you're wanting to download. Perfect for your mobile browser.

javascript:location.href='http://yourhost:8080/api?mode=addurl&name='+encodeURIComponent(location.href)+'&apikey=yourapikey';  

Be sure to insert your SABnzbd API key and hostname (or IP address) into the bookmarklet's code.

Wednesday, January 20, 2010

Perl Find and Replace One Liner

I'm constantly needing a way to easily change a line in a bunch of configuration files. I thought I'd share the way I do it here.

perl -p -i -e 's/oldstring/newstring/g' *

This allows you to use a regular expression and perform the find and replace.

Wednesday, June 24, 2009

HOWTO: Set the SSHD Idle Timeout

Here's something that I usually forget to change from the default and then get annoyed when my terminal hangs.
  1. As root open your sshd_config file in an editor.
    su -  
    vim /etc/ssh/sshd_config  
    

  2. Add the following lines.
     ClientAliveInterval 600  
     ClientAliveCountMax 3

  3. Restart the sshd process.
     service sshd restart
That's it. That will keep you logged in for 30 minutes at a time without activity. (600 seconds x 3)

Saturday, April 4, 2009

M3U Playlist Copy Script

A couple of months ago I picked up a Garmin Nuvi 760 on the cheap. As it turns out this great GPS unit can also play music quite well, especially for riding on the motorcycle. The only problem was there was no good way to get playlists on the device that I could find. Enter the following bash script. This script will read an m3u file, copy all associated mp3 files, and generate a new m3u file. Now all I have to do is specifiy m3u files that I exported from Mozilla Songbird and the path to the Garmin's SD card.

 #!/bin/bash  
 # April 4, 2009  
 # m3u_cp.sh  
 #  
 # Take an m3u file and copy all associated mp3 files  
 # to a destination directory and generate a new m3u.  
 #  
 # Used to copy m3u playlists from computer to Garmin.  
 if [ $# -lt 2 ]; then  
 echo "Usage: m3u_cp.sh some.m3u /dst"  
 exit 0  
 fi  
 # Read the m3u file into an array  
 declare -a M3U  
 exec 10<"$1"  
 let count=0  
 while read LINE <&10; do  
 M3U[$count]=$LINE  
 ((count++))  
 done  
 exec 10>&-  
 # Determine the m3u's filename  
 if [[ $1 =~ [^/]*m3u ]]; then  
 m3u_path="$2/$BASH_REMATCH"  
 fi  
 # If playlist arleady exists, delete it  
 if [ -f "$m3u_path" ]; then  
 rm -f "$m3u_path"  
 fi  
 # Loop through the m3u lines  
 i=0  
 while [ $i -lt ${#M3U[@]} ]; do  
 # The current line is a comment, do nothing with it  
 if [ ${M3U[$i]:0:1} = "#" ]; then  
 echo ${M3U[$i]} >> "$m3u_path"  
 #Current line is a path to an mp3 file  
 else  
 # Get the current songs filename  
 if [[ ${M3U[$i]} =~ [^/]*mp3 ]]; then  
 song=$BASH_REMATCH  
 mpath=$( echo ${M3U[$i]} | tr -d '\r' )  
 # if the song doesn't exist, copy it to the desitnation folder  
 if [ -f "$2/$song" ]; then  
 echo File Exists -- $song  
 else  
 echo Copying -- $song  
 cp "$mpath" "$2/$song"  
 fi  
 # Write the song in the m3u file  
 echo $song >> "$m3u_path"  
 else  
 echo "The regex for finding the song's filename is fraked up."  
 fi  
 fi  
 let i=i+1  
 done  
 exit 0  

In the middle of writing this I really started wondering why I used bash. Perl would've been a lot easier.

Wednesday, March 11, 2009

SVN+SSH Howto: Subversion Quick and Simple

Here's a quick and simple way to create a Subversion repository while maintaining security by using SSH and the filesystem permissions.
  1. Create users and add them to a group. There are a bunch of different ways to do this. I am only showing you how to create a group.
    groupadd svn-users

  2. Make a directory to house the repository.
    mkdir /var/lib/project

  3. Create the repository.
    svnadmin create /var/lib/project

  4. Change permissions to allow the group read/write access.
    cd /var/lib/project
    chgrp svn-users db db/transactions db/write-lock db/revs db/revprops hooks locks
    chmod 2770 db db/transactions db/revs db/revprops
    chmod 660 db/write-lock
    chmod 750 hooks
    chmod 770 locks
You can now have your users access the repository over SSH.

svn+ssh://username@server.example.com/var/lib/project

If your clients are on Windows, I recommend using TortoiseSVN.

Credit goes to the Carnival of Technology.