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
<?php
require_once 'bedita_base.php';
class ModuleShell extends BeditaBaseShell {
function startup() {
Configure::write('debug', 1);
}
public function plug($plugin = null) {
$op = (empty($this->params["name"])) ? "list" : "name";
$moduleModel = ClassRegistry::init("Module");
$pluggedModules = $moduleModel->find("list", array(
"fields" => array(
"id",
"name"
),
"conditions" => array(
"module_type" => "plugin"
)
));
$pluginPaths = App::path('plugins');
if (empty($plugin)) {
$plugin = $this->mandatoryArgument('name', 'use -name option');
}
$pluginsBasePath = false;
if (! empty($pluginPaths)) {
foreach ($pluginPaths as $pPath) {
if (file_exists($pPath . $plugin . DS . 'config' . DS . 'bedita_module_setup.php') && ! in_array($plugin, $pluggedModules)) {
$pluginsBasePath = $pPath;
}
}
}
if (! $pluginsBasePath) {
$this->out("Plugin doesn't exist");
return;
}
if (in_array($plugin, $pluggedModules)) {
$this->out("Module " . $plugin . " is already installed.");
return;
}
include $pluginsBasePath . $plugin . DS . 'config' . DS . 'bedita_module_setup.php';
$beditaVersion = Configure::read('version');
if (empty($moduleSetup['BEditaMinVersion'])) {
$this->out('');
$this->out('WARNING: mandatory "BEditaMinVersion" parameter missing in module config - unable to plug!');
return;
}
if ($beditaVersion < $moduleSetup['BEditaMinVersion']) {
$this->out('');
$this->out("WARNING: installed version and version required mismatched!");
$this->out('BEdita version: ' . $beditaVersion);
$this->out('Min BEdita version required by ' . $plugin . ': ' . $moduleSetup['BEditaMinVersion']);
$command = $this->in('Do you want continue anyway?', array(
'y',
'n'
), 'n');
if ($command != 'y') {
$this->out('Bye');
return;
}
}
$this->out('');
$this->out('You are about to plug in the module ' . $plugin . ' version ' . $moduleSetup['version']);
$this->out('Module description: ' . $moduleSetup['description']);
$this->out('');
$command = $this->in('Do you wanto to proceed?', array(
'y',
'n'
), 'y');
if ($command != 'y') {
$this->out('Bye');
return;
}
if (! $moduleModel->plugModule($plugin, $moduleSetup)) {
$this->out('Module install failed!');
return;
}
$this->out('Plugin ' . $plugin . ' installed successfully');
}
public function show() {
$moduleModel = ClassRegistry::init('Module');
$pluggedModules = $moduleModel->find('list', array(
'fields' => array('id', 'name'),
'conditions' => array('module_type' => array('plugin', 'addon'))
)
);
$this->hr();
$this->out('Plugged modules: ');
$this->out('');
if (empty($pluggedModules)) {
$this->out('No module plugged');
} else {
foreach ($pluggedModules as $key => $mod) {
$this->out(' ' . $mod);
}
}
$this->hr();
$this->out('');
$pluginPaths = App::path('plugins');
$unpluggedModules = array();
if (! empty($pluginPaths)) {
foreach ($pluginPaths as $pluginsBasePath) {
$folder = new Folder($pluginsBasePath);
$plugins = $folder->read(true, true);
foreach ($plugins[0] as $plugin) {
if (file_exists($pluginsBasePath . $plugin . DS . 'config' . DS . 'bedita_module_setup.php') && ! in_array($plugin, $pluggedModules)) {
$unpluggedModules[] = $plugin;
}
}
}
}
if (empty($unpluggedModules)) {
$this->out('No module to plug');
return;
}
$this->out('Available unplugged modules:');
$this->out('');
foreach ($unpluggedModules as $key => $um) {
$this->out(++$key . '. ' . $um);
}
$this->out('');
$moduleToPlug = $this->in('Choose module to plug. Digit name or corresponding number (enter to quit):');
if (empty($moduleToPlug)) {
$this->out('Bye');
return;
}
if (is_numeric($moduleToPlug) && !empty($unpluggedModules[$moduleToPlug-1])) {
$moduleToPlug = $unpluggedModules[$moduleToPlug-1];
}
if (!in_array($moduleToPlug, $unpluggedModules)) {
$this->out("Plugin doesn't exist");
return;
}
$this->plug($moduleToPlug);
}
public function unplug() {
$name = $this->mandatoryArgument('name', 'use -name option');
$moduleModel = ClassRegistry::init('Module');
$dirName = BEDITA_MODULES_PATH . DS . $name;
if (!file_exists($dirName)) {
throw new BeditaException(__('plugin folder not found ' . $dirName, true));
}
$fileName = $dirName . DS . 'config' . DS . 'bedita_module_setup.php';
if (!file_exists($fileName)) {
throw new BeditaException(__('config/bedita_module_setup.php not found', true));
}
include($fileName);
$id = $moduleModel->field('id', array('name' => $name));
if (empty($id)) {
$this->out('Module ' . $name . ' not found');
return;
}
$moduleModel->unplugModule($id, $moduleSetup);
$this->out('Module ' . $name . ' unplugged');
}
public function status() {
$name = $this->mandatoryArgument('name', 'use -name option');
$moduleModel = ClassRegistry::init('Module');
$status = $moduleModel->field('status', array('name' => $name));
if (empty($status)) {
$this->out('Module ' . $name . ' not found');
return;
}
$newStatus = ($status == 'on') ? 'off' : 'on';
$confirm = $this->in('Changing "' . $name . '" status from "' .
$status . '" to "' . $newStatus . '" confirm? [y/n]' );
if (empty($confirm) || strtolower($confirm) !== 'y') {
$this->out('Module status unchanged');
return;
}
$id = $moduleModel->field('id', array('name' => $name));
$moduleModel->id = $id;
$moduleModel->saveField('status', $newStatus);
$this->out('Module status changed');
}
private function findPluginPath($pluginName) {
$res = null;
$pluginPaths = App::pluginPath($pluginName);
if(file_exists($pluginPaths . DS . "config")) {
$res = $pluginPaths;
}
return $res;
}
public function schema() {
$pluginName = !empty($this->params['name']) ? $this->params['name'] : null;
$pluginPath = $this->findPluginPath($pluginName);
if($pluginPath == null) {
$this->out("Plugin $pluginName not found");
return;
}
$configPath = $pluginPath . DS . "config" . DS;
$setupFile = $configPath . "bedita_module_setup.php";
if(!file_exists($setupFile)) {
$this->out("Plugin setup file for $pluginName not found");
return;
}
include($setupFile);
if(empty($moduleSetup["tables"])) {
$this->out("No tables defined for plugin $pluginName");
return;
}
App::import('Core','ConnectionManager');
$db =& ConnectionManager::getDataSource("default");
$options = array();
$tables = $moduleSetup["tables"];
$beSchema = ClassRegistry::init("BeSchema");
$conf = Configure::getInstance();
$modelPaths = App::path('models');
if (!in_array($pluginPath . DS . "model" . DS, $modelPaths)){
App::build(array("models" => $pluginPath . DS . "model" . DS));
}
foreach ($tables as $t) {
$modelName = Inflector::camelize($t);
$model = ClassRegistry::init($modelName);
$options["tables"][$t] = $beSchema->tableMetadata($model, $db);
ClassRegistry::removeObject($modelName);
}
$schemaFile = $configPath . "sql". DS . "schema.php";
$skip = false;
if(file_exists($schemaFile)) {
$command = $this->in("Schema file $schemaFile exists. Overwrite?", array("y", "n"), "y");
if ($command == "n") {
$skip = true;
$this->out("Skipping schema file generation");
}
}
if(!$skip) {
$this->out("Creating schema file: $schemaFile");
$name = Inflector::camelize($pluginName);
$options['name'] = $name;
$options['path'] = $configPath . "sql";
$beSchema->path = $options['path'];
$beSchema->write($options);
}
require_once($schemaFile);
$schemaName = $name . "Schema";
$schema = new $schemaName();
$sqlSchema = $configPath . "sql". DS . $db->config["driver"] . "_schema.sql";
$skip = false;
if(file_exists($sqlSchema)) {
$command = $this->in("Schema file $sqlSchema exists. Overwrite?", array("y", "n"), "y");
if ($command == "n") {
$skip = true;
$this->out("Skipping schema file generation");
}
}
if(!$skip) {
$this->out("Creating schema file: $sqlSchema");
$contents = "#" . $schema->name . " sql generated on: " . date('Y-m-d H:i:s') . " : " . time() . "\n\n";
$contents .= $db->dropSchema($schema) . "\n\n". $db->createSchema($schema);
$file = new File($sqlSchema, true);
$file->write($contents);
}
$this->out("Done");
}
function help() {
$this->out('Available functions:');
$this->out(' ');
$this->out('0. plug: plug a new module plugin');
$this->out(' Usage: plug -name <module-plugin-name>');
$this->out(' ');
$this->out(" -name <module-plugin-name> \t name of plugin you want to install");
$this->out(' ');
$this->out('1. unplug: unplug module');
$this->out(' Usage: unplug -name <module-plugin-name>');
$this->out(' ');
$this->out(" -name <module-plugin-name> \t plugin name to unplug");
$this->out(' ');
$this->out('2. schema: generate schema files for a plugin');
$this->out(' Usage: schema [-list] [-name <module-plugin-name>]');
$this->out(' ');
$this->out(" -name <module-plugin-name> \t plugin name");
$this->out(' ');
$this->out('3. show: list all available modules and install selected plugin');
$this->out(' ');
$this->out('4. status: change module status');
$this->out(' Usage: status -name <module-plugin-name>');
$this->out(' ');
$this->out(" -name <module-plugin-name> \t plugin name to activate/deactivate");
$this->out(' ');
}
}
?>