croogo /Croogo/Console/Command/Task/UpgradeTask.php

Language PHP Lines 353
MD5 Hash 25e98c697c44b5e2cc8428c2ceb3f36c
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php

/**
 * UpgradeTask
 *
 * @package  Croogo.Croogo.Console.Command.Task
 * @since    1.5
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class UpgradeTask extends AppShell {

/**
 * maps 1.4 controllers to the current plugin
 */
	protected $_controllerMap = array(
		'attachments' => 'file_manager',
		'filemanager' => 'file_manager',
		'contacts' => 'contacts',
		'messages' => 'contacts',
		'terms' => 'taxonomy',
		'vocabularies' => 'taxonomy',
		'types' => 'taxonomy',
		'comments' => 'comments',
		'acl_actions' => 'acl',
		'acl_permissions' => 'acl',
		'roles' => 'users',
		'users' => 'users',
		'nodes' => 'nodes',
		'regions' => 'blocks',
		'blocks' => 'blocks',
		'languages' => 'settings',
		'settings' => 'settings',
		'menus' => 'menus',
		'links' => 'menus',
	);

/**
 * Setting instance
 */
	public $Setting = null;

/**
 * Load Settings plugin and model
 */
	protected function _loadSettingsPlugin() {
		if (!CakePlugin::loaded('Settings')) {
			CakePlugin::load('Settings');
		}
		if (!$this->Setting) {
			$this->Setting = ClassRegistry::init('Settings.Setting');
		}
	}

/**
 * getOptionParser
 */
	public function getOptionParser() {
		return parent::getOptionParser()
			->addSubCommand('acl', array(
				'help' => __d('croogo', 'Upgrade ACL database for core controllers.'),
				'parser' => array(
					'description' => __d('croogo',
		'Upgrades the ACO hierarchy from 1.3/1.4 so it follows the default ' .
		'behavior in normal CakePHP applications. The primary difference is ' .
		'plugin controllers now are stored underneath its own Plugin ACO record, ' .
		'whereas previous version assumes all Controllers belongs to the root ' .
		'\'controllers\' node.%s' .
		'<warning>Ensure that you have a backup of your aros, acos, and aros_acos table ' .
		'before upgrading.</warning>', $this->nl(2)
					),
				),
			))
			->addSubCommand('settings', array(
				'help' => __d('croogo', 'Create settings.json from database'),
			))
			->addSubCommand('bootstraps', array(
				'help' => __d('croogo', 'Update Hook.bootstrap settings'),
			))
			->addSubCommand('links', array(
				'help' => __d('croogo', 'Update Links in database'),
			))
			->addSubCommand('first_migrations', array(
				'help' => __d('croogo', 'Create first migration records'),
			))
			->addSubCommand('migrations', array(
				'help' => __d('croogo', 'Run all pending migrations for core plugins'),
			))
			->addSubCommand('all', array(
				'help' => __d('croogo', 'Run all upgrade tasks'),
			));
	}

/**
 * convert settings.yml to settings.json
 */
	public function settings($keys = array()) {
		$this->_loadSettingsPlugin();
		if (file_exists(APP . 'Config' . DS . 'settings.json')) {
			$this->err(__d('croogo', '<warning>Config/settings.json already exist</warning>'));
		} else {
			$defaultPlugins = array(
				'Settings', 'Comments', 'Contacts', 'Nodes', 'Meta', 'Menus',
				'Users', 'Blocks', 'Taxonomy', 'FileManager', 'Ckeditor',
			);
			$Setting = $this->Setting;
			$setting = $Setting->findByKey('Hook.bootstraps');
			$plugins = explode(',', $setting['Setting']['value']);
			if (is_array($plugins)) {
				foreach ($plugins as $plugin) {
					if (!in_array($plugin, $defaultPlugins)) {
						$defaultPlugins[] = $plugin;
					}
				}
			}
			$Setting->write('Hook.bootstraps', join(',', $defaultPlugins));
			if ($version = file_get_contents(APP . 'VERSION.txt')) {
				$Setting->write('Croogo.version', $version);
			}
			$Setting->write('Access Control.multiColumn', '', array(
				'title' => 'Allow login by username or email',
				'input_type' => 'checkbox',
				'editable' => true,
			));
			$Setting->write('Access Control.multiRole', 0, array(
				'title' => 'Enable Multiple Roles',
				'input_type' => 'checkbox',
				'editable' => true,
			));
			$Setting->write('Access Control.rowLevel', 0, array(
				'title' => 'Row Level Access Control',
				'input_type' => 'checkbox',
				'editable' => true,
			));
			$Setting->write('Access Control.autoLoginDuration', '+1 week', array(
				'title' => '"Remember Me" Cookie Lifetime',
				'description' => 'Eg: +1 day, +1 week',
				'input_type' => 'text',
				'editable' => true,
			));
			$this->out(__d('croogo', '<success>Config/settings.yml created based on `settings` table</success>'));
		}
	}

