/usr/share/php/Cake/Model/CakeSchema.php is in cakephp 2.8.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | <?php
/**
* Schema database management for CakePHP.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 1.2.0.5550
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Model', 'Model');
App::uses('AppModel', 'Model');
App::uses('ConnectionManager', 'Model');
App::uses('File', 'Utility');
/**
* Base Class for Schema management.
*
* @package Cake.Model
*/
class CakeSchema extends Object {
/**
* Name of the schema.
*
* @var string
*/
public $name = null;
/**
* Path to write location.
*
* @var string
*/
public $path = null;
/**
* File to write.
*
* @var string
*/
public $file = 'schema.php';
/**
* Connection used for read.
*
* @var string
*/
public $connection = 'default';
/**
* Plugin name.
*
* @var string
*/
public $plugin = null;
/**
* Set of tables.
*
* @var array
*/
public $tables = array();
/**
* Constructor
*
* @param array $options Optional load object properties.
*/
public function __construct($options = array()) {
parent::__construct();
if (empty($options['name'])) {
$this->name = preg_replace('/schema$/i', '', get_class($this));
}
if (!empty($options['plugin'])) {
$this->plugin = $options['plugin'];
}
if (strtolower($this->name) === 'cake') {
$this->name = 'App';
}
if (empty($options['path'])) {
$this->path = APP . 'Config' . DS . 'Schema';
}
$options = array_merge(get_object_vars($this), $options);
$this->build($options);
}
/**
* Builds schema object properties.
*
* @param array $data Loaded object properties.
* @return void
*/
public function build($data) {
$file = null;
foreach ($data as $key => $val) {
if (!empty($val)) {
if (!in_array($key, array('plugin', 'name', 'path', 'file', 'connection', 'tables', '_log'))) {
if ($key[0] === '_') {
continue;
}
$this->tables[$key] = $val;
unset($this->{$key});
} elseif ($key !== 'tables') {
if ($key === 'name' && $val !== $this->name && !isset($data['file'])) {
$file = Inflector::underscore($val) . '.php';
}
$this->{$key} = $val;
}
}
}
if (file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) {
$this->file = $file;
} elseif (!empty($this->plugin)) {
$this->path = CakePlugin::path($this->plugin) . 'Config' . DS . 'Schema';
}
}
/**
* Before callback to be implemented in subclasses.
*
* @param array $event Schema object properties.
* @return bool Should process continue.
*/
public function before($event = array()) {
return true;
}
/**
* After callback to be implemented in subclasses.
*
* @param array $event Schema object properties.
* @return void
*/
public function after($event = array()) {
}
/**
* Reads database and creates schema tables.
*
* @param array $options Schema object properties.
* @return array Set of name and tables.
*/
public function load($options = array()) {
if (is_string($options)) {
$options = array('path' => $options);
}
$this->build($options);
extract(get_object_vars($this));
$class = $name . 'Schema';
if (!class_exists($class) && !$this->_requireFile($path, $file)) {
$class = Inflector::camelize(Inflector::slug(Configure::read('App.dir'))) . 'Schema';
if (!class_exists($class)) {
$this->_requireFile($path, $file);
}
}
if (class_exists($class)) {
$Schema = new $class($options);
return $Schema;
}
return false;
}
/**
* Reads database and creates schema tables.
*
* Options
*
* - 'connection' - the db connection to use
* - 'name' - name of the schema
* - 'models' - a list of models to use, or false to ignore models
*
* @param array $options Schema object properties.
* @return array Array indexed by name and tables.
*/
public function read($options = array()) {
extract(array_merge(
array(
'connection' => $this->connection,
'name' => $this->name,
'models' => true,
),
$options
));
$db = ConnectionManager::getDataSource($connection);
if (isset($this->plugin)) {
App::uses($this->plugin . 'AppModel', $this->plugin . '.Model');
}
$tables = array();
$currentTables = (array)$db->listSources();
$prefix = null;
if (isset($db->config['prefix'])) {
$prefix = $db->config['prefix'];
}
if (!is_array($models) && $models !== false) {
if (isset($this->plugin)) {
$models = App::objects($this->plugin . '.Model', null, false);
} else {
$models = App::objects('Model');
}
}
if (is_array($models)) {
foreach ($models as $model) {
$importModel = $model;
$plugin = null;
if ($model === 'AppModel') {
continue;
}
if (isset($this->plugin)) {
if ($model === $this->plugin . 'AppModel') {
continue;
}
$importModel = $model;
$plugin = $this->plugin . '.';
}
App::uses($importModel, $plugin . 'Model');
if (!class_exists($importModel)) {
continue;
}
$vars = get_class_vars($model);
if (empty($vars['useDbConfig']) || $vars['useDbConfig'] != $connection) {
continue;
}
try {
$Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection));
} catch (CakeException $e) {
continue;
}
if (!is_object($Object) || $Object->useTable === false) {
continue;
}
$db = $Object->getDataSource();
$fulltable = $table = $db->fullTableName($Object, false, false);
if ($prefix && strpos($table, $prefix) !== 0) {
continue;
}
if (!in_array($fulltable, $currentTables)) {
continue;
}
$table = $this->_noPrefixTable($prefix, $table);
$key = array_search($fulltable, $currentTables);
if (empty($tables[$table])) {
$tables[$table] = $this->_columns($Object);
$tables[$table]['indexes'] = $db->index($Object);
$tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
unset($currentTables[$key]);
}
if (empty($Object->hasAndBelongsToMany)) {
continue;
}
foreach ($Object->hasAndBelongsToMany as $assocData) {
if (isset($assocData['with'])) {
$class = $assocData['with'];
}
if (!is_object($Object->$class)) {
continue;
}
$withTable = $db->fullTableName($Object->$class, false, false);
if ($prefix && strpos($withTable, $prefix) !== 0) {
continue;
}
if (in_array($withTable, $currentTables)) {
$key = array_search($withTable, $currentTables);
$noPrefixWith = $this->_noPrefixTable($prefix, $withTable);
$tables[$noPrefixWith] = $this->_columns($Object->$class);
$tables[$noPrefixWith]['indexes'] = $db->index($Object->$class);
$tables[$noPrefixWith]['tableParameters'] = $db->readTableParameters($withTable);
unset($currentTables[$key]);
}
}
}
}
if (!empty($currentTables)) {
foreach ($currentTables as $table) {
if ($prefix) {
if (strpos($table, $prefix) !== 0) {
continue;
}
$table = $this->_noPrefixTable($prefix, $table);
}
$Object = new AppModel(array(
'name' => Inflector::classify($table), 'table' => $table, 'ds' => $connection
));
$systemTables = array(
'aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n'
);
$fulltable = $db->fullTableName($Object, false, false);
if (in_array($table, $systemTables)) {
$tables[$Object->table] = $this->_columns($Object);
$tables[$Object->table]['indexes'] = $db->index($Object);
$tables[$Object->table]['tableParameters'] = $db->readTableParameters($fulltable);
} elseif ($models === false) {
$tables[$table] = $this->_columns($Object);
$tables[$table]['indexes'] = $db->index($Object);
$tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
} else {
$tables['missing'][$table] = $this->_columns($Object);
$tables['missing'][$table]['indexes'] = $db->index($Object);
$tables['missing'][$table]['tableParameters'] = $db->readTableParameters($fulltable);
}
}
}
ksort($tables);
return compact('name', 'tables');
}
/**
* Writes schema file from object or options.
*
* @param array|object $object Schema object or options array.
* @param array $options Schema object properties to override object.
* @return mixed False or string written to file.
*/
public function write($object, $options = array()) {
if (is_object($object)) {
$object = get_object_vars($object);
$this->build($object);
}
if (is_array($object)) {
$options = $object;
unset($object);
}
extract(array_merge(
get_object_vars($this), $options
));
$out = "class {$name}Schema extends CakeSchema {\n\n";
if ($path !== $this->path) {
$out .= "\tpublic \$path = '{$path}';\n\n";
}
if ($file !== $this->file) {
$out .= "\tpublic \$file = '{$file}';\n\n";
}
if ($connection !== 'default') {
$out .= "\tpublic \$connection = '{$connection}';\n\n";
}
$out .= "\tpublic function before(\$event = array()) {\n\t\treturn true;\n\t}\n\n\tpublic function after(\$event = array()) {\n\t}\n\n";
if (empty($tables)) {
$this->read();
}
foreach ($tables as $table => $fields) {
if (!is_numeric($table) && $table !== 'missing') {
$out .= $this->generateTable($table, $fields);
}
}
$out .= "}\n";
$file = new File($path . DS . $file, true);
$content = "<?php \n{$out}";
if ($file->write($content)) {
return $content;
}
return false;
}
/**
* Generate the schema code for a table.
*
* Takes a table name and $fields array and returns a completed,
* escaped variable declaration to be used in schema classes.
*
* @param string $table Table name you want returned.
* @param array $fields Array of field information to generate the table with.
* @return string Variable declaration for a schema class.
*/
public function generateTable($table, $fields) {
$out = "\tpublic \${$table} = array(\n";
if (is_array($fields)) {
$cols = array();
foreach ($fields as $field => $value) {
if ($field !== 'indexes' && $field !== 'tableParameters') {
if (is_string($value)) {
$type = $value;
$value = array('type' => $type);
}
$col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
unset($value['type']);
$col .= implode(', ', $this->_values($value));
} elseif ($field === 'indexes') {
$col = "\t\t'indexes' => array(\n\t\t\t";
$props = array();
foreach ((array)$value as $key => $index) {
$props[] = "'{$key}' => array(" . implode(', ', $this->_values($index)) . ")";
}
$col .= implode(",\n\t\t\t", $props) . "\n\t\t";
} elseif ($field === 'tableParameters') {
$col = "\t\t'tableParameters' => array(";
$props = $this->_values($value);
$col .= implode(', ', $props);
}
$col .= ")";
$cols[] = $col;
}
$out .= implode(",\n", $cols);
}
$out .= "\n\t);\n\n";
return $out;
}
/**
* Compares two sets of schemas.
*
* @param array|object $old Schema object or array.
* @param array|object $new Schema object or array.
* @return array Tables (that are added, dropped, or changed.)
*/
public function compare($old, $new = null) {
if (empty($new)) {
$new = $this;
}
if (is_array($new)) {
if (isset($new['tables'])) {
$new = $new['tables'];
}
} else {
$new = $new->tables;
}
if (is_array($old)) {
if (isset($old['tables'])) {
$old = $old['tables'];
}
} else {
$old = $old->tables;
}
$tables = array();
foreach ($new as $table => $fields) {
if ($table === 'missing') {
continue;
}
if (!array_key_exists($table, $old)) {
$tables[$table]['create'] = $fields;
} else {
$diff = $this->_arrayDiffAssoc($fields, $old[$table]);
if (!empty($diff)) {
$tables[$table]['add'] = $diff;
}
$diff = $this->_arrayDiffAssoc($old[$table], $fields);
if (!empty($diff)) {
$tables[$table]['drop'] = $diff;
}
}
foreach ($fields as $field => $value) {
if (!empty($old[$table][$field])) {
$diff = $this->_arrayDiffAssoc($value, $old[$table][$field]);
if (!empty($diff) && $field !== 'indexes' && $field !== 'tableParameters') {
$tables[$table]['change'][$field] = $value;
}
}
if (isset($tables[$table]['add'][$field]) && $field !== 'indexes' && $field !== 'tableParameters') {
$wrapper = array_keys($fields);
if ($column = array_search($field, $wrapper)) {
if (isset($wrapper[$column - 1])) {
$tables[$table]['add'][$field]['after'] = $wrapper[$column - 1];
}
}
}
}
if (isset($old[$table]['indexes']) && isset($new[$table]['indexes'])) {
$diff = $this->_compareIndexes($new[$table]['indexes'], $old[$table]['indexes']);
if ($diff) {
if (!isset($tables[$table])) {
$tables[$table] = array();
}
if (isset($diff['drop'])) {
$tables[$table]['drop']['indexes'] = $diff['drop'];
}
if ($diff && isset($diff['add'])) {
$tables[$table]['add']['indexes'] = $diff['add'];
}
}
}
if (isset($old[$table]['tableParameters']) && isset($new[$table]['tableParameters'])) {
$diff = $this->_compareTableParameters($new[$table]['tableParameters'], $old[$table]['tableParameters']);
if ($diff) {
$tables[$table]['change']['tableParameters'] = $diff;
}
}
}
return $tables;
}
/**
* Extended array_diff_assoc noticing change from/to NULL values.
*
* It behaves almost the same way as array_diff_assoc except for NULL values: if
* one of the values is not NULL - change is detected. It is useful in situation
* where one value is strval('') ant other is strval(null) - in string comparing
* methods this results as EQUAL, while it is not.
*
* @param array $array1 Base array.
* @param array $array2 Corresponding array checked for equality.
* @return array Difference as array with array(keys => values) from input array
* where match was not found.
*/
protected function _arrayDiffAssoc($array1, $array2) {
$difference = array();
foreach ($array1 as $key => $value) {
if (!array_key_exists($key, $array2)) {
$difference[$key] = $value;
continue;
}
$correspondingValue = $array2[$key];
if (($value === null) !== ($correspondingValue === null)) {
$difference[$key] = $value;
continue;
}
if (is_bool($value) !== is_bool($correspondingValue)) {
$difference[$key] = $value;
continue;
}
if (is_array($value) && is_array($correspondingValue)) {
continue;
}
if ($value === $correspondingValue) {
continue;
}
$difference[$key] = $value;
}
return $difference;
}
/**
* Formats Schema columns from Model Object.
*
* @param array $values Options keys(type, null, default, key, length, extra).
* @return array Formatted values.
*/
protected function _values($values) {
$vals = array();
if (is_array($values)) {
foreach ($values as $key => $val) {
if (is_array($val)) {
$vals[] = "'{$key}' => array(" . implode(", ", $this->_values($val)) . ")";
} else {
$val = var_export($val, true);
if ($val === 'NULL') {
$val = 'null';
}
if (!is_numeric($key)) {
$vals[] = "'{$key}' => {$val}";
} else {
$vals[] = "{$val}";
}
}
}
}
return $vals;
}
/**
* Formats Schema columns from Model Object.
*
* @param array &$Obj model object.
* @return array Formatted columns.
*/
protected function _columns(&$Obj) {
$db = $Obj->getDataSource();
$fields = $Obj->schema(true);
$columns = array();
foreach ($fields as $name => $value) {
if ($Obj->primaryKey === $name) {
$value['key'] = 'primary';
}
if (!isset($db->columns[$value['type']])) {
trigger_error(__d('cake_dev', 'Schema generation error: invalid column type %s for %s.%s does not exist in DBO', $value['type'], $Obj->name, $name), E_USER_NOTICE);
continue;
} else {
$defaultCol = $db->columns[$value['type']];
if (isset($defaultCol['limit']) && $defaultCol['limit'] == $value['length']) {
unset($value['length']);
} elseif (isset($defaultCol['length']) && $defaultCol['length'] == $value['length']) {
unset($value['length']);
}
unset($value['limit']);
}
if (isset($value['default']) && ($value['default'] === '' || ($value['default'] === false && $value['type'] !== 'boolean'))) {
unset($value['default']);
}
if (empty($value['length'])) {
unset($value['length']);
}
if (empty($value['key'])) {
unset($value['key']);
}
$columns[$name] = $value;
}
return $columns;
}
/**
* Compare two schema files table Parameters.
*
* @param array $new New indexes.
* @param array $old Old indexes.
* @return mixed False on failure, or an array of parameters to add & drop.
*/
protected function _compareTableParameters($new, $old) {
if (!is_array($new) || !is_array($old)) {
return false;
}
$change = $this->_arrayDiffAssoc($new, $old);
return $change;
}
/**
* Compare two schema indexes.
*
* @param array $new New indexes.
* @param array $old Old indexes.
* @return mixed False on failure or array of indexes to add and drop.
*/
protected function _compareIndexes($new, $old) {
if (!is_array($new) || !is_array($old)) {
return false;
}
$add = $drop = array();
$diff = $this->_arrayDiffAssoc($new, $old);
if (!empty($diff)) {
$add = $diff;
}
$diff = $this->_arrayDiffAssoc($old, $new);
if (!empty($diff)) {
$drop = $diff;
}
foreach ($new as $name => $value) {
if (isset($old[$name])) {
$newUnique = isset($value['unique']) ? $value['unique'] : 0;
$oldUnique = isset($old[$name]['unique']) ? $old[$name]['unique'] : 0;
$newColumn = $value['column'];
$oldColumn = $old[$name]['column'];
$diff = false;
if ($newUnique != $oldUnique) {
$diff = true;
} elseif (is_array($newColumn) && is_array($oldColumn)) {
$diff = ($newColumn !== $oldColumn);
} elseif (is_string($newColumn) && is_string($oldColumn)) {
$diff = ($newColumn != $oldColumn);
} else {
$diff = true;
}
if ($diff) {
$drop[$name] = null;
$add[$name] = $value;
}
}
}
return array_filter(compact('add', 'drop'));
}
/**
* Trim the table prefix from the full table name, and return the prefix-less
* table.
*
* @param string $prefix Table prefix.
* @param string $table Full table name.
* @return string Prefix-less table name.
*/
protected function _noPrefixTable($prefix, $table) {
return preg_replace('/^' . preg_quote($prefix) . '/', '', $table);
}
/**
* Attempts to require the schema file specified.
*
* @param string $path Filesystem path to the file.
* @param string $file Filesystem basename of the file.
* @return bool True when a file was successfully included, false on failure.
*/
protected function _requireFile($path, $file) {
if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
require_once $path . DS . $file;
return true;
} elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
require_once $path . DS . 'schema.php';
return true;
}
return false;
}
}
|