Apr 25

Linux — how to know if php run as cgi or module

For some reason you want to know how is php working with web server like apache. if you are the people who config the server,you got the answer in your head. what if you are not? well,php have a function for this:

php_sapi_name

Per php manual,call this function will returns the type of interface between web server and PHP

Source code    
<?php
php_sapi_name();
?>

The section below is take from the php manual:
Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

When php run in CGI mode( or any other mode other that module mode), you can not set php config via php_value directive in the .htaccess file.

you can also take a look at the phpinfo page to decide how php is working with your webserver.

Mar 25

zen cart — How to Add a Sidebox to Zen Cart

One of the best way to customize Zen Cart is to create a new Sidebox.A Zen Cart sidebox is what displays various contents in the left or right columns of your Zen Cart store. By default numerous sideboxes will show in either the left or right column of a Zen Cart online store. The followings are some of the most popular zen cart sidebox — The categories sidebox, the search sidebox, and the information sidebox. Zen cart comes with some builtin sidebox which could be a good tutor to start to learn how to add a new sidebox to Zen Cart.

A sidebox consists of three files which are located in the includes directory:

  1. modules/sideboxes/YOUR_TEMPLATE/new_sidebox.php
  2. languages/english/extra_definitions/YOUR_TEMPLATE/new_sidebox_defines.php
  3. templates/YOUR_TEMPLATE/sideboxes/tpl_new_sidebox.php

The ‘new’ up there should be replaced with your own sidebox name.

includes/modules/sideboxes/YOUR_TEMPLATE/new_sidebox.php

Source code    
<?php
 $show_new_sidebox = true;
 if ($show_new_sidebox == true){
   require($template->get_template_dir('tpl_new_sidebox.php',DIR_WS_TEMPLATE,
   $current_page_base,'sideboxes'). '/tpl_new_sidebox.php');
   $title =  BOX_HEADING_NEW_SIDEBOX;
   //do some db query and other processing here
 
   $left_corner = false;
   $right_corner = false;
   $right_arrow = false;
   require($template->get_template_dir($column_box_default, DIR_WS_TEMPLATE,
   $current_page_base,'common') . '/' . $column_box_default);
 }
?>

includes/languages/english/extra_definitions/YOUR_TEMPLATE/new_sidebox_defines.php

Source code    
<?php
  define('BOX_HEADING_NEW_SIDEBOX', 'Gallery');
?>

includes/templates/YOUR_TEMPLATE/sideboxes/tpl_new_sidebox.php

Source code    
<?php
 $content = <<< End_Of_Quote
  Enter your sidebox content here
 End_Of_Quote;
?>
Dec 06

linux — How to extract specified file/directory from a tar ball

well,you got a big tar ball file,but in fact you only need some special file or directory.Then you don’t want to extract  all files from the tar ball if it’s a big file.so how to get specified file or directory from a tar ball?

tar command allows to extract a single file or directory using the following format. It works under UNIX, Linux, and BSD operating systems.

tar xvf big.tar filename
tar -zxvf abc.tar.gz directory-name
tar -zxvf abc.tar.gz path/to/directory-name -C /tmp

Keep in mind that both the filename and directory-name should be existed in the tar ball file,and the path is relative to the tar ball root.

Aug 23

linux — how to exclude directory or files when bakup with rsync

rsync has been heavily used in server daily backup.I am not going to list all option of rsync. i just don’t want to backup some useless file such as cache and log file.

In a typical backup situation, you might want to exclude one or more files (or directories) from the backup. You might also want to exclude a specific file type from rsync.

To exclude a special directory,take a look at the command below:

Source code    
rsync -avze 'ssh -p7788' --exclude 'dir_abc'  /home/www  root@ip:/home/bak/

This command will not sync the /home/www/dir_abc

You can use * to match multiple directory to exclude like this:

Source code    
rsync -avze 'ssh -p7788' --exclude 'dir_*'  /home/www  root@ip:/home/bak/

You can also use multiple –exclude option to exclude:

Source code    
rsync -avze 'ssh -p7788' --exclude 'dir_abc' --exclude 'webcache' --exclude '*.log' /home/www  root@ip:/home/bak/

Keep in mind that rsync will always treat the value of the –exclude option as relative to the bakup dir even though you specified a absolute path,so

if there is a dir1 directory in /home/www

Source code    
rsync -avze --exclude '/home/www/dir1' /home/www /home/bak

rsync will explain /home/www/dir1 as /home/www/home/www/dir1,but this is not what you want.when you specified the path for –exclude option,make sure it’s relative to the source data diretory.this action will not be affected by add / to the exclude path.

Aug 09

zen cart — Basic search engine optimisation guide for zen cart

Meta tag optimisation

It’s important that meta tag tile and meta tag description contain enough search information for the search engine to index each product effectively,whilst also enticing browser to visit your site.

The meta tag tile and meta tag description are set up to automatically to show product name and descrition when you setting up product to your zen cart store.You can further enhance these tags for specific categories and products by clicking on ‘Meta Tags defined’ button in the catalog listing page within zen cart.

It’s normally advisalbe to show the product name,category and manufacturer in the Meta tag Title.The meta tag description should contain the product name and it’s main features.

product name optimisation

It’s important to include keywords in the product and category name.For example, a product with name ’30 x 78 mm (xyz666)  1 ply Till Roll’ will gain more search engine score than a name with ’30 x 78 mm (xyz666)’ without keywords.

product description optimisation

It’s beneficial to list keywords associated with a product in product description. This can be further enhanced by making the products keywords bold.or even more,you can consider randomly insert variant forms of the product keywords to the product descrption.

Jun 23

magento — how to check product page or category page?

you may need to do some specific work when magento are in product page,and not show some content in category list page.how to check ?

