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

Installing mod_pagespeed into cpanel/whm in centos

Here is another exciting thing that google has release and i really wish to document this down so that i could reuse it on all of my servers since i can see a good amount of optimization done on the server by mod_pagespeed. Anyway without anymore bull crap, i shall starts.

Firstly, you might want to visit mod_pagespeed download page and standby for the time being. And my machine is a 64bit machine so if you are running on 32bit just remove all the '64' you see in the instruction. eg. "lib64"

Firstly login into your server as "root" and start firing the below commands,

cd /usr/local/src
mkdir mod_pagespeed
cd mod_pagespeed
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
rpm2cpio mod-pagespeed-stable_current_x86_64.rpm | cpio -idmv
cp /usr/local/src/mod_pagespeed/usr/lib64/httpd/modules/mod_pagespeed.so /usr/local/apache/modules/
cp /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf /usr/local/apache/conf/
chmod 755 /usr/local/apache/modules/mod_pagespeed.so
mkdir /var/mod_pagespeed/{cache,files} -p
chown nobody:nobody /var/mod_pagespeed/*

mod_pagespeed has a dependency that you'll want to enable: mod_deflate (the httpd source directory (httpd-2.2.21) may vary depending on your install):

/usr/local/apache/bin/apxs -c -i /home/cpeasyapache/src/httpd-2.2.21/modules/filters/mod_deflate.c

After that, we'll have to edit the mod_pagespeed configuration file located at /usr/local/apache/conf/pagespeed.conf to reflect the correct paths, the LoadModule directives should fetch the modules in "modules/module_name.so":

LoadModule pagespeed_module /usr/local/apache/modules/mod_pagespeed.so
LoadModule deflate_module /usr/local/apache/modules/mod_deflate.so
ModPagespeedFileCachePath            "/var/mod_pagespeed/cache/"
ModPagespeedGeneratedFilePrefix      "/var/mod_pagespeed/files/"

And then open /usr/local/apache/conf/includes/pre_main_global.conf and add:

Include "/usr/local/apache/conf/pagespeed.conf"

Next restart apache

/scripts/buildhttpdconf
/etc/init.d/httpd restart

And we are done!

Average website response time bash script

Ok i make a little bash script to test a particular website response time. This is done via the command line through a bash script. Anyway, here's the script code.


#!/bin/bash

CURL="/usr/bin/curl"
echo -n "Please pass the url you want to measure: "
read url
URL="$url"
count=1;
total_connect=0
total_start=0 
total_time=0
	echo " Time_Connect Time_startTransfer Time_total ";
while [ $count -le 100 ]
do
	result=`$CURL -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} $URL`
	echo $result;

	var=$(echo $result | awk -F":" '{print $1, $2, $3}')
	set -- $var

	total_connect=`echo "scale=2; $total_connect + $1"  | bc`;
	total_start=`echo "scale=2; $total_start + $2" | bc`;
	total_time=`echo "scale=2; $total_time + $3" | bc`;
	count=$((count+1))
done
echo "average time connect: `echo "scale=2; $total_connect/100" | bc`";
echo "average time start: `echo "scale=2; $total_start/100" | bc`";
echo "average time taken: `echo "scale=2; $total_time/100" | bc`";

What the above does is to prompt you for a url to test and run it for 100 times on your machine and print out the average response time from the website server to your machine.

Assuming you save the above file in 'shell', you will call the above script via 'bash ./shell' and it will prompt you for an url and start executing the url for 100 times and gives you an average response time.

P.S: the above script required you to install bc, you can install via yum install bc or sudo apt-get install bc 😉