/
database.php
executable file
·1162 lines (987 loc) · 26.5 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Joomla.Installation
* @subpackage Model
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Database configuration model for the Joomla Core Installer.
*
* @since 3.1
*/
class InstallationModelDatabase extends JModelBase
{
/**
* The generated user ID.
*
* @var integer
* @since 3.1
*/
protected static $userId = 0;
/**
* Generates the user ID.
*
* @return integer The user ID.
*
* @since 3.1
*/
protected static function generateRandUserId()
{
$session = JFactory::getSession();
$randUserId = $session->get('randUserId');
if (empty($randUserId))
{
// Create the ID for the root user only once and store in session.
$randUserId = mt_rand(1, 1000);
$session->set('randUserId', $randUserId);
}
return $randUserId;
}
/**
* Resets the user ID.
*
* @return void
*
* @since 3.1
*/
public static function resetRandUserId()
{
self::$userId = 0;
$session = JFactory::getSession();
$session->set('randUserId', self::$userId);
}
/**
* Retrieves the default user ID and sets it if necessary.
*
* @return integer The user ID.
*
* @since 3.1
*/
public static function getUserId()
{
if (!self::$userId)
{
self::$userId = self::generateRandUserId();
}
return self::$userId;
}
/**
* Method to initialise the database.
*
* @param array $options The options to use for configuration.
*
* @return JDatabaseDriver|boolean Database object on success, boolean false on failure
*
* @since 3.1
*/
public function initialise($options)
{
// Get the application.
/* @var InstallationApplicationWeb $app */
$app = JFactory::getApplication();
// Get the options as an object for easier handling.
$options = JArrayHelper::toObject($options);
// Load the back-end language files so that the DB error messages work.
$lang = JFactory::getLanguage();
$currentLang = $lang->getTag();
// Load the selected language
if (JLanguage::exists($currentLang, JPATH_ADMINISTRATOR))
{
$lang->load('joomla', JPATH_ADMINISTRATOR, $currentLang, true);
}
// Pre-load en-GB in case the chosen language files do not exist.
else
{
$lang->load('joomla', JPATH_ADMINISTRATOR, 'en-GB', true);
}
// Ensure a database type was selected.
if (empty($options->db_type))
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_INVALID_TYPE'), 'notice');
return false;
}
// Ensure that a hostname and user name were input.
if (empty($options->db_host) || empty($options->db_user))
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_INVALID_DB_DETAILS'), 'notice');
return false;
}
// Ensure that a database name was input.
if (empty($options->db_name))
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_EMPTY_NAME'), 'notice');
return false;
}
// Validate database table prefix.
if (!preg_match('#^[a-zA-Z]+[a-zA-Z0-9_]*$#', $options->db_prefix))
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_PREFIX_MSG'), 'notice');
return false;
}
// Validate length of database table prefix.
if (strlen($options->db_prefix) > 15)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_FIX_TOO_LONG'), 'notice');
return false;
}
// Validate length of database name.
if (strlen($options->db_name) > 64)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_TOO_LONG'), 'notice');
return false;
}
// Workaround for UPPERCASE table prefix for postgresql
if ($options->db_type == 'postgresql')
{
if (strtolower($options->db_prefix) != $options->db_prefix)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_FIX_LOWERCASE'), 'notice');
return false;
}
}
// Get a database object.
try
{
return InstallationHelperDatabase::getDbo(
$options->db_type, $options->db_host, $options->db_user, $options->db_pass, $options->db_name, $options->db_prefix, $options->db_select
);
}
catch (RuntimeException $e)
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'notice');
return false;
}
}
/**
* Method to create a new database.
*
* @param array $options The configuration options
*
* @return boolean True on success.
*
* @since 3.1
*/
public function createDatabase($options)
{
// Get the application.
/* @var InstallationApplicationWeb $app */
$app = JFactory::getApplication();
// Disable autoselect database before it's created.
$tmpSelect = true;
if (isset($options['db_select']))
{
$tmpSelect = $options['db_select'];
}
$options['db_select'] = false;
if (!$db = $this->initialise($options))
{
return false;
}
// Get the options as an object for easier handling.
$options = JArrayHelper::toObject($options);
// Check database version.
$type = $options->db_type;
try
{
$db_version = $db->getVersion();
}
catch (RuntimeException $e)
{
/*
* We may get here if the database doesn't exist, if so then explain that to users instead of showing the database connector's error
* This only supports PostgreSQL and the PDO MySQL drivers presently
*
* Error Messages:
* PDO MySQL: [1049] Unknown database 'database_name'
* PostgreSQL: Error connecting to PGSQL database
*/
if ($type == 'pdomysql' && strpos($e->getMessage(), '[1049] Unknown database') === 42)
{
/*
* Now we're really getting insane here; we're going to try building a new JDatabaseDriver instance without the database name
* in order to trick the connection into creating the database
*/
$altDBoptions = array(
'driver' => $options->db_type,
'host' => $options->db_host,
'user' => $options->db_user,
'password' => $options->db_pass,
'prefix' => $options->db_prefix,
'select' => $options->db_select
);
$altDB = JDatabaseDriver::getInstance($altDBoptions);
// Try to create the database now using the alternate driver
try
{
$this->createDb($altDB, $options, $altDB->hasUTFSupport());
}
catch (RuntimeException $e)
{
// We did everything we could
$app->enqueueMessage(JText::_('INSTL_DATABASE_COULD_NOT_CREATE_DATABASE'), 'notice');
return false;
}
// If we got here, the database should have been successfully created, now try one more time to get the version
try
{
$db_version = $db->getVersion();
}
catch (RuntimeException $e)
{
// We did everything we could
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'notice');
return false;
}
}
elseif ($type == 'postgresql' && strpos($e->getMessage(), 'Error connecting to PGSQL database') === 42)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_COULD_NOT_CREATE_DATABASE'), 'notice');
return false;
}
// Anything getting into this part of the conditional either doesn't support manually creating the database or isn't that type of error
else
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_CONNECT', $e->getMessage()), 'notice');
return false;
}
}
if (!$db->isMinimumVersion())
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_INVALID_' . strtoupper($type) . '_VERSION', $db_version), 'notice');
return false;
}
if (($type == 'mysql') || ($type == 'mysqli') || ($type == 'pdomysql'))
{
// @internal MySQL versions pre 5.1.6 forbid . / or \ or NULL.
if ((preg_match('#[\\\/\.\0]#', $options->db_name)) && (!version_compare($db_version, '5.1.6', '>=')))
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_INVALID_NAME', $db_version), 'notice');
return false;
}
}
// @internal Check for spaces in beginning or end of name.
if (strlen(trim($options->db_name)) <> strlen($options->db_name))
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_INVALID_SPACES'), 'notice');
return false;
}
// @internal Check for asc(00) Null in name.
if (strpos($options->db_name, chr(00)) !== false)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_NAME_INVALID_CHAR'), 'notice');
return false;
}
// PostgreSQL database older than version 9.0.0 needs to run 'CREATE LANGUAGE' to create function.
if (($options->db_type == 'postgresql') && (!version_compare($db_version, '9.0.0', '>=')))
{
$db->setQuery("select lanpltrusted from pg_language where lanname='plpgsql'");
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_ERROR_POSTGRESQL_QUERY'), 'notice');
return false;
}
$column = $db->loadResult();
if ($column != 't')
{
$db->setQuery("CREATE LANGUAGE plpgsql");
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$app->enqueueMessage(JText::_('INSTL_DATABASE_ERROR_POSTGRESQL_QUERY'), 'notice');
return false;
}
}
}
// Get database's UTF support.
$utfSupport = $db->hasUTFSupport();
// Try to select the database.
try
{
$db->select($options->db_name);
}
catch (RuntimeException $e)
{
// If the database could not be selected, attempt to create it and then select it.
if ($this->createDb($db, $options, $utfSupport))
{
$db->select($options->db_name);
}
else
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_ERROR_CREATE', $options->db_name), 'notice');
return false;
}
}
$options = (array) $options;
// Remove *_errors value.
foreach ($options as $i => $option)
{
if (isset($i['1']) && $i['1'] == '*')
{
unset($options[$i]);
break;
}
}
$options = array_merge(array('db_created' => 1), $options);
// Restore autoselect value after database creation.
$options['db_select'] = $tmpSelect;
$session = JFactory::getSession();
$session->set('setup.options', $options);
return true;
}
/**
* Method to process the old database.
*
* @param array $options The options array.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function handleOldDatabase($options)
{
if (!isset($options['db_created']) || !$options['db_created'])
{
return $this->createDatabase($options);
}
if (!$db = $this->initialise($options))
{
return false;
}
// Get the options as an object for easier handling.
$options = JArrayHelper::toObject($options);
// Set the character set to UTF-8 for pre-existing databases.
$this->setDatabaseCharset($db, $options->db_name);
// Should any old database tables be removed or backed up?
if ($options->db_old == 'remove')
{
// Attempt to delete the old database tables.
if (!$this->deleteDatabase($db, $options->db_prefix))
{
// Message queued by method, simply return
return false;
}
}
else
{
// If the database isn't being deleted, back it up.
if (!$this->backupDatabase($db, $options->db_prefix))
{
return false;
}
}
return true;
}
/**
* Method to create the database tables.
*
* @param array $options The options array.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function createTables($options)
{
// Get the application.
/* @var InstallationApplicationWeb $app */
$app = JFactory::getApplication();
if (!isset($options['db_created']) || !$options['db_created'])
{
return $this->createDatabase($options);
}
if (!$db = $this->initialise($options))
{
return false;
}
// Get the options as an object for easier handling.
$options = JArrayHelper::toObject($options);
// Check database type.
$type = $options->db_type;
// Set the character set to UTF-8 for pre-existing databases.
$this->setDatabaseCharset($db, $options->db_name);
// Set the appropriate schema script based on UTF-8 support.
if (($type == 'mysql') || ($type == 'mysqli') || ($type == 'pdomysql'))
{
$schema = 'sql/mysql/joomla.sql';
}
elseif ($type == 'sqlsrv' || $type == 'sqlazure')
{
$schema = 'sql/sqlazure/joomla.sql';
}
else
{
$schema = 'sql/' . $type . '/joomla.sql';
}
// Check if the schema is a valid file
if (!is_file($schema))
{
$app->enqueueMessage(JText::sprintf('INSTL_ERROR_DB', JText::_('INSTL_DATABASE_NO_SCHEMA')), 'notice');
return false;
}
// Attempt to import the database schema.
if (!$this->populateDatabase($db, $schema))
{
return false;
}
// Get query object for later database access
$query = $db->getQuery(true);
// MySQL only: Attempt to update the table #__utf8_conversion.
$serverType = $db->getServerType();
if ($serverType === 'mysql')
{
$query->clear()
->update($db->quoteName('#__utf8_conversion'))
->set($db->quoteName('converted') . ' = ' . ($db->hasUTF8mb4Support() ? 2 : 1));
$db->setQuery($query);
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$app->enqueueMessage($e->getMessage(), 'notice');
return false;
}
}
// Attempt to update the table #__schema.
$pathPart = JPATH_ADMINISTRATOR . '/components/com_admin/sql/updates/';
if (($type == 'mysql') || ($type == 'mysqli') || ($type == 'pdomysql'))
{
$pathPart .= 'mysql/';
}
elseif ($type == 'sqlsrv' || $type == 'sqlazure')
{
$pathPart .= 'sqlazure/';
}
else
{
$pathPart .= $type . '/';
}
$files = JFolder::files($pathPart, '\.sql$');
if (empty($files))
{
$app->enqueueMessage(JText::_('INSTL_ERROR_INITIALISE_SCHEMA'), 'notice');
return false;
}
$version = '';
foreach ($files as $file)
{
if (version_compare($version, JFile::stripExt($file)) < 0)
{
$version = JFile::stripExt($file);
}
}
$query->clear()
->insert($db->quoteName('#__schemas'))
->columns(
array(
$db->quoteName('extension_id'),
$db->quoteName('version_id')
)
)
->values('700, ' . $db->quote($version));
$db->setQuery($query);
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$app->enqueueMessage($e->getMessage(), 'notice');
return false;
}
// Attempt to refresh manifest caches.
$query->clear()
->select('*')
->from('#__extensions');
$db->setQuery($query);
$return = true;
try
{
$extensions = $db->loadObjectList();
}
catch (RuntimeException $e)
{
$app->enqueueMessage($e->getMessage(), 'notice');
$return = false;
}
JFactory::$database = $db;
$installer = JInstaller::getInstance();
foreach ($extensions as $extension)
{
if (!$installer->refreshManifestCache($extension->extension_id))
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_COULD_NOT_REFRESH_MANIFEST_CACHE', $extension->name), 'notice');
return false;
}
}
// Load the localise.sql for translating the data in joomla.sql.
if (($type == 'mysql') || ($type == 'mysqli') || ($type == 'pdomysql'))
{
$dblocalise = 'sql/mysql/localise.sql';
}
elseif ($type == 'sqlsrv' || $type == 'sqlazure')
{
$dblocalise = 'sql/sqlazure/localise.sql';
}
else
{
$dblocalise = 'sql/' . $type . '/localise.sql';
}
if (is_file($dblocalise))
{
if (!$this->populateDatabase($db, $dblocalise))
{
return false;
}
}
// Handle default backend language setting. This feature is available for localized versions of Joomla.
$app = JFactory::getApplication();
$languages = $app->getLocaliseAdmin($db);
if (in_array($options->language, $languages['admin']) || in_array($options->language, $languages['site']))
{
// Build the language parameters for the language manager.
$params = array();
// Set default administrator/site language to sample data values.
$params['administrator'] = 'en-GB';
$params['site'] = 'en-GB';
if (in_array($options->language, $languages['admin']))
{
$params['administrator'] = $options->language;
}
if (in_array($options->language, $languages['site']))
{
$params['site'] = $options->language;
}
$params = json_encode($params);
// Update the language settings in the language manager.
$query->clear()
->update($db->quoteName('#__extensions'))
->set($db->quoteName('params') . ' = ' . $db->quote($params))
->where($db->quoteName('element') . ' = ' . $db->quote('com_languages'));
$db->setQuery($query);
try
{
$db->execute();
}
catch (RuntimeException $e)
{
$app->enqueueMessage($e->getMessage(), 'notice');
$return = false;
}
}
return $return;
}
/**
* Method to install the sample data.
*
* @param array $options The options array.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function installSampleData($options)
{
// Get the application.
/* @var InstallationApplicationWeb $app */
$app = JFactory::getApplication();
if (!isset($options['db_created']) || !$options['db_created'])
{
return $this->createDatabase($options);
}
if (!$db = $this->initialise($options))
{
return false;
}
// Get the options as an object for easier handling.
$options = JArrayHelper::toObject($options);
// Build the path to the sample data file.
$type = $options->db_type;
if ($type == 'mysqli' || $type == 'pdomysql')
{
$type = 'mysql';
}
elseif ($type == 'sqlsrv')
{
$type = 'sqlazure';
}
$data = JPATH_INSTALLATION . '/sql/' . $type . '/' . $options->sample_file;
// Attempt to import the database schema if one is chosen.
if ($options->sample_file != '')
{
if (!file_exists($data))
{
$app->enqueueMessage(JText::sprintf('INSTL_DATABASE_FILE_DOES_NOT_EXIST', $data), 'notice');
return false;
}
elseif (!$this->populateDatabase($db, $data))
{
return false;
}
$this->postInstallSampleData($db);
}
return true;
}
/**
* Sample data tables and data post install process.
*
* @param JDatabaseDriver $db Database connector object $db*.
*
* @return void
*
* @since 3.1
*/
protected function postInstallSampleData($db)
{
// Update the sample data user ids.
$this->updateUserIds($db);
}
/**
* Method to install the cms data.
*
* @param array $options The options array.
*
* @return boolean True on success.
*
* @since 3.6.1
*/
public function installCmsData($options)
{
// Attempt to create the database tables.
if (!$this->createTables($options))
{
return false;
}
if (!$db = $this->initialise($options))
{
return false;
}
// Run Cms data post install to update user ids.
return $this->postInstallCmsData($db);
}
/**
* Cms tables and data post install process.
*
* @param JDatabaseDriver $db Database connector object $db*.
*
* @return void
*
* @since 3.6.1
*/
protected function postInstallCmsData($db)
{
// Update the sample data user ids.
$this->updateUserIds($db);
}
/**
* Method to update the user id of sql data content to the new rand user id.
*
* @param JDatabaseDriver $db Database connector object $db*.
*
* @return boolean True on success.
*
* @since 3.6.1
*/
protected function updateUserIds($db)
{
// Create the ID for the root user.
$userId = self::getUserId();
// Update all created_by field of the tables with the random user id
// categories (created_user_id), contact_details, content, newsfeeds.
$updates_array = array(
'categories' => 'created_user_id',
'contact_details' => 'created_by',
'content' => 'created_by',
'newsfeeds' => 'created_by',
'tags' => 'created_user_id',
'ucm_content' => 'core_created_user_id',
'ucm_history' => 'editor_user_id',
);
foreach ($updates_array as $table => $field)
{
$db->setQuery(
'UPDATE ' . $db->quoteName('#__' . $table) .
' SET ' . $db->quoteName($field) . ' = ' . $db->quote($userId)
);
$db->execute();
}
}
/**
* Method to backup all tables in a database with a given prefix.
*
* @param JDatabaseDriver $db JDatabaseDriver object.
* @param string $prefix Database table prefix.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function backupDatabase($db, $prefix)
{
$return = true;
$backup = 'bak_' . $prefix;
// Get the tables in the database.
$tables = $db->getTableList();
if ($tables)
{
foreach ($tables as $table)
{
// If the table uses the given prefix, back it up.
if (strpos($table, $prefix) === 0)
{
// Backup table name.
$backupTable = str_replace($prefix, $backup, $table);
// Drop the backup table.
try
{
$db->dropTable($backupTable, true);
}
catch (RuntimeException $e)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('INSTL_DATABASE_ERROR_BACKINGUP', $e->getMessage()), 'notice');
$return = false;
}
// Rename the current table to the backup table.
try
{
$db->renameTable($table, $backupTable, $backup, $prefix);
}
catch (RuntimeException $e)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('INSTL_DATABASE_ERROR_BACKINGUP', $e->getMessage()), 'notice');
$return = false;
}
}
}
}
return $return;
}
/**
* Method to create a new database.
*
* @param JDatabaseDriver $db JDatabase object.
* @param JObject $options JObject coming from "initialise" function to pass user
* and database name to database driver.
* @param boolean $utf True if the database supports the UTF-8 character set.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function createDb($db, $options, $utf)
{
// Build the create database query.
try
{
// Run the create database query.
$db->createDatabase($options, $utf);
}
catch (RuntimeException $e)
{
// If an error occurred return false.
return false;
}
return true;
}
/**
* Method to delete all tables in a database with a given prefix.
*
* @param JDatabaseDriver $db JDatabaseDriver object.
* @param string $prefix Database table prefix.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function deleteDatabase($db, $prefix)
{
$return = true;
// Get the tables in the database.
$tables = $db->getTableList();
if ($tables)
{
foreach ($tables as $table)
{
// If the table uses the given prefix, drop it.
if (strpos($table, $prefix) === 0)
{
// Drop the table.
try
{
$db->dropTable($table);
}
catch (RuntimeException $e)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('INSTL_DATABASE_ERROR_DELETE', $e->getMessage()), 'notice');
$return = false;
}
}
}
}
return $return;
}
/**
* Method to import a database schema from a file.
*
* @param JDatabaseDriver $db JDatabase object.
* @param string $schema Path to the schema file.
*
* @return boolean True on success.
*
* @since 3.1
*/
public function populateDatabase($db, $schema)
{
// Get the application
/* @var InstallationApplicationWeb $app */
$app = JFactory::getApplication();
$return = true;