// if this is a product view page
if (Mage::registry(‘product’)) {
// get collection of categories this product is associated with
$categories = Mage::registry(‘product’)->getCategoryCollection()
->setPage(1, 1)

from the last block code from magento, when Mage::registry(‘product’) is not null,magento are work in product view page.

if (!$this->_currentCategoryKey) {
$category = Mage::registry(‘current_category’);
if ($category) {
$this->_currentCategoryKey = $category->getPath();

obviously, if Mage::registry(‘current_category’) is set and not null,then we are in category page.but,this not work in all version of magento.we can extend magento in its core to set some status variable for test.Mage::registry will be a good way to do this job.

 

Jun 22

How to override a Magento function

How to override a Global Magento function which was defined in app/code/core/Mage/Core/functions.php? With the help of magento override system,we can easily ovrride any global function defined in the function.php by make a copy of this file to app/code/local/Mage/Core/function.php.in the new file,do whatever changes you want.when magento call a function,it will use the definition in the new file.Kee in mind that code in the local code pool always takes precedence over that in the core pool.this is the way magento works always.

read more from:

http://stackoverflow.com/questions/5978153/how-to-override-a-magento-function-in-app-code-core-mage-core-functions-php

Jun 13

magento – Magento Service Temporarily Unavailable

If your magento website got this error page,

Service Temporarily Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

don’t be too worried!First,make sure you don’t put your magento store in maintenance mode.If Magento sees a file named maintenance.flag in the magento store root directory, it will automatically redirect all requests to the magento default 503 error page. This can be used  to prevent users from accessing the store during magento upgrades. To revert your magento store back to production mode,just delete/rename the maintenance.flag file.

However, this error page may also be caused by magento upgrade. If you tried to install magento theme/template/module,magento would temporally put magento store in maintenance mode if it needs to upgrade magento. if the upgrade didn’t finish successfully,it won’t revert the magento store back to production mode.If so,you only need to remove the maintenance.flag to get control back to magento backend. Remove the extension/plugins you installed,it will be ok.

 

Jun 07

magento – how to rewrite a model class to extend magento feature

To add  or extend magento core feature, we need to make changes to magento code.however,we would not want to change the code directly.To make our code be compatible with magento next release,we’d better make use of magento build in way to extend magento functionality.

To rewrite a magento existing core model,we have two choice.The first one is based upon php include path.Let’s take a look at magento code:

if (defined(‘COMPILER_INCLUDE_PATH’)) {
$appPath = COMPILER_INCLUDE_PATH;
set_include_path($appPath . PS . Mage::registry(‘original_include_path’));
include_once “Mage_Core_functions.php”;
include_once “Varien_Autoload.php”;
} else {
/**
* Set include path
*/
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘local’;
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘community’;
$paths[] = BP . DS . ‘app’ . DS . ‘code’ . DS . ‘core’;
$paths[] = BP . DS . ‘lib’;

$appPath = implode(PS, $paths);
set_include_path($appPath . PS . Mage::registry(‘original_include_path’));
include_once “Mage/Core/functions.php”;
include_once “Varien/Autoload.php”;
}

with the include_path configuration set,magento will search first at the local code pool,then community and core code pool.so we can just copy the file which contain the class you want to change from the core code pool to local code pool,and do whatever changes you want.For example,to rewrite the product model class,just copy core/Mage/Catalog/Model/Product.php to local/Mage/Catalog/Model/Product.php.Although the changes we made would keep when we update magento, we still lost the new updated/added feature.

magento has another powerful way to change its core code.this is magento another override mechanism.let’s take a look how it works.

when we try to get a model instance,we call Mage:getModel ,ie $p = Mage:getModel(‘catalog/product’); The real logic is implemented in the getGroupedClassName method of Mage_Core_Model_Config class defined in app\code\core\Mage\Core\Model\Config.php

public function getGroupedClassName($groupType, $classId, $groupRootNode=null)
{
if (empty($groupRootNode)) {
$groupRootNode = ‘global/’.$groupType.’s';
}

$classArr = explode(‘/’, trim($classId));
$group = $classArr[0];
$class = !empty($classArr[1]) ? $classArr[1] : null;

if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
return $this->_classNameCache[$groupRootNode][$group][$class];
}

$config = $this->_xml->global->{$groupType.’s'}->{$group};

// First – check maybe the entity class was rewritten
$className = null;
if (isset($config->rewrite->$class)) {
$className = (string)$config->rewrite->$class;
} else {
/**
* Backwards compatibility for pre-MMDB extensions.
* In MMDB release resource nodes <…_mysql4> were renamed to <…_resource>. So <deprecatedNode> is left
* to keep name of previously used nodes, that still may be used by non-updated extensions.
*/
if ($config->deprecatedNode) {
$deprecatedNode = $config->deprecatedNode;
$configOld = $this->_xml->global->{$groupType.’s'}->$deprecatedNode;
if (isset($configOld->rewrite->$class)) {
$className = (string) $configOld->rewrite->$class;
}
}
}

// Second – if entity is not rewritten then use class prefix to form class name
if (empty($className)) {
if (!empty($config)) {
$className = $config->getClassName();
}
if (empty($className)) {
$className = ‘mage_’.$group.’_’.$groupType;
}
if (!empty($class)) {
$className .= ‘_’.$class;
}
$className = uc_words($className);
}

$this->_classNameCache[$groupRootNode][$group][$class] = $className;
return $className;
}

so to rewrite a model class,we just need to add proper xml config and extend the orginal model class in the new class of your own namespace in the local code pool,so to rewrite product model,

first,set xml as below:

<global>
       <models>
           <catalog>
               <rewrite>
                   <product>Mzcart_Catalog_Model_Product</product>
               </rewrite>
           </catalog>
       </models>
 </global>
then declare the new class to textend the original class.no only will this class be compatible with next magento release,but also it would have any new/updated feature  of product model,because the new class extends the original class.
class Mzcart_Catalog_Model_Product extends Mage_Catalog_Model_Product {
  //do whatever changes you want
}
both the above  methods can be used to to rewrite any magento helper class,block class.