@@ -45,59 +45,72 @@ protected function execute(InputInterface $input, OutputInterface $output)
45
45
{
46
46
parent ::execute ($ input , $ output );
47
47
$ _configuration = $ this ->getConfigurationArray ();
48
- $ connection = $ this ->getConnection ($ input );
48
+ $ conn = $ this ->getConnection ($ input );
49
49
50
50
$ parent = $ input ->getArgument ('parent ' );
51
51
$ lang = $ input ->getArgument ('sublanguage ' );
52
- $ sql = "SELECT english_name FROM language WHERE english_name = ? " ;
53
- $ statement = $ connection ->executeQuery ($ sql , array ($ lang ));
54
- $ count = $ statement ->rowCount ();
52
+ if ($ conn instanceof \Doctrine \DBAL \Connection) {
53
+ $ langQuoted = $ conn ->quote ($ lang );
54
+ $ sql = "SELECT english_name FROM language WHERE english_name = $ langQuoted " ;
55
+ try {
56
+ $ stmt = $ conn ->prepare ($ sql );
57
+ $ stmt ->execute ();
58
+ } catch (\PDOException $ e ) {
59
+ $ output ->write ('SQL error! ' .PHP_EOL );
60
+ throw new \RuntimeException ($ e ->getMessage (), $ e ->getCode (), $ e );
61
+ }
55
62
56
- if ($ count ) {
57
- $ output ->writeln ($ lang .' already exists in the database. Pick another English name. ' );
58
- return null ;
59
- }
63
+ $ count = $ stmt ->rowCount ();
64
+ if ($ count ) {
65
+ $ output ->writeln ($ lang .' already exists in the database. Pick another English name. ' );
66
+ return null ;
67
+ }
60
68
61
- $ sql = "SELECT id, original_name, english_name, isocode, dokeos_folder
62
- FROM language WHERE english_name = ? " ;
63
- $ statement = $ connection ->prepare ($ sql );
64
- $ statement ->bindValue ('1 ' , $ parent );
65
- $ statement ->execute ();
66
- $ count = $ statement ->rowCount ();
67
- $ parentData = $ statement ->fetch ();
69
+ $ parentQuoted = $ conn ->quote ($ parent );
70
+ $ sql = "SELECT id, original_name, english_name, isocode, dokeos_folder
71
+ FROM language WHERE english_name = $ parentQuoted " ;
72
+ try {
73
+ $ stmt2 = $ conn ->prepare ($ sql );
74
+ $ stmt2 ->execute ();
75
+ } catch (\PDOException $ e ) {
76
+ $ output ->write ('SQL error! ' .PHP_EOL );
77
+ throw new \RuntimeException ($ e ->getMessage (), $ e ->getCode (), $ e );
78
+ }
79
+ $ count = $ stmt2 ->rowCount ();
80
+ $ parentData = $ stmt2 ->fetch ();
68
81
69
- if ($ count < 1 ) {
70
- $ output ->writeln (' The parent language ' . $ parent . ' does not exist. Please choose a valid parent. ' );
71
- return null ;
72
- }
82
+ if ($ count < 1 ) {
83
+ $ output ->writeln (" The parent language $ parentQuoted does not exist. Please choose a valid parent. " );
84
+ return null ;
85
+ }
73
86
74
- if (is_dir ($ _configuration ['root_sys ' ].'main/lang/ ' .$ lang )) {
75
- $ output ->writeln ('The destination directory ( ' .$ _configuration ['root_sys ' ].'main/lang/ ' .$ lang .') already exists. Please choose another sub-language name. ' );
76
- return null ;
77
- }
87
+ if (is_dir ($ _configuration ['root_sys ' ].'main/lang/ ' .$ lang )) {
88
+ $ output ->writeln ('The destination directory ( ' .$ _configuration ['root_sys ' ].'main/lang/ ' .$ lang .') already exists. Please choose another sub-language name. ' );
89
+ return null ;
90
+ }
78
91
79
- // Everything is OK so far, insert the sub-language
80
- /*$sql = "INSERT INTO language ()
81
- VALUES ('{$parentData['original_name']}-2','$lang','{$parentData['isocode']}','$lang',0,{$parentData['id']})";*/
82
- $ result = $ connection ->insert ('language ' , array (
83
- 'original_name ' => $ parentData ['original_name ' ]."-2 " ,
84
- 'english_name ' => $ lang ,
85
- 'isocode ' => $ parentData ['isocode ' ],
86
- 'dokeos_folder ' => $ lang ,
87
- 'available ' => 0 ,
88
- 'parent_id ' => $ parentData ['id ' ]
89
- )
90
- );
91
-
92
- if ($ result ) {
93
- $ output ->writeln ('Error in query: ' .mysql_error ());
94
- } else {
95
- //permissions gathering, copied from main_api.lib.php::api_get_permissions_for_new_directories()
92
+ // Everything is OK so far, insert the sub-language
93
+ try {
94
+ $ conn ->insert ('language ' , array (
95
+ 'original_name ' => $ parentData ['original_name ' ]."-2 " ,
96
+ 'english_name ' => $ lang ,
97
+ 'isocode ' => $ parentData ['isocode ' ],
98
+ 'dokeos_folder ' => $ lang ,
99
+ 'available ' => 0 ,
100
+ 'parent_id ' => $ parentData ['id ' ]
101
+ ));
102
+ } catch (\PDOException $ e ) {
103
+ $ output ->write ('SQL error! ' .PHP_EOL );
104
+ throw new \RuntimeException ($ e ->getMessage (), $ e ->getCode (), $ e );
105
+ }
106
+ //Permissions gathering, copied from main_api.lib.php::api_get_permissions_for_new_directories()
96
107
//require_once $_configuration['root_sys'].'main/inc/lib/main_api.lib.php';
97
108
//$perm = api_get_permissions_for_new_directories();
98
109
// @todo Improve permissions to force creating as user www-data
99
110
$ r = @mkdir ($ _configuration ['root_sys ' ].'main/lang/ ' .$ lang , 0777 );
100
111
$ output ->writeln ('Sub-language ' .$ lang .' of language ' .$ parent .' has been created but is disabled. Fill it, then enable to make available to users. Make sure you check the permissions for the newly created directory as well ( ' .$ _configuration ['root_sys ' ].'main/lang/ ' .$ lang .') ' );
112
+ } else {
113
+ $ output ->writeln ('The connection does not seem to be a valid PDO connection ' );
101
114
}
102
115
return null ;
103
116
}
0 commit comments