Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezlintschema.php
Go to the documentation of this file.
1 <?php
40 class eZLintSchema extends eZDBSchemaInterface
41 {
48 public function __construct( $db, $otherSchema )
49 {
50 parent::__construct( $db );
51 $this->OtherSchema = $otherSchema;
52 $this->CorrectSchema = false;
53 $this->IsLintChecked = false;
54 }
55
60 function schema( $params = array() )
61 {
62 if ( $this->IsLintChecked )
63 {
64 return $this->CorrectSchema;
65 }
66
67 $params = array_merge( array( 'meta_data' => false,
68 'format' => 'generic' ),
69 $params );
70
71 $this->CorrectSchema = $this->OtherSchema->schema( $params );
72 $this->lintCheckSchema( $this->CorrectSchema );
73 $this->IsLintChecked = true;
74 return $this->CorrectSchema;
75 }
76
80 function validate()
81 {
82 return $this->lintCheckSchema( $this->CorrectSchema );
83 }
84
88 function otherSchema()
89 {
90 return $this->OtherSchema;
91 }
92
96 function isLintChecked()
97 {
98 return $this->IsLintChecked;
99 }
100
104 function shortenIdentifier( $identifier, $limit, $shortenList )
105 {
106 reset( $shortenList );
107 // Replace one word at a time until we have a string that is short
108 // enough, or we run out of replace words
109 while ( strlen( $identifier ) > $limit and
110 current( $shortenList ) !== false )
111 {
112 $from = key( $shortenList );
113 $to = current( $shortenList );
114 next( $shortenList );
115 $identifier = str_replace( $from, $to, $identifier );
116 }
117
118 // It is still to large so we just cut it off
119 if ( strlen( $identifier ) > $limit )
120 {
121 $identifier = substr( $identifier, 0, $limit );
122 }
123 return $identifier;
124 }
125
131 function lintCheckSchema( &$schema )
132 {
133 $status = true;
134
135 $ini = eZINI::instance( 'dbschema.ini' );
136
137 // A mapping table that maps from a long name to a short name
138 // This will be used if an identifier/name is too long
139 $shortenList = $ini->variable( 'LintChecker', 'NameMap' );
140
141 // Limitation on the length of identifiers/names
142 // Oracle is the database with the most limit (30 characters) so the
143 // limit values must be equal or lower to that.
144 $tableNameLimit = (int)$ini->variable( 'LintChecker', 'TableLimit' );
145 $fieldNameLimit = (int)$ini->variable( 'LintChecker', 'FieldLimit' );
146 $indexNameLimit = (int)$ini->variable( 'LintChecker', 'IndexLimit' );
147
148 // Tables which do not get lint checked, they are currently
149 // handled with workarounds in the various schema handlers
150 // Note: The fields and indexes are still checked.
151 $ignoredTableList = $ini->variable( 'LintChecker', 'IgnoredTables' );
152
153 // Fields which do not get lint checked, they are currently
154 // handled with workarounds in the various schema handlers
155 $list = $ini->variable( 'LintChecker', 'IgnoredFields' );
156 $ignoredFieldList = array();
157 foreach ( $list as $entry )
158 {
159 list( $tableName, $fieldName ) = explode( '.', $entry, 2 );
160 if ( !isset( $ignoredFieldList[$tableName] ) )
161 $ignoredFieldList[$tableName] = array();
162 $ignoredFieldList[$tableName][] = $fieldName;
163 }
164
165 // Fields which do not get lint checked, they are currently
166 // handled with workarounds in the various schema handlers
167 $list = $ini->variable( 'LintChecker', 'IgnoredFieldSyntax' );
168 $ignoredFieldSyntaxList = array();
169 foreach ( $list as $entry )
170 {
171 list( $tableName, $fieldName ) = explode( '.', $entry, 2 );
172 if ( !isset( $ignoredFieldList[$tableName] ) )
173 $ignoredFieldSyntaxList[$tableName] = array();
174 $ignoredFieldSyntaxList[$tableName][] = $fieldName;
175 }
176
177 // Indexes which do not get lint checked, they are currently
178 // handled with workarounds in the various schema handlers
179 $ignoredIndexList = $ini->variable( 'LintChecker', 'IgnoredIndexes' );
180
181 $badTables = array();
182 foreach ( $schema as $tableName => $tableDef )
183 {
184 // Skip the info structure, this is not a table
185 if ( $tableName == '_info' )
186 continue;
187
188 $existingTableName = $tableName;
189 $tableComments = array();
190
191 // If table is not in ignore list we check the name
192 if ( !in_array( $tableName, $ignoredTableList ) )
193 {
194 // identifiers must be 30 or less
195 // for tables we require 26 or less to allow adding suffix or prefix for indexes etc.
196 if ( strlen( $tableName ) > $tableNameLimit )
197 {
198 $tableComment = "Table names must not exceed $tableNameLimit characters,\n'$tableName' is " . strlen( $tableName ) . " characters,\ndatabases like Oracle will have problems with this.";
199 $tableName = $this->shortenIdentifier( $tableName, $tableNameLimit, $shortenList );
200 $tableComment .= "\nNew name is '$tableName'";
201 $tableComments[] = $tableComment;
202 $status = false;
203 }
204
205 if ( strcmp( $tableName, $existingTableName ) != 0 )
206 {
207 $badTables[] = array( 'from' => $existingTableName,
208 'to' => $tableName );
209 }
210 }
211
212 if ( isset( $tableDef['fields'] ) )
213 {
214 $badFields = array();
215 foreach ( $tableDef['fields'] as $fieldName => $fieldDef )
216 {
217 $comments = array();
218 $existingFieldName = $fieldName;
219
220 // Do we ignore the field name?
221 if ( !isset( $ignoredFieldList[$existingTableName] ) or
222 !in_array( $fieldName, $ignoredFieldList[$existingTableName] ) )
223 {
224
225 // identifiers must be 30 or less
226 if ( strlen( $fieldName ) > $fieldNameLimit )
227 {
228 $comment = "Field names must not exceed $fieldNameLimit characters,\n'$fieldName' in table '$existingTableName' is " . strlen( $fieldName ) . " characters,\ndatabases like Oracle will have problems with this.";
229 $fieldName = $this->shortenIdentifier( $fieldName, $fieldNameLimit, $shortenList );
230 $comment .= "\nNew name is '$fieldName'";
231 $comments[] = $comment;
232 $status = false;
233 }
234 }
235
236 if ( !isset( $ignoredFieldSyntaxList[$existingTableName] ) or
237 !in_array( $fieldName, $ignoredFieldSyntaxList[$existingTableName] ) )
238 {
239 /* Temporarily disabled
240 if ( in_array( $fieldDef['type'],
241 array( 'varchar', 'char',
242 'longtext', 'mediumtext', 'shorttext' ) ) and
243 isset( $fieldDef['not_null'] ) and
244 $fieldDef['not_null'] and
245 $fieldDef['default'] === '' )
246 {
247 $comments[] = "The string type " . $fieldDef['type'] . " ($existingTableName.$fieldName) cannot have NOT NULL defined and an empty string as DEFAULT value\nDatabase like Oracle will have problems with this.";
248 $status = false;
249 }
250 */
251 }
252
253 if ( strcmp( $existingFieldName, $fieldName ) != 0 )
254 {
255 $badFields[] = array( 'from' => $existingFieldName,
256 'to' => $fieldName );
257 }
258
259 if ( count( $comments ) > 0 )
260 {
261 $schema[$existingTableName]['fields'][$existingFieldName]['comments'] = $comments;
262 foreach ( $comments as $comment )
263 {
264 eZDebug::writeWarning( $comment, __METHOD__ );
265 }
266 }
267 }
268
269 foreach ( $badFields as $badField )
270 {
271 $schema[$existingTableName]['fields'][$badField['to']] = $schema[$existingTableName]['fields'][$badField['from']];
272 // unset( $schema[$existingTableName]['fields'][$badField['from']] );
273 $schema[$existingTableName]['fields'][$badField['from']]['removed'] = true;
274 }
275 }
276
277 if ( isset( $tableDef['indexes'] ) )
278 {
279 $badIndexes = array();
280 foreach ( $tableDef['indexes'] as $indexName => $indexDef )
281 {
282 // Primary key
283 if ( $indexDef['type'] == 'primary' )
284 continue;
285
286 // Do we ignore the index?
287 if ( in_array( $indexName, $ignoredIndexList ) )
288 continue;
289
290 $comments = array();
291
292 $existingIndexName = $indexName;
293 if ( isset( $schema[$indexName] ) )
294 {
295 $comment = "Index named '$indexName' has same name as an existing table,\ndatabases like PostgreSQL and Oracle will have problems with this.";
296 $indexFieldText = '';
297 $i = 0;
298 foreach ( $indexDef['fields'] as $fieldDef )
299 {
300 if ( $i > 0 )
301 $indexFieldText .= '_';
302 if ( is_array( $fieldDef ) )
303 {
304 $indexFieldText .= $fieldDef['name'];
305 }
306 else
307 {
308 $indexFieldText .= $fieldDef;
309 }
310 }
311 $indexName = $indexName . '_' . $indexFieldText . '_i';
312 $comment .= "\nNew name is '$indexName'";
313 $comments[] = $comment;
314 $status = false;
315 }
316
317 // Primary indexes must be named PRIMARY
318 if ( $indexDef['type'] == 'primary' and
319 $indexName != 'PRIMARY' )
320 {
321 $comment = "Index named '$indexName' which is a primary key must be named PRIMARY.";
322 $indexName = "PRIMARY";
323 $comments[] = $comment;
324 $status = false;
325 }
326
327 // identifiers must be 30 or less
328 if ( strlen( $indexName ) > $indexNameLimit )
329 {
330 $comment = "Index names must not exceed $indexNameLimit characters,\n'$indexName' is " . strlen( $indexName ) . " characters,\ndatabases like Oracle will have problems with this.";
331 $indexName = $this->shortenIdentifier( $indexName, $indexNameLimit, $shortenList );
332 $comment .= "\nNew name is '$indexName'";
333 $comments[] = $comment;
334 $status = false;
335 }
336
337 // Check if there are some database specific entries
338 foreach ( $indexDef['fields'] as $fieldDef )
339 {
340 if ( is_array( $fieldDef ) )
341 {
342 $fieldName = $fieldDef['name'];
343 foreach ( $fieldDef as $fdName => $fdValue )
344 {
345 if ( preg_match( "#^([a-z0-9]+):#", $fdName, $matches ) )
346 {
347 $dbName = $matches[1];
348 $comments[] = "Found database specific entry ($dbName) at index $existingIndexName.$fieldName";
349 $status = false;
350 }
351 }
352 }
353 }
354
355 if ( strcmp( $existingIndexName, $indexName ) != 0 )
356 {
357 $badIndexes[] = array( 'from' => $existingIndexName,
358 'to' => $indexName );
359 }
360 if ( count( $comments ) > 0 )
361 {
362 $schema[$existingTableName]['indexes'][$existingIndexName]['comments'] = $comments;
363 foreach ( $comments as $comment )
364 {
365 eZDebug::writeWarning( $comment, __METHOD__ );
366 }
367 }
368 }
369
370 foreach ( $badIndexes as $badIndex )
371 {
372 $schema[$existingTableName]['indexes'][$badIndex['to']] = $schema[$existingTableName]['indexes'][$badIndex['from']];
373 $schema[$existingTableName]['indexes'][$badIndex['from']]['removed'] = true;
374 }
375 }
376
377 if ( count( $tableComments ) > 0 )
378 {
379 $schema[$existingTableName]['comments'] = $tableComments;
380 foreach ( $tableComments as $comment )
381 {
382 eZDebug::writeWarning( $comment, __METHOD__ );
383 }
384 }
385 }
386 foreach ( $badTables as $badTable )
387 {
388 $schema[$badTable['to']] = $schema[$badTable['from']];
389 $schema[$badTable['from']]['removed'] = true;
390 }
391 return $status;
392 }
393
397 function data( $schema = false, $tableNameList = false, $params = array() )
398 {
399 return $this->OtherSchema->data( $schema, $tableNameList, $params );
400 }
401
405 function generateSchemaFile( $schema, $params = array() )
406 {
407 return $this->OtherSchema->generateSchemaFile( $schema, $params );
408 }
409
413 function generateUpgradeFile( $differences, $params = array() )
414 {
415 return $this->OtherSchema->generateUpgradeFile( $differences, $params );
416 }
417
421 function generateDataFile( $schema, $data, $params )
422 {
423 return $this->OtherSchema->generateDataFile( $schema, $data, $params );
424 }
425
429 function generateTableSchema( $table, $tableDef, $params )
430 {
431 return $this->OtherSchema->generateTableSchema( $table, $tableDef, $params );
432 }
433
437 function generateTableInsert( $tableName, $tableDef, $dataEntries, $params )
438 {
439 return $this->OtherSchema->generateTableInsert( $tableName, $tableDef, $dataEntries, $params );
440 }
441
445 function generateDropTable( $table, $params )
446 {
447 return $this->OtherSchema->generateDropTable( $table, $params );
448 }
449
453 function generateAddFieldSql( $table, $field_name, $added_field, $params )
454 {
455 return $this->OtherSchema->generateAddFieldSql( $table, $field_name, $added_field, $params );
456 }
457
461 function generateAlterFieldSql( $table, $field_name, $changed_field, $params )
462 {
463 return $this->OtherSchema->generateAlterFieldSql( $table, $field_name, $changed_field, $params );
464 }
465
469 function generateDropFieldSql( $table, $field_name, $params )
470 {
471 return $this->OtherSchema->generateDropFieldSql( $table, $field_name, $params );
472 }
473
477 function generateAddIndexSql( $table, $index_name, $added_index, $params )
478 {
479 return $this->OtherSchema->generateAddIndexSql( $table, $index_name, $added_index, $params );
480 }
481
485 function generateDropIndexSql( $table, $index_name, $removed_index, $params )
486 {
487 return $this->OtherSchema->generateDropIndexSql( $table, $index_name, $removed_index, $params );
488 }
489
493 function isMultiInsertSupported()
494 {
495 return $this->OtherSchema->isMultiInsertSupported();
496 }
497
501 function generateDataValueTextSQL( $fieldDef, $value )
502 {
503 return $this->OtherSchema->generateDataValueTextSQL( $fieldDef, $value );
504 }
505
509 function schemaType()
510 {
511 return $this->OtherSchema->schemaType();
512 }
513
517 function schemaName()
518 {
519 return $this->OtherSchema->schemaName();
520 }
521
524 public $OtherSchema;
526 public $CorrectSchema;
528 public $IsLintChecked;
529 }
530
531 ?>
eZDBSchemaInterface
This class provide interface for DB schema library.
Definition: ezdbschemainterface.php:62
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZLintSchema
Provides lint checking of database schemas.
Definition: ezlintschema.php:41
eZLintSchema\$OtherSchema
$OtherSchema
Definition: ezlintschema.php:524
eZLintSchema\generateDropIndexSql
generateDropIndexSql( $table, $index_name, $removed_index, $params)
Definition: ezlintschema.php:485
eZLintSchema\isMultiInsertSupported
isMultiInsertSupported()
Definition: ezlintschema.php:493
eZLintSchema\generateDropFieldSql
generateDropFieldSql( $table, $field_name, $params)
Definition: ezlintschema.php:469
eZLintSchema\generateDropTable
generateDropTable( $table, $params)
Definition: ezlintschema.php:445
eZLintSchema\shortenIdentifier
shortenIdentifier( $identifier, $limit, $shortenList)
Definition: ezlintschema.php:104
eZLintSchema\generateAddIndexSql
generateAddIndexSql( $table, $index_name, $added_index, $params)
Definition: ezlintschema.php:477
eZLintSchema\schema
schema( $params=array())
Definition: ezlintschema.php:60
eZLintSchema\generateAlterFieldSql
generateAlterFieldSql( $table, $field_name, $changed_field, $params)
Definition: ezlintschema.php:461
eZLintSchema\validate
validate()
Definition: ezlintschema.php:80
eZLintSchema\otherSchema
otherSchema()
Definition: ezlintschema.php:88
eZLintSchema\generateDataFile
generateDataFile( $schema, $data, $params)
Definition: ezlintschema.php:421
eZLintSchema\lintCheckSchema
lintCheckSchema(&$schema)
Definition: ezlintschema.php:131
eZLintSchema\schemaName
schemaName()
Definition: ezlintschema.php:517
eZLintSchema\generateTableSchema
generateTableSchema( $table, $tableDef, $params)
Definition: ezlintschema.php:429
eZLintSchema\generateAddFieldSql
generateAddFieldSql( $table, $field_name, $added_field, $params)
Definition: ezlintschema.php:453
eZLintSchema\__construct
__construct( $db, $otherSchema)
Definition: ezlintschema.php:48
eZLintSchema\isLintChecked
isLintChecked()
Definition: ezlintschema.php:96
eZLintSchema\$CorrectSchema
$CorrectSchema
The corrected schema.
Definition: ezlintschema.php:526
eZLintSchema\$IsLintChecked
$IsLintChecked
Whether the schema has been checked or not.
Definition: ezlintschema.php:528
eZLintSchema\generateDataValueTextSQL
generateDataValueTextSQL( $fieldDef, $value)
Definition: ezlintschema.php:501
eZLintSchema\data
data( $schema=false, $tableNameList=false, $params=array())
Definition: ezlintschema.php:397
eZLintSchema\generateUpgradeFile
generateUpgradeFile( $differences, $params=array())
Definition: ezlintschema.php:413
eZLintSchema\schemaType
schemaType()
Definition: ezlintschema.php:509
eZLintSchema\generateTableInsert
generateTableInsert( $tableName, $tableDef, $dataEntries, $params)
Definition: ezlintschema.php:437
eZLintSchema\generateSchemaFile
generateSchemaFile( $schema, $params=array())
Definition: ezlintschema.php:405
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.