1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18: 
 19: 
 20: App::uses('Model', 'Model');
 21: App::uses('AppModel', 'Model');
 22: App::uses('ConnectionManager', 'Model');
 23: 
 24:  25:  26:  27:  28: 
 29: class CakeSchema extends Object {
 30: 
 31:  32:  33:  34:  35: 
 36:     public $name = null;
 37: 
 38:  39:  40:  41:  42: 
 43:     public $path = null;
 44: 
 45:  46:  47:  48:  49: 
 50:     public $file = 'schema.php';
 51: 
 52:  53:  54:  55:  56: 
 57:     public $connection = 'default';
 58: 
 59:  60:  61:  62:  63: 
 64:     public $plugin = null;
 65: 
 66:  67:  68:  69:  70: 
 71:     public $tables = array();
 72: 
 73:  74:  75:  76:  77: 
 78:     public function __construct($options = array()) {
 79:         parent::__construct();
 80: 
 81:         if (empty($options['name'])) {
 82:             $this->name = preg_replace('/schema$/i', '', get_class($this));
 83:         }
 84:         if (!empty($options['plugin'])) {
 85:             $this->plugin = $options['plugin'];
 86:         }
 87: 
 88:         if (strtolower($this->name) === 'cake') {
 89:             $this->name = Inflector::camelize(Inflector::slug(Configure::read('App.dir')));
 90:         }
 91: 
 92:         if (empty($options['path'])) {
 93:             $this->path = APP . 'Config' . DS . 'Schema';
 94:         }
 95: 
 96:         $options = array_merge(get_object_vars($this), $options);
 97:         $this->build($options);
 98:     }
 99: 
100: 101: 102: 103: 104: 105: 
106:     public function build($data) {
107:         $file = null;
108:         foreach ($data as $key => $val) {
109:             if (!empty($val)) {
110:                 if (!in_array($key, array('plugin', 'name', 'path', 'file', 'connection', 'tables', '_log'))) {
111:                     if ($key[0] === '_') {
112:                         continue;
113:                     }
114:                     $this->tables[$key] = $val;
115:                     unset($this->{$key});
116:                 } elseif ($key !== 'tables') {
117:                     if ($key === 'name' && $val !== $this->name && !isset($data['file'])) {
118:                         $file = Inflector::underscore($val) . '.php';
119:                     }
120:                     $this->{$key} = $val;
121:                 }
122:             }
123:         }
124:         if (file_exists($this->path . DS . $file) && is_file($this->path . DS . $file)) {
125:             $this->file = $file;
126:         } elseif (!empty($this->plugin)) {
127:             $this->path = CakePlugin::path($this->plugin) . 'Config' . DS . 'Schema';
128:         }
129:     }
130: 
131: 132: 133: 134: 135: 136: 
137:     public function before($event = array()) {
138:         return true;
139:     }
140: 
141: 142: 143: 144: 145: 146: 
147:     public function after($event = array()) {
148:     }
149: 
150: 151: 152: 153: 154: 155: 
156:     public function load($options = array()) {
157:         if (is_string($options)) {
158:             $options = array('path' => $options);
159:         }
160: 
161:         $this->build($options);
162:         extract(get_object_vars($this));
163: 
164:         $class =  $name .'Schema';
165: 
166:         if (!class_exists($class)) {
167:             if (file_exists($path . DS . $file) && is_file($path . DS . $file)) {
168:                 require_once($path . DS . $file);
169:             } elseif (file_exists($path . DS . 'schema.php') && is_file($path . DS . 'schema.php')) {
170:                 require_once($path . DS . 'schema.php');
171:             }
172:         }
173: 
174:         if (class_exists($class)) {
175:             $Schema = new $class($options);
176:             return $Schema;
177:         }
178: 
179:         return false;
180:     }
181: 
182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 
194:     public function read($options = array()) {
195:         extract(array_merge(
196:             array(
197:                 'connection' => $this->connection,
198:                 'name' => $this->name,
199:                 'models' => true,
200:             ),
201:             $options
202:         ));
203:         $db = ConnectionManager::getDataSource($connection);
204: 
205:         if (isset($this->plugin)) {
206:             App::uses($this->plugin . 'AppModel', $this->plugin . '.Model');
207:         }
208: 
209:         $tables = array();
210:         $currentTables = $db->listSources();
211: 
212:         $prefix = null;
213:         if (isset($db->config['prefix'])) {
214:             $prefix = $db->config['prefix'];
215:         }
216: 
217:         if (!is_array($models) && $models !== false) {
218:             if (isset($this->plugin)) {
219:                 $models = App::objects($this->plugin . '.Model', null, false);
220:             } else {
221:                 $models = App::objects('Model');
222:             }
223:         }
224: 
225:         if (is_array($models)) {
226:             foreach ($models as $model) {
227:                 $importModel = $model;
228:                 $plugin = null;
229:                 if ($model == 'AppModel') {
230:                     continue;
231:                 }
232: 
233:                 if (isset($this->plugin)) {
234:                     if ($model == $this->plugin . 'AppModel') {
235:                         continue;
236:                     }
237:                     $importModel = $model;
238:                     $plugin = $this->plugin . '.';
239:                 }
240: 
241:                 App::uses($importModel, $plugin . 'Model');
242:                 if (!class_exists($importModel)) {
243:                     continue;
244:                 }
245: 
246:                 $vars = get_class_vars($model);
247:                 if (empty($vars['useDbConfig']) || $vars['useDbConfig'] != $connection) {
248:                     continue;
249:                 }
250: 
251:                 try {
252:                     $Object = ClassRegistry::init(array('class' => $model, 'ds' => $connection));
253:                 } catch (CakeException $e) {
254:                     continue;
255:                 }
256: 
257:                 $db = $Object->getDataSource();
258:                 if (is_object($Object) && $Object->useTable !== false) {
259:                     $fulltable = $table = $db->fullTableName($Object, false);
260:                     if ($prefix && strpos($table, $prefix) !== 0) {
261:                         continue;
262:                     }
263:                     $table = $this->_noPrefixTable($prefix, $table);
264: 
265:                     if (in_array($fulltable, $currentTables)) {
266:                         $key = array_search($fulltable, $currentTables);
267:                         if (empty($tables[$table])) {
268:                             $tables[$table] = $this->_columns($Object);
269:                             $tables[$table]['indexes'] = $db->index($Object);
270:                             $tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
271:                             unset($currentTables[$key]);
272:                         }
273:                         if (!empty($Object->hasAndBelongsToMany)) {
274:                             foreach ($Object->hasAndBelongsToMany as $Assoc => $assocData) {
275:                                 if (isset($assocData['with'])) {
276:                                     $class = $assocData['with'];
277:                                 }
278:                                 if (is_object($Object->$class)) {
279:                                     $withTable = $db->fullTableName($Object->$class, false);
280:                                     if ($prefix && strpos($withTable, $prefix) !== 0) {
281:                                         continue;
282:                                     }
283:                                     if (in_array($withTable, $currentTables)) {
284:                                         $key = array_search($withTable, $currentTables);
285:                                         $noPrefixWith = $this->_noPrefixTable($prefix, $withTable);
286: 
287:                                         $tables[$noPrefixWith] = $this->_columns($Object->$class);
288:                                         $tables[$noPrefixWith]['indexes'] = $db->index($Object->$class);
289:                                         $tables[$noPrefixWith]['tableParameters'] = $db->readTableParameters($withTable);
290:                                         unset($currentTables[$key]);
291:                                     }
292:                                 }
293:                             }
294:                         }
295:                     }
296:                 }
297:             }
298:         }
299: 
300:         if (!empty($currentTables)) {
301:             foreach ($currentTables as $table) {
302:                 if ($prefix) {
303:                     if (strpos($table, $prefix) !== 0) {
304:                         continue;
305:                     }
306:                     $table = $this->_noPrefixTable($prefix, $table);
307:                 }
308:                 $Object = new AppModel(array(
309:                     'name' => Inflector::classify($table), 'table' => $table, 'ds' => $connection
310:                 ));
311: 
312:                 $systemTables = array(
313:                     'aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n'
314:                 );
315: 
316:                 $fulltable = $db->fullTableName($Object, false);
317: 
318:                 if (in_array($table, $systemTables)) {
319:                     $tables[$Object->table] = $this->_columns($Object);
320:                     $tables[$Object->table]['indexes'] = $db->index($Object);
321:                     $tables[$Object->table]['tableParameters'] = $db->readTableParameters($fulltable);
322:                 } elseif ($models === false) {
323:                     $tables[$table] = $this->_columns($Object);
324:                     $tables[$table]['indexes'] = $db->index($Object);
325:                     $tables[$table]['tableParameters'] = $db->readTableParameters($fulltable);
326:                 } else {
327:                     $tables['missing'][$table] = $this->_columns($Object);
328:                     $tables['missing'][$table]['indexes'] = $db->index($Object);
329:                     $tables['missing'][$table]['tableParameters'] = $db->readTableParameters($fulltable);
330:                 }
331:             }
332:         }
333: 
334:         ksort($tables);
335:         return compact('name', 'tables');
336:     }
337: 
338: 339: 340: 341: 342: 343: 344: 
345:     public function write($object, $options = array()) {
346:         if (is_object($object)) {
347:             $object = get_object_vars($object);
348:             $this->build($object);
349:         }
350: 
351:         if (is_array($object)) {
352:             $options = $object;
353:             unset($object);
354:         }
355: 
356:         extract(array_merge(
357:             get_object_vars($this), $options
358:         ));
359: 
360:         $out = "class {$name}Schema extends CakeSchema {\n\n";
361: 
362:         if ($path !== $this->path) {
363:             $out .= "\tpublic \$path = '{$path}';\n\n";
364:         }
365: 
366:         if ($file !== $this->file) {
367:             $out .= "\tpublic \$file = '{$file}';\n\n";
368:         }
369: 
370:         if ($connection !== 'default') {
371:             $out .= "\tpublic \$connection = '{$connection}';\n\n";
372:         }
373: 
374:         $out .= "\tpublic function before(\$event = array()) {\n\t\treturn true;\n\t}\n\n\tpublic function after(\$event = array()) {\n\t}\n\n";
375: 
376:         if (empty($tables)) {
377:             $this->read();
378:         }
379: 
380:         foreach ($tables as $table => $fields) {
381:             if (!is_numeric($table) && $table !== 'missing') {
382:                 $out .= $this->generateTable($table, $fields);
383:             }
384:         }
385:         $out .= "}\n";
386: 
387:         $file = new SplFileObject($path . DS . $file, 'w+');
388:         $content = "<?php \n/* generated on: " . date('Y-m-d H:i:s') . " : ". time() . " */\n{$out}";
389:         if ($file->fwrite($content)) {
390:             return $content;
391:         }
392:         return false;
393:     }
394: 
395: 396: 397: 398: 399: 400: 401: 402: 
403:     public function generateTable($table, $fields) {
404:         $out = "\tpublic \${$table} = array(\n";
405:         if (is_array($fields)) {
406:             $cols = array();
407:             foreach ($fields as $field => $value) {
408:                 if ($field != 'indexes' && $field != 'tableParameters') {
409:                     if (is_string($value)) {
410:                         $type = $value;
411:                         $value = array('type' => $type);
412:                     }
413:                     $col = "\t\t'{$field}' => array('type' => '" . $value['type'] . "', ";
414:                     unset($value['type']);
415:                     $col .= join(', ',  $this->_values($value));
416:                 } elseif ($field == 'indexes') {
417:                     $col = "\t\t'indexes' => array(";
418:                     $props = array();
419:                     foreach ((array)$value as $key => $index) {
420:                         $props[] = "'{$key}' => array(" . join(', ',  $this->_values($index)) . ")";
421:                     }
422:                     $col .= join(', ', $props);
423:                 } elseif ($field == 'tableParameters') {
424:                     $col = "\t\t'tableParameters' => array(";
425:                     $props = array();
426:                     foreach ((array)$value as $key => $param) {
427:                         $props[] = "'{$key}' => '$param'";
428:                     }
429:                     $col .= join(', ', $props);
430:                 }
431:                 $col .= ")";
432:                 $cols[] = $col;
433:             }
434:             $out .= join(",\n", $cols);
435:         }
436:         $out .= "\n\t);\n";
437:         return $out;
438:     }
439: 
440: 441: 442: 443: 444: 445: 446: 
447:     public function compare($old, $new = null) {
448:         if (empty($new)) {
449:             $new = $this;
450:         }
451:         if (is_array($new)) {
452:             if (isset($new['tables'])) {
453:                 $new = $new['tables'];
454:             }
455:         } else {
456:             $new = $new->tables;
457:         }
458: 
459:         if (is_array($old)) {
460:             if (isset($old['tables'])) {
461:                 $old = $old['tables'];
462:             }
463:         } else {
464:             $old = $old->tables;
465:         }
466:         $tables = array();
467:         foreach ($new as $table => $fields) {
468:             if ($table == 'missing') {
469:                 continue;
470:             }
471:             if (!array_key_exists($table, $old)) {
472:                 $tables[$table]['add'] = $fields;
473:             } else {
474:                 $diff = $this->_arrayDiffAssoc($fields, $old[$table]);
475:                 if (!empty($diff)) {
476:                     $tables[$table]['add'] = $diff;
477:                 }
478:                 $diff = $this->_arrayDiffAssoc($old[$table], $fields);
479:                 if (!empty($diff)) {
480:                     $tables[$table]['drop'] = $diff;
481:                 }
482:             }
483: 
484:             foreach ($fields as $field => $value) {
485:                 if (!empty($old[$table][$field])) {
486:                     $diff = $this->_arrayDiffAssoc($value, $old[$table][$field]);
487:                     if (!empty($diff) && $field !== 'indexes' && $field !== 'tableParameters') {
488:                         $tables[$table]['change'][$field] = array_merge($old[$table][$field], $diff);
489:                     }
490:                 }
491: 
492:                 if (isset($tables[$table]['add'][$field]) && $field !== 'indexes' && $field !== 'tableParameters') {
493:                     $wrapper = array_keys($fields);
494:                     if ($column = array_search($field, $wrapper)) {
495:                         if (isset($wrapper[$column - 1])) {
496:                             $tables[$table]['add'][$field]['after'] = $wrapper[$column - 1];
497:                         }
498:                     }
499:                 }
500:             }
501: 
502:             if (isset($old[$table]['indexes']) && isset($new[$table]['indexes'])) {
503:                 $diff = $this->_compareIndexes($new[$table]['indexes'], $old[$table]['indexes']);
504:                 if ($diff) {
505:                     if (!isset($tables[$table])) {
506:                         $tables[$table] = array();
507:                     }
508:                     if (isset($diff['drop'])) {
509:                         $tables[$table]['drop']['indexes'] = $diff['drop'];
510:                     }
511:                     if ($diff && isset($diff['add'])) {
512:                         $tables[$table]['add']['indexes'] = $diff['add'];
513:                     }
514:                 }
515:             }
516:             if (isset($old[$table]['tableParameters']) && isset($new[$table]['tableParameters'])) {
517:                 $diff = $this->_compareTableParameters($new[$table]['tableParameters'], $old[$table]['tableParameters']);
518:                 if ($diff) {
519:                     $tables[$table]['change']['tableParameters'] = $diff;
520:                 }
521:             }
522:         }
523:         return $tables;
524:     }
525: 
526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 
539:     protected function _arrayDiffAssoc($array1, $array2) {
540:         $difference = array();
541:         foreach ($array1 as $key => $value) {
542:             if (!array_key_exists($key, $array2)) {
543:                 $difference[$key] = $value;
544:                 continue;
545:             }
546:             $correspondingValue = $array2[$key];
547:             if (is_null($value) !== is_null($correspondingValue)) {
548:                 $difference[$key] = $value;
549:                 continue;
550:             }
551:             if (is_bool($value) !== is_bool($correspondingValue)) {
552:                 $difference[$key] = $value;
553:                 continue;
554:             }
555:             if (is_array($value) && is_array($correspondingValue)) {
556:                 continue;
557:             }
558:             if ($value === $correspondingValue) {
559:                 continue;
560:             }
561:             $difference[$key] = $value;
562:         }
563:         return $difference;
564:     }
565: 
566: 567: 568: 569: 570: 571: 
572:     protected function _values($values) {
573:         $vals = array();
574:         if (is_array($values)) {
575:             foreach ($values as $key => $val) {
576:                 if (is_array($val)) {
577:                     $vals[] = "'{$key}' => array('" . implode("', '",  $val) . "')";
578:                 } else if (!is_numeric($key)) {
579:                     $val = var_export($val, true);
580:                     $vals[] = "'{$key}' => {$val}";
581:                 }
582:             }
583:         }
584:         return $vals;
585:     }
586: 
587: 588: 589: 590: 591: 592: 
593:     protected function _columns(&$Obj) {
594:         $db = $Obj->getDataSource();
595:         $fields = $Obj->schema(true);
596: 
597:         $columns = $props = array();
598:         foreach ($fields as $name => $value) {
599:             if ($Obj->primaryKey == $name) {
600:                 $value['key'] = 'primary';
601:             }
602:             if (!isset($db->columns[$value['type']])) {
603:                 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);
604:                 continue;
605:             } else {
606:                 $defaultCol = $db->columns[$value['type']];
607:                 if (isset($defaultCol['limit']) && $defaultCol['limit'] == $value['length']) {
608:                     unset($value['length']);
609:                 } elseif (isset($defaultCol['length']) && $defaultCol['length'] == $value['length']) {
610:                     unset($value['length']);
611:                 }
612:                 unset($value['limit']);
613:             }
614: 
615:             if (isset($value['default']) && ($value['default'] === '' || $value['default'] === false)) {
616:                 unset($value['default']);
617:             }
618:             if (empty($value['length'])) {
619:                 unset($value['length']);
620:             }
621:             if (empty($value['key'])) {
622:                 unset($value['key']);
623:             }
624:             $columns[$name] = $value;
625:         }
626: 
627:         return $columns;
628:     }
629: 
630: 631: 632: 633: 634: 635: 636: 
637:     protected function _compareTableParameters($new, $old) {
638:         if (!is_array($new) || !is_array($old)) {
639:             return false;
640:         }
641:         $change = $this->_arrayDiffAssoc($new, $old);
642:         return $change;
643:     }
644: 
645: 646: 647: 648: 649: 650: 651: 
652:     protected function _compareIndexes($new, $old) {
653:         if (!is_array($new) || !is_array($old)) {
654:             return false;
655:         }
656: 
657:         $add = $drop = array();
658: 
659:         $diff = $this->_arrayDiffAssoc($new, $old);
660:         if (!empty($diff)) {
661:             $add = $diff;
662:         }
663: 
664:         $diff = $this->_arrayDiffAssoc($old, $new);
665:         if (!empty($diff)) {
666:             $drop = $diff;
667:         }
668: 
669:         foreach ($new as $name => $value) {
670:             if (isset($old[$name])) {
671:                 $newUnique = isset($value['unique']) ? $value['unique'] : 0;
672:                 $oldUnique = isset($old[$name]['unique']) ? $old[$name]['unique'] : 0;
673:                 $newColumn = $value['column'];
674:                 $oldColumn = $old[$name]['column'];
675: 
676:                 $diff = false;
677: 
678:                 if ($newUnique != $oldUnique) {
679:                     $diff = true;
680:                 } elseif (is_array($newColumn) && is_array($oldColumn)) {
681:                     $diff = ($newColumn !== $oldColumn);
682:                 } elseif (is_string($newColumn) && is_string($oldColumn)) {
683:                     $diff = ($newColumn != $oldColumn);
684:                 } else {
685:                     $diff = true;
686:                 }
687:                 if ($diff) {
688:                     $drop[$name] = null;
689:                     $add[$name] = $value;
690:                 }
691:             }
692:         }
693:         return array_filter(compact('add', 'drop'));
694:     }
695: 
696: 697: 698: 699: 700: 701: 702: 
703:     protected function _noPrefixTable($prefix, $table) {
704:         return preg_replace('/^' . preg_quote($prefix) . '/', '', $table);
705:     }
706: }
707: