Saverio Serravezza a little stupid journal

magento - python - reindex only when is needed

As you will know best way to reindex data in Magento is from shell, php cli.
Well,

php /path/to/magento/shell/indexer.php -reindexall
will reindex all data, also when no reindex is needed.. Lot's of waste of time and cpu load!!
Well.. or you can reindex a index only:
php /path/to/magento/shell/indexer.php --reindex catalog_url
But how to check if you need to reindex that particular index? Simply reading the output of:
php /path/to/magento/shell/indexer.php --status
I use the beautifull python "magento" library to connect with Magento Api and sync with ERP, stocks managements, CMS, etc..
So this solution is "pythonic" and only use php to call shell cli.. but can easily converted in pure php (if really needed.. python is so beautiful..)

// s.s. - s@saverioserravezza.it
// Magento - python/php - reindex only when is needed

import os
import subprocess

// dictionary to convert human index name in shell index name
dict_reind={'ProductAttributes':'catalog_product_attribute','ProductPrices':'catalog_product_price','CatalogURLRewrites':'catalog_url','ProductFlatData':'catalog_product_flat','CategoryFlatData':'catalog_category_flat','CategoryProducts':'catalog_category_product','CatalogSearchIndex':'catalogsearch_fulltext','Stockstatus':'cataloginventory_stock','TagAggregationData':'tag_summary'}

def reindex():
    // check what needed to be reindexed
    needed = subprocess.check_output('php /path/to/magento/shell/indexer.php --status', shell=True)
    needed = needed.replace(' ', '').split('\n')
    for line in needed:
        if line !='':
            if line.split(':')[1]=='RequireReindex':
                needreind=line.split(':')[0]
                print '--->>>> reindexing '+needreind
                os.system('php /var/www/unp/web/shell/indexer.php --reindex '+dict_reind[needreind])

magento missing canonical full path for products

Latest versions of Magento introduced canonical url both for category and product pages.
The problem is that canonical url for product is not really SEO friendly..
You will have "www.domain.com/uniqueproductname.html"
and not "www.domain.com/catname/subcatname/uniqueproductname.html"
as it should be in a SEO vision..

Here is a short workaround to build a full canonical url for products pages:

// s.s. - s@saverioserravezza.it
// Magento - php - canonical full path for products
$product  = Mage::registry('product');
if ($product) {
    $productid = Mage::registry('currentproduct')->getId();
    $cats = $product->getCategoryIds();
    foreach ($cats as $categoryid) {
        $cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryid);
            // get last category id
            $catid = $cat->getId();
        }
    $idpath = "product/".$productid."/".$catid;
    $storeId = Mage::app()->getStore()->getStoreId();
    $canonicalpath = Mage::getResourceModel('core/urlrewrite')
        ->getRequestPathByIdPath($idpath, $storeId);
    // get canonical
    $canonicalpath = Mage::getBaseUrl(MageCoreModelStore::URLTYPE_WEB).$canonicalpath;
    echo "";
}
// s.s. canonical full path for products

la stanza dei morti - lalish - Iraq 2011

Lalish village - Iraq [6-06-2011]

Lalish village - Iraq [6-06-2011]

apt-get update && apt-get install felicità

processing triggers for man-db …
setting up felicità
error processing man-db (--unpack):
subprocess installed post-installation script returned error exit status 1

triftling

… triftling all the time triftling all the time

a me medesimo (chi tu sie..)

Chi tu sie ch'io mi credea di conoscerti, tremula imago di me stesso..
Mi sapea di nobili natali, et ho errato.
Ti credeva amato da una bianca dama, et ho errato.
Ti ritenea gran capitano di grande impresa, et ho errato..
Verrò nomato..LO CAVALIERE ERRANTE!
[m. monicelli ]

tutto

6 CO2 + 6 H2O + hv → C6H12O6 + 6 O2

lucania un luogo vero

"La Lucania mi pare più di ogni altro, un luogo vero, uno dei luoghi più veri del mondo. qui ritrovo la misura delle cose. le lotte e i contrasti qui sono cose vere. il pane che manca è un vero pane, la casa che manca è una vera casa, il dolore che nessuno intende un vero dolore. la tensione interna di questo mondo è la ragione della sua verità: in esso storia e mitologia, attualità e eternità sono coincidenti"
Carlo Levi

cattiva dacia!

broken pipe

[kabul 14 march 2006]

© 2015 saverio serravezza. All rights reserved.