r98043 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98042‎ | r98043 | r98044 >
Date:23:41, 24 September 2011
Author:aaron
Status:deferred
Tags:
Comment:
Split out some backend function into a new ConfirmAccount.class.php file
Modified paths:
  • /trunk/extensions/ConfirmAccount/ConfirmAccount.php (modified) (history)
  • /trunk/extensions/ConfirmAccount/dataclasses (added) (history)
  • /trunk/extensions/ConfirmAccount/dataclasses/ConfirmAccount.class.php (added) (history)
  • /trunk/extensions/ConfirmAccount/presentation/specialpages/actions/ConfirmAccount_body.php (modified) (history)
  • /trunk/extensions/ConfirmAccount/presentation/specialpages/actions/RequestAccount_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ConfirmAccount/ConfirmAccount.php
@@ -166,6 +166,10 @@
167167 $wgAutoloadClasses['UserCredentialsPage'] = "$dir/actions/UserCredentials_body.php";
168168 $wgSpecialPageGroups['UserCredentials'] = 'users';
169169
 170+# Data functions
 171+$dir = dirname( __FILE__ ) . '/dataclasses';
 172+$wgAutoloadClasses['ConfirmAccount'] = "$dir/ConfirmAccount.class.php";
 173+
170174 $dir = dirname( __FILE__ ) . '/schema';
171175 # Schema changes
172176 $wgAutoloadClasses['ConfirmAccountUpdaterHooks'] = "$dir/ConfirmAccountUpdater.hooks.php";
Index: trunk/extensions/ConfirmAccount/dataclasses/ConfirmAccount.class.php
@@ -0,0 +1,93 @@
 2+<?php
 3+class ConfirmAccount {
 4+ /*
 5+ * Move old stale requests to rejected list. Delete old rejected requests.
 6+ */
 7+ public static function runAutoMaintenance() {
 8+ global $wgRejectedAccountMaxAge, $wgConfirmAccountFSRepos;
 9+
 10+ $dbw = wfGetDB( DB_MASTER );
 11+ # Select all items older than time $cutoff
 12+ $cutoff = $dbw->timestamp( time() - $wgRejectedAccountMaxAge );
 13+ $accountrequests = $dbw->tableName( 'account_requests' );
 14+ $sql = "SELECT acr_storage_key,acr_id FROM $accountrequests WHERE acr_rejected < '{$cutoff}'";
 15+ $res = $dbw->query( $sql );
 16+
 17+ $repo = new FSRepo( $wgConfirmAccountFSRepos['accountreqs'] );
 18+ # Clear out any associated attachments and delete those rows
 19+ while( $row = $dbw->fetchObject( $res ) ) {
 20+ $key = $row->acr_storage_key;
 21+ if( $key ) {
 22+ $path = $repo->getZonePath( 'public' ).'/'.
 23+ $key[0].'/'.$key[0].$key[1].'/'.$key[0].$key[1].$key[2].'/'.$key;
 24+ if( $path && file_exists($path) ) {
 25+ unlink($path);
 26+ }
 27+ }
 28+ $dbw->query( "DELETE FROM $accountrequests WHERE acr_id = {$row->acr_id}" );
 29+ }
 30+
 31+ # Select all items older than time $cutoff
 32+ global $wgConfirmAccountRejectAge;
 33+ $cutoff = $dbw->timestamp( time() - $wgConfirmAccountRejectAge );
 34+ # Old stale accounts will count as rejected. If the request was held, give it more time.
 35+ $dbw->update( 'account_requests',
 36+ array( 'acr_rejected' => $dbw->timestamp(),
 37+ 'acr_user' => 0, // dummy
 38+ 'acr_comment' => wfMsgForContent('confirmaccount-autorej'),
 39+ 'acr_deleted' => 1 ),
 40+ array( "acr_rejected IS NULL", "acr_registration < '{$cutoff}'", "acr_held < '{$cutoff}'" ),
 41+ __METHOD__ );
 42+
 43+ # Clear cache for notice of how many account requests there are
 44+ global $wgMemc;
 45+ $key = wfMemcKey( 'confirmaccount', 'noticecount' );
 46+ $wgMemc->delete( $key );
 47+ }
 48+
 49+ /**
 50+ * Flag a user's email as confirmed in the db
 51+ *
 52+ * @param sring $name
 53+ */
 54+ public function confirmEmail( $name ) {
 55+ global $wgMemc;
 56+ $dbw = wfGetDB( DB_MASTER );
 57+ $dbw->update( 'account_requests',
 58+ array( 'acr_email_authenticated' => $dbw->timestamp() ),
 59+ array( 'acr_name' => $name ),
 60+ __METHOD__ );
 61+ # Clear cache for notice of how many account requests there are
 62+ $key = wfMemcKey( 'confirmaccount', 'noticecount' );
 63+ $wgMemc->delete( $key );
 64+ }
 65+
 66+ /**
 67+ * Generate and store a new e-mail confirmation token, and return
 68+ * the URL the user can use to confirm.
 69+ * @param string $token
 70+ * @return string
 71+ */
 72+ public function confirmationTokenUrl( $token ) {
 73+ $title = SpecialPage::getTitleFor( 'RequestAccount' );
 74+ return $title->getFullUrl( array(
 75+ 'action' => 'confirmemail',
 76+ 'wpEmailToken' => $token
 77+ ) );
 78+ }
 79+
 80+ /**
 81+ * Generate, store, and return a new e-mail confirmation code.
 82+ * A hash (unsalted since it's used as a key) is stored.
 83+ * @param User $user
 84+ * @param string $expiration
 85+ * @return string
 86+ */
 87+ public function getConfirmationToken( $user, &$expiration ) {
 88+ global $wgConfirmAccountRejectAge;
 89+ $expires = time() + $wgConfirmAccountRejectAge;
 90+ $expiration = wfTimestamp( TS_MW, $expires );
 91+ $token = $user->generateToken( $user->getName() . $user->getEmail() . $expires );
 92+ return $token;
 93+ }
 94+}
