-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmediawiki.php
123 lines (107 loc) · 3.8 KB
/
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
<?php
require_once 'Page.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 ) {
require_once 'http.php';
$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;
}
public function query ($query, $post = null) {
$query = $this->queryString($query);
if ( $post == null )
$data = $this->http->get($this->url . $query);
else
$data = $this->http->post($this->url . $query, $post);
return json_decode($data, true);
}
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.
* @returns 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 );
var_dump($return);
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.
* @returns 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'];
}
}