-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreecad_wiki_sync_to_github_pages.php
234 lines (183 loc) · 8.17 KB
/
freecad_wiki_sync_to_github_pages.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
date_default_timezone_set('UTC'); // Set timezone to UTC
$githubRepoPath = "Reqrefusion/FreeCAD-Documentation-Project"; // GitHub repository path
$githubToken = "ghp_XXX"; // Place your GitHub API token here.
$invalidPrefixes = array('Translations:', 'Draft:', 'Help:', 'File:'); // Add more invalid terms if needed.
$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'
);
// Function to get the file content from GitHub
function getGithubFileContent($filePath) {
global $githubRepoPath, $githubToken;
$githubApiUrl = "https://api.github.com/repos/$githubRepoPath/contents/" . $filePath;
$ch = curl_init($githubApiUrl);
$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);
if ($httpcode == 200) {
$content = json_decode($response, true);
return base64_decode($content['content']); // Decode the Base64 content.
} else {
return null;
}
}
// Function to update or create a file on GitHub
function updateGithubFile($filePath, $newContent, $sha, $pageTitle, $user, $comment) {
global $githubRepoPath, $githubToken;
$githubApiUrl = "https://api.github.com/repos/$githubRepoPath/contents/" . $filePath;
$commitMessage = $comment ?: "Updated page $pageTitle"; // Default commit message if no comment
// FreeCAD Wiki message board URL for the user
$wikiUserUrl = "https://wiki.freecad.org/User:" . urlencode($user);
// Author information for the commit
$authorData = array(
"name" => $user, // FreeCAD Wiki username
"email" => $wikiUserUrl // Message board URL instead of email
);
// Data to send to GitHub API
$data = array(
"message" => $commitMessage,
"content" => base64_encode($newContent), // Encode the content in Base64
"sha" => $sha, // SHA of the previous file
"author" => $authorData // Author information
);
// Send the request via cURL
$ch = curl_init($githubApiUrl);
$headers = array(
'Authorization: token ' . $githubToken,
'User-Agent: PHP',
'Content-Type: application/json'
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseData = json_decode($response, true);
curl_close($ch);
if ($httpcode == 200) {
echo "SUCCESS: '$filePath' successfully updated on GitHub by $user.<br>";
return true;
} else {
echo "ERROR: Failed to update '$filePath' on GitHub. HTTP Code: $httpcode. Message: " . $responseData['message'] . "<br>";
return false;
}
}
// Function to get the SHA of an existing file on GitHub
function getShaForFile($filePath) {
global $githubRepoPath, $githubToken;
$githubApiUrl = "https://api.github.com/repos/$githubRepoPath/contents/" . $filePath;
$ch = curl_init($githubApiUrl);
$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);
if ($httpcode == 200) {
$content = json_decode($response, true);
return $content['sha']; // Return the SHA value
} else {
echo "ERROR: Could not fetch SHA for '$filePath'. HTTP Code: $httpcode.<br>";
return null;
}
}
// Function to create a valid file path based on the page title
function createFilePath($pageTitle, $type = "wikitext") {
global $languageCodes;
$parts = explode('/', $pageTitle);
$lastPart = end($parts);
// Check if the last part is a language code
$languageCode = null;
foreach ($languageCodes as $code) {
if ($lastPart === $code) {
$languageCode = $code;
break;
}
}
// Create content parts
$contentParts = $languageCode ? array_slice($parts, 0, -1) : $parts;
// Sanitize the content and build the file path
$sanitizedContent = array_map('sanitize_name', $contentParts);
$fileName = implode('/', $sanitizedContent); // Without language code
if ($type == "wikitext") {
$fileName .= ".wikitext";
}
// Prepend language code if present
if ($languageCode) {
return "wiki/" . $languageCode . "/" . $fileName;
}
return "wiki/" . $fileName; // If no language code
}
// Function to sanitize file names
function sanitize_name($name) {
return str_replace(array(' ', ':'), array('_', ';'), $name); // Replace invalid characters
}
// Check for recent changes made in the last 20 minutes
$currentTime = new DateTime(); // Get the current time
$timeLimit = clone $currentTime;
$timeLimit->modify('-20 minutes'); // Go back 20 minutes
// Format the timestamp
$timestampStart = $timeLimit->format('Y-m-d\TH:i:s\Z'); // 20 minutes ago timestamp
$wikiRecentChangesUrl = "https://wiki.freecad.org/api.php?action=query&list=recentchanges&rcprop=title|timestamp|user|comment&rclimit=500&rcend=" . $timestampStart . "&format=json";
echo $wikiRecentChangesUrl;
// Fetch the recent changes from the Wiki API
$recentChanges = file_get_contents($wikiRecentChangesUrl);
$recentChanges = json_decode($recentChanges, true);
// If there are no errors, process the changes
if (isset($recentChanges['query']['recentchanges'])) {
$recentPages = array_reverse($recentChanges['query']['recentchanges']); // Sort changes from oldest to newest
foreach ($recentPages as $page) {
$pageTitle = $page['title'];
$user = $page['user']; // User who made the change
$comment = $page['comment']; // Comment on the change
$timestamp = $page['timestamp'];
// Check if the page title starts with an invalid prefix
foreach ($invalidPrefixes as $prefix) {
if (strpos($pageTitle, $prefix) === 0) {
echo "INFO: '$pageTitle' starts with an invalid prefix '$prefix', skipping.<br>";
continue 2; // Skip to the next page if invalid
}
}
echo "Processing page: $pageTitle (Modified by: $user, Comment: '$comment')<br>";
// Fetch the Wiki page content
$wikiPageUrl = "https://wiki.freecad.org/api.php?action=query&titles=" . urlencode($pageTitle) . "&prop=revisions&rvprop=content&format=json";
$wikiContent = file_get_contents($wikiPageUrl);
$wikiContent = json_decode($wikiContent, true);
$pageId = array_key_first($wikiContent['query']['pages']);
$wikiText = $wikiContent['query']['pages'][$pageId]['revisions'][0]['*'];
// Generate the GitHub file path
$filePath = createFilePath($pageTitle);
// Get the existing file content from GitHub
$existingContent = getGithubFileContent($filePath);
// Fetch the SHA for the existing file
$sha = getShaForFile($filePath);
// Skip if the content is already up-to-date
if ($existingContent !== null && $wikiText === $existingContent) {
echo "INFO: '$filePath' is already up-to-date.<br>";
continue; // If content is the same, skip
}
// Update the file if it exists, or create a new one
if ($existingContent !== null) {
echo "INFO: Updating existing file '$filePath' on GitHub...<br>";
updateGithubFile($filePath, $wikiText, $sha, $pageTitle, $user, $comment);
} else {
echo "INFO: Creating new file '$filePath' on GitHub...<br>";
updateGithubFile($filePath, $wikiText, null, $pageTitle, $user, $comment);
}
}
} else {
echo "ERROR: Could not fetch recent changes from the wiki.<br>";
}
echo "All changes processed.<br>";
?>