Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 72be39c

Browse files
author
Florian Krämer
committed
Merge branch '2.0'
2 parents e14b56f + 4238217 commit 72be39c

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
._*
44
Icon
55

6-
Vendor/*
6+
Vendor/*
7+
/.idea

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Contributing to this Plugin
3737

3838
Please feel free to contribute to the plugin with new issues, requests, unit tests and code fixes or new features. If you want to contribute some code, create a feature branch from develop, and send us your pull request. Unit tests for new features and issues detected are mandatory to keep quality high.
3939

40-
* Pull requests must be send to the ```develop``` branch.
4140
* Contributions must follow the [PSR2 coding standard recommendation](https://github.com/php-fig-rectified/fig-rectified-standards).
4241
* [Unit tests](http://book.cakephp.org/3.0/en/development/testing.html) are required.
4342

src/Shell/PurifierShell.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Purifier Shell
4+
*
5+
* @author Florian Krämer
6+
* @copyright 2012 - 2015 Florian Krämer
7+
* @license MIT
8+
*/
9+
namespace Burzum\HtmlPurifier\Shell;
10+
11+
use Cake\Console\Shell;
12+
use Cake\ORM\Table;
13+
use Cake\ORM\TableRegistry;
14+
15+
class PurifierShell extends Shell {
16+
17+
public function main() {}
18+
19+
/**
20+
* Purifies data base content.
21+
*/
22+
public function purify() {
23+
$table = TableRegistry::get($this->param('table'));
24+
25+
$fields = explode(',', $this->param('fields'));
26+
foreach ($fields as $field) {
27+
if (!$table->hasField($field)) {
28+
$this->abort(sprintf('Table `%s` is missing the field `%s`.', $table->table(), $field));
29+
}
30+
}
31+
32+
if (!in_array('HtmlPurifier', $table->behaviors()->loaded())) {
33+
$table->addBehavior('Burzum/HtmlPurifier.HtmlPurifier', [
34+
'fields' => explode(',', $this->param('fields')),
35+
'purifierConfig' => $this->param('config')
36+
]);
37+
}
38+
39+
$query = $table->find();
40+
if ($table->hasFinder('purifier')) {
41+
$query->find('purifier');
42+
}
43+
$total = $query->all()->count();
44+
45+
$this->out(sprintf('Sanitizing fields `%s` in table `%s`', implode(',', $fields), $table->table()));
46+
47+
$this->helper('progress')->output([
48+
'total' => $total,
49+
'callback' => function($progress) use ($total, $table) {
50+
$chunkSize = 25;
51+
$chunkCount = 0;
52+
while ($chunkCount <= $total) {
53+
$this->_process($table, $chunkCount, $chunkSize);
54+
$chunkCount = $chunkCount + $chunkSize;
55+
$progress->increment($chunkSize);
56+
$progress->draw();
57+
}
58+
return;
59+
}
60+
]);
61+
}
62+
63+
/**
64+
* Processes the records.
65+
*
66+
* @param \Cake\ORM\Table $table
67+
* @param int $chunkCount
68+
* @param int $chunkSize
69+
* @return void
70+
*/
71+
protected function _process(Table $table, $chunkCount, $chunkSize) {
72+
$query = $table->find();
73+
if ($table->hasFinder('purifier')) {
74+
$query->find('purifier');
75+
}
76+
77+
$fields = explode(',', $this->param('fields'));
78+
$fields[] = $table->primaryKey();
79+
80+
$results = $query
81+
->select($fields)
82+
->offset($chunkCount)
83+
->limit($chunkSize)
84+
->orderDesc($table->aliasField($table->primaryKey()))
85+
->all();
86+
87+
if (empty($results)) {
88+
return;
89+
}
90+
91+
foreach ($results as $result) {
92+
try {
93+
$table->save($result);
94+
$chunkCount++;
95+
} catch (\Exception $e) {
96+
$this->error($e->getMessage());
97+
}
98+
}
99+
}
100+
101+
/**
102+
* {@inheritDoc}
103+
*/
104+
public function getOptionParser() {
105+
$parser = parent::getOptionParser();
106+
107+
$parser->addArgument('purify', [
108+
'help' => 'Purifies fields in a table.'
109+
]);
110+
111+
$parser->addOption('fields', [
112+
'short' => 'f',
113+
'help' => __('The field(s) to purify, comma separated.'),
114+
'required' => true
115+
])->addOption('table', [
116+
'short' => 't',
117+
'help' => __('The table you want to use.'),
118+
'required' => true
119+
])->addOption('config', [
120+
'short' => 'c',
121+
'help' => __('The purifier config you want to use.'),
122+
'default' => 'default'
123+
]);
124+
return $parser;
125+
}
126+
}

0 commit comments

Comments
 (0)