BothInteract_CleanProductTexts - Version 1.1.0

Version Notes

Added missing shell script in installation package. As usual, you can find the shell script in the folder "shell".

Download this release

Release Info

Developer Matthias Kerstner
Extension BothInteract_CleanProductTexts
Version 1.1.0
Comparing to
See all releases


Code changes from version 1.0.0 to 1.1.0

app/code/community/BothInteract/CleanProductTexts/Model/Observer.php CHANGED
@@ -38,29 +38,40 @@ class BothInteract_CleanProductTexts_Model_Observer {
38
  . mb_strtoupper($product->getTypeId())
39
  . ' product ' . $product->getId());
40
 
 
 
41
  foreach ($cleaningOptions as $cleaningOption) {
42
  $this->logToFile('Checking option ' . $cleaningOption);
43
 
44
  // remove control characters for description and short description
45
  if ($cleaningOption == BothInteract_CleanProductTexts_Model_System_Config_Source_View::$VALUE_CLEANING_TYPE_CONTROL_CHARACTERS) {
46
- // @see http://unicode-table.com/en/
47
- $this->logToFile('Cleaning product ' . $product->getId());
48
 
 
49
  $regexp = '/[^\PC\s]/u'; //i.e. /[\x00—\x1F\x80-\x9f]
50
- $product->setDescription(preg_replace('/\x0b/', '', preg_replace($regexp, '', $product->getDescription())));
51
- $product->setShortDescription(preg_replace('/\x0b/', '', preg_replace($regexp, '', $product->getShortDescription())));
 
 
 
 
 
 
 
 
52
  }
53
  }
54
 
55
- if (Mage::getStoreConfig(self::$_MODULE_NAMESPACE
56
- . '/general/is_simulation')) {
57
- $this->logToFile('************************************');
58
- $this->logToFile('SIMULATION: Not saving product '
59
- . $product->getId());
60
- $this->logToFile('************************************');
61
- } else {
62
- $this->logToFile('Saving product ' . $product->getId());
63
- $product->save();
 
 
64
  }
65
 
66
  $this->logToFile('Successfully handled product ' . $product->getId());
38
  . mb_strtoupper($product->getTypeId())
39
  . ' product ' . $product->getId());
40
 
41
+ $isChanged = false;
42
+
43
  foreach ($cleaningOptions as $cleaningOption) {
44
  $this->logToFile('Checking option ' . $cleaningOption);
45
 
46
  // remove control characters for description and short description
47
  if ($cleaningOption == BothInteract_CleanProductTexts_Model_System_Config_Source_View::$VALUE_CLEANING_TYPE_CONTROL_CHARACTERS) {
 
 
48
 
49
+ // @see http://unicode-table.com/en/
50
  $regexp = '/[^\PC\s]/u'; //i.e. /[\x00—\x1F\x80-\x9f]
51
+
52
+ if (preg_match($regexp, $product->getDescription()) ||
53
+ preg_match($regexp, $product->getShortDescription())) { // check to avoid unnecessary save()
54
+ $this->logToFile('Cleaning product ' . $product->getId());
55
+ $product->setDescription(preg_replace('/\x0b|\x0c/', '', preg_replace($regexp, '', $product->getDescription())));
56
+ $product->setShortDescription(preg_replace('/\x0b|\x0c/', '', preg_replace($regexp, '', $product->getShortDescription())));
57
+ $isChanged = true;
58
+ } else {
59
+ $this->logToFile('Ignoring product ' . $product->getId());
60
+ }
61
  }
62
  }
63
 
64
+ if ($isChanged) {
65
+ if (Mage::getStoreConfig(self::$_MODULE_NAMESPACE
66
+ . '/general/is_simulation')) {
67
+ $this->logToFile('************************************');
68
+ $this->logToFile('SIMULATION: Not saving product '
69
+ . $product->getId());
70
+ $this->logToFile('************************************');
71
+ } else {
72
+ $this->logToFile('Saving product ' . $product->getId());
73
+ $product->save();
74
+ }
75
  }
76
 
77
  $this->logToFile('Successfully handled product ' . $product->getId());
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>BothInteract_CleanProductTexts</name>
4
- <version>1.0.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://www.opensource.org/licenses/mit-license.php">MIT License (MITL)</license>
7
  <channel>community</channel>
