Archive

Author Archive

Magento: Ultimate cache clear script

June 16th, 2009

If you have been using Magento with a BIG database of products you will soon realize that the built-in “cache management” is quite limited due to the server’s usual time limit on requests. You just can’t refresh your catalog rewrites on 5000 products in less then a good 5 minutes and the other functions might give you the same problems as well.

By extracting the code from the cache management controller I was able to build a custom script that can be called via command line on the server and that will allow you to do a full cache refresh without any kind of time limit or connection timeout issues. The only requirement is shell access to your website files by SSH, Telnet or a terminal of some sort.

Put this file inside /app/ right next to the Mage.php file. To use it, you can simply navigate to the /app folder with your SSH client (or terminal if you run the website on your local PC) do php -f clearcache.php or for fastcgi adepts php-cgi -f clearcache.php

Call the file clearcache.php and you can comment out the parts you don’t need if you only want to refresh one kind of resource (ie: comment out the stock status try/catch block if you don’t want to refresh product stocks).
<?php

ini_set('memory_limit', '512M');

require './Mage.php';

Mage::app('admin');
Mage::setIsDeveloperMode(true);

if (!Mage::app()->isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit(1);
}

// Only for urls
// Don't remove this
$_SERVER['SCRIPT_FILENAME'] = 'index.php';

try
{
    // CLEAN CACHE
    Mage::app()->cleanCache();
    echo 'Cleared all caches' . "\n";

    // CATALOG REWRITES
    try {
        Mage::getSingleton('catalog/url')->refreshRewrites();
        echo 'Catalog Rewrites were refreshed successfully' . "\n";
    }
    catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    }
    catch (Exception $e) {
        echo 'Error while refreshed Catalog Rewrites. Please try again later' . "\n";
    }

    // IMAGE CACHE
    try {
        Mage::getModel('catalog/product_image')->clearCache();
        echo 'Image cache was cleared succesfuly' . "\n";
    }
    catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    }
    catch (Exception $e) {
        echo 'Error while cleared Image cache. Please try again later' . "\n";
    }

    // LAYERED NAV
    try {
        $flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
        if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
            $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
            $kill->setFlagData($flag->getFlagData())->save();
        }

        $flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();
        Mage::getSingleton('catalogindex/indexer')->plainReindex();
        echo 'Layered Navigation Indices were refreshed successfully' . "\n";
    }
    catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    }
    catch (Exception $e) {
        echo 'Error while refreshed Layered Navigation Indices. Please try again later' . "\n";
    }

    // SEARCH INDEX
    try {
        Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();
        echo 'Search Index was rebuilded successfully' . "\n";
    }
    catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    }
    catch (Exception $e) {
        echo 'Error while rebuilded Search Index. Please try again later' . "\n";
    }

    // STOCK STATUS
    try {
        Mage::getSingleton('cataloginventory/stock_status')->rebuild();
        echo 'CatalogInventory Stock Status was rebuilded successfully' . "\n";
    }
    catch (Mage_Core_Exception $e) {
        echo $e->getMessage() . "\n";
    }
    catch (Exception $e) {
        echo 'Error while rebuilded CatalogInventory Stock Status. Please try again later' . "\n";
    }

    // CLEAN CACHE
    Mage::app()->cleanCache();
    echo 'Cleared all caches' . "\n";    

    echo  "\n" . 'Cache clear complete!' . "\n";

    exit(0);
}
catch (Exception $e) {
    Mage::printException($e);
}

exit(1);

mystic Blog News, Magento, Programming, php , , , , , , ,

Magento: How to change the admin theme

March 17th, 2009

So you want to use Magento for your company and now you have to change the look of the backend. Of course you don’t want to change the default Magento adminhtml theme and kill any chances of upgrading your templates later on. So thats where this post comes into play. ;)

Theres an easy way to add your own theme folder and use it to customize the look of your admin control panel. All files that aren’t included in your theme will fall back to the default Magento theme, thus avoiding any problems with missing templates and making it a lot easier to change only a few files.

Add a new adminhtml theme

Start by adding a new folder inside the app/design/adminhtml/default folder. To start out, the folder should also contain one sub-folders called template.

So, for example, you add a folder called mytheme, and inside it you add another folder called template.

Overriding Magento configuration

All you have to do is add a new config.xml file inside app/code/local/MyCompany/Adminhtml/etc. Add the following code inside the file:

Note: if you created this file by following one of my earlier guides you don’t have to create it again and you would simply add the <stores> section at the appropriate location inside the existing file.
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_Adminhtml>
            <version>0.1.1</version>
        </MyCompany_Adminhtml>
    </modules>
    <stores>
        <admin>
            <!-- override default admin design package and theme -->
            <design>
                <package>
                    <name>default</name>
                </package>
                <theme>
                    <default>mytheme</default>
                </theme>
            </design>
        </admin>
    </stores>
</config>

You will also have to tell Magento about this new module in an XML file placed inside /app/etc/modules. This file could be called MyCompany.xml and inside you would copy/paste:

Note: if you created this file by following one of my earlier guides you don’t have to create it again.
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_Adminhtml>
	    <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
            </depends>
        </MyCompany_Adminhtml>
    </modules>
</config>

Changing template files

Now to change the default Magento templates you basically copy the .phtml files from the app/design/adminhtml/default/default/template folder into your own template folder and change the contents of the .phtml file to suit your needs.

For example, if you want to change the login box and remove the Magento copyright message:

Copy app/design/adminhtml/default/default/template/login.phml into the app/design/adminhtml/default/mytheme/template folder and then change the <p class=”legal”></p> to put your own legal note.

mystic Magento, Programming , , , , ,

Magento: How to disable update notifications

March 12th, 2009

A short tutorial on how to block the “New Magento Version” notifications in the Magento admin without modifying the core packages.

Create a new package under app/code/local , for this example lets make a “MyCompany” package. If you already have an existing package for your magento mods you can use that one but you will have to change all references to MyCompany in the following code.

Create folder app/code/local/MyCompany

Create a new module called “Adminhtml” containing a “Block” folder and a “etc” folder.

Create folder app/code/local/MyCompany/Adminhtml
Create folder app/code/local/MyCompany/Adminhtml/Block
Create folder app/code/local/MyCompany/Adminhtml/etc

Add a “Notification” folder inside the Block folder.

Create folder app/code/local/MyCompany/Adminhtml/Block/Notification

Create Toolbar.php inside the MyCompany/Block/Notification folder.

<?php
/**
 * Block all notifications
 */
class MyCompany_Adminhtml_Block_Notification_Toolbar extends Mage_Adminhtml_Block_Template
{
    public function isShow()
    {
	return false;
    }

    public function isMessageWindowAvailable()
    {
        return false;
    }
}

Create Window.php inside the MyCompany/Adminhtml/Block/Notification folder.

<?php
/**
 * Prevent popup window
 */
class MyCompany_Adminhtml_Block_Notification_Window extends Mage_Adminhtml_Block_Notification_Window
{
    public function canShow()
    {
        return false;
    }
}

Create config.xml file inside the MyCompany/Adminhtml/etc folder

<?xml version="1.0" encoding="UTF-8"?>

<config>

    <modules>
        <MyCompany_Adminhtml>
            <version>0.1.0</version>
        </MyCompany_Adminhtml>
    </modules>

    <global>

        <blocks>
          <adminhtml>
              <rewrite>
             	  <notification_window>MyCompany_Adminhtml_Block_Notification_Window</notification_window>
		  <notification_toolbar>MyCompany_Adminhtml_Block_Notification_Toolbar</notification_toolbar>
              </rewrite>
          </adminhtml>
        </blocks>

    </global>

</config>

Last but not least, you have to create a new Modules XML file to load your module. Create MyCompany.xml inside the app/etc/modules folder.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_Adminhtml>
	    <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
            </depends>
        </MyCompany_Adminhtml>
    </modules>
</config>

mystic Magento , ,

How to Enable Ant For Zend Studio

December 16th, 2008

This week I found out that Zend Studio has Ant disabled by default (no idea why). Then I tried find a way to reactivate it by installing plugins or opening the workspace in a standard eclipse. Finally I found a nice post on the internet that explains how to have Zend reactivate the Ant plugins which are actually already installed and ready to be used.

1. Click File->New…->Other…

2. Click on the Java folder and select Existing project using Ant buildfile….

3. Click next

4. A message should popup asking you if you want to enable Ant, click OK

5. Close the new project wizard, you don’t have to actually create a project.

Go to Window->Show View->Other…, the Ant view should now be in the list.

Enjoy

mystic Programming , , ,

Running MySQL Proxy as a daemon

August 6th, 2008
I had to figure out how to setup mySQL Proxy to run as a daemon (system service).
The original information came from this page and has been slightly modified.
It is assumed that mySQL Proxy has been installed and the mysql-proxy executable is located at /usr/local/sbin/mysql-proxy.
The first file is the init.d launch script:

/etc/init.d/mysql-proxy

#!/bin/sh
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: - 78 30
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon to mysql

# Source function library.
. /etc/rc.d/init.d/functions

PROXY_PATH="/usr/local/sbin"

prog="mysql-proxy"

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Set default mysql-proxy configuration.
PROXY_OPTIONS="--daemon"
PROXY_PID=/var/run/mysql-proxy.pid

# Source mysql-proxy configuration.
if [ -f /etc/sysconfig/mysql-proxy ] ; then
        . /etc/sysconfig/mysql-proxy
fi

PATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH

# By default it's all good
RETVAL=0

# See how we were called.
case "$1" in
  start)
        # Start daemon.
        echo -n $"Starting $prog: "
        daemon $NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --pid-file $PROXY_PID
        RETVAL=$?
        echo
        if [ $RETVAL = 0 ]; then
                touch /var/lock/subsys/mysql-proxy
        fi
        ;;
  stop)
        # Stop daemons.
        echo -n $"Stopping $prog: "
        killproc $prog
        RETVAL=$?
        echo
        if [ $RETVAL = 0 ]; then
                rm -f /var/lock/subsys/mysql-proxy
                rm -f $PROXY_PID
        fi
        ;;
  restart)
        $0 stop
        sleep 3
        $0 start
        ;;
  condrestart)
       [ -e /var/lock/subsys/mysql-proxy ] && $0 restart
       ;;
  status)
        status mysql-proxy
        RETVAL=$?
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status|condrestart}"
        RETVAL=1
        ;;
esac

exit $RETVAL
The launch script will either use the default configuration or read it from the file /etc/sysconfig/mysql-proxy if it exists.
To set mysql-proxy parameters create:

/etc/sysconfig/mysql-proxy

# Options to mysql-proxy
# do not remove --daemon
PROXY_OPTIONS="
 --daemon
 --proxy-backend-addresses=127.0.0.1:3306
 --proxy-backend-addresses=127.0.0.1:3307
 --proxy-lua-script=load-balancing.lua"

mystic Computers, Linux, MySQL