Created
December 5, 2012 15:09
-
-
Save pkdavies/4216284 to your computer and use it in GitHub Desktop.
META data updater for Mageworx SEO Ultimate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Magento META data populator for us with Mageworx SEO Suite Ultimate. | |
* Copyright Juicy Media Ltd | |
* | |
* Features: | |
* -> wipes META description and title | |
* -> updates META keywords based on product description | |
* | |
* Usage: | |
* 1) place the file in the root folder of the magento installation | |
* 2) run from the command line: php ./metafix.php | |
* | |
*/ | |
ini_set('memory_limit', '512M'); | |
ini_set('max_input_time', 900); | |
ini_set('max_execution_time',900); | |
/** | |
* Pass a description fields and it will filter common words: | |
* http://www.hashbangcode.com/blog/extract-keywords-text-string-php-412.html | |
* | |
* @param string $string | |
* @param integer $returnno | |
* @return array | |
*/ | |
function extractCommonWords($string,$returnno){ | |
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www'); | |
$string = preg_replace('/\s\s+/i', '', $string); // replace whitespace | |
$string = trim($string); // trim the string | |
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too… | |
$string = strtolower($string); // make it lowercase | |
preg_match_all('/\b.*?\b/i', $string, $matchWords); | |
$matchWords = $matchWords[0]; | |
foreach ( $matchWords as $key=>$item ) { | |
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) { | |
unset($matchWords[$key]); | |
} | |
} | |
$wordCountArr = array(); | |
if ( is_array($matchWords) ) { | |
foreach ( $matchWords as $key => $val ) { | |
$val = strtolower($val); | |
if ( isset($wordCountArr[$val]) ) { | |
$wordCountArr[$val]++; | |
} else { | |
$wordCountArr[$val] = 1; | |
} | |
} | |
} | |
arsort($wordCountArr); | |
$wordCountArr = array_slice($wordCountArr, 0, $returnno); | |
return $wordCountArr; | |
} | |
require_once ('app/Mage.php'); | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
// process products by getting a collection | |
$collection = Mage::getModel('catalog/product')->getCollection() | |
->addAttributeToSelect('description') | |
->addAttributeToSelect('meta_title') | |
->addAttributeToSelect('meta_keywords') | |
->addAttributeToSelect('meta_description') | |
->load(); | |
// backup existing data | |
$fp = fopen("prod-data-".date("Ymd-his").".csv", 'w'); | |
foreach ($collection as $oneproduct) { | |
// check sku and id exist, only continnue if they do | |
$product['id'] = $oneproduct->getId(); | |
$product['sku'] = $oneproduct->getSku(); | |
$product['meta_description'] = $oneproduct->getMetaDescription(); | |
$product['meta_keywords'] = $oneproduct->getMetaKeyword(); | |
$product['meta_title'] = $oneproduct->getMetaTitle(); | |
fputcsv($fp, $product, ";", '"'); | |
} | |
fclose($fp); | |
// loop through products | |
foreach ($collection as $oneproduct) { | |
// check sku and id exist, only continnue if they do | |
$sku = $oneproduct->getSku(); | |
$productId = $oneproduct->getId(); | |
if ($productId > 0 && !empty($sku)) { | |
echo "--> ".$sku.", id - ".$productId."\n"; | |
ob_flush(); | |
// obtain and process description in to keywords | |
$description = $oneproduct->getDescription(); | |
$meta_keywords = implode(',',array_keys(extractCommonWords($description,10))); | |
// as long as the result is not empty update the product | |
if (!empty($meta_keywords)){ | |
echo "----------> ".$meta_keywords."\n"; | |
// save the product with the new data | |
$oneproduct->setMetaTitle(null) | |
->setMetaDescription(null) | |
->setMetaKeyword($meta_keywords) | |
->save(); | |
echo "----------> Updated ".$sku."\n"; | |
} else { | |
echo "**********> ERROR on ".$sku."\n"; | |
} | |
ob_flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment