Flush every Magento cache from the command line

If you need to clean every Magento cache from the command line (maybe to execute a cron) you can use this simple code:

<?php
date_default_timezone_set("Europe/Madrid");
echo "Start Cleaning all caches at ... " . date("Y-m-d H:i:s") . "\n\n";
ini_set("display_errors", 1);

require '../app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);
Mage::getConfig()->init();

$types = Mage::app()->getCacheInstance()->getTypes();

try {
    echo "Cleaning data cache... \n";
    flush();
    foreach ($types as $type => $data) {
        echo "Removing $type ... ";
        echo Mage::app()->getCacheInstance()->clean($data["tags"]) ? "[OK]" : "[ERROR]";
        echo "\n";
    }
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}

echo "\n";

try {
    echo "Cleaning stored cache... ";
    flush();
    echo Mage::app()->getCacheInstance()->clean() ? "[OK]" : "[ERROR]";
    echo "\n\n";
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}

try {
    echo "Cleaning merged JS/CSS...";
    flush();
    Mage::getModel('core/design_package')->cleanMergedJsCss();
    Mage::dispatchEvent('clean_media_cache_after');
    echo "[OK]\n\n";
} catch (Exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}

try {
    echo "Cleaning image cache... ";
    flush();
    echo Mage::getModel('catalog/product_image')->clearCache();
    echo "[OK]\n";
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");
}

Just put the code in a cleanAllCaches.php file, put it into the Magento shell directory and execute it via:

php cleanAllCache.php

, , ,

No Comments

Better indexer script for Magento

If you use Magento you’ll know how frustrating the indexing process could be. Thankfully Magento gives us a useful php script (shell/indexer.php) to execute the reindex via the command line, but it has minor problems. The first one is that it doesn’t give you a clue on which reindexing process is actually going on, the second one is that it’s difficult to determine how long does it takes to execute the script.
With the following small changes I address these problems.
First of all you can add this function to the indexer.php script:

/**
 * Returns the actual microtime in seconds
 * @return float seconds
 */
protected function chrono() {
    list($msec, $sec) = explode(' ', microtime());
    return ((float) $msec + (float) $sec);
}

Then change the function run() from this:

foreach ($processes as $process) {
    /* @var $process Mage_Index_Model_Process */
    try {
        $process->reindexEverything();
        echo $process->getIndexer()->getName() . " index was rebuilt successfully\n";
    } catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    } catch (Exception $e) {
        echo $process->getIndexer()->getName() . " index process unknown error:\n";
        echo $e . "\n";
    }
}

to this:

$totalstart = $this->chrono();
foreach ($processes as $process) {
    /* @var $process Mage_Index_Model_Process */
    try {

        echo "Started " . $process->getIndexer()->getName() . " reindexing process\n";
        $start = $this->chrono();
        $process->reindexEverything();
        $end = $this->chrono();
        $chrono = round($end - $start, 3);
        echo $process->getIndexer()->getName() . " index was rebuilt successfully in $chrono seconds\n";
    } catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    } catch (Exception $e) {
        echo $process->getIndexer()->getName() . " index process unknown error:\n";
        echo $e . "\n";
    }
}
$totalend = $this->chrono();
$totalchrono = round($totalend - $totalstart, 3);
echo "\n";
echo "All completed in $totalchrono" . " seconds\n";

Now the output for this command:

php indexer.php --reindex tag_summary,cataloginventory_stock

will be something like:

Started Tag Aggregation Data reindexing process
Tag Aggregation Data index was rebuilt successfully in 0.159 seconds
Started Stock Status reindexing process
Stock Status index was rebuilt successfully in 4.218 seconds

All completed in 4.387 seconds

, ,

No Comments

Solve “This account is locked.” problem in Magento

If your user has been locked out from the backend, you can reset the lock with one simple query:


UPDATE `admin_user` SET `failures_num` = 0, `first_failure` = NULL, `lock_expires` = NULL WHERE `user_id` = 1;

You can find your user_id with this query:

SELECT `user_id` FROM `admin_user` WHERE `username` = 'admin'

,

No Comments

Run Magento indexer from the command line

Magento offers a convenient way to run the reindexing process trough the command line, without the risks of a timeout. Go to the /shell Magento directory and run:

