Thursday, June 30, 2011

split a large file into small files

If we want to split a large file into several smaller files, we can use split command (on linux).

split --bytes=SIZE largeFile smallFilePrefix

For example, i have a file a little larger than 50M(well, maybe that is not really large). If i want to split it into several small 1M files, i can do:

split --bytes=1M largeFile smallFile

And the you will find a lot of smallFile* files was generated. Let 's count them:

ls smallFile* | wc -l

I got probably 53 of them.

If we want to restore the original file, we can do

cat smallFile* > originalFile

using sed to replace spaces

I want to replace multiple white spaces(but still keep tabs) with one space only in a file. Here is how we can do it:

sed 's/ \+/ /g' targetFile > newFile

install xdebug on windows

1. Download xdebug windows binary file from http://xdebug.org/download.php. If you are using Apache, make sure you download the "PHP X.X XXX TS(XX XXX)" one. TS here means thread safe. If you are using IIS, then download NTS version.

2. After download the .dll file, put it in the php/ext folder and rename it to php_xdebug.dll

3. Edit the php.ini file, add these lines:

extension=php_xdebug.dll

[Xdebug]
xdebug.profiler_enable=on
xdebug.trace_output_dir="d:\xdebug"
xdebug.profiler_output_dir="d:\xdebug"
xdebug.dump.GET=*
xdebug.show_local_vars=1

"d:\xdebug" is just the folder where you want to save xdebug's output.

Restart the web server and run phpinfo(). If you find xdebug section, then the installation succeeds.

Tuesday, June 28, 2011

javascript tricky questions, their answers & explanation

Questions below are just tricky questions. I try to answer them simply for fun. 

1.
(function () { 
    return typeof arguments; 
})(); 

A. "object"
B. "array"
C. "arguments"
D. "undefined"

Answer: B
Reason: Javascript function is actually object so they have properties. arguments is a property of javascript function. It consists of an array of all the arguments passed to a function. Although arguments property is an array, typeof arguments  will simply tell 'object'. Although this information is not that useful, it is correct, because javascript array is also object.

2.
var f = function g() {
        return 23;
    };
typeof g();

A. "number"
B. "undefined"
C. "function"
D. Error

Answer: D
Reason: This is a named function expression. 'g' is the function name. The important part is this name can be used by the code inside the function(g()) to refer to the function itself, but the code outside the function cannot see it at all.

3.
(function (x) {
    delete x;
    return x;
})(1);

A. 1
B. null
C. undefined
D. Error

Answer: A
Reason: note that delete can only delete object properties. You can't use it to delete variables, no matter the variables are functions or objects.

4.
(function f(f) {
    return typeof f();
})(function () {
    return 1;
});

A. "number"
B. "undefined"
C. "function"
D. Error

Answer: A
Reason: Not hard. It is just the code not easy to read. 

5.
var foo = {
    bar: function () {
        return this.baz;
    },
    baz: 1
};
(function () {
    return typeof arguments[0]();
})(foo.bar);

A. "undefined"
B. "object"
C. "number"
D. "function"

Answer:A
Reason: Don't be fooled by picking C. This is a typicl callback scope issue: arguments[0]() is a callback just like you try to call foo.bar(); However, inside foo.bar, there is a keyword 'this'. We must remember that the callback method uses 'this' to refer to the object it belongs to. So, where is 'this' pointing to? We must note the way we do callback here: arguments[0](). 'this' is actully referring to the object 'arguments'! If we modify the code a little bit:
(function () {
    arguments.baz = 3;
    return typeof arguments[0]();
})(foo.bar);

Well this time we will get 'number'. I made a post about javascript callback function scope: http://hengrui-li.blogspot.com/2011/05/javascript-callback-function-scope.html

6.
var f = (function f() {
    return "1";
}, function g() {
    return 2;
})();
typeof f;

A. "string"
B. "number"
C. "function"
D. "undefined"

Answer: B
Reason: immediate function. f === 2.

7.
var x = 1;
if (function f() {}) {
    x += typeof f;
}
x;

A. 1
B. "1function"
C. "1undefined"
D. NaN

Answer:C
Reason:This is exactly like:
var x = 1;
if (g=function f(){}) {
    x += typeof f;
}
x;

8. var x = [typeof x, typeof y][1];
typeof typeof x;

A. "number"
B. "string"
C. "undefined"
D. "object"

