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