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