/**
 * Upgrade ACL database
 */
	public function acl() {
		App::uses('AclUpgrade', 'Acl.Lib');
		if (!CakePlugin::loaded('Acl') || !class_exists('AclUpgrade')) {
			$this->err('AclUpgrade class not found or Acl plugin not loaded');
			$this->_stop();
		}
		$Upgrade = new AclUpgrade();
		if (($result = $Upgrade->upgrade()) !== true) {
			$this->err($result);
		} else {
			$this->out('<success>ACL Upgrade completed successfully</success>');
		}
	}

	public function links() {
		if (!CakePlugin::loaded('Menus')) {
			CakePlugin::load('Menus');
		}
		App::uses('View', 'View');
		App::uses('AppHelper', 'View/Helper');
		App::uses('MenusHelper', 'Menus.View/Helper');
		$Menus = new MenusHelper(new View());
		$Link = ClassRegistry::init('Menus.Link');
		$links = $Link->find('all', array('fields' => array('id', 'title', 'link')));

		$count = 0;
		foreach ($links as $link) {
			if (!strstr($link['Link']['link'], 'controller:')) {
				continue;
			}
			if (strstr($link['Link']['link'], 'plugin:')) {
				continue;
			}
			$url = $Menus->linkStringToArray($link['Link']['link']);
			if (isset($this->_controllerMap[$url['controller']])) {
				$url['plugin'] = $this->_controllerMap[$url['controller']];
				$linkString = $Menus->urlToLinkString($url);
				$Link->id = $link['Link']['id'];
				$this->out(__d('croogo', 'Updating Link %s', $Link->id));
				$this->warn(__d('croogo', '- %s', $link['Link']['link']));
				$this->success(__d('croogo', '+ %s', $linkString), 2);
				$Link->saveField('link', $linkString, false);
				$count++;
			}
		}
		$this->out(__d('croogo', 'Links updated: %d rows', $count));
	}

/**
 * Upgrade Hook.bootstraps
 */
	public function bootstraps() {
		$this->_loadSettingsPlugin();

		$bootstraps = Configure::read('Hook.bootstraps');
		$plugins = explode(',', $bootstraps);

		$plugins = $this->_bootstrapReorderByDependency($plugins);
		$plugins = $this->_bootstrapSetupEditor($plugins);

		$this->Setting->write('Hook.bootstraps', join(',', $plugins));
		$this->out(__d('croogo', 'Hook.bootstraps updated'));
	}

