1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\Api\Service; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Mediawiki\Api\MediawikiApi; |
7
|
|
|
use Mediawiki\Api\SimpleRequest; |
8
|
|
|
use Mediawiki\Api\UsageException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @access private |
12
|
|
|
* |
13
|
|
|
* @author Addshore |
14
|
|
|
*/ |
15
|
|
|
class UserCreator { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var MediawikiApi |
19
|
|
|
*/ |
20
|
|
|
private $api; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param MediawikiApi $api |
24
|
|
|
*/ |
25
|
|
|
public function __construct( MediawikiApi $api ) { |
26
|
|
|
$this->api = $api; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $username |
31
|
|
|
* @param string $password |
32
|
|
|
* @param string|null $email |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public function create( $username, $password, $email = null ) { |
37
|
|
|
if ( !is_string( $username ) ) { |
38
|
|
|
throw new InvalidArgumentException( '$username should be a string' ); |
39
|
|
|
} |
40
|
|
|
if ( !is_string( $password ) ) { |
41
|
|
|
throw new InvalidArgumentException( '$password should be a string' ); |
42
|
|
|
} |
43
|
|
|
if ( !is_string( $email ) && !is_null( $email ) ) { |
44
|
|
|
throw new InvalidArgumentException( '$email should be a string or null' ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$params = [ |
48
|
|
|
'createreturnurl' => $this->api->getApiUrl(), |
49
|
|
|
'createtoken' => $this->api->getToken( 'createaccount' ), |
50
|
|
|
'username' => $username, |
51
|
|
|
'password' => $password, |
52
|
|
|
'retype' => $password, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
if ( !is_null( $email ) ) { |
56
|
|
|
$params['email'] = $email; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$result = $this->api->postRequest( new SimpleRequest( 'createaccount', $params ) ); |
61
|
|
|
return $result['createaccount']['status'] === 'PASS'; |
62
|
|
|
} catch ( UsageException $exception ) { |
63
|
|
|
// If the above request failed, try again in the old way. |
64
|
|
|
if ( $exception->getApiCode() === 'noname' ) { |
65
|
|
|
return $this->createPreOneTwentySeven( $params ); |
66
|
|
|
} |
67
|
|
|
throw $exception; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a user in the pre 1.27 manner. |
73
|
|
|
* @link https://www.mediawiki.org/wiki/API:Account_creation/pre-1.27 |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function createPreOneTwentySeven( $params ) { |
77
|
|
|
$newParams = [ |
78
|
|
|
'name' => $params['username'], |
79
|
|
|
'password' => $params['password'], |
80
|
|
|
]; |
81
|
|
|
if ( array_key_exists( 'email', $params ) ) { |
82
|
|
|
$newParams['email'] = $params['email']; |
83
|
|
|
} |
84
|
|
|
// First get the token. |
85
|
|
|
$tokenRequest = new SimpleRequest( 'createaccount', $newParams ); |
86
|
|
|
$result = $this->api->postRequest( $tokenRequest ); |
87
|
|
|
if ( $result['createaccount']['result'] == 'NeedToken' ) { |
88
|
|
|
// Then send the token to create the account. |
89
|
|
|
$newParams['token'] = $result['createaccount']['token']; |
90
|
|
|
$request = new SimpleRequest( 'createaccount', $newParams ); |
91
|
|
|
$result = $this->api->postRequest( $request ); |
92
|
|
|
} |
93
|
|
|
return ( $result['createaccount']['result'] === 'Success' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|