1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application |
4
|
|
|
* 2019 : Philippe M. <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, please view the LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Infrastructure; |
11
|
|
|
|
12
|
|
|
use App\Application\WikiPageAction; |
13
|
|
|
use Exception; |
14
|
|
|
use Mediawiki\Api\ApiUser; |
15
|
|
|
use Mediawiki\Api\MediawikiApi; |
16
|
|
|
use Mediawiki\Api\MediawikiFactory; |
17
|
|
|
use Mediawiki\Api\UsageException; |
18
|
|
|
use Mediawiki\DataModel\EditInfo; |
19
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
20
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ServiceFactory. |
24
|
|
|
*/ |
25
|
|
|
class ServiceFactory |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var AMQPStreamConnection |
29
|
|
|
*/ |
30
|
|
|
private static $AMQPConnection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MediawikiFactory |
34
|
|
|
*/ |
35
|
|
|
private static $wikiApi; |
36
|
|
|
|
37
|
|
|
// private static $dbConnection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MediawikiApi |
41
|
|
|
*/ |
42
|
|
|
private static $api; |
43
|
|
|
|
44
|
|
|
private function __construct() |
45
|
|
|
{ |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* AMQP queue (actual RabbitMQ) |
50
|
|
|
* todo $param |
51
|
|
|
* todo $channel->close(); $AMQPConnection->close();. |
52
|
|
|
* |
53
|
|
|
* @param string $queueName |
54
|
|
|
* |
55
|
|
|
* @return AMQPChannel |
56
|
|
|
*/ |
57
|
|
|
public static function queueChannel(string $queueName): AMQPChannel |
58
|
|
|
{ |
59
|
|
|
if (!isset(self::$AMQPConnection)) { |
60
|
|
|
self::$AMQPConnection = new AMQPStreamConnection( |
61
|
|
|
getenv('AMQP_HOST'), |
62
|
|
|
getenv('AMQP_PORT'), |
63
|
|
|
getenv('AMQP_USER'), |
64
|
|
|
getenv('AMQP_PASSWORD'), |
65
|
|
|
getenv('AMQP_VHOST') |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$channel = self::$AMQPConnection->channel(); |
70
|
|
|
|
71
|
|
|
$channel->queue_declare( |
72
|
|
|
$queueName, |
73
|
|
|
false, |
74
|
|
|
true, // won't be lost if MQ server restarts |
75
|
|
|
false, |
76
|
|
|
false |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
return $channel; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// --Commented out by Inspection START (21/04/2020 02:45): |
83
|
|
|
// /** |
84
|
|
|
// * @throws Exception |
85
|
|
|
// */ |
86
|
|
|
// public static function closeAMQPconnection() |
87
|
|
|
// { |
88
|
|
|
// if (isset(self::$AMQPConnection)) { |
89
|
|
|
// self::$AMQPConnection->close(); |
90
|
|
|
// self::$AMQPConnection = null; |
91
|
|
|
// } |
92
|
|
|
// } |
93
|
|
|
// --Commented out by Inspection STOP (21/04/2020 02:45) |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* todo? replace that singleton pattern ??? (multi-lang wiki?). |
98
|
|
|
* |
99
|
|
|
* @param bool|null $forceLogin |
100
|
|
|
* |
101
|
|
|
* @return MediawikiFactory |
102
|
|
|
* @throws UsageException |
103
|
|
|
*/ |
104
|
|
|
public static function wikiApi(?bool $forceLogin = false): MediawikiFactory |
105
|
|
|
{ |
106
|
|
|
if (isset(self::$wikiApi) && !$forceLogin) { |
107
|
|
|
return self::$wikiApi; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
self::$api = new MediawikiApi(getenv('WIKI_API_URL')); |
111
|
|
|
self::$api->login( |
112
|
|
|
new ApiUser(getenv('WIKI_API_USERNAME'), getenv('WIKI_API_PASSWORD')) |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
self::$wikiApi = new MediawikiFactory(self::$api); |
116
|
|
|
|
117
|
|
|
return self::$wikiApi; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// /** |
121
|
|
|
// * @return DbAdapter |
122
|
|
|
// */ |
123
|
|
|
// public static function sqlConnection(): DbAdapter |
124
|
|
|
// { |
125
|
|
|
// if (isset(self::$dbConnection)) { |
126
|
|
|
// return self::$dbConnection; |
127
|
|
|
// } |
128
|
|
|
// self::$dbConnection = new DbAdapter(); |
129
|
|
|
// |
130
|
|
|
// return self::$dbConnection; |
131
|
|
|
// } |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $title |
135
|
|
|
* @param bool $forceLogin |
136
|
|
|
* |
137
|
|
|
* @return WikiPageAction |
138
|
|
|
* @throws UsageException |
139
|
|
|
* @throws Exception |
140
|
|
|
*/ |
141
|
|
|
public static function wikiPageAction(string $title, $forceLogin = false): WikiPageAction |
142
|
|
|
{ |
143
|
|
|
$wiki = self::wikiApi($forceLogin); |
144
|
|
|
|
145
|
|
|
return new WikiPageAction($wiki, $title); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public static function editInfo($summary = '', $minor = false, $bot = false, $maxLag = 5) |
149
|
|
|
{ |
150
|
|
|
return new EditInfo($summary, $minor, $bot, $maxLag); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|