Merge pull request #4607 from samuelsov/CRM-15637
[civicrm-core.git] / CRM / Core / CodeGen / Specification.php
1 <?php
2
3 /**
4 * Read the schema specification and parse into internal data structures
5 */
6 class CRM_Core_CodeGen_Specification {
7 public $tables;
8 public $database;
9
10 protected $classNames;
11
12 /**
13 * Read and parse.
14 *
15 * @param $schemaPath
16 * @param string $buildVersion which version of the schema to build
17 */
18 public function parse($schemaPath, $buildVersion) {
19 $this->buildVersion = $buildVersion;
20
21 echo "Parsing schema description ".$schemaPath."\n";
22 $dbXML = CRM_Core_CodeGen_Util_Xml::parse($schemaPath);
23 // print_r( $dbXML );
24
25 echo "Extracting database information\n";
26 $this->database = &$this->getDatabase($dbXML);
27 // print_r( $this->database );
28
29 $this->classNames = array();
30
31 # TODO: peel DAO-specific stuff out of getTables, and spec reading into its own class
32 echo "Extracting table information\n";
33 $this->tables = $this->getTables($dbXML, $this->database);
34
35 $this->resolveForeignKeys($this->tables, $this->classNames);
36 $this->tables = $this->orderTables($this->tables);
37
38 // add archive tables here
39 $archiveTables = array( );
40 foreach ($this->tables as $name => $table ) {
41 if ( $table['archive'] == 'true' ) {
42 $name = 'archive_' . $table['name'];
43 $table['name'] = $name;
44 $table['archive'] = 'false';
45 if ( isset($table['foreignKey']) ) {
46 foreach ($table['foreignKey'] as $fkName => $fkValue) {
47 if ($this->tables[$fkValue['table']]['archive'] == 'true') {
48 $table['foreignKey'][$fkName]['table'] = 'archive_' . $table['foreignKey'][$fkName]['table'];
49 $table['foreignKey'][$fkName]['uniqName'] =
50 str_replace( 'FK_', 'FK_archive_', $table['foreignKey'][$fkName]['uniqName'] );
51 }
52 }
53 $archiveTables[$name] = $table;
54 }
55 }
56 }
57 }
58
59 /**
60 * @param $dbXML
61 *
62 * @return array
63 */
64 public function &getDatabase(&$dbXML) {
65 $database = array('name' => trim((string ) $dbXML->name));
66
67 $attributes = '';
68 $this->checkAndAppend($attributes, $dbXML, 'character_set', 'DEFAULT CHARACTER SET ', '');
69 $this->checkAndAppend($attributes, $dbXML, 'collate', 'COLLATE ', '');
70 $database['attributes'] = $attributes;
71
72 $tableAttributes_modern = $tableAttributes_simple = '';
73 $this->checkAndAppend($tableAttributes_modern, $dbXML, 'table_type', 'ENGINE=', '');
74 $this->checkAndAppend($tableAttributes_simple, $dbXML, 'table_type', 'TYPE=', '');
75 $database['tableAttributes_modern'] = trim($tableAttributes_modern . ' ' . $attributes);
76 $database['tableAttributes_simple'] = trim($tableAttributes_simple);
77
78 $database['comment'] = $this->value('comment', $dbXML, '');
79
80 return $database;
81 }
82
83 /**
84 * @param $dbXML
85 * @param $database
86 *
87 * @return array
88 */
89 public function getTables($dbXML, &$database) {
90 $tables = array();
91 foreach ($dbXML->tables as $tablesXML) {
92 foreach ($tablesXML->table as $tableXML) {
93 if ($this->value('drop', $tableXML, 0) > 0 and $this->value('drop', $tableXML, 0) <= $this->buildVersion) {
94 continue;
95 }
96
97 if ($this->value('add', $tableXML, 0) <= $this->buildVersion) {
98 $this->getTable($tableXML, $database, $tables);
99 }
100 }
101 }
102
103 return $tables;
104 }
105
106 /**
107 * @param $tables
108 * @param string $classNames
109 */
110 public function resolveForeignKeys(&$tables, &$classNames) {
111 foreach (array_keys($tables) as $name) {
112 $this->resolveForeignKey($tables, $classNames, $name);
113 }
114 }
115
116 /**
117 * @param $tables
118 * @param string $classNames
119 * @param string $name
120 */
121 public function resolveForeignKey(&$tables, &$classNames, $name) {
122 if (!array_key_exists('foreignKey', $tables[$name])) {
123 return;
124 }
125
126 foreach (array_keys($tables[$name]['foreignKey']) as $fkey) {
127 $ftable = $tables[$name]['foreignKey'][$fkey]['table'];
128 if (!array_key_exists($ftable, $classNames)) {
129 echo "$ftable is not a valid foreign key table in $name\n";
130 continue;
131 }
132 $tables[$name]['foreignKey'][$fkey]['className'] = $classNames[$ftable];
133 $tables[$name]['foreignKey'][$fkey]['fileName'] = str_replace('_', '/', $classNames[$ftable]) . '.php';
134 $tables[$name]['fields'][$fkey]['FKClassName'] = $classNames[$ftable];
135 }
136 }
137
138 /**
139 * @param $tables
140 *
141 * @return array
142 */
143 public function orderTables(&$tables) {
144 $ordered = array();
145
146 while (!empty($tables)) {
147 foreach (array_keys($tables) as $name) {
148 if ($this->validTable($tables, $ordered, $name)) {
149 $ordered[$name] = $tables[$name];
150 unset($tables[$name]);
151 }
152 }
153 }
154 return $ordered;
155 }
156
157 /**
158 * @param $tables
159 * @param int $valid
160 * @param string $name
161 *
162 * @return bool
163 */
164 public function validTable(&$tables, &$valid, $name) {
165 if (!array_key_exists('foreignKey', $tables[$name])) {
166 return TRUE;
167 }
168
169 foreach (array_keys($tables[$name]['foreignKey']) as $fkey) {
170 $ftable = $tables[$name]['foreignKey'][$fkey]['table'];
171 if (!array_key_exists($ftable, $valid) && $ftable !== $name) {
172 return FALSE;
173 }
174 }
175 return TRUE;
176 }
177
178 /**
179 * @param $tableXML
180 * @param $database
181 * @param $tables
182 */
183 public function getTable($tableXML, &$database, &$tables) {
184 $name = trim((string ) $tableXML->name);
185 $klass = trim((string ) $tableXML->class);
186 $base = $this->value('base', $tableXML);
187 $sourceFile = "xml/schema/{$base}/{$klass}.xml";
188 $daoPath = "{$base}/DAO/";
189 $pre = str_replace('/', '_', $daoPath);
190 $this->classNames[$name] = $pre . $klass;
191
192 $localizable = FALSE;
193 foreach ($tableXML->field as $fieldXML) {
194 if ($fieldXML->localizable) {
195 $localizable = TRUE;
196 break;
197 }
198 }
199
200 $table = array(
201 'name' => $name,
202 'base' => $daoPath,
203 'sourceFile' => $sourceFile,
204 'fileName' => $klass . '.php',
205 'objectName' => $klass,
206 'labelName' => substr($name, 8),
207 'className' => $this->classNames[$name],
208 'attributes_simple' => trim($database['tableAttributes_simple']),
209 'attributes_modern' => trim($database['tableAttributes_modern']),
210 'comment' => $this->value('comment', $tableXML),
211 'localizable' => $localizable,
212 'log' => $this->value('log', $tableXML, 'false'),
213 'archive' => $this->value('archive', $tableXML, 'false'),
214 );
215
216 $fields = array();
217 foreach ($tableXML->field as $fieldXML) {
218 if ($this->value('drop', $fieldXML, 0) > 0 and $this->value('drop', $fieldXML, 0) <= $this->buildVersion) {
219 continue;
220 }
221
222 if ($this->value('add', $fieldXML, 0) <= $this->buildVersion) {
223 $this->getField($fieldXML, $fields);
224 }
225 }
226
227 $table['fields'] = &$fields;
228
229 if ($this->value('primaryKey', $tableXML)) {
230 $this->getPrimaryKey($tableXML->primaryKey, $fields, $table);
231 }
232
233 // some kind of refresh?
234 CRM_Core_Config::singleton(FALSE);
235 if ($this->value('index', $tableXML)) {
236 $index = array();
237 foreach ($tableXML->index as $indexXML) {
238 if ($this->value('drop', $indexXML, 0) > 0 and $this->value('drop', $indexXML, 0) <= $this->buildVersion) {
239 continue;
240 }
241
242 $this->getIndex($indexXML, $fields, $index);
243 }
244 $table['index'] = &$index;
245 }
246
247 if ($this->value('foreignKey', $tableXML)) {
248 $foreign = array();
249 foreach ($tableXML->foreignKey as $foreignXML) {
250 // print_r($foreignXML);
251
252 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) {
253 continue;
254 }
255 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion) {
256 $this->getForeignKey($foreignXML, $fields, $foreign, $name);
257 }
258 }
259 $table['foreignKey'] = &$foreign;
260 }
261
262 if ($this->value('dynamicForeignKey', $tableXML)) {
263 $dynamicForeign = array();
264 foreach ($tableXML->dynamicForeignKey as $foreignXML) {
265 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) {
266 continue;
267 }
268 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion) {
269 $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name);
270 }
271 }
272 $table['dynamicForeignKey'] = $dynamicForeign;
273 }
274
275 $tables[$name] = &$table;
276 return;
277 }
278
279 /**
280 * @param $fieldXML
281 * @param $fields
282 */
283 public function getField(&$fieldXML, &$fields) {
284 $name = trim((string ) $fieldXML->name);
285 $field = array('name' => $name, 'localizable' => $fieldXML->localizable);
286 $type = (string ) $fieldXML->type;
287 switch ($type) {
288 case 'varchar':
289 case 'char':
290 $field['length'] = (int) $fieldXML->length;
291 $field['sqlType'] = "$type({$field['length']})";
292 $field['phpType'] = 'string';
293 $field['crmType'] = 'CRM_Utils_Type::T_STRING';
294 $field['size'] = $this->getSize($fieldXML);
295 break;
296
297 case 'text':
298 $field['sqlType'] = $field['phpType'] = $type;
299 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
300 // CRM-13497 see fixme below
301 $field['rows'] = isset($fieldXML->html) ? $this->value('rows', $fieldXML->html) : NULL;
302 $field['cols'] = isset($fieldXML->html) ? $this->value('cols', $fieldXML->html) : NULL;
303 break;
304 break;
305
306 case 'datetime':
307 $field['sqlType'] = $field['phpType'] = $type;
308 $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME';
309 break;
310
311 case 'boolean':
312 // need this case since some versions of mysql do not have boolean as a valid column type and hence it
313 // is changed to tinyint. hopefully after 2 yrs this case can be removed.
314 $field['sqlType'] = 'tinyint';
315 $field['phpType'] = $type;
316 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
317 break;
318
319 case 'decimal':
320 $length = $fieldXML->length ? $fieldXML->length : '20,2';
321 $field['sqlType'] = 'decimal(' . $length . ')';
322 $field['phpType'] = 'float';
323 $field['crmType'] = 'CRM_Utils_Type::T_MONEY';
324 $field['precision'] = $length;
325 break;
326
327 case 'float':
328 $field['sqlType'] = 'double';
329 $field['phpType'] = 'float';
330 $field['crmType'] = 'CRM_Utils_Type::T_FLOAT';
331 break;
332
333 default:
334 $field['sqlType'] = $field['phpType'] = $type;
335 if ($type == 'int unsigned') {
336 $field['crmType'] = 'CRM_Utils_Type::T_INT';
337 }
338 else {
339 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
340 }
341 break;
342 }
343
344 $field['required'] = $this->value('required', $fieldXML);
345 $field['collate'] = $this->value('collate', $fieldXML);
346 $field['comment'] = $this->value('comment', $fieldXML);
347 $field['default'] = $this->value('default', $fieldXML);
348 $field['import'] = $this->value('import', $fieldXML);
349 if ($this->value('export', $fieldXML)) {
350 $field['export'] = $this->value('export', $fieldXML);
351 }
352 else {
353 $field['export'] = $this->value('import', $fieldXML);
354 }
355 $field['rule'] = $this->value('rule', $fieldXML);
356 $field['title'] = $this->value('title', $fieldXML);
357 if (!$field['title']) {
358 $field['title'] = $this->composeTitle($name);
359 }
360 $field['headerPattern'] = $this->value('headerPattern', $fieldXML);
361 $field['dataPattern'] = $this->value('dataPattern', $fieldXML);
362 $field['uniqueName'] = $this->value('uniqueName', $fieldXML);
363 $field['html'] = $this->value('html', $fieldXML);
364 if (!empty($field['html'])) {
365 $validOptions = array(
366 'type',
367 /* Fixme: prior to CRM-13497 these were in a flat structure
368 // CRM-13497 moved them to be nested within 'html' but there's no point
369 // making that change in the DAOs right now since we are in the process of
370 // moving to docrtine anyway.
371 // So translating from nested xml back to flat structure for now.
372 'rows',
373 'cols',
374 'size', */
375 );
376 $field['html'] = array();
377 foreach ($validOptions as $htmlOption) {
378 if(!empty($fieldXML->html->$htmlOption)){
379 $field['html'][$htmlOption] = $this->value($htmlOption, $fieldXML->html);
380 }
381 }
382 }
383 $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML);
384 if(!empty($field['pseudoconstant'])){
385 //ok this is a bit long-winded but it gets there & is consistent with above approach
386 $field['pseudoconstant'] = array();
387 $validOptions = array(
388 // Fields can specify EITHER optionGroupName OR table, not both
389 // (since declaring optionGroupName means we are using the civicrm_option_value table)
390 'optionGroupName',
391 'table',
392 // If table is specified, keyColumn and labelColumn are also required
393 'keyColumn',
394 'labelColumn',
395 // Non-translated machine name for programmatic lookup. Defaults to 'name' if that column exists
396 'nameColumn',
397 // Where clause snippet (will be joined to the rest of the query with AND operator)
398 'condition',
399 // callback funtion incase of static arrays
400 'callback',
401 );
402 foreach ($validOptions as $pseudoOption) {
403 if(!empty($fieldXML->pseudoconstant->$pseudoOption)){
404 $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant);
405 }
406 }
407 // For now, fields that have option lists that are not in the db can simply
408 // declare an empty pseudoconstant tag and we'll add this placeholder.
409 // That field's BAO::buildOptions fn will need to be responsible for generating the option list
410 if (empty($field['pseudoconstant'])) {
411 $field['pseudoconstant'] = 'not in database';
412 }
413 }
414 $fields[$name] = &$field;
415 }
416
417 /**
418 * @param string $name
419 *
420 * @return string
421 */
422 public function composeTitle($name) {
423 $names = explode('_', strtolower($name));
424 $title = '';
425 for ($i = 0; $i < count($names); $i++) {
426 if ($names[$i] === 'id' || $names[$i] === 'is') {
427 // id's do not get titles
428 return NULL;
429 }
430
431 if ($names[$i] === 'im') {
432 $names[$i] = 'IM';
433 }
434 else {
435 $names[$i] = ucfirst(trim($names[$i]));
436 }
437
438 $title = $title . ' ' . $names[$i];
439 }
440 return trim($title);
441 }
442
443 /**
444 * @param $primaryXML
445 * @param $fields
446 * @param $table
447 */
448 public function getPrimaryKey(&$primaryXML, &$fields, &$table) {
449 $name = trim((string ) $primaryXML->name);
450
451 /** need to make sure there is a field of type name */
452 if (!array_key_exists($name, $fields)) {
453 echo "primary key $name in $table->name does not have a field definition, ignoring\n";
454 return;
455 }
456
457 // set the autoincrement property of the field
458 $auto = $this->value('autoincrement', $primaryXML);
459 $fields[$name]['autoincrement'] = $auto;
460 $primaryKey = array(
461 'name' => $name,
462 'autoincrement' => $auto,
463 );
464 $table['primaryKey'] = &$primaryKey;
465 }
466
467 /**
468 * @param $indexXML
469 * @param $fields
470 * @param $indices
471 */
472 public function getIndex(&$indexXML, &$fields, &$indices) {
473 //echo "\n\n*******************************************************\n";
474 //echo "entering getIndex\n";
475
476 $index = array();
477 // empty index name is fine
478 $indexName = trim((string)$indexXML->name);
479 $index['name'] = $indexName;
480 $index['field'] = array();
481
482 // populate fields
483 foreach ($indexXML->fieldName as $v) {
484 $fieldName = (string)($v);
485 $length = (string)($v['length']);
486 if (strlen($length) > 0) {
487 $fieldName = "$fieldName($length)";
488 }
489 $index['field'][] = $fieldName;
490 }
491
492 $index['localizable'] = FALSE;
493 foreach ($index['field'] as $fieldName) {
494 if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) {
495 $index['localizable'] = TRUE;
496 break;
497 }
498 }
499
500 // check for unique index
501 if ($this->value('unique', $indexXML)) {
502 $index['unique'] = TRUE;
503 }
504
505 //echo "\$index = \n";
506 //print_r($index);
507
508 // field array cannot be empty
509 if (empty($index['field'])) {
510 echo "No fields defined for index $indexName\n";
511 return;
512 }
513
514 // all fieldnames have to be defined and should exist in schema.
515 foreach ($index['field'] as $fieldName) {
516 if (!$fieldName) {
517 echo "Invalid field defination for index $indexName\n";
518 return;
519 }
520 $parenOffset = strpos($fieldName, '(');
521 if ($parenOffset > 0) {
522 $fieldName = substr($fieldName, 0, $parenOffset);
523 }
524 if (!array_key_exists($fieldName, $fields)) {
525 echo "Table does not contain $fieldName\n";
526 print_r($fields);
527 exit();
528 }
529 }
530 $indices[$indexName] = &$index;
531 }
532
533 /**
534 * @param $foreignXML
535 * @param $fields
536 * @param $foreignKeys
537 * @param string $currentTableName
538 */
539 public function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) {
540 $name = trim((string ) $foreignXML->name);
541
542 /** need to make sure there is a field of type name */
543 if (!array_key_exists($name, $fields)) {
544 echo "foreign $name in $currentTableName does not have a field definition, ignoring\n";
545 return;
546 }
547
548 /** need to check for existence of table and key **/
549 $table = trim($this->value('table', $foreignXML));
550 $foreignKey = array(
551 'name' => $name,
552 'table' => $table,
553 'uniqName' => "FK_{$currentTableName}_{$name}",
554 'key' => trim($this->value('key', $foreignXML)),
555 'import' => $this->value('import', $foreignXML, FALSE),
556 'export' => $this->value('import', $foreignXML, FALSE),
557 // we do this matching in a seperate phase (resolveForeignKeys)
558 'className' => NULL,
559 'onDelete' => $this->value('onDelete', $foreignXML, FALSE),
560 );
561 $foreignKeys[$name] = &$foreignKey;
562 }
563
564 /**
565 * @param $foreignXML
566 * @param $dynamicForeignKeys
567 */
568 public function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) {
569 $foreignKey = array(
570 'idColumn' => trim($foreignXML->idColumn),
571 'typeColumn' => trim($foreignXML->typeColumn),
572 'key' => trim($this->value('key', $foreignXML)),
573 );
574 $dynamicForeignKeys[] = $foreignKey;
575 }
576
577 /**
578 * @param $key
579 * @param $object
580 * @param null $default
581 *
582 * @return null|string
583 */
584 protected function value($key, &$object, $default = NULL) {
585 if (isset($object->$key)) {
586 return (string ) $object->$key;
587 }
588 return $default;
589 }
590
591 /**
592 * @param $attributes
593 * @param $object
594 * @param string $name
595 * @param null $pre
596 * @param null $post
597 */
598 protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) {
599 if (!isset($object->$name)) {
600 return;
601 }
602
603 $value = $pre . trim($object->$name) . $post;
604 $this->append($attributes, ' ', trim($value));
605 }
606
607 /**
608 * @param $str
609 * @param $delim
610 * @param $name
611 */
612 protected function append(&$str, $delim, $name) {
613 if (empty($name)) {
614 return;
615 }
616
617 if (is_array($name)) {
618 foreach ($name as $n) {
619 if (empty($n)) {
620 continue;
621 }
622 if (empty($str)) {
623 $str = $n;
624 }
625 else {
626 $str .= $delim . $n;
627 }
628 }
629 }
630 else {
631 if (empty($str)) {
632 $str = $name;
633 }
634 else {
635 $str .= $delim . $name;
636 }
637 }
638 }
639
640 /**
641 * Sets the size property of a textfield
642 * See constants defined in CRM_Utils_Type for possible values
643 */
644 protected function getSize($fieldXML) {
645 // Extract from <size> tag if supplied
646 if (!empty($fieldXML->html) && $this->value('size', $fieldXML->html)) {
647 $const = 'CRM_Utils_Type::' . strtoupper($fieldXML->html->size);
648 if (defined($const)) {
649 return $const;
650 }
651 }
652 // Infer from <length> tag if <size> was not explicitly set or was invalid
653
654 // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper
655 // Because we usually want fields to render as smaller than their maxlength
656 $sizes = array(
657 2 => 'TWO',
658 4 => 'FOUR',
659 6 => 'SIX',
660 8 => 'EIGHT',
661 16 => 'TWELVE',
662 32 => 'MEDIUM',
663 64 => 'BIG',
664 );
665 foreach ($sizes as $length => $name) {
666 if ($fieldXML->length <= $length) {
667 return "CRM_Utils_Type::$name";
668 }
669 }
670 return 'CRM_Utils_Type::HUGE';
671 }
672 }