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