@@ -22,11 +22,11 @@
22
  &lt;p&gt;In addition this extension enabled you to logs messages to a custom log file that can be specified in the configuration options to your log directory under &lt;i&gt;General&lt;/i&gt; / &lt;i&gt;Log File&lt;/i&gt;.&lt;/p&gt; &#xD;
23
  &#xD;
24
  &lt;p&gt;So you have the option to easily track events handled by this extension and check for possible problems.&lt;/p&gt;</description>
25
- <notes>Initial public release.</notes>
26
  <authors><author><name>Matthias Kerstner</name><user>mkbothinteract</user><email>matthias@both-interact.com</email></author></authors>
27
- <date>2015-03-06</date>
28
- <time>12:52:21</time>
29
- <contents><target name="magecommunity"><dir name="BothInteract"><dir name="CleanProductTexts"><dir name="Model"><file name="Observer.php" hash="fa5e9b0483e429093f7ed413727f99bb"/><dir name="System"><dir name="Config"><dir name="Source"><file name="View.php" hash="06c8cab69af1427591ccd80a5ff84b85"/></dir></dir></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="09afe6511e6b0c1ee544c2ca87d59474"/><file name="config.xml" hash="8b3dceb0487024d8822a504af9c0241e"/><file name="system.xml" hash="9250f6bd8e3089b823c46138e1d6622f"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="BothInteract_CleanProductTexts.xml" hash="c685308779c1427921191dd9860627a1"/></dir></target></contents>
30
  <compatible/>
31
  <dependencies><required><php><min>5.3.0</min><max>5.6.6</max></php></required></dependencies>
32
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>BothInteract_CleanProductTexts</name>
4
+ <version>1.1.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://www.opensource.org/licenses/mit-license.php">MIT License (MITL)</license>
7
  <channel>community</channel>
22
  &lt;p&gt;In addition this extension enabled you to logs messages to a custom log file that can be specified in the configuration options to your log directory under &lt;i&gt;General&lt;/i&gt; / &lt;i&gt;Log File&lt;/i&gt;.&lt;/p&gt; &#xD;
23
  &#xD;
24
  &lt;p&gt;So you have the option to easily track events handled by this extension and check for possible problems.&lt;/p&gt;</description>
25
+ <notes>Added missing shell script in installation package. As usual, you can find the shell script in the folder "shell".</notes>
26
  <authors><author><name>Matthias Kerstner</name><user>mkbothinteract</user><email>matthias@both-interact.com</email></author></authors>
27
+ <date>2015-04-07</date>
28
+ <time>13:36:12</time>
29
+ <contents><target name="magecommunity"><dir name="BothInteract"><dir name="CleanProductTexts"><dir name="Model"><file name="Observer.php" hash="fd3b0a87136f323fa55746487714d4c9"/><dir name="System"><dir name="Config"><dir name="Source"><file name="View.php" hash="06c8cab69af1427591ccd80a5ff84b85"/></dir></dir></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="09afe6511e6b0c1ee544c2ca87d59474"/><file name="config.xml" hash="8b3dceb0487024d8822a504af9c0241e"/><file name="system.xml" hash="9250f6bd8e3089b823c46138e1d6622f"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="BothInteract_CleanProductTexts.xml" hash="c685308779c1427921191dd9860627a1"/></dir></target><target name="mage"><dir><dir name="shell"><file name="clean_product_texts.php" hash="be3c1e00b6705bb6d2bc9ce4f1546c1d"/></dir></dir></target></contents>
30
  <compatible/>
31
  <dependencies><required><php><min>5.3.0</min><max>5.6.6</max></php></required></dependencies>
32
  </package>
