├── .gitignore ├── LICENSE.txt ├── README.md └── shell ├── cache.php └── snapshot.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Open Software License ("OSL") v. 3.0 3 | 4 | This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: 5 | 6 | Licensed under the Open Software License version 3.0 7 | 8 | 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: 9 | 10 | 1. to reproduce the Original Work in copies, either alone or as part of a collective work; 11 | 12 | 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; 13 | 14 | 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; 15 | 16 | 4. to perform the Original Work publicly; and 17 | 18 | 5. to display the Original Work publicly. 19 | 20 | 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 21 | 22 | 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 23 | 24 | 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 25 | 26 | 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 27 | 28 | 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 29 | 30 | 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 31 | 32 | 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 33 | 34 | 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 35 | 36 | 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 37 | 38 | 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 39 | 40 | 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 41 | 42 | 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 43 | 44 | 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 45 | 46 | 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 47 | 48 | 16. Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | magento-shell-tools 2 | =================== 3 | 4 | Mage_Shell scripts to help manage Magento installations. 5 | 6 | 7 | Description 8 | ------------------- 9 | 10 | Magento contains a little-known shell abstract class to manage Magento via CLI. 11 | While there are a few concrete classes, the core doesn't ship with much functionality. 12 | This projects aims to augment the CLI interface and provide some useful tools. 13 | 14 | 15 | 16 | The Tools 17 | ------------------- 18 | 19 | - **cache**: All functionality that exists in the admin cache management panel. Plus some more! 20 | Really useful in deployment scripts. 21 | - **snapshot**: Create a compressed tar archive of the /media directory and a database dump into 22 | a directory called /snapshot. Useful for developers bootstrapping their local environments off 23 | of an existing development environment. 24 | - More to be added... 25 | 26 | Usage 27 | ------------------- 28 | You can use this shell script like the other Magento shells. Help is provided. 29 | 30 | Here's example help output from the cache tool: 31 | 32 | Usage: php -f cache.php -- [options] 33 | info Show Magento cache types. 34 | --enable Enable caching for a cachetype. 35 | --disable Disable caching for a cachetype. 36 | --refresh Clean cache types. 37 | --flush Flushes slow|fast cache storage. 38 | 39 | cleanmedia Clean the JS/CSS cache. 40 | cleanimages Clean the image cache. 41 | destroy Clear all caches. 42 | help This help. 43 | 44 | Comma separated cache codes or value "all" for all caches 45 | 46 | 47 | Requirements 48 | ------------------- 49 | 50 | Magento (with shell). The script was developed on EE 1.9, but should work with any Magento version that has the /shell 51 | directory. 52 | 53 | 54 | Installation 55 | -------------------- 56 | 57 | Installation is very simple! Clone/copy the contents of /shell to your Magento /shell directory. 58 | 59 | 60 | License 61 | ------------------- 62 | http://www.opensource.org/licenses/osl-3.0.php 63 | 64 | -------------------------------------------------------------------------------- /shell/cache.php: -------------------------------------------------------------------------------- 1 | _getCacheTypeCodes(); 38 | foreach ($collection as $cache) { 39 | $cachetypes[] = $cache; 40 | } 41 | } else if (!empty($string)) { 42 | $codes = explode(',', $string); 43 | foreach ($codes as $code) { 44 | // do any validation on cache type? 45 | #$process = $this->_getIndexer()->getProcessByCode(trim($code)); 46 | #if (!$process) { 47 | # echo 'Warning: Unknown indexer with code ' . trim($code) . "\n"; 48 | #} else { 49 | $cachetypes[] = $code; 50 | #} 51 | } 52 | } 53 | 54 | return $cachetypes; 55 | } 56 | 57 | /** 58 | * Gets Magento cache types. 59 | * @return 60 | */ 61 | private function _getCacheTypes() { 62 | //return Mage::helper('core')->getCacheTypes(); 63 | return Mage::getModel('core/cache')->getTypes(); 64 | } 65 | 66 | /** 67 | * Gets an array of cache type code. 68 | * @return array Cache type codes. 69 | */ 70 | private function _getCacheTypeCodes() { 71 | return array_keys($this->_getCacheTypes()); 72 | } 73 | 74 | /** 75 | * Gets Magento cache types. 76 | * @return 77 | */ 78 | private function _getCacheBetaTypes() { 79 | return Mage::helper('core')->getCacheBetaTypes(); 80 | } 81 | 82 | /** 83 | * Gets a list of invalidated cache types that should be refreshed. 84 | * @return array Array of invalidated types. 85 | */ 86 | private function _getInvalidatedTypes() { 87 | return Mage::getModel('core/cache')->getInvalidatedTypes(); 88 | } 89 | 90 | /** 91 | * Gets an array of cache type code. 92 | * @return array Cache type codes. 93 | */ 94 | private function _getCacheBetaTypeCodes() { 95 | return array_keys($this->_getCacheBetaTypes()); 96 | } 97 | 98 | /** 99 | * Returns a list of cache types. 100 | * @return void 101 | */ 102 | public function info() { 103 | $invalidTypes = $this->_getInvalidatedTypes(); 104 | foreach($this->_getCacheTypes() as $cache) { 105 | $enabled = ($cache->status)? 'Enabled':'Disabled'; 106 | if($enabled=='Enabled') { 107 | $invalid = (array_key_exists($cache, $invalidTypes))? 'Invalid':'Valid'; 108 | } else { 109 | $invalid = 'N/A'; 110 | } 111 | 112 | echo sprintf('%-16s', $cache->id); 113 | echo sprintf('%-12s', $enabled); 114 | echo sprintf('%-10s', $invalid); 115 | echo $cache->cache_type . "\n"; 116 | } 117 | } 118 | 119 | /** 120 | * Enables caching for provided array of cache types. 121 | * @param $types An array of cache types. 122 | * @return void 123 | */ 124 | public function enable($types) { 125 | $allTypes = Mage::app()->useCache(); 126 | $updatedTypes = 0; 127 | foreach ($types as $code) { 128 | if (empty($allTypes[$code])) { 129 | $allTypes[$code] = 1; 130 | $updatedTypes++; 131 | } 132 | } 133 | if ($updatedTypes > 0) { 134 | Mage::app()->saveUseCache($allTypes); 135 | echo "$updatedTypes cache type(s) enabled.\n"; 136 | } 137 | } 138 | 139 | /** 140 | * Disables caching for provided array of cache types. 141 | * @param $types An array of cache types. 142 | * @return void 143 | */ 144 | public function disable($types) { 145 | $allTypes = Mage::app()->useCache(); 146 | $updatedTypes = 0; 147 | foreach ($types as $code) { 148 | if (!empty($allTypes[$code])) { 149 | $allTypes[$code] = 0; 150 | $updatedTypes++; 151 | } 152 | $tags = Mage::app()->getCacheInstance()->cleanType($code); 153 | } 154 | if ($updatedTypes > 0) { 155 | Mage::app()->saveUseCache($allTypes); 156 | echo "$updatedTypes cache type(s) disabled.\n"; 157 | } 158 | } 159 | 160 | /** 161 | * Flushes the cache storage (Magentos "fast" caching). Flushing cache storage can clear other shared cache data on 162 | * the storage. Please take care when using this method.. 163 | * @return void 164 | */ 165 | public function flushAll() { 166 | try { 167 | Mage::app()->getCacheInstance()->flush(); 168 | echo "The cache storage has been flushed.\n"; 169 | } catch (Exception $e) { 170 | echo "Exception:\n"; 171 | echo $e . "\n"; 172 | } 173 | } 174 | 175 | /** 176 | * Flushes the Magento cache (Magentos "slow" caching). 177 | * @return void 178 | */ 179 | public function flushSystem() { 180 | try { 181 | Mage::app()->cleanCache(); 182 | echo "The Magento cache storage has been flushed.\n"; 183 | } catch (Exception $e) { 184 | echo "Exception:\n"; 185 | echo $e . "\n"; 186 | } 187 | } 188 | 189 | /** 190 | * Refreshes caches for the provided cache types. 191 | * @param $types 192 | * @return void 193 | */ 194 | public function refresh($types) { 195 | $updatedTypes = 0; 196 | if (!empty($types)) { 197 | foreach ($types as $type) { 198 | try { 199 | $tags = Mage::app()->getCacheInstance()->cleanType($type); 200 | $updatedTypes++; 201 | } catch (Exception $e) { 202 | echo $type . " cache unknown error:\n"; 203 | echo $e . "\n"; 204 | } 205 | } 206 | } 207 | if ($updatedTypes > 0) { 208 | echo "$updatedTypes cache type(s) refreshed.\n"; 209 | } 210 | } 211 | 212 | /** 213 | * Clears the merged JS/CSS cache. 214 | * @return void 215 | */ 216 | public function cleanMedia() { 217 | try { 218 | Mage::getModel('core/design_package')->cleanMergedJsCss(); 219 | Mage::dispatchEvent('clean_media_cache_after'); 220 | echo "The JavaScript/CSS cache has been cleaned.\n"; 221 | } 222 | catch (Exception $e) { 223 | echo "An error occurred while clearing the JavaScript/CSS cache.\n"; 224 | echo $e->toString() . "\n"; 225 | } 226 | } 227 | 228 | /** 229 | * Clears the image cache. 230 | * @return void 231 | */ 232 | public function cleanImages() { 233 | try { 234 | Mage::getModel('catalog/product_image')->clearCache(); 235 | Mage::dispatchEvent('clean_catalog_images_cache_after'); 236 | echo "The image cache was cleaned.\n"; 237 | } 238 | catch (Mage_Core_Exception $e) { 239 | $this->_getSession()->addError($e->getMessage()); 240 | } 241 | } 242 | 243 | /** 244 | * Clears all possible Magento cache in one command. Be careful, This will DESTROY the cache! 245 | * @return void 246 | */ 247 | public function destroy() { 248 | $types = $this->_parseCacheTypeString('all'); 249 | $this->refresh($types); 250 | $this->cleanImages(); 251 | $this->cleanMedia(); 252 | $this->flushSystem(); 253 | $this->flushAll(); 254 | } 255 | 256 | /** 257 | * Run script 258 | * 259 | */ 260 | public function run() 261 | { 262 | // info 263 | if ($this->getArg('info')) { 264 | $this->info(); 265 | } else if ($this->getArg('enable')) { 266 | $types = $this->_parseCacheTypeString($this->getArg('enable')); 267 | $this->enable($types); 268 | 269 | // --disable 270 | } else if ($this->getArg('disable')) { 271 | $types = $this->_parseCacheTypeString($this->getArg('disable')); 272 | echo print_r($types, true); 273 | $this->disable($types); 274 | 275 | // --flush 276 | } else if ($this->getArg('flush')) { 277 | $type = $this->getArg('flush'); 278 | if($type == 'magento') { 279 | $this->flushSystem(); 280 | } else if($type == 'storage') { 281 | $this->flushAll(); 282 | } else { 283 | echo "The flush type must be magento|storage\n"; 284 | } 285 | 286 | // --refresh 287 | } else if ($this->getArg('refresh')) { 288 | if ($this->getArg('refresh')) { 289 | $types = $this->_parseCacheTypeString($this->getArg('refresh')); 290 | } else { 291 | $types = $this->_parseCacheTypeString('all'); 292 | } 293 | $this->refresh($types); 294 | 295 | // cleanmedia 296 | } else if ($this->getArg('cleanmedia')) { 297 | $this->cleanMedia(); 298 | 299 | // cleanimages 300 | } else if ($this->getArg('cleanimages')) { 301 | $this->cleanImages(); 302 | 303 | // destroy 304 | } else if ($this->getArg('destroy')) { 305 | $this->destroy(); 306 | 307 | // help 308 | } else { 309 | echo $this->usageHelp(); 310 | } 311 | } 312 | 313 | /** 314 | * Retrieve Usage Help Message 315 | * 316 | */ 317 | public function usageHelp() 318 | { 319 | return << Enable caching for a cachetype. 323 | --disable Disable caching for a cachetype. 324 | --refresh Clean cache types. 325 | --flush Flushes slow|fast cache storage. 326 | 327 | cleanmedia Clean the JS/CSS cache. 328 | cleanimages Clean the image cache. 329 | destroy Clear all caches. 330 | help This help. 331 | 332 | Comma separated cache codes or value "all" for all caches 333 | 334 | USAGE; 335 | } 336 | } 337 | 338 | $shell = new Guidance_Shell_Cache(); 339 | $shell->run(); 340 | 341 | -------------------------------------------------------------------------------- /shell/snapshot.php: -------------------------------------------------------------------------------- 1 | getNode('global/resources/default_setup/connection'); 37 | $rootpath = $this->_getRootPath(); 38 | $snapshot = $rootpath.'snapshot'; 39 | 40 | # Create the snapshot directory if not exists 41 | $io = new Varien_Io_File(); 42 | $io->mkdir($snapshot); 43 | 44 | # Create the media archive 45 | exec("tar -chz -C \"$rootpath\" -f \"{$snapshot}/media.tgz\" media"); 46 | 47 | # Dump the database 48 | exec("mysqldump -h {$connection->host} -u {$connection->username} --password={$connection->password} {$connection->dbname} | gzip > \"{$snapshot}/{$connection->dbname}.sql.gz\""); 49 | } 50 | 51 | /** 52 | * Run script 53 | */ 54 | public function run() 55 | { 56 | if ($this->getArg('snapshot')) { 57 | $this->_snapshot(); 58 | } else { 59 | echo $this->usageHelp(); 60 | } 61 | } 62 | 63 | /** 64 | * Retrieve Usage Help Message 65 | * 66 | */ 67 | public function usageHelp() 68 | { 69 | global $argv; 70 | $self = basename($argv[0]); 71 | return <<run(); 92 | } 93 | --------------------------------------------------------------------------------