/**
 * Activate/move Wysiwyg before Ckeditor/Tinymce when appropriate
 */
	protected function _bootstrapSetupEditor($plugins) {
		$plugins = array_flip($plugins);
		if (empty($plugins['Ckeditor']) && empty($plugins['Tinymce'])) {
			return;
		}
		foreach ($plugins as $plugin => &$value) {
			$value *= 10;
		}

		if (!empty($plugins['Ckeditor']) && !empty($plugins['Tinymce'])) {
			$editor = ($plugins['Ckeditor'] < $plugins['Tinymce']) ? $plugins['Ckeditor'] : $plugins['Tinymce'];
		} elseif (!empty($plugins['Ckeditor'])) {
			$editor = $plugins['Ckeditor'];
		} else {
			$editor = $plugins['Tinymce'];
		}

		if (empty($plugins['Wysiwyg'])) {
			$plugins['Wysiwyg'] = $editor - 1;
		} else {
			if ($plugins['Wysiwyg'] >= $editor) {
				$plugins['Wysiwyg'] = $editor - 1;
			}
		}

		asort($plugins);
		$plugins = array_flip($plugins);
		return $plugins;
	}

/**
 * Re-order plugins based on dependencies:
 * for e.g, Ckeditor depends on Wysiwyg
 * if in Hook.bootstraps Ckeditor appears before Wysiwyg,
 * we will reorder it so that it loads right after Wysiwyg
 */
	protected function _bootstrapReorderByDependency($plugins) {
		$pluginsOrdered = $plugins;
		foreach ($plugins as $p) {
			$jsonPath = APP . 'Plugin' . DS . $p . DS . 'Config' . DS . 'plugin.json';
			if (file_exists($jsonPath)) {
				$pluginData = json_decode(file_get_contents($jsonPath), true);
				if (isset($pluginData['dependencies']['plugins'])) {
					foreach ($pluginData['dependencies']['plugins'] as $d) {
						$k = array_search($p, $pluginsOrdered);
						$dk = array_search($d, $pluginsOrdered);
						if ($dk > $k) {
							unset($pluginsOrdered[$k]);
							$pluginsOrdered = array_slice($pluginsOrdered, 0, $k + 1, true) +
								array($p => $p) +
								array_slice($pluginsOrdered, $k + 1, count($pluginsOrdered) - 1, true);
							$pluginsOrdered = array_values($pluginsOrdered);
						}
					}
				}
			}
		}
		return $pluginsOrdered;
	}

/**
 * create schema_migrations record for $plugin
 */
	protected function _createFirstMigration($plugin) {
		static $Migration;
		if (empty($Migration)) {
			$Migration = ClassRegistry::init(array(
				'class' => 'AppModel',
				'table' => 'schema_migrations',
			));
		}
		$className = 'FirstMigration' . $plugin;
		$migration = $Migration->findByClass($className);
		if (!empty($migration)) {
			return true;
		}
		$Migration->create();
		return $Migration->save(array(
			'class' => $className,
			'type' => $plugin,
		));
	}

/**
 * Create default FirstMigration records for installations using croogo_data.sql
 */
	public function first_migrations() {
		foreach ((array)Configure::read('Core.corePlugins') as $plugin) {
			$result = $this->_createFirstMigration($plugin);
			if (!$result) {
				$this->error(sprintf('Unable to setup FirstMigration records for %s', $plugin));
			}
		}
		$this->success('FirstMigration default records created');
	}

/**
 * Runs all available pending migrations for core plugins
 */
	public function migrations() {
		$CroogoPlugin = new CroogoPlugin();
		foreach ((array)Configure::read('Core.corePlugins') as $plugin) {
			$result = $CroogoPlugin->migrate($plugin);
			if (!$result) {
				$this->out($CroogoPlugin->migrationErrors);
			}
		}
	}

/**
 * Runs all available subcommands
 */
	public function all() {
		foreach ($this->OptionParser->subcommands() as $command) {
			$name = $command->name();
			if ($name === 'all') {
				continue;
			}
			$this->out(__d('croogo', 'Upgrade "%s"', $name));
			$this->$name();
		}
	}

	public function execute() {
		if (empty($this->args)) {
			return $this->out($this->OptionParser->help());
		}
		$commands = array_keys($this->OptionParser->subcommands('croogo'));
		$command = $this->args[0];
		if ($command[0] != '_' && in_array($command, $commands)) {
			return $this->{$command}();
		} else {
			$this->out(__d('croogo', 'Command not recognized'));
		}
	}

}
Back to Top