From a3cbeaeefdf048b67da26c53f59ac48cb3152bae Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Wed, 17 Dec 2008 17:27:43 +0000 Subject: [PATCH] * Short circuit EmailNotification::notify() to not call EmailNotification::commonMessageKeys() if there're no users to notify. This is a hack to work arround the following exception: A database query syntax error has occurred. The last attempted database query was: "SELECT gu_id, lu_wiki, gu_salt, gu_password,gu_auth_token, gu_locked,gu_hidden,gu_registration,gu_email,gu_email_authenticated FROM `parsertest_globaluser` LEFT OUTER JOIN `parsertest_localuser` ON gu_name=lu_name AND lu_wiki='test2wiki-parsertest_' WHERE gu_name='127.0.0.1'" from within function "Database::safeQuery". MySQL returned error "1146: Table 'centralauth.parsertest_globaluser' doesn't exist (127.0.0.1)" * Whitespaces fixes --- includes/Hooks.php | 61 +++++++++++++++--------------- includes/UserMailer.php | 83 +++++++++++++++++++++-------------------- 2 files changed, 73 insertions(+), 71 deletions(-) diff --git a/includes/Hooks.php b/includes/Hooks.php index 2fd9ab245e..ebb82ceee7 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -23,40 +23,39 @@ */ /* Private */ -function _wfInvokeInternalGoop($event, $hook) { +function _wfInvokeInternalGoop( $event, $hook ) { $object = NULL; $method = NULL; $func = NULL; $data = NULL; $have_data = false; - if (is_array($hook)) { - if (count($hook) < 1) { - throw new MWException("Empty array in hooks for " . $event . "\n"); - } else if (is_object($hook[0])) { + if ( is_array( $hook ) ) { + if ( count( $hook ) < 1 ) { + throw new MWException( "Empty array in hooks for " . $event . "\n" ); + } else if ( is_object( $hook[0] ) ) { $object = $hook[0]; - if (count($hook) < 2) { + if ( count( $hook ) < 2 ) { $method = "on" . $event; } else { $method = $hook[1]; - if (count($hook) > 2) { + if ( count( $hook ) > 2 ) { $data = $hook[2]; $have_data = true; } } - } else if (is_string($hook[0])) { + } else if ( is_string( $hook[0] ) ) { $func = $hook[0]; - if (count($hook) > 1) { + if ( count( $hook ) > 1 ) { $data = $hook[1]; $have_data = true; } } else { - var_dump( $wgHooks ); - throw new MWException("Unknown datatype in hooks for " . $event . "\n"); + throw new MWException( "Unknown datatype in hooks for " . $event . "\n" ); } - } else if (is_string($hook)) { # functions look like strings, too + } else if ( is_string( $hook ) ) { # functions look like strings, too $func = $hook; - } else if (is_object($hook)) { + } else if ( is_object( $hook ) ) { $object = $hook; $method = "on" . $event; } else { @@ -76,8 +75,8 @@ function _wfInvokeInternalGoop($event, $hook) { } /* Return a string describing the hook for debugging purposes. */ -function wfFormatInvocation($event, $hook, $args = array()) { - list($callback, $func, $data) = _wfInvokeInternalGoop($event, $hook, $args); +function wfFormatInvocation( $event, $hook, $args = array() ) { + list( $callback, $func, $data ) = _wfInvokeInternalGoop( $event, $hook, $args ); if( is_array( $callback ) ) { if( is_object( $callback[0] ) ) { @@ -103,12 +102,12 @@ function wfFormatInvocation($event, $hook, $args = array()) { * If arguments are provided both as part of the hook itself, and when * calling wfCallFancyCallback, the two arrays are merged. */ -function wfInvoke($event, $hook, $args = array()) { - list($callback, $func, $data) = _wfInvokeInternalGoop($event, $hook, $args); +function wfInvoke( $event, $hook, $args = array() ) { + list( $callback, $func, $data ) = _wfInvokeInternalGoop( $event, $hook, $args ); /* We put the first data element on, if needed. */ - if ($data) { - $hook_args = array_merge(array($data), $args); + if ( $data ) { + $hook_args = array_merge( array( $data ), $args ); } else { $hook_args = $args; } @@ -129,40 +128,40 @@ function wfInvoke($event, $hook, $args = array()) { * careful about its contents. So, there's a lot more error-checking * in here than would normally be necessary. */ -function wfRunHooks($event, $args = array()) { +function wfRunHooks( $event, $args = array() ) { global $wgHooks; - if (!is_array($wgHooks)) { - throw new MWException("Global hooks array is not an array!\n"); + if ( !is_array( $wgHooks ) ) { + throw new MWException( "Global hooks array is not an array!\n" ); return false; } - if (!array_key_exists($event, $wgHooks)) { + if (!array_key_exists( $event, $wgHooks ) ) { return true; } - if (!is_array($wgHooks[$event])) { - throw new MWException("Hooks array for event '$event' is not an array!\n"); + if ( !is_array( $wgHooks[$event] ) ) { + throw new MWException( "Hooks array for event '$event' is not an array!\n" ); return false; } - foreach ($wgHooks[$event] as $index => $hook) { + foreach ( $wgHooks[$event] as $index => $hook ) { - $retval = wfInvoke($event, $hook, $args); + $retval = wfInvoke( $event, $hook, $args ); /* String return is an error; false return means stop processing. */ - if (is_string($retval)) { + if ( is_string( $retval ) ) { global $wgOut; - $wgOut->showFatalError($retval); + $wgOut->showFatalError( $retval ); return false; } elseif( $retval === null ) { - $prettyFunc = wfFormatInvocation($event, $hook, $args); + $prettyFunc = wfFormatInvocation( $event, $hook, $args ); throw new MWException( "Detected bug in an extension! " . "Hook $prettyFunc failed to return a value; " . "should return true to continue hook processing or false to abort." ); - } else if (!$retval) { + } else if ( !$retval ) { return false; } } diff --git a/includes/UserMailer.php b/includes/UserMailer.php index 05ba9a311f..11d9575de5 100644 --- a/includes/UserMailer.php +++ b/includes/UserMailer.php @@ -262,38 +262,41 @@ class EmailNotification { * The recipient is appended to the arguments given to messageCompositionFunction. * Both callbacks are to be given in the same formats accepted by the hook system. */ - static function notify($editor, $timestamp, $userListFunction, $messageCompositionFunction) { + static function notify( $editor, $timestamp, $userListFunction, $messageCompositionFunction ) { global $wgEnotifUseRealName, $wgEnotifImpersonal; global $wgLang; - $common_keys = self::commonMessageKeys($editor); - $users = wfInvoke("userList", $userListFunction); - foreach($users as $u) { - list($user_keys, $body_msg_name, $subj_msg_name) = - wfInvoke("message", $messageCompositionFunction, array($u)); - $keys = array_merge($common_keys, $user_keys); + $users = wfInvoke( 'userList', $userListFunction ); + if( !count( $users ) ) + return; + + $common_keys = self::commonMessageKeys( $editor ); + foreach( $users as $u ) { + list( $user_keys, $body_msg_name, $subj_msg_name ) = + wfInvoke( 'message', $messageCompositionFunction, array( $u ) ); + $keys = array_merge( $common_keys, $user_keys ); if( $wgEnotifImpersonal ) { - $keys['$WATCHINGUSERNAME'] = wfMsgForContent('enotif_impersonal_salutation'); - $keys['$PAGEEDITDATE'] = $wgLang->timeanddate($timestamp, true, false, false); + $keys['$WATCHINGUSERNAME'] = wfMsgForContent( 'enotif_impersonal_salutation' ); + $keys['$PAGEEDITDATE'] = $wgLang->timeanddate( $timestamp, true, false, false ); } else { $keys['$WATCHINGUSERNAME'] = $wgEnotifUseRealName ? $u->getRealName() : $u->getName(); - $keys['$PAGEEDITDATE'] = $wgLang->timeAndDate($timestamp, true, false, - $u->getOption('timecorrection')); + $keys['$PAGEEDITDATE'] = $wgLang->timeAndDate( $timestamp, true, false, + $u->getOption( 'timecorrection' ) ); } - $subject = strtr(wfMsgForContent( $subj_msg_name ), $keys); + $subject = strtr( wfMsgForContent( $subj_msg_name ), $keys ); $body = wordwrap( strtr( wfMsgForContent( $body_msg_name ), $keys ), 72 ); - $to = new MailAddress($u); + $to = new MailAddress( $u ); $from = $keys['$FROM_HEADER']; $replyto = $keys['$REPLYTO_HEADER']; - UserMailer::send($to, $from, $subject, $body, $replyto); + UserMailer::send( $to, $from, $subject, $body, $replyto ); } } - static function commonMessageKeys($editor) { + static function commonMessageKeys( $editor ) { global $wgEnotifUseRealName, $wgEnotifRevealEditorAddress; global $wgNoReplyAddress, $wgPasswordSender; @@ -320,11 +323,11 @@ class EmailNotification { $keys['$REPLYTO_HEADER'] = $replyto; if( $editor->isAnon() ) { - $keys['$PAGEEDITOR'] = wfMsgForContent('enotif_anon_editor', $name); + $keys['$PAGEEDITOR'] = wfMsgForContent( 'enotif_anon_editor', $name ); $keys['$PAGEEDITOR_EMAIL'] = wfMsgForContent( 'noemailtitle' ); } else{ $keys['$PAGEEDITOR'] = $name; - $keys['$PAGEEDITOR_EMAIL'] = SpecialPage::getSafeTitleFor('Emailuser', $name)->getFullUrl(); + $keys['$PAGEEDITOR_EMAIL'] = SpecialPage::getSafeTitleFor( 'Emailuser', $name )->getFullUrl(); } $keys['$PAGEEDITOR_WIKI'] = $editor->getUserPage()->getFullUrl(); @@ -357,13 +360,13 @@ class PageChangeNotification { * @param $minorEdit * @param $oldid (default: false) */ - static function notifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid = false) { + static function notifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid = false ) { global $wgEnotifUseJobQ; - if( $title->getNamespace() < 0 ) + if ( $title->getNamespace() < 0 ) return; - if ($wgEnotifUseJobQ) { + if ( $wgEnotifUseJobQ ) { $params = array( "editor" => $editor->getName(), "editorID" => $editor->getID(), @@ -374,7 +377,7 @@ class PageChangeNotification { $job = new EnotifNotifyJob( $title, $params ); $job->insert(); } else { - self::actuallyNotifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid); + self::actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit, $oldid ); } } @@ -392,15 +395,15 @@ class PageChangeNotification { * @param $minorEdit * @param $oldid (default: false) */ - static function actuallyNotifyOnPageChange($editor, $title, $timestamp, - $summary, $minorEdit, $oldid=false) { + static function actuallyNotifyOnPageChange( $editor, $title, $timestamp, + $summary, $minorEdit, $oldid = false ) { global $wgShowUpdatedMarker, $wgEnotifWatchlist; wfProfileIn( __METHOD__ ); - EmailNotification::notify($editor, $timestamp, - array('PageChangeNotification::usersList', array($editor, $title, $minorEdit)), - array('PageChangeNotification::message', array($oldid, $minorEdit, $summary, $title, $editor) ) ); + EmailNotification::notify( $editor, $timestamp, + array( 'PageChangeNotification::usersList', array( $editor, $title, $minorEdit ) ), + array( 'PageChangeNotification::message', array( $oldid, $minorEdit, $summary, $title, $editor ) ) ); $latestTimestamp = Revision::getTimestampFromId( $title, $title->getLatestRevID() ); // Do not update watchlists if something else already did. @@ -428,7 +431,7 @@ class PageChangeNotification { static function message( $stuff ) { global $wgEnotifImpersonal; - list($oldid, $medit, $summary, $title, $user) = $stuff; + list( $oldid, $medit, $summary, $title, $user ) = $stuff; $keys = array(); # regarding the use of oldid as an indicator for the last visited version, see also @@ -448,28 +451,28 @@ class PageChangeNotification { if ($wgEnotifImpersonal && $oldid) { # For impersonal mail, show a diff link to the last revision. - $keys['$NEWPAGE'] = wfMsgForContent('enotif_lastdiff', - $title->getFullURL("oldid={$oldid}&diff=prev")); + $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastdiff', + $title->getFullURL( "oldid={$oldid}&diff=prev" ) ); } $keys['$PAGETITLE'] = $title->getPrefixedText(); $keys['$PAGETITLE_URL'] = $title->getFullUrl(); $keys['$PAGEMINOREDIT'] = $medit ? wfMsg( 'minoredit' ) : ''; - $keys['$PAGESUMMARY'] = ($summary == '') ? ' - ' : $summary; + $keys['$PAGESUMMARY'] = ( $summary == '' ) ? ' - ' : $summary; - return array($keys, 'enotif_body', 'enotif_subject'); + return array( $keys, 'enotif_body', 'enotif_subject' ); } - static function usersList($stuff) { + static function usersList( $stuff ) { global $wgEnotifWatchlist, $wgEnotifMinorEdits, $wgUsersNotifiedOnAllChanges; - list($editor, $title, $minorEdit) = $stuff; + list( $editor, $title, $minorEdit ) = $stuff; $recipients = array(); # User talk pages: $userTalkId = false; - if( $title->getNamespace() == NS_USER_TALK && (!$minorEdit || $wgEnotifMinorEdits) ) { - $targetUser = User::newFromName($title->getText()); + if( $title->getNamespace() == NS_USER_TALK && ( !$minorEdit || $wgEnotifMinorEdits ) ) { + $targetUser = User::newFromName( $title->getText() ); if ( !$targetUser || $targetUser->isAnon() ) $msg = "user talk page edited, but user does not exist"; @@ -477,7 +480,7 @@ class PageChangeNotification { else if ( $targetUser->getId() == $editor->getId() ) $msg = "user edited their own talk page, no notification sent"; - else if ( !$targetUser->getOption('enotifusertalkpages') ) + else if ( !$targetUser->getOption( 'enotifusertalkpages' ) ) $msg = "talk page owner doesn't want notifications"; else if ( !$targetUser->isEmailConfirmed() ) @@ -488,11 +491,11 @@ class PageChangeNotification { $recipients[] = $targetUser; $userTalkId = $targetUser->getId(); # won't be included in watchlist, below. } - wfDebug( __METHOD__ .": ". $msg . "\n" ); + wfDebug( __METHOD__ . ': ' . $msg . "\n" ); } - wfDebug("Did not send a user-talk notification.\n"); + wfDebug( "Did not send a user-talk notification.\n" ); - if( $wgEnotifWatchlist && (!$minorEdit || $wgEnotifMinorEdits) ) { + if( $wgEnotifWatchlist && ( !$minorEdit || $wgEnotifMinorEdits ) ) { // Send updates to watchers other than the current editor $userCondition = 'wl_user != ' . $editor->getID(); @@ -525,7 +528,7 @@ class PageChangeNotification { } foreach ( $wgUsersNotifiedOnAllChanges as $name ) { - $recipients[] = User::newFromName($name); + $recipients[] = User::newFromName( $name ); } return $recipients; -- 2.20.1