Property changes on: trunk/extensions/ConfirmAccount/dataclasses/ConfirmAccount.class.php
___________________________________________________________________
Added: svn:eol-style
195 + native
Index: trunk/extensions/ConfirmAccount/presentation/specialpages/actions/RequestAccount_body.php
@@ -350,7 +350,7 @@
351351 $repo->storeBatch( array($triplet) ); // save!
352352 }
353353 $expires = null; // passed by reference
354 - $token = $this->getConfirmationToken( $u, $expires );
 354+ $token = ConfirmAccount::getConfirmationToken( $u, $expires );
355355 # Insert into pending requests...
356356 $acr_id = $dbw->nextSequenceValue( 'account_requests_acr_id_seq' );
357357 $dbw->begin();
@@ -505,7 +505,7 @@
506506 $name = $this->requestFromEmailToken( $code );
507507 if ( $name !== false ) {
508508 # Send confirmation email to prospective user
509 - $this->confirmEmail( $name );
 509+ ConfirmAccount::confirmEmail( $name );
510510 # Send mail to admin after e-mail has been confirmed
511511 if ( $wgConfirmAccountContact != '' ) {
512512 $target = new MailAddress( $wgConfirmAccountContact );
@@ -559,23 +559,6 @@
560560 }
561561
562562 /**
563 - * Flag a user's email as confirmed in the db
564 - *
565 - * @param sring $name
566 - */
567 - protected function confirmEmail( $name ) {
568 - $dbw = wfGetDB( DB_MASTER );
569 - $dbw->update( 'account_requests',
570 - array( 'acr_email_authenticated' => $dbw->timestamp() ),
571 - array( 'acr_name' => $name ),
572 - __METHOD__ );
573 - # Clear cache for notice of how many account requests there are
574 - global $wgMemc;
575 - $key = wfMemcKey( 'confirmaccount', 'noticecount' );
576 - $wgMemc->delete( $key );
577 - }
578 -
579 - /**
580563 * Generate a new e-mail confirmation token and send a confirmation
581564 * mail to the user's given address.
582565 *
@@ -586,7 +569,7 @@
587570 */
588571 protected function sendConfirmationMail( $user, $token, $expiration ) {
589572 global $wgContLang;
590 - $url = $this->confirmationTokenUrl( $token );
 573+ $url = ConfirmAccount::confirmationTokenUrl( $token );
591574 return $user->sendMail( wfMsg( 'requestaccount-email-subj' ),
592575 wfMsg( 'requestaccount-email-body',
593576 wfGetIP(),
@@ -596,33 +579,4 @@
597580 $wgContLang->date( $expiration, false ) ,
598581 $wgContLang->time( $expiration, false ) ) );
599582 }
600 -
601 - /**
602 - * Generate and store a new e-mail confirmation token, and return
603 - * the URL the user can use to confirm.
604 - * @param string $token
605 - * @return string
606 - */
607 - protected function confirmationTokenUrl( $token ) {
608 - $title = SpecialPage::getTitleFor( 'RequestAccount' );
609 - return $title->getFullUrl( array(
610 - 'action' => 'confirmemail',
611 - 'wpEmailToken' => $token
612 - ) );
613 - }
614 -
615 - /**
616 - * Generate, store, and return a new e-mail confirmation code.
617 - * A hash (unsalted since it's used as a key) is stored.
618 - * @param User $user
619 - * @param string $expiration
620 - * @return string
621 - */
622 - protected function getConfirmationToken( $user, &$expiration ) {
623 - global $wgConfirmAccountRejectAge;
624 - $expires = time() + $wgConfirmAccountRejectAge;
625 - $expiration = wfTimestamp( TS_MW, $expires );
626 - $token = $user->generateToken( $user->getName() . $user->getEmail() . $expires );
627 - return $token;
628 - }
629583 }
Index: trunk/extensions/ConfirmAccount/presentation/specialpages/actions/ConfirmAccount_body.php
@@ -389,11 +389,11 @@
390390 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
391391 $wgRequest->response()->header( 'Pragma: no-cache' );
392392
393 - require_once( "$IP/includes/StreamFile.php" );
394393 $repo = new FSRepo( $wgConfirmAccountFSRepos['accountreqs'] );
395394 $path = $repo->getZonePath( 'public' ).'/'.
396395 $key[0].'/'.$key[0].$key[1].'/'.$key[0].$key[1].$key[2].'/'.$key;
397 - wfStreamFile( $path );
 396+
 397+ StreamFile::stream( $path );