shell/clean_product_texts.php ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once 'abstract.php';
4
+
5
+ /**
6
+ * @author Matthias Kerstner <matthias@both-interact.com>
7
+ * @version 1.0.0
8
+ * @copyright (c) 2015, Both Interact GmbH
9
+ */
10
+ class Mage_Shell_Clean_Product_Texts extends Mage_Shell_Abstract {
11
+
12
+ const PAGE_SIZE = 100;
13
+
14
+ /**
15
+ * Parse $string with comma separated values and return array.
16
+ *
17
+ * @param string $string
18
+ * @return array
19
+ */
20
+ protected function _parseString($string) {
21
+ $values = array();
22
+ if (!empty($string)) {
23
+ $values = explode(',', $string);
24
+ $values = array_map('trim', $values);
25
+ }
26
+ return $values;
27
+ }
28
+
29
+ /**
30
+ * Run script based on CL options specified.
31
+ */
32
+ public function run() {
33
+ if ($this->getArg('products')) {
34
+
35
+ // switch to admin event area
36
+ Mage::app()->addEventArea('admin');
37
+
38
+ // product model observer to be called on products
39
+ $productModelObserver = new BothInteract_CleanProductTexts_Model_Observer();
40
+
41
+ // allowed attribute types
42
+ $types = array('varchar', 'text', 'decimal', 'datetime', 'int');
43
+
44
+ // attribute sets array
45
+ $attributeSets = array();
46
+
47
+ // user defined attribute ids
48
+ $entityType = Mage::getModel('eav/entity_type')
49
+ ->loadByCode('catalog_product');
50
+
51
+ $attributeCollection = $entityType
52
+ ->getAttributeCollection()
53
+ ->addFilter('is_user_defined', '1')
54
+ ->getItems();
55
+ $attrIds = array();
56
+ foreach ($attributeCollection as $attribute) {
57
+ $attrIds[] = $attribute->getId();
58
+ }
59
+ $userDefined = implode(',', $attrIds);
60
+
61
+ // product collection based on attribute filters
62
+ $collection = Mage::getModel('catalog/product')->getCollection();
63
+ $entityTable = $collection
64
+ ->getTable(Mage::getModel('eav/entity_type')
65
+ ->loadByCode('catalog_product')
66
+ ->getEntityTable());
67
+
68
+ // load specific product_types (currently supported: simple, configurable)
69
+ $cliTypes = $this->getArg('types');
70
+ if (!empty($cliTypes) && $cliTypes != 'all') {
71
+ $productTypes = $this->_parseString($this->getArg('types'));
72
+
73
+ foreach ($productTypes as $k => $productType) {
74
+ if (!in_array($productType, array('simple', 'configurable'))) {
75
+ unset($productTypes[$k]);
76
+ }
77
+ }
78
+ $collection->addAttributeToFilter('type_id', array('in' => $productTypes));
79
+ }
80
+
81
+ // load product IDs specified only
82
+ if ($this->getArg('products') != 'all') {
83
+ if ($ids = $this->_parseString($this->getArg('products'))) {
84
+ $collection->addAttributeToFilter('entity_id', array('in' => $ids));
85
+ }
86
+ }
87
+
88
+ $collection->setPageSize(self::PAGE_SIZE);
89
+
90
+ $pages = $collection->getLastPageNumber();
91
+ $currentPage = 1;
92
+
93
+ //light product collection iterating
94
+ while ($currentPage <= $pages) {
95
+
96
+ echo 'Processing products page ' . $currentPage . ' of ' . $pages
97
+ . '...' . PHP_EOL;
98
+
99
+ $collection->setCurPage($currentPage);
100
+ $collection->load();
101
+
102
+ foreach ($collection->getItems() as $item) {
103
+
104
+ // load product to manipulate
105
+ $product = Mage::getModel('catalog/product')
106
+ ->load($item->getId());
107
+
108
+ // manually call our observer to update child products
109
+ $productModelObserver->processProduct($product);
110
+ }
111
+
112
+ $currentPage++;
113
+ $collection->clear();
114
+ }
115
+
116
+ echo 'Done!' . PHP_EOL;
117
+ } else {
118
+ echo $this->usageHelp();
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Retrieve Usage Help Message.
124
+ */
125
+ public function usageHelp() {
126
+ return <<<USAGE
127
+
128
+ Usage: php -f clean_product_texts -- [options]
129
+
130
+ --products all Clean all products
131
+ --products <product_ids> Clean products by IDs
132
+ --types all Clean all product types
133
+ --types <product_types> Clean only product types specified
134
+ help Show this help
135
+
136
+ <product_ids> Comma separated IDs of products
137
+ <product_types> Comma separated list of product types: simple and/or configurable
138
+
139
+ USAGE;
140
+ }
141
+
142
+ }
143
+
144
+ $shell = new Mage_Shell_Clean_Product_Texts();
145
+ $shell->run();