forked from legoktm/harej-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_mediawiki.php
325 lines (285 loc) · 8.68 KB
/
new_mediawiki.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
class mediawiki {
protected $username; /* @string - The bot's username (if it has one). */
protected $password; /* @string - The bot's password (if it has one). */
protected $loggedin; /* @bool - Is the bot logged in? */
protected $edittoken; /* @string - Our edit token (see http://www.mediawiki.org/wiki/Manual:Edit_token). */
protected $url; /* @string - The url to api.php. */
protected $lasterror; /* @string - Contains the last error the bot had. */
public $http; /* @class - Our http client. */
/**
* @desc Our constructor. Sets up the http client for us to use and sets up all the variables. Will also log the user in if a username and password are suppied.
* @param string (default=http://en.wikipedia.org/w/api.php) $url - A url to mediawiki's api.php.
* @param string (default=null) $username - The account's username.
* @param string (default=null) $password - The account's password.
* @param int (default=0) $maxlag - The maxlag level to use when making an edit.
*/
public function __construct ( $username = null, $password = null, $url = 'https://en.wikipedia.org/w/api.php', $maxlag = 0 ) {
$this->http = new http();
$this->http->useragent = 'PHP Mediawiki Client';
$this->ectimestamp = null;
$this->edittoken = null;
$this->username = $username;
$this->password = $password;
$this->loggedin = false;
$this->url = $url;
$this->lasterror = null;
$this->maxlag = $maxlag;
if ( $username != null && $password != null )
{
$return = $this->login( $username, $password );
if ( ! $return )
die( 'Error logging in: ' . $this->lasterror );
}
}
/**
* @desc Returns the last error the script ran into.
* @returns string
*/
public function lasterror () {
return $this->lasterror;
}
/**
* Make an API request
* @param $query array
* @param bool|null $post
* @return mixed
*/
public function query( $query, $post = null ) {
if ( is_array($query) ) {
$query = $this->queryString($query);
} else {
$query .= '&format=json';
}
if ( $post === null || $post === false ) {
$data = $this->http->get($this->url . $query);
} else {
$data = $this->http->post($this->url . $query, $post);
}
return json_decode($data, true);
}
/**
* @param $query array
* @return string
*/
protected function queryString( $query ) {
$return = "?format=json";
foreach ($query as $key => $value) {
$return .= "&" . urlencode($key) . "=" . urlencode($value);
}
return $return;
}
/**
* @desc Logs the account into mediawiki.
* @param string $username - The account's username.
* @param string $password - The account's password.
* @return bool - Returns true on success, false on failure.
*/
public function login ( $username, $password ) {
$post = array(
'lgname' => $username,
'lgpassword' => $password
);
while (true) {
$return = $this->query( array('action' => 'login'), $post );
if ($return['login']['result'] == 'Success') {
$this->loggedin = true;
return true;
} elseif ( $return['login']['result'] == 'NeedToken' ) {
$post['lgtoken'] = $return['login']['token'];
} else {
$this->lasterror = $return['login']['code'];
return false;
}
}
}
/**
* @desc Logs the account out of the wiki and destroys all their session data.
*/
public function logout () {
$this->query( array('action' => 'logout') );
$this->edittoken = null;
$this->lasterror = null;
$this->loggedin = false;
}
public function page ($title) {
return new MediawikiPage($this,$title);
}
/**
* @desc Returns the bot's edit token.
* @param bool (default=false) $force - Force the script to get a fresh edit token.
* @return mixed - Returns the account's edittoken on success or false on failure.
*/
public function getedittoken ($force = false) {
if ( $this->edittoken != null && $force == false )
return $this->edittoken;
$x = $this->query( array('action' => 'query', 'meta' => 'tokens' ) );
return $x['query']['tokens']['csrftoken'];
}
// TODO: rewrite this function to be neat
public function getTransclusions( $page, $sleep=null ) {
$continue = '';
$pages = array();
while (true) {
$q = array(
'action' => 'query',
'list' => 'embeddedin',
'eititle' => $page,
'eilimit' => 500,
'rawcontinue' => 1,
);
if ($continue != '')
$q['eicontinue'] = $continue;
$ret = $this->query($q);
if ($sleep != null)
sleep( $sleep );
foreach ($ret['query']['embeddedin'] as $x) {
$pages[] = $x['title'];
}
if (isset($ret['query-continue']['embeddedin']['eicontinue'])) {
$continue = $ret['query-continue']['embeddedin']['eicontinue'];
} else {
return $pages;
}
}
}
public function getpage( $title ) {
$pg = new MediawikiPage($this, $title);
return $pg->content();
}
}
class MediawikiPage {
/** @var mediawiki */
private $wiki;
/** @var string */
private $title;
/** @var string */
private $content;
/** @var int */
private $id;
/** @var bool */
private $exists;
private $ecTimestamp = null;
public function __construct ($wiki,$title) {
echo "New page [[$title]].\n";
$this->wiki = $wiki;
$this->title = str_replace('_',' ',$title);
$this->getContent();
}
private function getContent ($retry = 1) {
echo "Getting content for [[".$this->title."]]\n";
$this->exists = true;
$x = $this->wiki->query( array(
'action' => 'query',
'prop' => 'revisions',
'titles' => $this->title,
'rvlimit' => 1,
'rvprop' => 'content|timestamp'
) );
if ($this->wiki->http->http_code() != "200") {
echo "HTTP Eror Code: " . $this->wiki->http->http_code() . "\n";
if ($retry < 10) {
echo "Error getting content, retrying.\n";
sleep(($retry * 2));
$this->getContent(++$retry);
} else {
echo "Error getting content, dying.\n";
throw new Exception("HTTP Error.");
}
}
if (isset($x['query']['pages'][-1]['missing']) || isset($x['query']['pages'][-1]['invalid'])) {
echo "Page does not exist.\n";
$this->exists = false;
return;
}
@$this->id = key($x['query']['pages']);
if (!isset($x['query']['pages'][$this->id]['revisions'][0]['*'])) {
throw new Exception("Unknown Error retreiving page.");
}
$this->content = $x['query']['pages'][$this->id]['revisions'][0]['*'];
$this->ecTimestamp = $x['query']['pages'][$this->id]['revisions'][0]['timestamp'];
}
public function __toString () {
return $this->title;
}
public function title () {
return $this->title;
}
public function content () {
return $this->content;
}
/**
* @param string $content
* @param string $summary
* @param bool $minor
* @param bool $bot
* @param bool $retry whether to retry on bad token errors
* @return array
*/
public function edit ($content,$summary="",$minor=false,$bot=true,$retry=true) {
echo "Saving [[".$this->title."]]\n";
$post = array(
'title' => $this->title,
'text' => $content,
'md5' => md5( $content ),
'summary' => $summary,
'token' => $this->wiki->getedittoken(),
'basetimestamp' => $this->ecTimestamp
);
if ( $minor )
$post['minor'] = true;
if ( $bot )
$post['bot'] = true;
$x = $this->wiki->query( array('action' => 'edit'), $post );
if ( $x['error']['code'] == 'badtoken' && $retry ) {
$this->wiki->getedittoken( true );
return $this->edit($content,$summary,$minor,$bot,false);
}
$this->getContent();
return $x;
}
/**
* Add a new section to the page
* @param $heading string
* @param $content string
* @param bool $minor
* @param bool $bot
* @param bool $retry
* @return mixed
*/
public function addSection ( $heading, $content, $minor=false, $bot=true, $retry=true ) {
echo "Adding Section \"$heading\" to [[".$this->title."]]\n";
$post = array(
'title' => $this->title,
'text' => $content,
'md5' => md5( $content ),
'sectiontitle' => $heading,
'section' => 'new',
'token' => $this->wiki->getedittoken(),
'basetimestamp' => $this->ecTimestamp
);
if ( $minor ) {
$post['minor'] = true;
}
if ( $bot ) {
$post['bot'] = true;
}
$x = $this->wiki->query( array('action' => 'edit'), $post );
if ( $x['error']['code'] == 'badtoken' && $retry ) {
$this->wiki->getedittoken( true );
return $this->addSection( $heading, $content, $minor, $bot, false );
}
$this->getContent();
return $x;
}
public function exists () {
return $this->exists;
}
public function resolveRedirects () {
echo "Resoloving redirect.\n";
if (preg_match('/^\s*#redirect\s+\[\[(.+?)\]\]/i',$this->content,$m)) {
echo "[[".$this->title."]] redirects to [[".$m[1]."]]\n";
$this->__construct($this->wiki,$m[1]);
}
}
}