MediaWiki r60638 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r60637‎ | r60638 (on ViewVC)‎ | r60639 >
Date:00:43, 5 January 2010
Author:ashley
Status:deferred
Tags:
Comment:
ContactPage:
*add some comments to ContactPage.php
*added ContactFormBeforeMessage hook to SpecialContact.php so that extensions can add new fields to Special:Contact
*else if -> elseif
*more braces
*NULL -> null
*spacing tweaks
*double quotes -> single quotes where appropriate
Modified paths:

Diff [purge]

Index: trunk/extensions/ContactPage/SpecialContact.php
===================================================================
--- trunk/extensions/ContactPage/SpecialContact.php	(revision 60637)
+++ trunk/extensions/ContactPage/SpecialContact.php	(revision 60638)
@@ -57,7 +57,7 @@
 		if ( 'success' == $action ) {
 			wfDebug( __METHOD__ . ": success.\n" );
 			$f->showSuccess();
-		} else if ( 'submit' == $action && $wgRequest->wasPosted() && $f->hasAllInfo() ) {
+		} elseif ( 'submit' == $action && $wgRequest->wasPosted() && $f->hasAllInfo() ) {
 			$token = $wgRequest->getVal( 'wpEditToken' );
 
 			if( $wgUser->isAnon() ) {
@@ -69,10 +69,10 @@
 			}
 
 			if ( !$tokenOk ) {
-				wfDebug( __METHOD__ . ": bad token (".( $wgUser->isAnon() ? 'anon' : 'user' )."): $token\n" );
+				wfDebug( __METHOD__ . ": bad token (" . ( $wgUser->isAnon() ? 'anon' : 'user' ) . "): $token\n" );
 				$wgOut->addWikiMsg( 'sessionfailure' );
 				$f->showForm();
-			} else if ( !$f->passCaptcha() ) {
+			} elseif ( !$f->passCaptcha() ) {
 				wfDebug( __METHOD__ . ": captcha failed" );
 				$wgOut->addWikiMsg( 'contactpage-captcha-failed' );
 				$f->showForm();
@@ -113,8 +113,12 @@
 		$this->fromaddress = $wgRequest->getText( 'wpFromAddress' );
 
 		if( $wgUser->isLoggedIn() ) {
-			if( !$this->fromname ) $this->fromname = $wgUser->getName();
-			if( !$this->fromaddress ) $this->fromaddress = $wgUser->getEmail();
+			if( !$this->fromname ) {
+				$this->fromname = $wgUser->getName();
+			}
+			if( !$this->fromaddress ) {
+				$this->fromaddress = $wgUser->getEmail();
+			}
 		}
 
 		// prepare captcha if applicable
@@ -128,18 +132,33 @@
 	function hasAllInfo() {
 		global $wgContactRequireAll;
 
-		if ( $this->text === NULL ) return false;
-		else $this->text = trim( $this->text );
-		if ( $this->text === '' ) return false;
+		if ( $this->text === null ) {
+			return false;
+		} else {
+			$this->text = trim( $this->text );
+		}
+		if ( $this->text === '' ) {
+			return false;
+		}
 
 		if ( $wgContactRequireAll ) {
-			if ( $this->fromname === NULL ) return false;
-			else $this->fromname = trim( $this->fromname );
-			if ( $this->fromname === '' ) return false;
+			if ( $this->fromname === null ) {
+				return false;
+			} else {
+				$this->fromname = trim( $this->fromname );
+			}
+			if ( $this->fromname === '' ) {
+				return false;
+			}
 
-			if ( $this->fromaddress === NULL ) return false;
-			else $this->fromaddress = trim( $this->fromaddress );
-			if ( $this->fromaddress === '' ) return false;
+			if ( $this->fromaddress === null ) {
+				return false;
+			} else {
+				$this->fromaddress = trim( $this->fromaddress );
+			}
+			if ( $this->fromaddress === '' ) {
+				return false;
+			}
 		}
 
 		return true;
@@ -163,73 +182,81 @@
 		$action = $titleObj->getLocalURL( 'action=submit' );
 		$token = $wgUser->isAnon() ? EDIT_TOKEN_SUFFIX : $wgUser->editToken(); //this kind of sucks, really...
 
-		$wgOut->addHTML(
+		$form =
 			Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) .
 			Xml::openElement( 'fieldset' ) .
 			Xml::element( 'legend', null, wfMsg( 'contactpage-legend' ) ) .
 			Xml::openElement( 'table', array( 'id' => 'mailheader' ) ) .
-			"<tr>
-				<td class='mw-label'>" .
+			'<tr>
+				<td class="mw-label">' .
 					Xml::label( wfMsg( 'emailsubject' ), 'wpSubject' ) .
-				"</td>
-				<td class='mw-input' id='mw-contactpage-subject'>" .
+				'</td>
+				<td class="mw-input" id="mw-contactpage-subject">' .
 					Xml::input( 'wpSubject', 60, $this->subject, array( 'type' => 'text', 'maxlength' => 200 ) ) .
-				"</td>
+				'</td>
 			</tr>
 			<tr>
-				<td class='mw-label'>" .
+				<td class="mw-label">' .
 					Xml::label( wfMsg( "contactpage-fromname$msgSuffix" ), 'wpFromName' ) .
-				"</td>
-				<td class='mw-input' id='mw-contactpage-from'>" .
+				'</td>
+				<td class="mw-input" id="mw-contactpage-from">' .
 					Xml::input( 'wpFromName', 60, $this->fromname, array( 'type' => 'text', 'maxlength' => 200 ) ) .
-				"</td>
+				'</td>
 			</tr>
 			<tr>
-				<td class='mw-label'>" .
+				<td class="mw-label">' .
 					Xml::label( wfMsg( "contactpage-fromaddress$msgSuffix" ), 'wpFromAddress' ) .
-				"</td>
-				<td class='mw-input' id='mw-contactpage-address'>" .
+				'</td>
+				<td class="mw-input" id="mw-contactpage-address">' .
 					Xml::input( 'wpFromAddress', 60, $this->fromaddress, array( 'type' => 'text', 'maxlength' => 200 ) ) .
-				"</td>
-			</tr>
-			<tr>
+				'</td>
+			</tr>';
+
+			// Allow other extensions to add more fields into Special:Contact
+			wfRunHooks( 'ContactFormBeforeMessage', array( $this, &$form ) );
+
+			$form .= '<tr>
 				<td></td>
-				<td class='mw-input' id='mw-contactpage-formfootnote'>
-					<small>" . wfMsg( "contactpage-formfootnotes$msgSuffix" ) . "</small>
+				<td class="mw-input" id="mw-contactpage-formfootnote">
+					<small>' . wfMsg( "contactpage-formfootnotes$msgSuffix" ) . '</small>
 				</td>
 			</tr>
 			<tr>
-				<td class='mw-label'>" .
+				<td class="mw-label">' .
 					Xml::label( wfMsg( 'emailmessage' ), 'wpText' ) .
-				"</td>
-				<td class='mw-input'>" .
+				'</td>
+				<td class="mw-input">' .
 					Xml::textarea( 'wpText', $this->text, 80, 20, array( 'id' => 'wpText' ) ) .
-				"</td>
+				'</td>
 			</tr>
 			<tr>
 				<td></td>
-				<td class='mw-input'>" .
+				<td class="mw-input">' .
 					Xml::checkLabel( wfMsg( 'emailccme' ), 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) .
-					"<br />" . $this->getCaptcha() .
-				"</td>
+					'<br />' . $this->getCaptcha() .
+				'</td>
 			</tr>
 			<tr>
 				<td></td>
-				<td class='mw-submit'>" .
+				<td class="mw-submit">' .
 					Xml::submitButton( wfMsg( 'emailsend' ), array( 'name' => 'wpSend', 'accesskey' => 's' ) ) .
-				"</td>
-			</tr>" .
+				'</td>
+			</tr>' .
 			Xml::hidden( 'wpEditToken', $token ) .
 			Xml::closeElement( 'table' ) .
 			Xml::closeElement( 'fieldset' ) .
-			Xml::closeElement( 'form' )
-		);
+			Xml::closeElement( 'form' );
+		$wgOut->addHTML( $form );
 	}
 
 	function useCaptcha() {
 		global $wgCaptchaClass, $wgCaptchaTriggers, $wgUser;
-		if ( !$wgCaptchaClass ) return false; // no captcha installed
-		if ( !@$wgCaptchaTriggers['contactpage'] ) return false; // don't trigger on contact form
+		if ( !$wgCaptchaClass ) {
+			return false; // no captcha installed
+		}
+		if ( !@$wgCaptchaTriggers['contactpage'] ) {
+			return false; // don't trigger on contact form
+		}
 
 		if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
 			wfDebug( "EmailContactForm::useCaptcha: user group allows skipping captcha\n" );
@@ -241,19 +268,23 @@
 
 	function getCaptcha() {
 		global $wgCaptcha;
-		if ( !$this->useCaptcha() ) return ''; 
+		if ( !$this->useCaptcha() ) {
+			return '';
+		}
 
 		wfSetupSession(); #NOTE: make sure we have a session. May be required for captchas to work.
 
-		return "<div class='captcha'>" .
-		$wgCaptcha->getForm() .
-		wfMsgWikiHtml( 'contactpage-captcha' ) .
+		return '<div class="captcha">' .
+			$wgCaptcha->getForm() .
+			wfMsgWikiHtml( 'contactpage-captcha' ) .
 		"</div>\n";
 	}
 
 	function passCaptcha() {
 		global $wgCaptcha;
-		if ( !$this->useCaptcha() ) return true;
+		if ( !$this->useCaptcha() ) {
+			return true;
+		}
 
 		return $wgCaptcha->passCaptcha();
 	}
@@ -289,7 +320,7 @@
 
 		if ( $this->fromname !== '' ) {
 			$subject = wfMsgForContent( 'contactpage-subject-and-sender', $subject, $this->fromname );
-		} else if ( $this->fromaddress !== '' ) {
+		} elseif ( $this->fromaddress !== '' ) {
 			$subject = wfMsgForContent( 'contactpage-subject-and-sender', $subject, $this->fromaddress );
 		}
 
@@ -298,9 +329,9 @@
 			return;
 		}
 
-		wfDebug( __METHOD__ . ": sending mail from ".$submitterAddress->toString().
-			" to ".$targetAddress->toString().
-			" replyto ".( $replyto == null ? '-/-' : $replyto->toString() )."\n" );
+		wfDebug( __METHOD__ . ": sending mail from " . $submitterAddress->toString() .
+			" to " . $targetAddress->toString().
+			" replyto " . ( $replyto == null ? '-/-' : $replyto->toString() ) . "\n" );
 
 		$mailResult = UserMailer::send( $targetAddress, $submitterAddress, $subject, $this->text, $replyto );
 
@@ -313,10 +344,10 @@
 		// if the user requested a copy of this mail, do this now,
 		// unless they are emailing themselves, in which case one copy of the message is sufficient.
 		if( $this->cc_me && $this->fromaddress ) {
-			$cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
+			$cc_subject = wfMsg( 'emailccsubject', $this->target->getName(), $subject );
 			if( wfRunHooks( 'ContactForm', array( &$submitterAddress, &$contactSender, &$cc_subject, &$this->text ) ) ) {
-				wfDebug( __METHOD__ . ": sending cc mail from ".$contactSender->toString().
-					" to ".$submitterAddress->toString()."\n" );
+				wfDebug( __METHOD__ . ": sending cc mail from " . $contactSender->toString() .
+					" to " . $submitterAddress->toString() . "\n" );
 				$ccResult = UserMailer::send( $submitterAddress, $contactSender, $cc_subject, $this->text );
 				if( WikiError::isError( $ccResult ) ) {
 					// At this stage, the user's CC mail has failed, but their
@@ -333,7 +364,7 @@
 		wfDebug( __METHOD__ . ": success\n" );
 
 		$titleObj = SpecialPage::getTitleFor( 'Contact' );
-		$wgOut->redirect( $titleObj->getFullURL( "action=success" ) );
+		$wgOut->redirect( $titleObj->getFullURL( 'action=success' ) );
 		wfRunHooks( 'ContactFromComplete', array( $targetAddress, $replyto, $subject, $this->text ) );
 
 		wfDebug( __METHOD__ . ": end\n" );
Index: trunk/extensions/ContactPage/ContactPage.php
===================================================================
--- trunk/extensions/ContactPage/ContactPage.php	(revision 60637)
+++ trunk/extensions/ContactPage/ContactPage.php	(revision 60638)
@@ -15,6 +15,7 @@
 	die( 1 );
 }
 
+// Extension credits that will show up on Special:Version
 $wgExtensionCredits['specialpage'][] = array(
 	'path' => __FILE__,
 	'name' => 'ContactPage',
@@ -24,15 +25,25 @@
 	'descriptionmsg' => 'contactpage-desc',
 );
 
-$dir = dirname(__FILE__) . '/';
+// Set up the new special page
+$dir = dirname( __FILE__ ) . '/';
 $wgExtensionMessagesFiles['ContactPage'] = $dir . 'ContactPage.i18n.php';
 $wgExtensionAliasesFiles['ContactPage'] = $dir . 'ContactPage.alias.php';
 
 $wgAutoloadClasses['SpecialContact'] = $dir . 'SpecialContact.php';
 $wgSpecialPages['Contact'] = 'SpecialContact';
 
-$wgContactUser = NULL;
-$wgContactSender = NULL;
+# Configuration
+// Name of a registered wiki user who will receive the mails
+$wgContactUser = null;
+// E-mail address used as the sender of the contact email, if the visitor does
+// not supply an email address. Defaults to $wgEmergencyContact.
+$wgContactSender = null;
+
+// The name to be used with $wgContactSender.
+// This will be shown in the recipient's e-mail program
 $wgContactSenderName = 'Contact Form on ' . $wgSitename;
 
+// If true, users will be required to supply a name and an e-mail address
+// on Special:Contact.
 $wgContactRequireAll = false;

Status & tagging log

  • 09:58, 5 January 2010 ^demon (Talk | contribs) changed the status of r60638 [removed: new added: deferred]
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox