@@ -303,16 +303,7 @@ public function isLoggedin() {
303
303
*/
304
304
public function login ( ApiUser $ apiUser ) {
305
305
$ this ->logger ->log ( LogLevel::DEBUG , 'Logging in ' );
306
-
307
- $ credentials = array (
308
- 'lgname ' => $ apiUser ->getUsername (),
309
- 'lgpassword ' => $ apiUser ->getPassword (),
310
- );
311
-
312
- if ( !is_null ( $ apiUser ->getDomain () ) ) {
313
- $ credentials ['lgdomain ' ] = $ apiUser ->getDomain ();
314
- }
315
-
306
+ $ credentials = $ this ->getLoginParams ( $ apiUser );
316
307
$ result = $ this ->postRequest ( new SimpleRequest ( 'login ' , $ credentials ) );
317
308
if ( $ result ['login ' ]['result ' ] == "NeedToken " ) {
318
309
$ result = $ this ->postRequest ( new SimpleRequest ( 'login ' , array_merge ( array ( 'lgtoken ' => $ result ['login ' ]['token ' ] ), $ credentials ) ) );
@@ -327,68 +318,63 @@ public function login( ApiUser $apiUser ) {
327
318
return false ;
328
319
}
329
320
321
+ /**
322
+ * @param ApiUser $apiUser
323
+ *
324
+ * @return string[]
325
+ */
326
+ private function getLoginParams ( ApiUser $ apiUser ) {
327
+ $ params = array (
328
+ 'lgname ' => $ apiUser ->getUsername (),
329
+ 'lgpassword ' => $ apiUser ->getPassword (),
330
+ );
331
+
332
+ if ( !is_null ( $ apiUser ->getDomain () ) ) {
333
+ $ params ['lgdomain ' ] = $ apiUser ->getDomain ();
334
+ }
335
+ return $ params ;
336
+ }
337
+
330
338
/**
331
339
* @param array $result
332
340
*
333
341
* @throws UsageException
334
342
*/
335
343
private function throwLoginUsageException ( $ result ) {
336
344
$ loginResult = $ result ['login ' ]['result ' ];
345
+
346
+ throw new UsageException (
347
+ 'login- ' . $ loginResult ,
348
+ $ this ->getLoginExceptionMessage ( $ loginResult ),
349
+ $ result
350
+ );
351
+ }
352
+
353
+ /**
354
+ * @param string $loginResult
355
+ *
356
+ * @return string
357
+ */
358
+ private function getLoginExceptionMessage ( $ loginResult ) {
337
359
switch ( $ loginResult ) {
338
360
case 'Illegal ' ;
339
- throw new UsageException (
340
- 'login- ' . $ loginResult ,
341
- 'You provided an illegal username ' ,
342
- $ result
343
- );
361
+ return 'You provided an illegal username ' ;
344
362
case 'NotExists ' ;
345
- throw new UsageException (
346
- 'login- ' . $ loginResult ,
347
- 'The username you provided doesn \'t exist ' ,
348
- $ result
349
- );
363
+ return 'The username you provided doesn \'t exist ' ;
350
364
case 'WrongPass ' ;
351
- throw new UsageException (
352
- 'login- ' . $ loginResult ,
353
- 'The password you provided is incorrect ' ,
354
- $ result
355
- );
365
+ return 'The password you provided is incorrect ' ;
356
366
case 'WrongPluginPass ' ;
357
- throw new UsageException (
358
- 'login- ' . $ loginResult ,
359
- 'An authentication plugin rather than MediaWiki itself rejected the password ' ,
360
- $ result
361
- );
367
+ return 'An authentication plugin rather than MediaWiki itself rejected the password ' ;
362
368
case 'CreateBlocked ' ;
363
- throw new UsageException (
364
- 'login- ' . $ loginResult ,
365
- 'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation ' ,
366
- $ result
367
- );
369
+ return 'The wiki tried to automatically create a new account for you, but your IP address has been blocked from account creation ' ;
368
370
case 'Throttled ' ;
369
- throw new UsageException (
370
- 'login- ' . $ loginResult ,
371
- 'You \'ve logged in too many times in a short time. ' ,
372
- $ result
373
- );
371
+ return 'You \'ve logged in too many times in a short time. ' ;
374
372
case 'Blocked ' ;
375
- throw new UsageException (
376
- 'login- ' . $ loginResult ,
377
- 'User is blocked ' ,
378
- $ result
379
- );
373
+ return 'User is blocked ' ;
380
374
case 'NeedToken ' ;
381
- throw new UsageException (
382
- 'login- ' . $ loginResult ,
383
- 'Either you did not provide the login token or the sessionid cookie. ' ,
384
- $ result
385
- );
375
+ return 'Either you did not provide the login token or the sessionid cookie. ' ;
386
376
default :
387
- throw new UsageException (
388
- 'login- ' . $ loginResult ,
389
- $ loginResult ,
390
- $ result
391
- );
377
+ return $ loginResult ;
392
378
}
393
379
}
394
380
0 commit comments