4 * Read the schema specification and parse into internal data structures
6 class CRM_Core_CodeGen_Specification
{
10 protected $classNames;
16 * @param string $buildVersion
17 * Which version of the schema to build.
19 public function parse($schemaPath, $buildVersion) {
20 $this->buildVersion
= $buildVersion;
22 echo "Parsing schema description " . $schemaPath . "\n";
23 $dbXML = CRM_Core_CodeGen_Util_Xml
::parse($schemaPath);
26 echo "Extracting database information\n";
27 $this->database
= &$this->getDatabase($dbXML);
28 // print_r( $this->database );
30 $this->classNames
= array();
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
);
36 $this->resolveForeignKeys($this->tables
, $this->classNames
);
37 $this->tables
= $this->orderTables($this->tables
);
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']);
54 $archiveTables[$name] = $table;
65 public function &getDatabase(&$dbXML) {
66 $database = array('name' => trim((string ) $dbXML->name
));
69 $this->checkAndAppend($attributes, $dbXML, 'character_set', 'DEFAULT CHARACTER SET ', '');
70 $this->checkAndAppend($attributes, $dbXML, 'collate', 'COLLATE ', '');
71 $database['attributes'] = $attributes;
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);
79 $database['comment'] = $this->value('comment', $dbXML, '');
90 public function getTables($dbXML, &$database) {
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
) {
98 if ($this->value('add', $tableXML, 0) <= $this->buildVersion
) {
99 $this->getTable($tableXML, $database, $tables);
109 * @param string $classNames
111 public function resolveForeignKeys(&$tables, &$classNames) {
112 foreach (array_keys($tables) as $name) {
113 $this->resolveForeignKey($tables, $classNames, $name);
119 * @param string $classNames
120 * @param string $name
122 public function resolveForeignKey(&$tables, &$classNames, $name) {
123 if (!array_key_exists('foreignKey', $tables[$name])) {
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";
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];
144 public function orderTables(&$tables) {
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]);
161 * @param string $name
165 public function validTable(&$tables, &$valid, $name) {
166 if (!array_key_exists('foreignKey', $tables[$name])) {
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) {
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;
193 $localizable = FALSE;
194 foreach ($tableXML->field
as $fieldXML) {
195 if ($fieldXML->localizable
) {
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'),
218 foreach ($tableXML->field
as $fieldXML) {
219 if ($this->value('drop', $fieldXML, 0) > 0 and $this->value('drop', $fieldXML, 0) <= $this->buildVersion
) {
223 if ($this->value('add', $fieldXML, 0) <= $this->buildVersion
) {
224 $this->getField($fieldXML, $fields);
228 $table['fields'] = &$fields;
230 if ($this->value('primaryKey', $tableXML)) {
231 $this->getPrimaryKey($tableXML->primaryKey
, $fields, $table);
234 // some kind of refresh?
235 CRM_Core_Config
::singleton(FALSE);
236 if ($this->value('index', $tableXML)) {
238 foreach ($tableXML->index
as $indexXML) {
239 if ($this->value('drop', $indexXML, 0) > 0 and $this->value('drop', $indexXML, 0) <= $this->buildVersion
) {
243 $this->getIndex($indexXML, $fields, $index);
245 $table['index'] = &$index;
248 if ($this->value('foreignKey', $tableXML)) {
250 foreach ($tableXML->foreignKey
as $foreignXML) {
251 // print_r($foreignXML);
253 if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion
) {
256 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion
) {
257 $this->getForeignKey($foreignXML, $fields, $foreign, $name);
260 $table['foreignKey'] = &$foreign;
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
) {
269 if ($this->value('add', $foreignXML, 0) <= $this->buildVersion
) {
270 $this->getDynamicForeignKey($foreignXML, $dynamicForeign, $name);
273 $table['dynamicForeignKey'] = $dynamicForeign;
276 $tables[$name] = &$table;
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
;
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);
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;
308 $field['sqlType'] = $field['phpType'] = $type;
309 $field['crmType'] = 'CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME';
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);
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;
329 $field['sqlType'] = 'double';
330 $field['phpType'] = 'float';
331 $field['crmType'] = 'CRM_Utils_Type::T_FLOAT';
335 $field['sqlType'] = $field['phpType'] = $type;
336 if ($type == 'int unsigned') {
337 $field['crmType'] = 'CRM_Utils_Type::T_INT';
340 $field['crmType'] = 'CRM_Utils_Type::T_' . strtoupper($type);
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);
354 $field['export'] = $this->value('import', $fieldXML);
356 $field['rule'] = $this->value('rule', $fieldXML);
357 $field['title'] = $this->value('title', $fieldXML);
358 if (!$field['title']) {
359 $field['title'] = $this->composeTitle($name);
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(
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.
377 $field['html'] = array();
378 foreach ($validOptions as $htmlOption) {
379 if (!empty($fieldXML->html
->$htmlOption)) {
380 $field['html'][$htmlOption] = $this->value($htmlOption, $fieldXML->html
);
385 // in multilingual context popup, we need extra information to create appropriate widget
386 if ($fieldXML->localizable
) {
387 if (isset($fieldXML->html
)) {
388 $field['widget'] = (array) $fieldXML->html
;
392 $field['widget'] = array('type' => 'Text');
394 if (isset($fieldXML->required
)) {
395 $field['widget']['required'] = $this->value('required', $fieldXML);
399 $field['pseudoconstant'] = $this->value('pseudoconstant', $fieldXML);
400 if (!empty($field['pseudoconstant'])) {
401 //ok this is a bit long-winded but it gets there & is consistent with above approach
402 $field['pseudoconstant'] = array();
403 $validOptions = array(
404 // Fields can specify EITHER optionGroupName OR table, not both
405 // (since declaring optionGroupName means we are using the civicrm_option_value table)
408 // If table is specified, keyColumn and labelColumn are also required
411 // Non-translated machine name for programmatic lookup. Defaults to 'name' if that column exists
413 // Where clause snippet (will be joined to the rest of the query with AND operator)
415 // callback function incase of static arrays
417 // Path to options edit form
420 foreach ($validOptions as $pseudoOption) {
421 if (!empty($fieldXML->pseudoconstant
->$pseudoOption)) {
422 $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant
);
425 if (!isset($field['pseudoconstant']['optionEditPath']) && !empty($field['pseudoconstant']['optionGroupName'])) {
426 $field['pseudoconstant']['optionEditPath'] = 'civicrm/admin/options/' . $field['pseudoconstant']['optionGroupName'];
428 // For now, fields that have option lists that are not in the db can simply
429 // declare an empty pseudoconstant tag and we'll add this placeholder.
430 // That field's BAO::buildOptions fn will need to be responsible for generating the option list
431 if (empty($field['pseudoconstant'])) {
432 $field['pseudoconstant'] = 'not in database';
435 $fields[$name] = &$field;
439 * @param string $name
443 public function composeTitle($name) {
444 $names = explode('_', strtolower($name));
446 for ($i = 0; $i < count($names); $i++
) {
447 if ($names[$i] === 'id' ||
$names[$i] === 'is') {
448 // id's do not get titles
452 if ($names[$i] === 'im') {
456 $names[$i] = ucfirst(trim($names[$i]));
459 $title = $title . ' ' . $names[$i];
469 public function getPrimaryKey(&$primaryXML, &$fields, &$table) {
470 $name = trim((string ) $primaryXML->name
);
472 /** need to make sure there is a field of type name */
473 if (!array_key_exists($name, $fields)) {
474 echo "primary key $name in $table->name does not have a field definition, ignoring\n";
478 // set the autoincrement property of the field
479 $auto = $this->value('autoincrement', $primaryXML);
480 $fields[$name]['autoincrement'] = $auto;
483 'autoincrement' => $auto,
485 $table['primaryKey'] = &$primaryKey;
493 public function getIndex(&$indexXML, &$fields, &$indices) {
494 //echo "\n\n*******************************************************\n";
495 //echo "entering getIndex\n";
498 // empty index name is fine
499 $indexName = trim((string) $indexXML->name
);
500 $index['name'] = $indexName;
501 $index['field'] = array();
504 foreach ($indexXML->fieldName
as $v) {
505 $fieldName = (string) ($v);
506 $length = (string) ($v['length']);
507 if (strlen($length) > 0) {
508 $fieldName = "$fieldName($length)";
510 $index['field'][] = $fieldName;
513 $index['localizable'] = FALSE;
514 foreach ($index['field'] as $fieldName) {
515 if (isset($fields[$fieldName]) and $fields[$fieldName]['localizable']) {
516 $index['localizable'] = TRUE;
521 // check for unique index
522 if ($this->value('unique', $indexXML)) {
523 $index['unique'] = TRUE;
526 //echo "\$index = \n";
529 // field array cannot be empty
530 if (empty($index['field'])) {
531 echo "No fields defined for index $indexName\n";
535 // all fieldnames have to be defined and should exist in schema.
536 foreach ($index['field'] as $fieldName) {
538 echo "Invalid field defination for index $indexName\n";
541 $parenOffset = strpos($fieldName, '(');
542 if ($parenOffset > 0) {
543 $fieldName = substr($fieldName, 0, $parenOffset);
545 if (!array_key_exists($fieldName, $fields)) {
546 echo "Table does not contain $fieldName\n";
551 $indices[$indexName] = &$index;
557 * @param $foreignKeys
558 * @param string $currentTableName
560 public function getForeignKey(&$foreignXML, &$fields, &$foreignKeys, &$currentTableName) {
561 $name = trim((string ) $foreignXML->name
);
563 /** need to make sure there is a field of type name */
564 if (!array_key_exists($name, $fields)) {
565 echo "foreign $name in $currentTableName does not have a field definition, ignoring\n";
569 /** need to check for existence of table and key **/
570 $table = trim($this->value('table', $foreignXML));
574 'uniqName' => "FK_{$currentTableName}_{$name}",
575 'key' => trim($this->value('key', $foreignXML)),
576 'import' => $this->value('import', $foreignXML, FALSE),
577 'export' => $this->value('import', $foreignXML, FALSE),
578 // we do this matching in a separate phase (resolveForeignKeys)
580 'onDelete' => $this->value('onDelete', $foreignXML, FALSE),
582 $foreignKeys[$name] = &$foreignKey;
587 * @param $dynamicForeignKeys
589 public function getDynamicForeignKey(&$foreignXML, &$dynamicForeignKeys) {
591 'idColumn' => trim($foreignXML->idColumn
),
592 'typeColumn' => trim($foreignXML->typeColumn
),
593 'key' => trim($this->value('key', $foreignXML)),
595 $dynamicForeignKeys[] = $foreignKey;
601 * @param null $default
603 * @return null|string
605 protected function value($key, &$object, $default = NULL) {
606 if (isset($object->$key)) {
607 return (string ) $object->$key;
615 * @param string $name
619 protected function checkAndAppend(&$attributes, &$object, $name, $pre = NULL, $post = NULL) {
620 if (!isset($object->$name)) {
624 $value = $pre . trim($object->$name) . $post;
625 $this->append($attributes, ' ', trim($value));
633 protected function append(&$str, $delim, $name) {
638 if (is_array($name)) {
639 foreach ($name as $n) {
656 $str .= $delim . $name;
662 * Sets the size property of a textfield.
664 protected function getSize($fieldXML) {
665 // Extract from <size> tag if supplied
666 if (!empty($fieldXML->html
) && $this->value('size', $fieldXML->html
)) {
667 return $this->value('size', $fieldXML->html
);
669 // Infer from <length> tag if <size> was not explicitly set or was invalid
670 // This map is slightly different from CRM_Core_Form_Renderer::$_sizeMapper
671 // Because we usually want fields to render as smaller than their maxlength
681 foreach ($sizes as $length => $name) {
682 if ($fieldXML->length
<= $length) {
683 return "CRM_Utils_Type::$name";
686 return 'CRM_Utils_Type::HUGE';