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