Answer: B
Reason: Actually, you don't really have to figure out what x really is. No matter what it is, typeof x must be a string indicating what x type is. It is either 'undefined' ,'string', 'object' or 'number', typeof typeof x must be string then.

9.
(function (foo) {
    return typeof foo.bar;
})({
    foo: {
        bar: 1
    }
});

A "undefined"
B "object" 
C "number"
D Error

Answer: A
Reason: another wholy tricky question. in return typeof foo.bar, foo is actally the object {foo:{bar:1}}.

10.
(function f() {
    function f() {
        return 1;
    }
    return f();

    function f() {
        return 2;
    }
})();

A.1 
B.2 
C.Error (e.g. "Too much recursion") 
D.undefined

Answer:B
Reason:function hoisting.

function f() {
    return f;
}
new f() instanceof f;

A.true 
B.false

Answer: B
Reason: simply remember in javascript, everything is a singleton. More about javascript singleton: http://hengrui-li.blogspot.com/2011/05/javascript-design-patterns-singleton.html

Sunday, June 26, 2011

Cool PHP Documentation update

1. pman - php man pages
Linux users know that 'man' command is very handy. If you want to learn the usage of 'ls' command, you can simply type man ls.
pman, is php man pages. To install:

sudo pear channel-update doc.php.net
sudo pear install doc.php.net/pman

Ater install ok, you can simply use 'man <php cmd/function>'. For example, if you want to learn PHP's empty function, you can type: man empty

2. Enhanced CHM PHP manual. (Download: http://www.php.net/download-docs.php)
I know we can get help information from internet anytime anywhere now. But i always like to keep a copy of offline manual. There are several benefits of keeping an offline document: 
* you can access it even the internet connection is lost. 
* you can read and learn it just like reading a book. So you won't be limited to a specific topic. You can browse the big picture.
* It is fast, easy to browse and search.

PHP manual, however, did NOT contain those user notes, which contain very valuable information sometimes. This enchanced PHP manual addresses this issue. The manual contains the user notes. This make the offline manual much better and more valuable.

This is the online document editor. You can login as anonymous user. I didn't spend much time on this. The only interesting part to me is, well, this online document editor is obviously powered by ExtJS, which i was working on recently.

Thursday, June 23, 2011

mysql replace text in field

Task: Assume that we have a table called articles. This table has a column/field called title. We have a lot of titles like "ubuntu administration tips xxx". One day we found that all these tips actually apply to all linxu systems, so we want to change all the titles from "ubuntu administration tips xxx" to "linux administration tips xxx"

Here is how we can do it quickly:

UPDATE articles SET title = REPLACE(title, 'ubuntu', 'linux');

Monday, June 20, 2011

PHP $_REQUEST: why or why not

Many PHP developers hold this as a rule: avoid using $_REQUEST and always use $_POST or $_GET. If you ask why, the answer may be 'it is not secure'. Why using $_REQUEST is not secure? Well, i believe you can NOT find a strong proof. You may bring up Cross-Site Request Forgeries (CSRF), but that is not $_REQUEST's fault. Even you use $_GET or $_POST, you still may suffer CSRF, easily.

Another reason might be that in your system, you have this very bad practice: http://hengrui-li.blogspot.com/2011/06/php-get-post-precedence.html, which you should not.

A more decent reason is, prior to PHP5.3, COOKIE data is also populated into $_REQUEST, and takes the higher priority than GET and POST(Open php.ini, you can find variables_order = "GPCS", read the document and you will know more). In PHP5.3, this issue has gone. COOKIE data will not get populated in $_REQUEST by default. Prior to PHP5.3, you can change php.ini if you have control on your server. If you don't? Well, talk about this situation later.

So, what is the benefit of using $_REQUEST? The truth is, in most cases, we don't care if the data is submitted via GET or POST. By using $_REQUEST, we gain so much flexibility on both client side and server side. For example, if we are developing an API, we define this API accepts several parameters, and return the result in certain format. We don't really want to restrict the users that they can only call this API via POST. That is all up to the user. As long as a user submits the correct data, the API should work as expected. It also helps in debugging and testing by allowing both POST and GET. We can easily simulate an API request with test data.

Ok, back to the question. What if we are in PHP5.2 and cannot change server settings and we still want to gain the flexibility? Well, simply do this: $params = array_merge($_GET, $_POST);