1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
<?php
require_once 'bedita_base.php';
class EditorialContentsShell extends BeditaBaseShell
{
public $uses = array('BEObject');
public function help() {
$this->out('Update `publisher` field for all users that belong to a group');
$this->out('that grants them access to backend app with default publisher configurated.');
$this->out();
$this->out(' Usage: editorial_contents [--reset-empty-publisher]');
$this->out();
$this->out(' --reset-empty-publisher change object publisher from empty string to NULL value');
$this->out();
}
public function main() {
$resetEmptyPublisher = !empty($this->params['-reset-empty-publisher']);
$query = array(
'fields' => array('COUNT(DISTINCT BEObject.id) AS count'),
'contain' => array(),
);
if ($resetEmptyPublisher) {
$query['conditions'] = array('BEObject.publisher' => '');
} else {
$query['joins'] = array(
array(
'table' => 'groups_users',
'alias' => 'GroupsUser',
'type' => 'INNER',
'conditions' => array(
'GroupsUser.user_id = BEObject.user_created',
),
),
array(
'table' => 'groups',
'alias' => 'Group',
'type' => 'INNER',
'conditions' => array(
'Group.id = GroupsUser.group_id',
'Group.backend_auth' => 1,
),
),
);
$query['conditions'] = array(
'OR' => array(
'BEObject.publisher' => null,
'AND' => array(
'BEObject.publisher' => '',
'1 = 1',
),
),
);
}
$count = $this->BEObject->find('first', $query);
$count = $count[0]['count'];
if (!$count) {
$this->out('=====> All objects are updated');
exit(1);
}
$question = sprintf('=====> %d objects will be updated. Do you wish to continue?', $count);
if ($this->in($question, array('y', 'n'), 'n') !== 'y') {
$this->err('Aborting');
exit(1);
}
App::import('Sanitize');
$this->out('=====> Updating rows in database...');
if ($resetEmptyPublisher) {
$this->BEObject->query(
'UPDATE `objects`' . PHP_EOL .
' SET `objects`.`publisher` = NULL' . PHP_EOL .
' WHERE `objects`.`publisher` = \'\''
);
} else {
$publisher = Configure::read('editorialContents.defaultPublisher');
if (empty($publisher)) {
$this->out('=====> Configuration item `editorialContents.defaultPublisher` must not be empty');
$this->err('Aborting');
exit(1);
} else {
$publisher = sprintf('\'%s\'', Sanitize::escape($publisher));
}
$this->BEObject->query(
'UPDATE `objects`' . PHP_EOL .
' INNER JOIN `groups_users` ON (`groups_users`.`user_id` = `objects`.`user_created`)' . PHP_EOL .
' INNER JOIN `groups` ON (`groups`.`id` = `groups_users`.`group_id`)' . PHP_EOL .
' SET `objects`.`publisher` = ' . $publisher . PHP_EOL .
' WHERE (`objects`.`publisher` = \'\' OR `objects`.`publisher` IS NULL) AND `groups`.`backend_auth` = 1'
);
}
$question = '=====> It is STRONGLY suggested that you empty your cache, in order to avoid having outdated publisher fields.' . PHP_EOL;
$question .= '=====> Do you want to clear your cache now?';
if ($this->in($question, array('y', 'n'), 'y') !== 'n') {
$this->out('=====> Cleaning up cache...');
ClassRegistry::init('Utility')
->call('cleanupCache', array(
'basePath' => TMP,
'frontendsToo' => false,
'cleanAll' => true,
));
}
$this->out('=====> DONE');
}
}