php indexer.php reindexall

If you want to run only one index:

php indexer.php --reindex catalog_product_price

If you want to run more than one list them separated by comma (without spaces):

php indexer.php --reindex catalog_url,catalog_category_flat

You can see all the possible indexes with:

php indexer.php info

, ,

No Comments

List every loaded Apache module

Just run this command:

apachectl -t -D DUMP_MODULES

the output will list every loaded module:

Loaded Modules:
core_module (static)
mpm_prefork_module (static)
http_module (static)
so_module (static)
auth_basic_module (shared)
auth_digest_module (shared)
authn_file_module (shared)
authn_alias_module (shared)
...

,

No Comments

Simple script to add a new Virtual Host on Mac OSX

Ideally when you develop a lot of websites on a local machine on OSX you have to create a virtual host for each website.
Below we will describe a really simple script to do this boring operation automatically.

Firstly a simple control to remember you have to choose a host name:

#!/bin/sh
if [ $# -lt 1 ]
then
echo
echo "You have to choose a host name!"
exit 0
fi

Then append a new host to the hosts file and verify it’s all right with a cat:

echo "\n" >> /etc/hosts
echo "127.0.0.1 $1" >> /etc/hosts
cat /etc/hosts

Now you can append the new Virtual Host to the apache configuration file httpd-vhosts.conf:

echo "\n" >> /etc/apache2/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerAdmin [email protected]" >> /etc/apache2/extra/httpd-vhosts.conf
echo " DocumentRoot /Library/WebServer/Documents/$1/" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerName $1" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ErrorLog \"/private/var/log/apache2/$1-error_log\"" >> /etc/apache2/extra/httpd-vhosts.conf
echo " CustomLog \"/private/var/log/apache2/$1-access_log\" common" >> /etc/apache2/extra/httpd-vhosts.conf
echo " <Directory \"/Library/WebServer/Documents/$1/\">" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Options All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " AllowOverride All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Order allow,deny" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Allow from all" >> /etc/apache2/extra/httpd-vhosts.conf
echo " </Directory>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
cat /etc/apache2/extra/httpd-vhosts.conf

Finally restart apache:

apachectl restart

This is the final script:

#!/bin/sh
if [ $# -lt 1 ]
then
echo
echo "You have to choose a host name!"
exit 0
fi
echo "\n" >> /etc/hosts
echo "127.0.0.1 " >> /etc/hosts
cat /etc/hosts
echo "\n" >> /etc/apache2/extra/httpd-vhosts.conf
echo "<VirtualHost *:80>" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerAdmin [email protected]" >> /etc/apache2/extra/httpd-vhosts.conf
echo " DocumentRoot /Library/WebServer/Documents//" >> /etc/apache2/extra/httpd-vhosts.conf
echo " ServerName " >> /etc/apache2/extra/httpd-vhosts.conf
echo " ErrorLog \"/private/var/log/apache2/-error_log\"" >> /etc/apache2/extra/httpd-vhosts.conf
echo " CustomLog \"/private/var/log/apache2/-access_log\" common" >> /etc/apache2/extra/httpd-vhosts.conf
echo " <Directory \"/Library/WebServer/Documents//\">" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Options All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " AllowOverride All" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Order allow,deny" >> /etc/apache2/extra/httpd-vhosts.conf
echo " Allow from all" >> /etc/apache2/extra/httpd-vhosts.conf
echo " </Directory>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
cat /etc/apache2/extra/httpd-vhosts.conf
apachectl restart

download it here.
Once unzipped remember to edit the email on the script, and to make it executable with:

chmod +x newHost.sh

To execute the command you have to sudo (apache and the 2 files are root property):

sudo ./newHost chosenHostName

, , , , ,

No Comments

How to execute remote ssh commands without interactive password

When you manage a lot of remote machines sometimes you need to write scripts to automate commands to be executed or open multiple shells to look at logs.
For security reasons SSH doesn’t give you a -p option to set a password to launch a session, but this can be circumvented with a command line tool called sshpass (you can find it in several repositories, Google is your friend). Mind you! SSH doesn’t give you this ability because is really difficult to store password in a secure way! So don’t blame me if your system will be compromised :D
Basically sshpass is a wrapper that add the -p option, so if you want to launch an ssh session just type this:
sshpass -p password ssh user@host

Sometimes you want to execute commands on the remote machine (like open a given folder) and keep the connection open. To do this just add the -t option and the command followed by the command bash separated by a semi-colon:
sshpass -p password ssh user@host -t "cd /some/folder/you/want/to/open;bash"

Another cool trick if you are under gnome is to open a separated terminal window:
gnome-terminal -e "sshpass -p password ssh user@host -t \"cd /some/folder/you/want/to/open;bash\""

For convenience you can add the –maximize option at the end to open the terminal full screen.

You can even open multiple tabs in the same window with a title for each one:
gnome-terminal --tab -e "sshpass -p password ssh user@host1 -t \"cd /some/folder/you/want/to/open;bash\"" --title "Server1" --tab -e "sshpass -p password ssh user@host2 -t \"cd /some/folder/you/want/to/open;bash\"" --title "Server2"

If you are a Lubuntu (or LXDE) user like me you’ll like this:
lxterminal -e "sshpass -p password ssh user@host -t \"cd /some/folder/you/want/to/open;bash\""

Just keep in mind that this method is quite unsecure, so use it at your own risk.

Update
Just discovered that in Ubuntu 11.10 sshpass installed from the official Ubuntu repository doesn’t work. So you can follow the instructions in this launchpad comment thread sshpass 1.04 hangs – use 1.05

, , , ,

No Comments

Alternative PHP Cache (APC)

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

Intermediate code is the internal memory structures produced during compilation that are fed into the executor. APC increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. APC stores and executes compiled PHP scripts from shared memory.

alternative_php_cache

To activate APC on Windows with Xampp simply uncomment this string on the php.ini file in the php directory of Xampp. From:


To:


APC comes with useful graphical monitoring tool. It provides information about the APC state, cached files, amount of used and free memory, etc. Using this APC application, you can also clear all the caches without restarting Apache. This tool is also useful for tuning memory usage.

Access the application from a web browser via the URL http://localhost/apc.php.

The screenshot below shows the APC application:


, , ,

1 Comment

Export tables from MS-Access to Mysql

Note: The following guide was tested on WindowsXP, MS-Access 2007, Mysql ODBC connector 5.1

Prerequisites: Mysql and MS-Access installed in your localhost

Sometimes could happens that we are not familiar with some tools, in this case I was not familiar with MS-Access and I needed to export a table I need from Access to Mysql.

There are several tools which allow you to export from Access to Mysql but there is a very simple procedure you can perform to solve this issue very quickly using the ODBC driver.

So the first thing to do is to download the mysql connector from http://dev.mysql.com/downloads/connector/odbc/5.1.html accordingly with your OS.

After the install click start -> run and load the following command: control admintools; run Data Sources (ODBC) to set up your mysql database connection.

Under ‘User DSN’ tab click the ‘add’ button:

  • select the MYSQL ODBC (version of the connector) Driver
  • Edit the connection parameters to connect through ODBC driver to your database

If all worked pressing the ‘test’ button will confirm if the configuration settings are correct.

Open your mdb file with MS-Access:

  • Right click on the table you want to export to mysql
  • In the contextual menu selet the ‘export’ -> ‘ODBC Database’ option
  • A new Export window will appear telling you the name of the table you want to export; press the ok button
  • In the new window ‘Select Data Source’, select the ‘Machine Data Source’ and select the Mysql Database profile you created in the first steps of this guide to  finish the import procedure.

After these commands, the database you selected in the last step of this guide, will be populated with the table selected.

, , ,

No Comments

Rollback to previous version with Tortoise SVN

How to rollback to a previous SVN version with TortoiseSVN

  • Update the repository
  • Right click on your local repository and in the TortoiseSVN options select the Merge one
  • In the new window select the option Merge a range of revisions and click next
  • Edit the option URL to merge from with the address of the repository location (in this case http://your-typo3-repository)
  • Edit the option Revision range to merge in the following way:
    revision_from-revision_to (so 4-8 will merge from revision 4 to 8 )
  • Check the option Reverse Merge and click next
  • In the new window select Working Copy as Merge Depth and leave the default selection for the Radio Box

, , ,

2 Comments