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