4d14624b90b5dec8da691dd0d594cbc95fb4e824
[core.git] / inc / classes / main / database / databases / class_LocalFileDatabase.php
1 <?php
2 /**
3  * Database backend class for storing objects in locally created files.
4  *
5  * This class serializes arrays stored in the dataset instance and saves them
6  * to local files. Every file (except 'info') represents a single line. Every
7  * directory within the 'db' (base) directory represents a table.
8  *
9  * A configurable 'file_io_class' is being used as "storage backend".
10  *
11  * @author              Roland Haeder <webmaster@ship-simu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.ship-simu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class LocalFileDatabase extends BaseDatabaseBackend implements DatabaseBackendInterface {
31         /**
32          * The file's extension
33          */
34         private $fileExtension = 'serialized';
35
36         /**
37          * The last read file's name
38          */
39         private $lastFile = '';
40
41         /**
42          * The last read file's content including header information
43          */
44         private $lastContents = array();
45
46         /**
47          * Whether the "connection is already up
48          */
49         private $alreadyConnected = false;
50
51         /**
52          * Table information array
53          */
54         private $tableInfo = array();
55
56         /**
57          * Element for index
58          */
59         private $indexKey = '__idx';
60
61         /**
62          * The protected constructor. Do never instance from outside! You need to
63          * set a local file path. The class will then validate it.
64          *
65          * @return      void
66          */
67         protected function __construct () {
68                 // Call parent constructor
69                 parent::__construct(__CLASS__);
70         }
71
72         /**
73          * Create an object of LocalFileDatabase and set the save path from
74          * configuration for local files.
75          *
76          * @return      $databaseInstance       An instance of LocalFileDatabase
77          */
78         public static final function createLocalFileDatabase () {
79                 // Get an instance
80                 $databaseInstance = new LocalFileDatabase();
81
82                 // Get a new compressor channel instance
83                 $compressorInstance = ObjectFactory::createObjectByConfiguredName('compressor_channel_class');
84
85                 // Set the compressor channel
86                 $databaseInstance->setCompressorChannel($compressorInstance);
87
88                 // Get a file IO handler
89                 $fileIoInstance = ObjectFactory::createObjectByConfiguredName('file_io_class');
90
91                 // ... and set it
92                 $databaseInstance->setFileIoInstance($fileIoInstance);
93
94                 // "Connect" to the database
95                 $databaseInstance->connectToDatabase();
96
97                 // Return database instance
98                 return $databaseInstance;
99         }
100
101         /**
102          * Setter for the last read file
103          *
104          * @param       $fqfn   The FQFN of the last read file
105          * @return      void
106          */
107         private final function setLastFile ($fqfn) {
108                 // Cast string and set it
109                 $this->lastFile = (string) $fqfn;
110         }
111
112         /**
113          * Getter for last read file
114          *
115          * @return      $lastFile       The last read file's name with full path
116          */
117         public final function getLastFile () {
118                 return $this->lastFile;
119         }
120
121         /**
122          * Setter for contents of the last read file
123          *
124          * @param               $contents       An array with header and data elements
125          * @return      void
126          */
127         private final function setLastFileContents (array $contents) {
128                 // Set array
129                 $this->lastContents = $contents;
130         }
131
132         /**
133          * Getter for last read file's content as an array
134          *
135          * @return      $lastContent    The array with elements 'header' and 'data'.
136          */
137         public final function getLastContents () {
138                 return $this->lastContents;
139         }
140
141         /**
142          * Getter for file extension
143          *
144          * @return      $fileExtension  The array with elements 'header' and 'data'.
145          */
146         public final function getFileExtension () {
147                 return $this->fileExtension;
148         }
149
150         /**
151          * Getter for index key
152          *
153          * @return      $indexKey       Index key
154          */
155         public final function getIndexKey () {
156                 return $this->indexKey;
157         }
158
159         /**
160          * Reads a local data file  and returns it's contents in an array
161          *
162          * @param       $fqfn   The FQFN for the requested file
163          * @return      $dataArray
164          */
165         private function getDataArrayFromFile ($fqfn) {
166                 // Debug message
167                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
168
169                 // Init compressed data
170                 $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
171                 $compressedData = $compressedData['data'];
172
173                 // Close the file and throw the instance away
174                 unset($fileInstance);
175
176                 // Decompress it
177                 $serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
178
179                 // Unserialize it
180                 $dataArray = unserialize($serializedData);
181
182                 // Debug message
183                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
184                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
185
186                 // Finally return it
187                 return $dataArray;
188         }
189
190         /**
191          * Writes data array to local file
192          *
193          * @param       $fqfn           The FQFN of the local file
194          * @param       $dataArray      An array with all the data we shall write
195          * @return      void
196          */
197         private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
198                 // Debug message
199                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
200                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, TRUE));
201
202                 // Serialize and compress it
203                 $compressedData = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($dataArray));
204
205                 // Write data
206                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
207
208                 // Write this data BASE64 encoded to the file
209                 $this->getFileIoInstance()->saveFile($fqfn, $compressedData);
210
211                 // Debug message
212                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
213         }
214
215         /**
216          * Getter for table information file contents or an empty if info file was not created
217          *
218          * @param       $dataSetInstance        An instance of a database set class
219          * @return      $infoArray                      An array with all table informations
220          */
221         private function getContentsFromTableInfoFile (StoreableCriteria $dataSetInstance) {
222                 // Default content is no data
223                 $infoArray = array();
224
225                 // Create FQFN for getting the table information file
226                 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
227
228                 // Get the file contents
229                 try {
230                         $infoArray = $this->getDataArrayFromFile($fqfn);
231                 } catch (FileIoException $e) {
232                         // Not found, so ignore it here
233                 }
234
235                 // ... and return it
236                 return $infoArray;
237         }
238
239         /**
240          * Generates an FQFN from given dataset instance and string
241          *
242          * @param       $dataSetInstance        An instance of a database set class
243          * @param       $rowName                        Name of the row
244          * @return      $fqfn                           The FQFN for this row
245          */
246         private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
247                 // This is the FQFN
248                 $fqfn = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/' . $rowName . '.' . $this->getFileExtension();
249
250                 // Return it
251                 return $fqfn;
252         }
253
254         /**
255          * Creates the table info file from given dataset instance
256          *
257          * @param       $dataSetInstance        An instance of a database set class
258          * @return      void
259          */
260         private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
261                 // Create FQFN for creating the table information file
262                 $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
263
264                 // Get the data out from dataset in a local array
265                 $this->tableInfo[$dataSetInstance->getTableName()] = array(
266                         'primary'      => $dataSetInstance->getPrimaryKey(),
267                         'created'      => time(),
268                         'last_updated' => time()
269                 );
270
271                 // Write the data to the file
272                 $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
273         }
274
275         /**
276          * Updates the primary key information or creates the table info file if not found
277          *
278          * @param       $dataSetInstance        An instance of a database set class
279          * @return      void
280          */
281         private function updatePrimaryKey (StoreableCriteria $dataSetInstance) {
282                 // Get the information array from lower method
283                 $infoArray = $this->getContentsFromTableInfoFile($dataSetInstance);
284
285                 // Is the primary key there?
286                 if (!isset($this->tableInfo['primary'])) {
287                         // Then create the info file
288                         $this->createTableInfoFile($dataSetInstance);
289                 } elseif (($this->getConfigInstance()->getConfigEntry('db_update_primary_forced') == 'Y') && ($dataSetInstance->getPrimaryKey() != $this->tableInfo['primary'])) {
290                         // Set the array element
291                         $this->tableInfo[$dataSetInstance->getTableName()]['primary'] = $dataSetInstance->getPrimaryKey();
292
293                         // Update the entry
294                         $this->updateTableInfoFile($dataSetInstance);
295                 }
296         }
297
298         /**
299          * Makes sure that the database connection is alive
300          *
301          * @return      void
302          * @todo        Do some checks on the database directory and files here
303          */
304         public function connectToDatabase () {
305         }
306
307         /**
308          * Starts a SELECT query on the database by given return type, table name
309          * and search criteria
310          *
311          * @param       $tableName              Name of the database table
312          * @param       $criteria               Local search criteria class
313          * @return      $resultData             Result data of the query
314          * @throws      UnsupportedCriteriaException    If the criteria is unsupported
315          * @throws      SqlException                                    If an 'SQL error' occurs
316          */
317         public function querySelect ($tableName, LocalSearchCriteria $criteriaInstance) {
318                 // The result is null by any errors
319                 $resultData = NULL;
320
321                 // Create full path name
322                 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $tableName . '/';
323
324                 // A 'select' query is not that easy on local files, so first try to
325                 // find the 'table' which is in fact a directory on the server
326                 try {
327                         // Get a directory pointer instance
328                         $directoryInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
329
330                         // Initialize the result data, this need to be rewritten e.g. if a local file cannot be read
331                         $resultData = array(
332                                 BaseDatabaseBackend::RESULT_INDEX_STATUS => self::RESULT_OKAY,
333                                 BaseDatabaseBackend::RESULT_INDEX_ROWS   => array()
334                         );
335
336                         // Initialize limit/skip
337                         $limitFound = 0;
338                         $skipFound = 0;
339                         $idx = 1;
340
341                         // Read the directory with some exceptions
342                         while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $criteriaInstance->getLimit()) || ($criteriaInstance->getLimit() == 0))) {
343                                 // Does the extension match?
344                                 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
345                                         // Skip this file!
346                                         continue;
347                                 } // END - if
348
349                                 // Read the file
350                                 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
351                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray,true));
352
353                                 // Is this an array?
354                                 if (is_array($dataArray)) {
355                                         // Search in the criteria with FMFW (First Matches, First Wins)
356                                         foreach ($dataArray as $key => $value) {
357                                                 // Get criteria element
358                                                 $criteria = $criteriaInstance->getCriteriaElemnent($key);
359
360                                                 // Is the criteria met or none set?
361                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteria[' . gettype($criteria) . ']=' . $criteria . ';()=' . strlen($criteria) . ',criteriaInstance()=' . $criteriaInstance->count() . ',value(' . strlen($value) . ')=' . $value);
362                                                 if (((!is_null($criteria)) && ($criteria == $value)) || ($criteriaInstance->count() == 0))  {
363                                                         // Shall we skip this entry?
364                                                         if ($criteriaInstance->getSkip() > 0) {
365                                                                 // We shall skip some entries
366                                                                 if ($skipFound < $criteriaInstance->getSkip()) {
367                                                                         // Skip this entry
368                                                                         $skipFound++;
369                                                                         break;
370                                                                 } // END - if
371                                                         } // END - if
372
373                                                         // Set id number
374                                                         $dataArray[$this->getIndexKey()] = $idx;
375
376                                                         // Entry found!
377                                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: indexKey=' . $this->getIndexKey() . ',idx=' . $idx . ',dataArray=' . print_r($dataArray, true));
378                                                         $resultData[BaseDatabaseBackend::RESULT_INDEX_ROWS][] = $dataArray;
379
380                                                         // Count found entries up
381                                                         $limitFound++;
382                                                         break;
383                                                 } // END - if
384                                         } // END - foreach
385                                 } else {
386                                         // Throw an exception here
387                                         throw new SqlException(array($this, sprintf("File &#39;%s&#39; contains invalid data.", $dataFile), self::DB_CODE_DATA_FILE_CORRUPT), self::EXCEPTION_SQL_QUERY);
388                                 }
389
390                                 // Count entry up
391                                 $idx++;
392                         } // END - while
393
394                         // Close directory and throw the instance away
395                         $directoryInstance->closeDirectory();
396                         unset($directoryInstance);
397
398                         // Reset last exception
399                         $this->resetLastException();
400                 } catch (PathIsNoDirectoryException $e) {
401                         // Path not found means "table not found" for real databases...
402                         $this->setLastException($e);
403
404                         // So throw an SqlException here with faked error message
405                         throw new SqlException (array($this, sprintf("Table &#39;%s&#39; not found", $tableName), self::DB_CODE_TABLE_MISSING), self::EXCEPTION_SQL_QUERY);
406                 } catch (FrameworkException $e) {
407                         // Catch all exceptions and store them in last error
408                         $this->setLastException($e);
409                 }
410
411                 // Return the gathered result
412                 return $resultData;
413         }
414
415         /**
416          * "Inserts" a data set instance into a local file database folder
417          *
418          * @param       $dataSetInstance        A storeable data set
419          * @return      void
420          * @throws      SqlException    If an SQL error occurs
421          */
422         public function queryInsertDataSet (StoreableCriteria $dataSetInstance) {
423                 // Try to save the request away
424                 try {
425                         // Create full path name
426                         $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
427
428                         // Write the data away
429                         $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
430
431                         // Update the primary key
432                         $this->updatePrimaryKey($dataSetInstance);
433
434                         // Reset last exception
435                         $this->resetLastException();
436                 } catch (FrameworkException $e) {
437                         // Catch all exceptions and store them in last error
438                         $this->setLastException($e);
439
440                         // Throw an SQL exception
441                         throw new SqlException(array($this, sprintf("Cannot write data to table &#39;%s&#39;, is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
442                 }
443         }
444
445         /**
446          * "Updates" a data set instance with a database layer
447          *
448          * @param       $dataSetInstance        A storeable data set
449          * @return      void
450          * @throws      SqlException    If an SQL error occurs
451          */
452         public function queryUpdateDataSet (StoreableCriteria $dataSetInstance) {
453                 // Create full path name
454                 $pathName = $this->getConfigInstance()->getConfigEntry('local_db_path') . $dataSetInstance->getTableName() . '/';
455
456                 // Try all the requests
457                 try {
458                         // Get a file pointer instance
459                         $directoryInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
460
461                         // Initialize limit/skip
462                         $limitFound = 0;
463                         $skipFound = 0;
464
465                         // Get the criteria array from the dataset
466                         $criteriaArray = $dataSetInstance->getCriteriaArray();
467
468                         // Get search criteria
469                         $searchInstance = $dataSetInstance->getSearchInstance();
470
471                         // Read the directory with some exceptions
472                         while (($dataFile = $directoryInstance->readDirectoryExcept(array('.', '..', '.htaccess', '.svn', 'info.' . $this->getFileExtension()))) && (($limitFound < $searchInstance->getLimit()) || ($searchInstance->getLimit() == 0))) {
473                                 // Does the extension match?
474                                 if (substr($dataFile, -(strlen($this->getFileExtension()))) !== $this->getFileExtension()) {
475                                         // Debug message
476                                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',getFileExtension()=' . $this->getFileExtension() . ' - SKIPPED!');
477                                         // Skip this file!
478                                         continue;
479                                 } // END - if
480
481                                 // Open this file for reading
482                                 $dataArray = $this->getDataArrayFromFile($pathName . $dataFile);
483                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',dataArray='.print_r($dataArray,true));
484
485                                 // Is this an array?
486                                 if (is_array($dataArray)) {
487                                         // Search in the criteria with FMFW (First Matches, First Wins)
488                                         foreach ($dataArray as $key => $value) {
489                                                 // Get criteria element
490                                                 $criteria = $searchInstance->getCriteriaElemnent($key);
491                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: dataFile=' . $dataFile . ',key=' . $key . ',criteria=' . $criteria);
492
493                                                 // Is the criteria met?
494                                                 if (((!is_null($criteria)) && ($criteria == $value)) || ($searchInstance->count() == 0))  {
495                                                         // Shall we skip this entry?
496                                                         if ($searchInstance->getSkip() > 0) {
497                                                                 // We shall skip some entries
498                                                                 if ($skipFound < $searchInstance->getSkip()) {
499                                                                         // Skip this entry
500                                                                         $skipFound++;
501                                                                         break;
502                                                                 } // END - if
503                                                         } // END - if
504
505                                                         // Entry found, so update it
506                                                         foreach ($criteriaArray as $criteriaKey => $criteriaValue) {
507                                                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DATABASE: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
508                                                                 $dataArray[$criteriaKey] = $criteriaValue;
509                                                         } // END - foreach
510
511                                                         // Write the data to a local file
512                                                         $this->writeDataArrayToFqfn($pathName . $dataFile, $dataArray);
513
514                                                         // Count it
515                                                         $limitFound++;
516                                                         break;
517                                                 } // END - if
518                                         } // END - foreach
519                                 } // END - if
520                         } // END - while
521
522                         // Close the file pointer
523                         $directoryInstance->closeDirectory();
524
525                         // Update the primary key
526                         $this->updatePrimaryKey($dataSetInstance);
527
528                         // Reset last exception
529                         $this->resetLastException();
530                 } catch (FrameworkException $e) {
531                         // Catch all exceptions and store them in last error
532                         $this->setLastException($e);
533
534                         // Throw an SQL exception
535                         throw new SqlException(array($this, sprintf("Cannot write data to table &#39;%s&#39;, is the table created?", $dataSetInstance->getTableName()), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY);
536                 }
537         }
538
539         /**
540          * Getter for primary key of specified table or if not found null will be
541          * returned. This must be database-specific.
542          *
543          * @param       $tableName              Name of the table we need the primary key from
544          * @return      $primaryKey             Primary key column of the given table
545          */
546         public function getPrimaryKeyOfTable ($tableName) {
547                 // Default key is null
548                 $primaryKey = NULL;
549
550                 // Does the table information exist?
551                 if (isset($this->tableInfo[$tableName])) {
552                         // Then return the primary key
553                         $primaryKey = $this->tableInfo[$tableName]['primary'];
554                 } // END - if
555
556                 // Return the column
557                 return $primaryKey;
558         }
559 }
560
561 // [EOF]
562 ?>