22
22
23
23
use Pydio \Core \Model \ContextInterface ;
24
24
25
+ use Pydio \Core \PluginFramework \SqlTableProvider ;
26
+ use Pydio \Core \Utils \DBHelper ;
25
27
use Pydio \Core \Utils \FileHelper ;
26
28
use Pydio \Core \Utils \Vars \OptionsHelper ;
27
29
37
39
* @package AjaXplorer_Plugins
38
40
* @subpackage Mq
39
41
*/
40
- class SqlMessageExchanger extends Plugin implements IMessageExchanger
42
+ class SqlMessageExchanger extends Plugin implements IMessageExchanger, SqlTableProvider
41
43
{
42
44
43
45
/**
@@ -77,9 +79,11 @@ public function loadChannel($channelName, $create = false)
77
79
if (isSet (self ::$ channels ) && is_array (self ::$ channels [$ channelName ])) {
78
80
return ;
79
81
}
80
- if (is_file ($ this ->getPluginWorkDir ()."/queues/channel- $ channelName " )) {
82
+ $ res = dibi::query ('SELECT [content] FROM [ajxp_mq_queues] WHERE [channel_name] = %s ' , $ channelName );
83
+ if ($ res ->count ()){
81
84
if (!isset (self ::$ channels )) self ::$ channels = array ();
82
- $ data = FileHelper::loadSerialFile ($ this ->getPluginWorkDir () . "/queues/channel- $ channelName " );
85
+ $ single = $ res ->fetchSingle ();
86
+ $ data = unserialize (base64_decode ($ single ));
83
87
if (is_array ($ data )) {
84
88
if (!is_array ($ data ["MESSAGES " ])) $ data ["MESSAGES " ] = array ();
85
89
if (!is_array ($ data ["CLIENTS " ])) $ data ["CLIENTS " ] = array ();
@@ -97,9 +101,36 @@ public function loadChannel($channelName, $create = false)
97
101
public function __destruct ()
98
102
{
99
103
if (isSet (self ::$ channels ) && is_array (self ::$ channels )) {
104
+ $ inserts = [];
105
+ $ deletes = [];
106
+ $ driver = $ this ->sqlDriver ["driver " ];
100
107
foreach (self ::$ channels as $ channelName => $ data ) {
101
108
if (is_array ($ data )) {
102
- FileHelper::saveSerialFile ($ this ->getPluginWorkDir () . "/queues/channel- $ channelName " , $ data );
109
+ if (isSet ($ data ["CLIENTS " ]) && count ($ data ["CLIENTS " ])) {
110
+ $ serialized = base64_encode (serialize ($ data ));
111
+ if ($ driver === "postgre " ){
112
+ dibi::query ("DELETE FROM [ajxp_mq_queues] WHERE [channel_name] = %s " , $ channelName );
113
+ dibi::query ('INSERT INTO [ajxp_mq_queues] ' , ["channel_name " => $ channelName , "content " => $ serialized ]);
114
+ }else {
115
+ $ inserts [] = "(' $ channelName', ' " .$ serialized ."') " ;
116
+ }
117
+ }else {
118
+ $ deletes [] = $ channelName ;
119
+ }
120
+ }
121
+ }
122
+ if (count ($ inserts )){
123
+ try {
124
+ dibi::query ('REPLACE INTO [ajxp_mq_queues] ([channel_name],[content]) VALUES ' .implode (", " , $ inserts ));
125
+ }catch (\DibiException $ dE ){
126
+ $ this ->logError (__CLASS__ , $ dE ->getMessage ());
127
+ }
128
+ }
129
+ if (count ($ deletes )){
130
+ try {
131
+ dibi::query ('DELETE FROM [ajxp_mq_queues] WHERE [channel_name] IN %s ' , $ deletes );
132
+ }catch (\DibiException $ dE ){
133
+ $ this ->logError (__CLASS__ , $ dE ->getMessage ());
103
134
}
104
135
}
105
136
}
@@ -337,4 +368,14 @@ public function publishInstantMessage(ContextInterface $ctx, $channel, $message)
337
368
self ::$ channels [$ channel ]["MESSAGES " ] = $ clean ;
338
369
}
339
370
371
+ /**
372
+ * @param array $param
373
+ * @return string
374
+ * @throws \Exception
375
+ */
376
+ public function installSQLTables ($ param )
377
+ {
378
+ $ p = OptionsHelper::cleanDibiDriverParameters (isSet ($ param ) && isSet ($ param ["SQL_DRIVER " ]) ? $ param ["SQL_DRIVER " ] : $ this ->sqlDriver );
379
+ return DBHelper::runCreateTablesQuery ($ p , $ this ->getBaseDir () . "/create.sql " );
380
+ }
340
381
}
0 commit comments