1 | <?php |
||
15 | class UserCreator { |
||
16 | |||
17 | /** |
||
18 | * @var MediawikiApi |
||
19 | */ |
||
20 | private $api; |
||
21 | |||
22 | /** |
||
23 | * @param MediawikiApi $api |
||
24 | */ |
||
25 | 1 | public function __construct( MediawikiApi $api ) { |
|
28 | |||
29 | /** |
||
30 | * @param string $username |
||
31 | * @param string $password |
||
32 | * @param string|null $email |
||
33 | * |
||
34 | * @return bool |
||
35 | */ |
||
36 | 1 | public function create( $username, $password, $email = null ) { |
|
37 | 1 | if ( !is_string( $username ) ) { |
|
38 | throw new InvalidArgumentException( '$username should be a string' ); |
||
39 | } |
||
40 | 1 | if ( !is_string( $password ) ) { |
|
41 | throw new InvalidArgumentException( '$password should be a string' ); |
||
42 | } |
||
43 | 1 | if ( !is_string( $email ) && !is_null( $email ) ) { |
|
44 | throw new InvalidArgumentException( '$email should be a string or null' ); |
||
45 | } |
||
46 | |||
47 | $params = [ |
||
48 | 1 | 'createreturnurl' => $this->api->getApiUrl(), |
|
49 | 1 | 'createtoken' => $this->api->getToken( 'createaccount' ), |
|
50 | 1 | 'username' => $username, |
|
51 | 1 | 'password' => $password, |
|
52 | 1 | 'retype' => $password, |
|
53 | 1 | ]; |
|
54 | |||
55 | 1 | if ( !is_null( $email ) ) { |
|
56 | $params['email'] = $email; |
||
57 | } |
||
58 | |||
59 | try { |
||
60 | 1 | $result = $this->api->postRequest( new SimpleRequest( 'createaccount', $params ) ); |
|
61 | 1 | 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 ) { |
||
95 | |||
96 | } |
||
97 |