User::create()   F
last analyzed

Complexity

Conditions 17
Paths 961

Size

Total Lines 72
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
eloc 52
nc 961
nop 7
dl 0
loc 72
rs 2.7777
c 0
b 0
f 0
ccs 0
cts 61
cp 0
crap 306

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