Completed
Push — master ( 70ba8d...e05048 )
by Maximilian
11s
created

User::__construct()   D

Complexity

Conditions 13
Paths 76

Size

Total Lines 87
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 13
eloc 59
c 5
b 1
f 0
nc 76
nop 2
dl 0
loc 87
rs 4.9922
ccs 0
cts 64
cp 0
crap 182

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
This file is part of Peachy MediaWiki Bot API
5
6
Peachy is free software: you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
/**
21
 * @file
22
 * User object
23
 */
24
25
/**
26
 * User class, stores methods that relate to a specific user
27
 */
28
class User {
29
30
	/**
31
	 * Wiki class
32
	 *
33
	 * @var Wiki
34
	 * @access protected
35
	 */
36
	protected $wiki;
37
38
	/**
39
	 * Username
40
	 *
41
	 * @var string
42
	 * @access protected
43
	 */
44
	protected $username;
45
46
	/**
47
	 * Whether or not user exists
48
	 *
49
	 * @var bool
50
	 * @access protected
51
	 */
52
	protected $exists = true;
53
54
	/**
55
	 * Whether or not user is blocked
56
	 *
57
	 * @var bool
58
	 * @access protected
59
	 */
60
	protected $blocked;
61
62
	/**
63
	 * Array of block parameters
64
	 *
65
	 * (default value: array())
66
	 *
67
	 * @var array
68
	 * @access protected
69
	 */
70
	protected $blockinfo = array();
71
72
	/**
73
	 * Rough estimate as to number of edits
74
	 *
75
	 * @var int
76
	 * @access protected
77
	 */
78
	protected $editcount;
79
80
	/**
81
	 * List of groups user is a member of
82
	 *
83
	 * @var array
84
	 * @access protected
85
	 */
86
	protected $groups;
87
88
	/**
89
	 * Whether or not user is an IP
90
	 *
91
	 * @var bool
92
	 * @access protected
93
	 */
94
	protected $ip = false;
95
96
	/**
97
	 * Whether or not user has email enabled
98
	 *
99
	 * @var bool
100
	 * @access protected
101
	 */
102
	protected $hasemail = false;
103
104
	/**
105
	 * Date the user registered
106
	 *
107
	 * @var string
108
	 * @access protected
109
	 */
110
	protected $registration;
111
112
	/**
113
	 * Construction method for the User class
114
	 *
115
	 * @fixme    Return in constructor method.
116
	 *
117
	 * @access public
118
	 * @param Wiki $wikiClass
119
	 * @param mixed $pgUsername Username
120
	 * @throws AssertFailure
121
	 * @throws LoggedOut
122
	 * @throws MWAPIError
123
	 */
124
	public function __construct( Wiki &$wikiClass, $pgUsername ) {
125
126
		$this->wiki = & $wikiClass;
127
128
		pecho( "Getting user information for $pgUsername...\n\n", PECHO_NORMAL );
129
		if(is_numeric(ip2long( $pgUsername))) {
130
			$uiRes = $this->wiki->apiQuery(
131
				array(
132
					'action'  => 'query',
133
					'list'    => 'users|blocks',
134
					'ususers' => $pgUsername,
135
					'bkip' => $pgUsername,
136
					'usprop'  => 'editcount|groups|blockinfo|emailable|registration'
137
				)
138
			);
139
		}
140
		else {
141
			$uiRes = $this->wiki->apiQuery(
142
					array(
143
							'action'  => 'query',
144
							'list'    => 'users|blocks',
145
							'ususers' => $pgUsername,
146
							'bkusers' => $pgUsername,
147
							'usprop'  => 'editcount|groups|blockinfo|emailable|registration'
148
					)
149
			);
150
		}
151
152
		if ( !$uiRes ) {
153
			$this->username = $pgUsername;
154
			$this->exists = false;
155
		} else {
156
			$this->exists = true;
157
		}
158
159
		$this->username = $uiRes['query']['users'][0]['name'];
160
161
		if(is_numeric(ip2long( $pgUsername))) {
162
			$this->exists = false;
163
			$this->ip = true;
164
165
			if( isset( $uiRes['query']['blocks'][0]['expiry'] ) && isset($uiRes['query']['blocks'][0])) {
166
				$this->blocked = true;
167
				$this->blockinfo = array(
168
					'by'     => $uiRes['query']['blocks'][0]['by'],
169
					'when'   => $uiRes['query']['blocks'][0]['timestamp'],
170
					'reason' => $uiRes['query']['blocks'][0]['reason'],
171
					'expiry' => $uiRes['query']['blocks'][0]['expiry']
172
				);
173
			} else {
174
				$this->blocked = false;
175
				$this->blockinfo = array();
176
			}
177
		} elseif( isset( $uiRes['query']['users'][0]['missing'] ) || isset( $uiRes['query']['users'][0]['invalid'] ) ) {
178
			$this->exists = false;
179
180
			return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
181
		} else {
182
			$this->editcount = $uiRes['query']['users'][0]['editcount'];
183
184
			if( isset( $uiRes['query']['users'][0]['groups'] ) ) {
185
				$this->groups = $uiRes['query']['users'][0]['groups'];
186
			}
187
188
			if( isset( $uiRes['query']['blocks'][0]['expiry'] ) && isset($uiRes['query']['blocks'][0])) {
189
				$this->blocked = true;
190
				$this->blockinfo = array(
191
					'by'     => $uiRes['query']['blocks'][0]['by'],
192
					'when'   => $uiRes['query']['blocks'][0]['timestamp'],
193
					'reason' => $uiRes['query']['blocks'][0]['reason'],
194
					'expiry' => $uiRes['query']['blocks'][0]['expiry']
195
				);
196
			} else {
197
				$this->blocked = false;
198
				$this->blockinfo = array();
199
			}
200
201
202
			if( isset( $uiRes['query']['users'][0]['emailable'] ) ) {
203
				$this->hasemail = true;
204
			}
205
206
			if( isset( $uiRes['query']['users'][0]['registration'] ) ) {
207
				$this->registration = $uiRes['query']['users'][0]['registration'];
208
			}
209
		}
210
	}
211
212
	/**
213
	 * Creates the account with the specified parameters
214
	 *
215
	 * @access public
216
	 * @param string $password Password (ignored if mailpassword is set). Default null.
217
	 * @param string $email Email address of user (optional). Default null.
218
	 * @param bool $mailpassword If set to true, a random password will be emailed to the user. Default false.
219
	 * @param string $reason Optional reason for creating the account to be put in the logs. Default null.
220
	 * @param string $realname Real name of user (optional). Default null.
221
	 * @param bool $tboverride Override the title blacklist.  Requires the tboverride right.  Default false.
0 ignored issues
show
Bug introduced by
There is no parameter named $tboverride. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
222
	 * @param string $language Language code to set as default for the user (optional, defaults to content language). Default null.
223
	 * @param string $domain Domain for external authentication (optional). Default null.
224
	 * @return bool True on success, false otherwise
225
	 */
226
	public function create( $password = null, $email = null, $mailpassword = false, $reason = null, $realname = null, $language = null, $domain = null ) {
227
		global $pgNotag, $pgTag;
228
		pecho( "Creating user account " . $this->username . "...\n\n", PECHO_NOTICE );
229
230
		try{
231
			$this->preEditChecks( "Create" );
232
		} catch( EditError $e ){
233
			pecho( "Error: $e\n\n", PECHO_FATAL );
234
			return false;
235
		}
236
237
		$token = $this->wiki->apiQuery(
238
			array(
239
				'action' => 'createaccount',
240
				'name'   => $this->username
241
			), true
242
		);
243
244
		$token = $token['createaccount']['token'];
245
246
		$apiArray = array(
247
			'action' => 'createaccount',
248
			'token'  => $token,
249
			'name'   => $this->username
250
		);
251
252
		if( !$password == null ) $apiArray['password'] = $password;
253
		if( !$email == null ) $apiArray['email'] = $email;
254
		if( !$realname == null ) $apiArray['realname'] = $realname;
255
		if( !$domain == null ) $apiArray['domain'] = $domain;
256
		if( !$reason == null ) {
257
			if( !$pgNotag ) $reason .= $pgTag;
258
			$apiArray['reason'] = $reason;
259
		}
260
		if( !$language == null ) $apiArray['language'] = $language;
261
262
		if( $this->exists() ) {
263
			pecho( "Error: User account already exists.\n\n", PECHO_ERROR );
264
			return false;
265
		}
266
		if( $password == null && !$mailpassword ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $password of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
267
			pecho( "Error: neither a password or the mailpassword have been set.\n\n", PECHO_ERROR );
268
			return false;
269
		}
270
		if( $mailpassword ) {
271
			if( $email == null ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $email of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
272
				pecho( "Error: Email not specified.\n\n", PECHO_ERROR );
273
				return false;
274
			} else $apiArray['mailpassword'] = 'yes';
275
		} else {
276
			if( is_null( $password ) ) {
277
				pecho( "Error: No password specified.\n\n", PECHO_ERROR );
278
				return false;
279
			}
280
		}
281
282
		$result = $this->wiki->apiQuery( $apiArray, true );
283
284
		$this->__construct( $this->wiki, $this->username );
285
286
		if( isset( $result['createaccount']['result'] ) ) {
287
			if( $result['createaccount']['result'] == 'success' ) {
288
				return true;
289
			} else {
290
				pecho( "Create error...\n\n" . print_r( $result['createaccount'], true ), PECHO_FATAL );
291
				return false;
292
			}
293
		} else {
294
			pecho( "Create error...\n\n" . print_r( $result['createaccount'], true ), PECHO_FATAL );
295
			return false;
296
		}
297
	}
298
299
	/**
300
	 * Returns whether or not the user is blocked
301
	 *
302
	 * @access public
303
	 * @param bool $force Whether or not to use the locally stored cache. Default false.
304
	 * @return bool
305
	 */
306
	public function is_blocked( $force = false ) {
307
308
		if( !$force && $this->blocked !== null ) {
309
			return $this->blocked;
310
		}
311
312
		pecho( "Checking if {$this->username} is blocked...\n\n", PECHO_NORMAL );
313
314
		$this->__construct( $this->wiki, $this->username );
315
316
		return $this->blocked;
317
	}
318
319
	/**
320
	 * get_blockinfo function.
321
	 *
322
	 * @access public
323
	 * @return array
324
	 */
325
	public function get_blockinfo() {
326
		return $this->blockinfo;
327
	}
328
329
	/**
330
	 * is_ip function.
331
	 *
332
	 * @access public
333
	 * @return boolean
334
	 */
335
	public function is_ip() {
336
		return $this->ip;
337
	}
338
339
	/**
340
	 * Blocks the user
341
	 *
342
	 * @access public
343
	 * @param string $reason Reason for blocking. Default null
344
	 * @param string $expiry Expiry. Can be a date, {@link http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html GNU formatted date}, indefinite, or anything else that MediaWiki accepts. Default indefinite.
345
	 * @param array $params Parameters to set. Options are anononly, nocreate, autoblock, noemail, hidename, noallowusertalk. Defdault array().
346
	 * @param bool $watch Watch the user/IP's user and talk pages. Default false.
347
	 * @param int $range The number of CIDR prefix bits to use for a rangeblock. Default null.
348
	 * @return bool
349
	 */
350
	public function block( $reason = null, $expiry = 'indefinite', $params = array(), $watch = false, $range = null ) {
351
		global $pgNotag, $pgTag;
352
		$token = $this->wiki->get_tokens();
353
		$target = $this->username;
354
355
		if( !in_array( 'block', $this->wiki->get_userrights() ) ) {
356
			pecho( "User is not allowed to block users.\n\n", PECHO_FATAL );
357
			return false;
358
		}
359
360
		if( !$this->exists() && !$this->is_ip() ) {
361
			pecho( "User does not exist.\n\n", PECHO_FATAL );
362
			return false;
363
		}
364
365
		if( $range !== null ) {
366
			// intval() returns 0 or 1 on failure
367
			$range = intval( $range );
368
			if( !$this->is_ip() ) {
369
				pecho( "Can only combine a range with an IP address, not a username.\n\n", PECHO_WARN );
370
			}
371
			if( $range !== null && ( $range < 2 || $range > 32 ) ) {
372
				pecho( "Range must be an integer between 2 and 32 inclusive (more restrictive limits may also apply).\n\n", PECHO_WARN );
373
			}
374
			$target .= '/' . $range;
375
		}
376
377
		if( !array_key_exists( 'block', $token ) ) return false;
378
379
		$apiArr = array(
380
			'action'        => 'block',
381
			'user'          => $target,
382
			'token'         => $token['block'],
383
			'expiry'        => $expiry,
384
			'reblock'       => 'yes',
385
			'allowusertalk' => 'yes'
386
		);
387
388
		if( !is_null( $reason ) ) {
389
			if( !$pgNotag ) $reason .= $pgTag;
390
			$apiArr['reason'] = $reason;
391
		}
392
393
		foreach( $params as $param ){
394
			switch( $param ){
395
				case 'anononly':
396
					$apiArr['anononly'] = 'yes';
397
					break;
398
				case 'nocreate':
399
					$apiArr['nocreate'] = 'yes';
400
					break;
401
				case 'autoblock':
402
					$apiArr['autoblock'] = 'yes';
403
					break;
404
				case 'noemail':
405
					$apiArr['noemail'] = 'yes';
406
					break;
407
				case 'hidename':
408
					$apiArr['hidename'] = 'yes';
409
					break;
410
				case 'noallowusertalk':
411
					unset( $apiArr['allowusertalk'] );
412
					break;
413
414
			}
415
		}
416
417
		if( $watch ) $apiArr['watchuser'] = 'yes';
418
419
		Hooks::runHook( 'StartBlock', array( &$apiArr ) );
420
421
		pecho( "Blocking $target...\n\n", PECHO_NOTICE );
422
423
		try{
424
			$this->preEditChecks( "Block" );
425
		} catch( EditError $e ){
426
			pecho( "Error: $e\n\n", PECHO_FATAL );
427
			return false;
428
		}
429
430
		$result = $this->wiki->apiQuery( $apiArr, true );
431
432
		if( isset( $result['block'] ) ) {
433
			if( !isset( $result['error'] ) ) {
434
				$this->__construct( $this->wiki, $this->username );
435
				return true;
436
			} else {
437
				pecho( "Block error...\n\n" . print_r( $result['block'], true ) . "\n\n", PECHO_FATAL );
438
				return false;
439
			}
440
		} else {
441
			pecho( "Block error...\n\n" . print_r( $result, true ), PECHO_FATAL );
442
			return false;
443
		}
444
445
	}
446
447
	/**
448
	 * Unblocks the user, or a block ID
449
	 *
450
	 * @access public
451
	 * @param string $reason Reason for unblocking. Default null
452
	 * @param int $id Block ID to unblock. Default null
453
	 * @return bool
454
	 */
455
	public function unblock( $reason = null, $id = null ) {
456
		global $pgNotag, $pgTag;
457
		if( !in_array( 'block', $this->wiki->get_userrights() ) ) {
458
			pecho( "User is not allowed to unblock users", PECHO_FATAL );
459
			return false;
460
		}
461
462
		$token = $this->wiki->get_tokens();
463
464
		if( !array_key_exists( 'block', $token ) ) return false;
465
466
		$apiArr = array(
467
			'action' => 'unblock',
468
			'user'   => $this->username,
469
			'token'  => $token['unblock'],
470
		);
471
472
		if( !is_null( $id ) ) {
473
			$apiArr['id'] = $id;
474
			unset( $apiArr['user'] );
475
		}
476
		if( !is_null( $reason ) ) {
477
			if( !$pgNotag ) $reason .= $pgTag;
478
			$apiArr['reason'] = $reason;
479
		}
480
481
		Hooks::runHook( 'StartUnblock', array( &$apiArr ) );
482
483
		pecho( "Unblocking {$this->username}...\n\n", PECHO_NOTICE );
484
485
		try{
486
			$this->preEditChecks( "Unblock" );
487
		} catch( EditError $e ){
488
			pecho( "Error: $e\n\n", PECHO_FATAL );
489
			return false;
490
		}
491
492
		$result = $this->wiki->apiQuery( $apiArr, true );
493
494
		if( isset( $result['unblock'] ) ) {
495
			if( isset( $result['unblock']['user'] ) ) {
496
				$this->__construct( $this->wiki, $this->username );
497
				return true;
498
			} else {
499
				pecho( "Unblock error...\n\n" . print_r( $result['unblock'], true ) . "\n\n", PECHO_FATAL );
500
				return false;
501
			}
502
		} else {
503
			pecho( "Unblock error...\n\n" . print_r( $result, true ), PECHO_FATAL );
504
			return false;
505
		}
506
	}
507
508
	/**
509
	 * Returns the editcount of the user
510
	 *
511
	 * @access public
512
	 * @param bool $force Whether or not to use the locally stored cache. Default false.
513
	 * @param Database &$database Use an instance of the mysqli class to get a more accurate count
514
	 * @param bool $liveonly Whether or not to only get the live edit count. Only works with $database. Default false.
515
	 * @return int Edit count
516
	 */
517
	public function get_editcount( $force = false, &$database = null, $liveonly = false ) {
518
		global $pgUseLabs;
519
		//First check if $database exists, because that returns a more accurate count
520
		if( !is_null( $database ) && $database instanceOf mysqli ) {
521
522
			pecho( "Getting edit count for {$this->username} using the Database class...\n\n", PECHO_NORMAL );
523
524
			if( !$liveonly && $result = mysqli_query( $database, "SELECT COUNT(*) AS count FROM " . ( $pgUseLabs ? "archive_userindex" : "archive" ) . " WHERE `ar_user_text` = '{$this->username}';" ) ) {
525
                $res = mysqli_fetch_assoc( $result );
526
                $del_count = $res['count'];
527
                mysqli_free_result( $result );
528
                unset( $res );
529
            } else $del_count = 0;
530
            
531
            if( $result = mysqli_query( $database, "SELECT COUNT(*) AS count FROM " . ( $pgUseLabs ? "revision_userindex" : "revision" ) . " WHERE `rev_user_text` = '{$this->username}';" ) ) {
532
                $res = mysqli_fetch_assoc( $result );
533
                $live_count = $res['count'];
534
                mysqli_free_result( $result );
535
                unset( $res );
536
            } else $live_count = 0;          
537
538
			$this->editcount = $del_count + $live_count;
539
		} else {
540
			if( $force ) {
541
				$this->__construct( $this->wiki, $this->username );
542
			}
543
		}
544
		return $this->editcount;
545
	}
546
547
	/**
548
	 * Returns a list of all user contributions
549
	 *
550
	 * @access public
551
	 * @param bool $mostrecentfirst Set to true to get the most recent edits first. Default true.
552
	 * @param bool $limit Only get this many edits. Default null.
553
	 * @return array Array, first level indexed, second level associative with keys user, pageid, revid, ns, title, timestamp, size and comment (edit summary).
554
	 */
555
	public function get_contribs( $mostrecentfirst = true, $limit = null ) {
556
		if( !$this->exists ) return array();
557
558
		$ucArray = array(
559
			'_code'  => 'uc',
560
			'ucuser' => $this->username,
561
			'action' => 'query',
562
			'list'   => 'usercontribs',
563
			'_limit' => $limit,
564
		);
565
566
		if( $mostrecentfirst ) {
567
			$ucArray['ucdir'] = "older";
568
		} else {
569
			$ucArray['ucdir'] = "newer";
570
		}
571
572
		$result = $this->wiki->listHandler( $ucArray );
573
574
		pecho( "Getting list of contributions by {$this->username}...\n\n", PECHO_NORMAL );
575
576
		return $result;
577
	}
578
579
	/**
580
	 * Returns whether or not the user has email enabled
581
	 *
582
	 * @access public
583
	 * @return bool
584
	 */
585
	public function has_email() {
586
		return $this->hasemail;
587
	}
588
589
	/**
590
	 * Returns the usergroups, NULL if user is IP.
591
	 *
592
	 * @access public
593
	 * @param bool force Force use of the API.  Default false;
594
	 * @return array
595
	 */
596
	public function get_usergroups( $force = false ) {
597
		if( $force ) {
598
599
			$uiRes = $this->wiki->apiQuery(
600
				array(
601
					'action'  => 'query',
602
					'list'    => 'users',
603
					'ususers' => $this->username,
604
					'usprop'  => 'groups'
605
				)
606
			);
607
608
			$this->groups = $uiRes['query']['users'][0]['groups'];
609
		}
610
		return $this->groups;
611
	}
612
613
	/**
614
	 * Returns date the user registered
615
	 *
616
	 * @access public
617
	 * @return string
618
	 */
619
	public function get_registration() {
620
		return $this->registration;
621
	}
622
623
	/**
624
	 * Returns whether or not the user exists
625
	 *
626
	 * @access public
627
	 * @return bool
628
	 */
629
	public function exists() {
630
		return $this->exists;
631
	}
632
633
	/**
634
	 * Returns the raw username
635
	 *
636
	 * @access public
637
	 * @return string
638
	 */
639
	public function username() {
640
		return $this->username;
641
	}
642
643
	/**
644
	 * Send an email to another wiki user
645
	 *
646
	 * @access public
647
	 * @param string $text Text to send
648
	 * @param string $subject Subject of email. Default 'Wikipedia Email'
649
	 * @param bool $ccme Whether or not to send a copy of the email to "myself". Default false.
650
	 * @throws EmailError
651
	 * @return bool True on success, false otherwise.
652
	 */
653
	public function email( $text = null, $subject = "Wikipedia Email", $ccme = false ) {
654
		global $pgNotag;
655
		if( !$this->has_email() ) {
656
			pecho( "Cannot email {$this->username}, user has email disabled", PECHO_FATAL );
657
			return false;
658
		}
659
660
		$tokens = $this->wiki->get_tokens();
661
		if( !$pgNotag ) $text .= "\n\nPowered by Peachy " . PEACHYVERSION;
662
		$editarray = array(
663
			'action'  => 'emailuser',
664
			'target'  => $this->username,
665
			'token'   => $tokens['email'],
666
			'subject' => $subject,
667
			'text'    => $text
668
		);
669
670
		if( $ccme ) $editarray['ccme'] = 'yes';
671
672
		Hooks::runHook( 'StartEmail', array( &$editarray ) );
673
674
		pecho( "Emailing {$this->username}...\n\n", PECHO_NOTICE );
675
676
		try{
677
			$this->preEditChecks( "Email" );
678
		} catch( EditError $e ){
679
			pecho( "Error: $e\n\n", PECHO_FATAL );
680
			return false;
681
		}
682
683
		$result = $this->wiki->apiQuery( $editarray, true );
684
685
		if( isset( $result['error'] ) ) {
686
			throw new EmailError( $result['error']['code'], $result['error']['info'] );
687
		} elseif( isset( $result['emailuser'] ) ) {
688
			if( $result['emailuser']['result'] == "Success" ) {
689
				$this->__construct( $this->wiki, $this->username );
690
				return true;
691
			} else {
692
				pecho( "Email error...\n\n" . print_r( $result['emailuser'], true ) . "\n\n", PECHO_FATAL );
693
				return false;
694
			}
695
		} else {
696
			pecho( "Email error...\n\n" . print_r( $result['edit'], true ) . "\n\n", PECHO_FATAL );
697
			return false;
698
		}
699
	}
700
701
	public function userrights( $add = array(), $remove = array(), $reason = '' ) {
702
		global $pgNotag, $pgTag;
703
704
        $tokens = $this->wiki->get_tokens();
705
        
706
		if( !$pgNotag ) $reason .= $pgTag;
707
		$apiArr = array(
708
			'action' => 'userrights',
709
			'user'   => $this->username,
710
			'token'  => $tokens['userrights'],
711
			'add'    => implode( '|', $add ),
712
			'remove' => implode( '|', $remove ),
713
			'reason' => $reason
714
		);
715
716
		Hooks::runHook( 'StartUserrights', array( &$apiArr ) );
717
718
		pecho( "Assigning user rights to {$this->username}...\n\n", PECHO_NOTICE );
719
720
		try{
721
			$this->preEditChecks( "Rights" );
722
		} catch( EditError $e ){
723
			pecho( "Error: $e\n\n", PECHO_FATAL );
724
			return false;
725
		}
726
727
		$result = $this->wiki->apiQuery( $apiArr, true );
728
729
		if( isset( $result['userrights'] ) ) {
730
			if( isset( $result['userrights']['user'] ) ) {
731
				$this->__construct( $this->wiki, $this->username );
732
				return true;
733
			} else {
734
				pecho( "Userrights error...\n\n" . print_r( $result['userrights'], true ) . "\n\n", PECHO_FATAL );
735
				return false;
736
			}
737
		} else {
738
			pecho( "Userrights error...\n\n" . print_r( $result, true ), PECHO_FATAL );
739
			return false;
740
		}
741
742
	}
743
744
	/**
745
	 * List all deleted contributions.
746
	 * The logged in user must have the 'deletedhistory' right
747
	 *
748
	 * @access public
749
	 * @param bool $content Whether or not to return content of each contribution. Default false
750
	 * @param string $start Timestamp to start at. Default null.
751
	 * @param string $end Timestamp to end at. Default null.
752
	 * @param string $dir Direction to list. Default 'older'
753
	 * @param array $prop Information to retrieve. Default array( 'revid', 'user', 'parsedcomment', 'minor', 'len', 'content', 'token' )
754
	 * @return array
755
	 */
756
	public function deletedcontribs( $content = false, $start = null, $end = null, $dir = 'older', $prop = array(
757
		'revid', 'user', 'parsedcomment', 'minor', 'len', 'content', 'token'
758
	) ) {
759
		if( !in_array( 'deletedhistory', $this->wiki->get_userrights() ) ) {
760
			pecho( "User is not allowed to view deleted revisions", PECHO_FATAL );
761
			return false;
762
		}
763
764
		if( $content ) $prop[] = 'content';
765
766
		$drArray = array(
767
			'_code'  => 'dr',
768
			'list'   => 'deletedrevs',
769
			'druser' => $this->username,
770
			'drprop' => implode( '|', $prop ),
771
			'drdir'  => $dir
772
		);
773
774
		if( !is_null( $start ) ) $drArray['drstart'] = $start;
775
		if( !is_null( $end ) ) $drArray['drend'] = $end;
776
777
		Hooks::runHook( 'StartDelrevs', array( &$drArray ) );
778
779
		pecho( "Getting deleted revisions by {$this->username}...\n\n", PECHO_NORMAL );
780
781
		return $this->wiki->listHandler( $drArray );
782
	}
783
784
	/*
785
	 * Performs new message checking, etc
786
	 *
787
	 * @access public
788
	 * @return void
789
	 */
790
	protected function preEditChecks( $action = "Edit" ) {
791
		$this->wiki->preEditChecks( $action );
792
	}
793
794
	/**
795
	 * Returns a page class for the userpage
796
	 *
797
	 * @return Page
798
	 */
799
	public function &getPageclass() {
800
		$user_page = new Page( $this->wiki, "User:" . $this->username );
801
		return $user_page;
802
	}
803
804
}
805