Skip to content

Commit 2b799ac

Browse files
committed
Initial commit
0 parents  commit 2b799ac

File tree

24 files changed

+914
-0
lines changed

24 files changed

+914
-0
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[{*.md,*.slim}]
13+
trim_trailing_whitespace = false

CONTRIBUTING.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Contributing
2+
3+
## Issues
4+
5+
If you want to request a feature or report a bug, please use the following template.
6+
7+
```markdown
8+
## Description
9+
10+
[Add feature/bug description here]
11+
12+
## How to reproduce
13+
14+
[Add steps on how to reproduce this issue]
15+
16+
## What do you expect
17+
18+
[Describe what do you expect to happen]
19+
20+
## What happened instead
21+
22+
[Describe the actual results]
23+
24+
## Extension version:
25+
26+
Extension version: [Add extension version here]
27+
```
28+
29+
## Writing code
30+
31+
Once you've made your great commits (include tests, please):
32+
33+
1. [Fork](http://help.github.com/forking/) the [original repository](http://github.com/kasterweb/kasterweb-magento-cleaner)
34+
2. Create a topic branch - `git checkout -b my_branch`
35+
3. Push to your branch - `git push origin my_branch`
36+
4. [Create an Issue](http://github.com/kasterweb/kasterweb-magento-cleaner/issues) with a link to your branch
37+
5. That's it!
38+
39+
Please respect the code style using [editorconfig](http://editorconfig.org/) in your text editor.

Kasterweb_Cleaner-1.0.0.tgz

7.04 KB
Binary file not shown.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Kasterweb Magento Cleaner
2+
Lightweight module to provide some cleaner feature for caches and logs
3+
4+
## Features
5+
- Magento log tables
6+
- Magento log directories
7+
- Redis cache through socket
8+
- Memcached cache through socket
9+
- EdgeCast CDN (HTTP Large) through API
10+
11+
## Requirements
12+
- [Magento] Community 1.5.x, 1.6.x, 1.7.x, 1.8.x, 1.9.x
13+
- [PHP] 5.3+
14+
- [SPL]
15+
- [cURL]
16+
17+
## Installation
18+
- Download the lastest stable version [clicking here](https://github.com/kasterweb/kasterweb-magento-cleaner/archive/master.zip) and extract it
19+
- In your admin area go to System > Magento Connect > Magento Connect Manager
20+
- Uninstall previous versions
21+
- In the section `Direct package file upload` choose the `Kasterweb_Cleaner-{$version}.tgz` file and then upload it
22+
- That's all
23+
24+
## Contributing
25+
Check [CONTRIBUTING.md](https://github.com/kasterweb/kasterweb-magento-cleaner/blob/master/CONTRIBUTING.md).
26+
27+
## License
28+
Kasterweb Magento Cleaner is licensed under the [MIT license](https://github.com/kasterweb/kasterweb-magento-cleaner/blob/master/LICENSE).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
class Kasterweb_Cleaner_Block_Adminhtml_Cache extends Mage_Adminhtml_Block_Template
4+
{
5+
public function getMageTablesUrl()
6+
{
7+
return $this->getUrl('*/*/mageTables');
8+
}
9+
10+
public function getMageDirUrl()
11+
{
12+
return $this->getUrl('*/*/mageDir');
13+
}
14+
15+
public function getRedisUrl()
16+
{
17+
return $this->getUrl('*/*/redis');
18+
}
19+
20+
public function getMemcachedUrl()
21+
{
22+
return $this->getUrl('*/*/memcached');
23+
}
24+
25+
public function getCdnUrl()
26+
{
27+
return $this->getUrl('*/*/cdn');
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Kasterweb_Cleaner_Helper_Data extends Mage_Core_Helper_Abstract
4+
{
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
class Kasterweb_Cleaner_Model_Cdn extends Mage_Core_Model_Abstract
4+
{
5+
const MEDIA_TYPE_FLASH_MEDIA_STREAMING = 2;
6+
const MEDIA_TYPE_HTTP_LARGE = 3;
7+
const MEDIA_TYPE_HTTP_SMALL = 8;
8+
const MEDIA_TYPE_APPLICATION_DELIVERY_NETWORK = 14;
9+
10+
protected function _construct()
11+
{
12+
$this->_init('cleaner/cdn');
13+
}
14+
15+
public function truncate($mediaPath = '*', $mediaType = self::MEDIA_TYPE_HTTP_LARGE)
16+
{
17+
$accountId = Mage::getStoreConfig('cleaner/cdn/account_id');
18+
$accessToken = Mage::getStoreConfig('cleaner/cdn/access_token');
19+
$baseUrl = Mage::getStoreConfig('cleaner/cdn/base_url');
20+
21+
if (empty($accountId) || empty($accessToken) || empty($baseUrl)) {
22+
throw new InvalidArgumentException('Account ID, Access token or Base URL is missing. Please set it in System > Configuration > Kasterweb > Cleaner > CDN');
23+
}
24+
25+
$response = $this->sendRequest($accountId, $accessToken, array(
26+
'MediaPath' => "{$baseUrl}/{$mediaPath}",
27+
'MediaType' => $mediaType
28+
));
29+
30+
if ($response->isError()) {
31+
$error = json_decode($response->getRawBody(), true);
32+
throw new CdnException($error['Message']);
33+
}
34+
35+
return $response;
36+
}
37+
38+
protected function sendRequest($accountId, $accessToken, $body) {
39+
$client = new Zend_Http_Client();
40+
$client->setUri("https://api.edgecast.com/v2/mcc/customers/{$accountId}/edge/purge");
41+
$client->setHeaders(array(
42+
'Authorization' => "TOK:{$accessToken}",
43+
'Content-Type' => 'application/json'
44+
));
45+
$client->setRawData(json_encode($body));
46+
return $client->request(Zend_Http_Client::PUT);
47+
}
48+
}
49+
50+
class CdnException extends Exception { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class Kasterweb_Cleaner_Model_Job extends Mage_Core_Model_Abstract
4+
{
5+
public function performRedis()
6+
{
7+
return Mage::getSingleton('cleaner/redis')->truncate();
8+
}
9+
10+
public function performMemcached()
11+
{
12+
return Mage::getSingleton('cleaner/memcached')->truncate();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
class Kasterweb_Cleaner_Model_MagentoDatabase extends Mage_Core_Model_Abstract
4+
{
5+
/**
6+
* Resource singleton
7+
*
8+
* @var \Mage_Core_Model_Resource
9+
*/
10+
protected $coreResource;
11+
12+
/**
13+
* Write connection to DB
14+
*
15+
* @var \Varien_Db_Adapter_Interface
16+
*/
17+
protected $dbWrite;
18+
19+
/**
20+
* Read connection to DB
21+
*
22+
* @var \Varien_Db_Adapter_Interface
23+
*/
24+
protected $dbRead;
25+
26+
/**
27+
* List of tables in DB
28+
*
29+
* @var string[]
30+
*/
31+
protected $tables;
32+
33+
/**
34+
* Resources to truncate
35+
*
36+
* @var array
37+
*/
38+
protected $resources = array(
39+
// Cache
40+
'core/cache',
41+
'core/cache_tag',
42+
43+
// Catalog
44+
'catalog_index/aggregation',
45+
'catalog_index/aggregation/tag',
46+
'catalog_index/aggregation_to_tag',
47+
48+
// Session
49+
'core/session',
50+
51+
// Dataflow
52+
'dataflow/batch_export',
53+
'dataflow/batch_import',
54+
55+
// Enterprise Admin Logs
56+
'enterprise_logging/event',
57+
'enterprise_logging/event_changes',
58+
59+
// Index
60+
'index/event',
61+
'index/process_event',
62+
63+
// Logs
64+
'log/customer',
65+
'log/quote_table',
66+
'log/summary_table',
67+
'log/summary_type_table',
68+
'log/url_table',
69+
'log/url_info_table',
70+
'log/visitor',
71+
'log/visitor_info',
72+
'log/visitor_online',
73+
74+
// Reports
75+
'reports/event',
76+
'reports/viewed_product_index',
77+
'reports/viewed_aggregated_daily',
78+
'reports/viewed_aggregated_monthly',
79+
'reports/viewed_aggregated_yearly',
80+
);
81+
82+
protected function _construct()
83+
{
84+
$this->_init('cleaner/magentoDatabase');
85+
86+
$this->coreResource = Mage::getSingleton('core/resource');
87+
$this->dbRead = $this->coreResource->getConnection('core_read');
88+
$this->dbWrite = $this->coreResource->getConnection('core_write');
89+
$this->tables = $this->getTables();
90+
}
91+
92+
public function truncate()
93+
{
94+
$truncatedResources = array();
95+
$this->withoutForeignKeyChecks(array($this, 'truncateResources'));
96+
return $truncatedResources;
97+
}
98+
99+
protected function truncateResources()
100+
{
101+
foreach ($this->prepareResources($this->resources) as $resourceTable) {
102+
$this->dbWrite->truncateTable($resourceTable);
103+
$truncatedResources[] = $resourceTable;
104+
}
105+
}
106+
107+
protected function withoutForeignKeyChecks($closure)
108+
{
109+
$this->setForeignKeyCheck(false);
110+
$return = call_user_func($closure);
111+
$this->setForeignKeyCheck(true);
112+
return $return;
113+
}
114+
115+
protected function setForeignKeyCheck($value)
116+
{
117+
return $this->dbWrite->query(sprintf('SET foreign_key_checks = %s', (int) $value));
118+
}
119+
120+
/**
121+
* Extract table names and prepare for cli rendering
122+
*
123+
* @param array $resources
124+
*
125+
* @return void
126+
*/
127+
protected function prepareResources($resources)
128+
{
129+
$preparedResources = array();
130+
foreach ($resources as $resource) {
131+
try {
132+
$preparedResources[] = $this->coreResource->getTableName($resource);
133+
} catch (Mage_Core_Exception $e) { }
134+
}
135+
return $preparedResources;
136+
}
137+
138+
/**
139+
* Get tables in DB
140+
*
141+
* @return string[]
142+
*/
143+
protected function getTables()
144+
{
145+
$stmt = $this->dbRead->query('SHOW TABLES');
146+
$stmt->execute();
147+
return $stmt->fetchAll(PDO::FETCH_COLUMN);
148+
}
149+
}

0 commit comments

Comments
 (0)