|
| 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