-
Notifications
You must be signed in to change notification settings - Fork 0
/
freecad_wiki_sync_to_wiki.php
205 lines (161 loc) · 6.76 KB
/
freecad_wiki_sync_to_wiki.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
<?php
require_once('botclasses.php');
date_default_timezone_set('UTC'); // Set timezone to UTC
$githubRepoPath = "Reqrefusion/FreeCAD-Documentation-Project";
$githubToken = "ghp_XXX";
$wikiUsername = 'XXX';
$wikiPassword = 'XXX';
$apiUrl = 'https://wiki.freecad.org/api.php';
$webhookSecret = "XXX";
$languageCodes = array(
'bg', 'cs', 'de', 'en', 'es', 'fi', 'fr', 'hr', 'hu', 'id', 'it', 'ja',
'ko', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sk', 'sv', 'tr', 'uk', 'vi', 'zh', 'zh-cn', 'zh-tw', 'zh-hant'
);
// Function to retrieve file content from GitHub repository
function getGithubFileContent($filePath) {
global $githubRepoPath, $githubToken;
$githubApiUrl = "https://api.github.com/repos/$githubRepoPath/contents/" . $filePath;
$ch = curl_init($githubApiUrl);
// Set authorization and user-agent headers
$headers = array(
'Authorization: token ' . $githubToken,
'User-Agent: PHP'
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Decode response if successful
if ($httpcode == 200) {
$content = json_decode($response, true);
return array(
'content' => base64_decode($content['content']), // Decode base64 content
'sha' => $content['sha'], // Get file SHA
);
} else {
return null;
}
}
// Function to reverse file path for Wiki format
function reverseFilePath($filePath) {
global $languageCodes;
// Remove 'wiki/' prefix from the file path
$filePath = str_replace('wiki/', '', $filePath);
$parts = explode('/', $filePath);
// Check for language code in the path
$languageCode = null;
if (in_array($parts[0], $languageCodes)) {
$languageCode = array_shift($parts); // Remove and save language code
}
// Get file name, removing ".wikitext" and replacing characters
$fileName = str_replace('.wikitext', '', array_pop($parts));
$fileName = str_replace('_', ' ', $fileName);
$fileName = str_replace(';', ':', $fileName);
// Append language code to file name if available
if ($languageCode) {
$fileName .= '/' . $languageCode;
}
// Rebuild path if there are remaining parts
if (!empty($parts)) {
$fileName = implode('/', $parts) . '/' . $fileName;
}
return $fileName;
}
// Function to upload a file to MediaWiki
function uploadFileToWiki($filePath, $fileContent, $summary, $bot) {
// Save the file locally (temporarily)
$tempFilePath = sys_get_temp_dir() . '/' . basename($filePath);
file_put_contents($tempFilePath, $fileContent);
// Extract the file title
$fileTitle = basename($filePath);
$fileTitle = str_replace('wiki/File/', '', $fileTitle); // Remove 'wiki/File/' prefix
// Attempt to upload the file
$uploadResult = $bot->upload($fileTitle, $tempFilePath, $summary);
// Delete the temporary file
unlink($tempFilePath);
// Return the upload result
return $uploadResult;
}
// Verify webhook signature
$payload = file_get_contents('php://input');
$signature = 'sha256=' . hash_hmac('sha256', $payload, $webhookSecret);
$headers = getallheaders();
if (!isset($headers['X-Hub-Signature-256']) || !hash_equals($signature, $headers['X-Hub-Signature-256'])) {
http_response_code(403);
exit('Invalid webhook signature');
}
$data = json_decode($payload, true);
// Initialize bot and login once
$bot = new wikipedia($apiUrl, $wikiUsername, $wikiPassword);
if (!$bot->login($wikiUsername, $wikiPassword)) {
echo "Login failed.\n";
exit;
}
echo "Successfully logged in.\n";
if (!empty($data['commits'])) {
foreach ($data['commits'] as $commit) {
// Check if the author email is valid
if (!str_ends_with($commit['author']['email'], '@users.noreply.github.com')) {
echo "No action taken: Author email is invalid ({$commit['author']['email']}).\n";
continue;
}
$authorUserName = $commit['author']['username'];
$commitMessage = $commit['message'];
$shortCommitId = substr($commit['id'], 0, 7);
// Prepare the summary string once
$summary = "$shortCommitId $commitMessage (Author: $authorUserName)";
// Process both added and modified files together
$filesToProcess = array_merge($commit['added'], $commit['modified']);
foreach ($filesToProcess as $filePath) {
// Skip files outside the 'wiki/' directory
if (strpos($filePath, "wiki/") !== 0) {
continue;
}
// Retrieve the file content from GitHub
$githubFile = getGithubFileContent($filePath);
if ($githubFile) {
if (strpos($filePath, "wiki/File/") === 0) {
// File upload support
echo "Uploading file: $filePath\n";
$uploadResult = uploadFileToWiki($filePath, $githubFile['content'], $summary, $bot);
if ($uploadResult['upload']['result'] === 'Success') {
echo "File $filePath successfully uploaded.\n";
} else {
echo "File $filePath upload failed.\n";
print_r($uploadResult);
}
} else {
// Process wiki pages
$wikiTitle = reverseFilePath($filePath);
echo "Updated Wiki Title: $wikiTitle\n";
echo "Author: $authorUserName\n";
echo "Commit Message: $commitMessage\n";
$wikiContent = $bot->getpage($wikiTitle);
// Check if the content has changed
if (trim($wikiContent) != trim($githubFile['content'])) {
$editToken = $bot->getedittoken($wikiTitle);
if (!$editToken) {
echo "ERROR: Failed to retrieve edit token.\n";
continue;
}
$editResult = $bot->edit($wikiTitle, $githubFile['content'], $summary, $editToken);
if ($editResult['edit']['result'] === 'Success') {
echo "$wikiTitle successfully updated.\n";
} else {
echo "$wikiTitle update failed.\n";
print_r($editResult);
}
} else {
echo "$wikiTitle is already up-to-date.\n";
}
}
} else {
echo "Failed to retrieve GitHub file content: $filePath\n";
}
}
}
} else {
echo "No files found in the commit.\n";
}
?>