-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipush.go
230 lines (170 loc) · 5.12 KB
/
wikipush.go
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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
mw "github.com/sadbox/mediawiki"
)
const (
doneDir = "done"
dupeDir = "dupes"
skipDir = "skipped"
)
var (
extension string
pause time.Duration
summary string
doneCount, dupeCount, skipCount, errCount, moveErrCount int
)
func main() {
run := flag.Bool("run", false, "actually perform the upload")
url := flag.String("url", "", "API url (typically http://.../w/api.php)")
flag.StringVar(&extension, "ext", ".txt", "file extension (including dot)")
flag.DurationVar(&pause, "pause", 500*time.Millisecond, "wait time between uploads")
flag.StringVar(&summary, "summary", "Bulk upload by wikipush", "message for the revision log")
flag.Parse()
files, err := filepath.Glob("*" + extension)
if err != nil {
log.Fatalf("Error reading filenames: %s", err)
}
if !*run {
fmt.Printf("%d files would be uploaded.\n", len(files))
if len(files) > 0 {
fmt.Println("Start wikipush with `-run' flag to perform the upload.")
}
return
}
if len(files) == 0 {
fmt.Println("No files found.")
return
}
if *url == "" {
log.Fatal("Error: Missing URL: Please specify your MediaWiki API URL using the `-url' flag.")
}
checkDir(doneDir, "done")
checkDir(dupeDir, "duplicates")
checkDir(skipDir, "skipped")
client, err := mw.New(*url, "wikipush")
if err != nil {
log.Fatalf("Error connecting to MediaWiki: %s\n", err)
}
in := bufio.NewReader(os.Stdin)
fmt.Print("Enter username: ")
username, _, err := in.ReadLine()
if err != nil {
log.Fatalf("Error reading username: %s\n", err)
}
fmt.Print("Enter password (will be echoed): ")
password, _, err := in.ReadLine()
if err != nil {
log.Fatalf("Error reading password: %s\n", err)
}
err = client.Login(string(username), string(password))
if err != nil {
log.Fatalf("Error logging in (wrong username/password?): %s\n", err)
}
defer client.Logout()
fmt.Printf("%d files will be uploaded.\n", len(files))
duration := pause * time.Duration(len(files))
fmt.Printf("This will take at least %s with the current throttle setting.\n", duration)
push(client, files)
fmt.Println("Done.")
fmt.Printf("Out of %d files,\n", len(files))
fmt.Printf("%d were uploaded successfully (see %s directory),\n", doneCount, doneDir)
fmt.Printf("%d were skipped because a page with different content already existed (see %s directory),\n", dupeCount, dupeDir)
fmt.Printf("%d were skipped because they were alreay in the wiki (see %s directory).\n", skipCount, skipDir)
fmt.Printf("%d couldn't be processed because of errors.\n", errCount)
if moveErrCount > 0 {
fmt.Printf("Additionally, %d files couldn't be moved into the correct directory after they were processed.\n", moveErrCount)
}
}
func push(client *mw.MWApi, files []string) {
for _, file := range files {
local, err := ioutil.ReadFile(file)
if err != nil {
log.Printf("Error reading file %s, skipping...\n", file)
errCount++
continue
}
title := strings.TrimSuffix(file, extension)
time.Sleep(pause)
page, err := client.Read(title)
if err != nil {
log.Printf("Error fetching page %s, skipping...\n", title)
errCount++
continue
}
if len(page.Revisions) == 0 {
edit := map[string]string{
"title": title,
"summary": summary,
"text": string(local),
}
err = client.Edit(edit)
if err != nil {
log.Printf("Error uploading page %s, skipping...\n", title)
errCount++
continue
}
log.Printf("Successfully uploaded \"%s\", page didn't exist before.\n", title)
doneCount++
err := os.Rename(file, path.Join(doneDir, file))
if err != nil {
log.Printf("Error moving file \"%s\" into \"%s\" directory, skipping...\n", file, doneDir)
moveErrCount++
}
} else {
remote := page.Revisions[0].Body
if strings.TrimSpace(string(local)) == strings.TrimSpace(remote) {
log.Printf("Skipped \"%s\", because it's already in the wiki.\n", title)
skipCount++
err := os.Rename(file, path.Join(skipDir, file))
if err != nil {
log.Printf("Error moving file \"%s\" into \"%s\" directory, skipping...\n", file, skipDir)
moveErrCount++
}
} else {
log.Printf("Duplicate page \"%s\" with different content.\n", title)
dupeCount++
err := os.Rename(file, path.Join(dupeDir, file))
if err != nil {
log.Printf("Error moving file \"%s\" into \"%s\" directory, skipping...\n", file, dupeDir)
moveErrCount++
}
}
}
}
}
func checkDir(dir string, description string) {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
log.Fatalf("Error creating %s directory \"%s\": %s\n", description, dir, err)
}
isEmpty, err := isDirEmpty(dir)
if err != nil {
log.Fatalf("Error checking if %s directory \"%s\" is empty: %s\n", description, dir, err)
}
if !isEmpty {
log.Fatalf("Error: Please make sure the %s directory \"%s\" is empty.\n", description, dir)
}
}
func isDirEmpty(name string) (bool, error) {
file, err := os.Open(name)
if err != nil {
return false, err
}
defer file.Close()
_, err = file.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}