398398 }
399399
400400 protected function doSubmit() {
@@ -738,6 +738,9 @@
739739 }
740740 }
741741
 742+ /*
 743+ * Get requested account request row and load some fields
 744+ */
742745 function getRequest( $forUpdate = false ) {
743746 if( !$this->acrID ) return false;
744747
@@ -857,55 +860,10 @@
858861
859862 # Every 30th view, prune old deleted items
860863 if( 0 == mt_rand( 0, 29 ) ) {
861 - $this->runAutoMaintenance();
 864+ ConfirmAccount::runAutoMaintenance();
862865 }
863866 }
864867
865 - /*
866 - * Move old stale requests to rejected list. Delete old rejected requests.
867 - */
868 - private function runAutoMaintenance() {
869 - global $wgRejectedAccountMaxAge, $wgConfirmAccountFSRepos;
870 -
871 - $dbw = wfGetDB( DB_MASTER );
872 - # Select all items older than time $cutoff
873 - $cutoff = $dbw->timestamp( time() - $wgRejectedAccountMaxAge );
874 - $accountrequests = $dbw->tableName( 'account_requests' );
875 - $sql = "SELECT acr_storage_key,acr_id FROM $accountrequests WHERE acr_rejected < '{$cutoff}'";
876 - $res = $dbw->query( $sql );
877 -
878 - $repo = new FSRepo( $wgConfirmAccountFSRepos['accountreqs'] );
879 - # Clear out any associated attachments and delete those rows
880 - while( $row = $dbw->fetchObject( $res ) ) {
881 - $key = $row->acr_storage_key;
882 - if( $key ) {
883 - $path = $repo->getZonePath( 'public' ).'/'.
884 - $key[0].'/'.$key[0].$key[1].'/'.$key[0].$key[1].$key[2].'/'.$key;
885 - if( $path && file_exists($path) ) {
886 - unlink($path);
887 - }
888 - }
889 - $dbw->query( "DELETE FROM $accountrequests WHERE acr_id = {$row->acr_id}" );
890 - }
891 -
892 - # Select all items older than time $cutoff
893 - global $wgConfirmAccountRejectAge;
894 - $cutoff = $dbw->timestamp( time() - $wgConfirmAccountRejectAge );
895 - # Old stale accounts will count as rejected. If the request was held, give it more time.
896 - $dbw->update( 'account_requests',
897 - array( 'acr_rejected' => $dbw->timestamp(),
898 - 'acr_user' => 0, // dummy
899 - 'acr_comment' => wfMsgForContent('confirmaccount-autorej'),
900 - 'acr_deleted' => 1 ),
901 - array( "acr_rejected IS NULL", "acr_registration < '{$cutoff}'", "acr_held < '{$cutoff}'" ),
902 - __METHOD__ );
903 -
904 - # Clear cache for notice of how many account requests there are
905 - global $wgMemc;
906 - $key = wfMemcKey( 'confirmaccount', 'noticecount' );
907 - $wgMemc->delete( $key );
908 - }
909 -
910868 public function formatRow( $row ) {
911869 global $wgLang, $wgUser, $wgUseRealNamesOnly, $wgAllowRealName;
912870
@@ -977,7 +935,9 @@
978936 class ConfirmAccountsPager extends ReverseChronologicalPager {
979937 public $mForm, $mConds;
980938
981 - function __construct( $form, $conds = array(), $type, $rejects=false, $showHeld=false, $showStale=false ) {
 939+ function __construct(
 940+ $form, $conds = array(), $type, $rejects=false, $showHeld=false, $showStale=false
 941+ ) {
982942 $this->mForm = $form;
983943 $this->mConds = $conds;
984944

Status & tagging log