Removing Brew and Reinstalling it

I faced an issue with my brew installation which caused a lot of ruby error popping out every time i uses Brew. Therefore i needed a way to reinstall brew.

Uninstall Brew

A quick Google search lead me to Uninstalling brew (so I can reinstall) by Eneko Alonso and he has all the instruction listed to cleanly removing your brew installation. But make sure the first one (`brew –prefix`) returns the path where homebrew was installed properly. If not, you might ending up removing stuff from your computer you did not intend to remove.

cd `brew --prefix`
rm -rf Cellar
brew prune
rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew
rm -rf ~/Library/Caches/Homebrew

Reinstalling Brew

All you have to do is fire this on your terminal

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Sit back and relax until Brew is fully installed. Once its done, remember to use "brew doctor" to make sure it said  the following


$ brew doctor
Your system is raring to brew.

and we are good to go!

Varnish Error 418 I’m a teapot

Recently i installed varnish into our cPanel server on my web hosting company in Malaysia. The installation is pretty straight forward and simple but the only issue here is that my main website is getting an 'error' text rather than showing me my varnish website.  If you inspect element, it will gives you Error 418 in its http headache with the error "I'm a teapot" which makes me a little irritated with it.

I was getting this error because my main website has a dedicated ip which is different from the rest of the account. This also means that my varnish configuration is a little different from all other account in it. But getting 'error' isn't what i was after. Normally one will just skip varnish entirely because it doesn't work. However,  you shouldn't be worry too much on it. Apparently the error was caused by varnish cached and if you plunge it, your website should be showing up fine. Below you can find purge command for varnish 3.0

sudo varnishadm "ban req.http.host ~ www.mydomain.com"

or

sudo varnishadm "ban req.http.host ~ /"

The only thing you might need to take note is the browser cache showing on your browser which might mislead you and makes you panic for a bit. If it still showing error 418, all you need to do is wait a little bit and it should show up nicely.

Mysql make slug function

New update on slugify

DROP FUNCTION IF EXISTS `slugify`;
DELIMITER ;;
CREATE DEFINER=CURRENT_USER
FUNCTION `slugify`(dirty_string varchar(200))
RETURNS varchar(200) CHARSET latin1
DETERMINISTIC
BEGIN
    DECLARE x, y , z, i Int;
    Declare temp_string, allowed_chars, new_string VarChar(200);
    Declare is_allowed Bool;
    Declare c, check_char VarChar(1);
    set i = 0;

    set allowed_chars = "abcdefghijklmnopqrstuvwxyz0123456789-";
    set temp_string = LOWER(dirty_string);

    Select temp_string Regexp('&') Into x;
    If x = 1 Then
        Set temp_string = replace(temp_string, '&', ' and ');
    End If;

    Select temp_string Regexp('[^a-z0-9]+') into x;
    If x = 1 then
        set z = 1;
        While z <= Char_length(temp_string) Do
            Set c = Substring(temp_string, z, 1);
            Set is_allowed = False;
            Set y = 1;
            Inner_Check: While y <= Char_length(allowed_chars) Do
                If (strCmp(ascii(Substring(allowed_chars,y,1)), Ascii(c)) = 0) Then
                    Set is_allowed = True;
                    Leave Inner_Check;
                End If;
                Set y = y + 1;
            End While;
            If is_allowed = False Then
                Set temp_string = Replace(temp_string, c, '-');
            End If;

            set z = z + 1;
        End While;
    End If;

    Select temp_string Regexp("^-|-$|'") into x;
    If x = 1 Then
        Set temp_string = Replace(temp_string, "'", '');
        Set z = Char_length(temp_string);
        Set y = Char_length(temp_string);
        Dash_check: While z > 1 Do
            If Strcmp(SubString(temp_string, -1, 1), '-') = 0 Then
                Set temp_string = Substring(temp_string,1, y-1);
                Set y = y - 1;
            Else
                Leave Dash_check;
            End If;
            Set z = z - 1;
        End While;
    End If;

    Repeat
        Select temp_string Regexp("--") into x;
        If x = 1 Then
            Set temp_string = Replace(temp_string, "--", "-");
        End If;
    Until x <> 1 End Repeat;

    If LOCATE('-', temp_string) = 1 Then
        Set temp_string = SUBSTRING(temp_string, 2);
    End If;

SELECT COUNT(*) INTO i FROM fanpage WHERE slug LIKE CONCAT(temp_string,'%');
If i > 0 Then
	Set temp_string = CONCAT(temp_string,'-',i+1);
End If;

Return temp_string;
END;;
DELIMITER ;

P.S: change "fanpage" to your own table - this is to prevent duplicate on slug

credit goes to http://nastyhabit.wordpress.com/2008/09/25/mysql-slug-maker-function-aka-the-slugifier/

Yii renderPartial duplication solution

Its been almost a year since i found something interested to write since im busy working and didn't really get the time to write some useful stuff. Today i came across a well known issue in Yii solution that caused duplicate js request whenever we are doing ajax stuff with renderPartial. Here's a small js script that will save our asses.

        $.ajaxSetup({
                global: true,
                dataFilter: function(data,type){
                        //  only 'text' and 'html' dataType should be filtered
                        if (type && type != "html" && type != "text")
                        {
                                return data;
                        }
         
                        var selector = 'script[src]';
         
                        // get loaded scripts from DOM the first time we execute.
                        if (!$._loadedScripts) 
                        {
                                $._loadedScripts = {};
                                $._dataHolder = $(document.createElement('div'));
 
                                var loadedScripts = $(document).find(selector);
 
                                //fetching scripts from the DOM
                                for (var i = 0, len = loadedScripts.length; i < len; i++) 
                                {
                                        $._loadedScripts[loadedScripts[i].src] = 1;
                                }
                        }
                 
                        //$._dataHolder.html(data) does not work
                        $._dataHolder[0].innerHTML = data;
         
                        // iterate over new scripts and remove if source is already in DOM:
                        var incomingScripts = $($._dataHolder).find(selector);
                        for (var i = 0, len = incomingScripts.length; i < len; i++)
                        {
                                
                                if ($._loadedScripts[incomingScripts[i].src] && incomingScripts[i].src.indexOf('search') == -1)
                                {
                                        $(incomingScripts[i]).remove();
                                }
                                else
                                {
                                        $._loadedScripts[incomingScripts[i].src] = 1;
                                }
                        }
                        return $._dataHolder[0].innerHTML;
                }
        });

Credit goes to Yii forum contributor! but not all scenario will work, still, its good enough at least

lvextend connect() failed on local socket: Connection refused

A small problem that i would like to record it down in this blog. What happen here is that i have setup a server with gfs previously and after i deactivated gfs, i'm getting connect failed on local socket connection refused on my lvm2 and this has been going about for a while and i finally manage to get over it by doing the following

lvmconf --disable-cluster

Now, try to fire the lvextend again,

lvextend -L+1G /dev/myvg/homevol

It should works. Hope this helps