Wednesday, May 29, 2013

asynchronous php thread (asynchronous cake shell execution)

While cumbersome if not impossible to actually manage threads in PHP, you can always execute in background a php script.

Here's how:

While in a cakephp framework method (of course the important details are the ones in colored background):

$str = 'path/to/cakelib/cake/console/cake -working '.ROOT . DS . APP_DIR.' -app ' . ROOT . DS . APP_DIR . ' my_shell_script > /dev/null 2> '.LOGS.'my_log.log &';

exec($str, $output2, $status);




I used the ideas from this post: http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php

This answer:
If it "doesn't care about the output", couldn't the exec to the script be called with the & to background the process?
EDIT - incorporating what @AdamTheHut commented to this post, you can add this to a call to exec:
"> /dev/null 2>/dev/null &"
That will redirect both stdio (first >) and stderr (2>) to /dev/null and run in the background.
There are other ways to do the same thing, but this is the simplest to read.

An alternative to the above double-redirect:
" &> /dev/null &"


Tuesday, May 21, 2013

mysql dump to a file with where clause

This is very useful to export records in a table to a file, to import later...


mysqldump --databases dbname -h hostname --tables tablename --where="condition='1' and condition='2'" -u username -p --no-create-db --no-create-info > myfile.dump

to import

mysql --databases dbname -h hostname --tables tablename -u username -p < myfile.dump

Wednesday, May 15, 2013

Translating month names in CakePHP (i18n)

I found this, and I think it is worth saving.

Taken from: http://www.bravo-kernel.com/2010/12/using-lc_time-with-cakephp/


USING LC_TIME WITH CAKEPHP

CakePHP supports LC_TIME translations since version 1.3 and since it took me some time to completely figure out the logic behind it I am storing my notes here as a mental reminder to self (again).
First things first… make sure you read  the i18n paragraph on LC_TIME in the Book to get some basic understanding of what we are trying to do here.

Preparing for LC_TIME

For __c(), $this->Time->format() and $this->Time->i18nFormat() to work:
  1. create a file called /app/locale/dut/LC_TIME
  2. on your local Linux workstation open /usr/share/i18n/locales/nl_NL
  3. copy everything between LC_TIME and END LC_TIME to the file created in step 1 and save that file
Note: make sure to add the escape_char and comment_char definitions to your LC_TIME file as well or your setup will not be fully functional (see this page for more info):
1
2
comment_char %
escape_char  /

strftime()

There is no need to use php’s strftime() if you consistently stick to using the CakePHP functions mentioned above. However, if you do need to get strftime() up and running you should add the following line to one of your controllers.
1
setlocale(LC_TIME, 'nl_NL.UTF8');
Please note (and understand) that this will use your server’s locales and NOT your manually created Cake LC_TIME file.

Testing your LC_TIME setup

Add the following lines to one of your views (and make sure the TimeHelper is available):
1
2
3
4
5
6
7
8
9
10
$timestamp = time();
 $timestring = $this->Time->format('Y-m-d H:i:s', $timestamp);
 $months = __c('mon', 5 ,true);
 
 pr("Timestamp = $timestamp");
 pr("Timestring = $timestring");
 pr("strftime() translated = " . strftime("%A %e %B %Y", strtotime($timestring)));
 pr("i18nFormat  translated = " . $this->Time->i18nFormat($timestring, "%A %e %B %Y"));
 pr("Time::format translated = " . $this->Time->format($timestring, '%A %e %B %Y'));
 pr($months);
If your setup is fully operational it should display the following LC_TIME translations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Timestamp = 1292417839
Timestring = 2010-12-15 13:57:19
strftime() translated = Wednesday 15 December 2010
i18nFormat  translated = woensdag 15 december 2010
Time::format translated = woensdag 15 december 2010
Array
(
    [0] => januari
    [1] => februari
    [2] => maart
    [3] => april
    [4] => mei
    [5] => juni
    [6] => juli
    [7] => augustus
    [8] => september
    [9] => oktober
    [10] => november
    [11] => december
)
Note: you might have spotted that the strftime() translation is not translated. This is intentional since I always stick to Cake methods. See the paragraph on strftime() if you do need to use that function.
Enjoy your time translations ;)