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\Application; |
11
|
|
|
|
12
|
|
|
use App\Domain\Utils\TextUtil; |
13
|
|
|
use App\Infrastructure\ServiceFactory; |
14
|
|
|
use Exception; |
15
|
|
|
use Mediawiki\Api\UsageException; |
16
|
|
|
use Mediawiki\DataModel\EditInfo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Freaky customization of WikiBotConfig class |
20
|
|
|
* Class ZiziBotConfig. |
21
|
|
|
*/ |
22
|
|
|
class ZiziBotConfig extends WikiBotConfig |
23
|
|
|
{ |
24
|
|
|
const BOT_TALK_SUMMARY = 'Réponse artificielle'; |
25
|
|
|
|
26
|
|
|
const BOT_TALK_FILE = __DIR__.'/resources/phrases_zizibot.txt'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add a freaky response in the bottom of the talk page. |
30
|
|
|
* |
31
|
|
|
* @param string|null $pageTitle |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
* @throws UsageException |
35
|
|
|
*/ |
36
|
|
|
public function botTalk(?string $pageTitle = null): bool |
37
|
|
|
{ |
38
|
|
|
// ugly dependency |
39
|
|
|
$wiki = ServiceFactory::wikiApi(); |
40
|
|
|
if (!$pageTitle) { |
41
|
|
|
$pageTitle = 'Discussion utilisateur:'.getenv('BOT_NAME'); |
42
|
|
|
} |
43
|
|
|
$page = new WikiPageAction($wiki, $pageTitle); |
44
|
|
|
$last = $page->page->getRevisions()->getLatest(); |
45
|
|
|
|
46
|
|
|
// No response if the last edition from bot or bot owner |
47
|
|
|
if (!$last->getUser() || in_array($last->getUser(), [getenv('BOT_NAME'), getenv('BOT_OWNER')])) { |
48
|
|
|
// compare with timestamp |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$addText = $this->generateTalkText($last->getUser()); |
53
|
|
|
|
54
|
|
|
echo "Prepare to talk on $pageTitle / Sleep 5 min...\n"; |
55
|
|
|
echo sprintf("-> %s \n", $addText); |
56
|
|
|
sleep(300); |
57
|
|
|
|
58
|
|
|
$editInfo = new EditInfo(static::BOT_TALK_SUMMARY); |
59
|
|
|
$success = $page->addToBottomOfThePage($addText, $editInfo); |
60
|
|
|
|
61
|
|
|
return (bool)$success; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|null $toEditor |
66
|
|
|
* @param string|null $identation |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
private function generateTalkText(?string $toEditor = null, ?string $identation = ':') |
72
|
|
|
{ |
73
|
|
|
$to = ($toEditor) ? sprintf('@[[User:%s|%s]] : ', $toEditor, $toEditor) : ''; // {{notif}} |
74
|
|
|
$sentence = TextUtil::mb_ucfirst($this->getRandomSentence()); |
|
|
|
|
75
|
|
|
if (!$sentence) { |
76
|
|
|
throw new Exception('no sentence'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return sprintf('%s%s%s --~~~~', $identation, $to, $sentence); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getRandomSentence(): ?string |
83
|
|
|
{ |
84
|
|
|
$sentences = file(self::BOT_TALK_FILE); |
85
|
|
|
if (!$sentences) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return (string)trim($sentences[array_rand($sentences)]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Todo |
94
|
|
|
* https://www.mediawiki.org/wiki/API:Usercontribs. |
95
|
|
|
*/ |
96
|
|
|
public function botContribs(): string |
97
|
|
|
{ |
98
|
|
|
$url |
99
|
|
|
= 'https://fr.wikipedia.org/w/api.php?action=query&list=usercontribs&ucuser='.getenv('BOT_NAME') |
100
|
|
|
.'&ucnamespace=0&uclimit=40&ucprop=title|timestamp|comment&format=json'; |
101
|
|
|
|
102
|
|
|
return file_get_contents($url); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|