r60670 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60669‎ | r60670 | r60671 >
Date:17:14, 5 January 2010
Author:yaron
Status:deferred
Tags:
Comment:
Contains "SemanticTasksMailer" class - holds functions formerly contained in file ST_Notify_Assignment.php
Modified paths:
  • /trunk/extensions/SemanticTasks/SemanticTasks.classes.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticTasks/SemanticTasks.classes.php
@@ -0,0 +1,220 @@
 2+<?php
 3+
 4+/**
 5+ * This class handles the creation and sending of notification emails.
 6+ */
 7+class SemanticTasksMailer {
 8+ function mailAssigneesUpdatedTask( $article, $current_user, $text, $summary, $minoredit, $watchthis, $sectionanchor, $flags, $revision ) {
 9+ if ( !$minoredit ) {
 10+ // i18n
 11+ wfLoadExtensionMessages( 'SemanticTasks' );
 12+
 13+ // Grab the wiki name
 14+ global $wgSitename;
 15+
 16+ // Get the revision count to determine if new article
 17+ $rev = $article->estimateRevisionCount();
 18+
 19+ if ( $rev == 1 ) {
 20+ self::mailAssignees( $article, $current_user, '[' . $wgSitename . '] ' . wfMsg( 'semantictasks-newtask' ), 'semantictasks-assignedtoyou-msg', /*diff?*/ false, /*Page text*/ true );
 21+ } else {
 22+ self::mailAssignees( $article, $current_user, '[' . $wgSitename . '] ' . wfMsg( 'semantictasks-taskupdated' ), 'semantictasks-updatedtoyou-msg', /*diff?*/ true, /*Page text*/ false );
 23+ }
 24+ }
 25+ return TRUE;
 26+ }
 27+
 28+ function mailAssignees( $article, $user, $pre_title, $message, $display_diff, $display_text ) {
 29+ $title = $article->getTitle();
 30+
 31+ // Send notifications to assignees and ccs
 32+ self::mailNotification( 'Assigned to', $article, $user, $pre_title, $message, $display_diff, $display_text );
 33+ self::mailNotification( 'Carbon copy', $article, $user, $pre_title, $message, $display_diff, $display_text );
 34+ return TRUE;
 35+ }
 36+
 37+ /**
 38+ * Sends mail notifications
 39+ * @param $query_word String: the property that designate the users to notify.
 40+ */
 41+ function mailNotification( $query_word, $article, $user, $pre_title, $message, $display_diff, $display_text ) {
 42+ $title = $article->getTitle();
 43+
 44+ // get the result of the query "[[$title]][[$query_word::+]]"
 45+ $properties_to_display = array();
 46+ $properties_to_display[0] = $query_word;
 47+ $results = self::getQueryResults( "[[$title]][[$query_word::+]]", $properties_to_display, false );
 48+
 49+ // In theory, there is only one row
 50+ while ( $row = $results->getNext() ) {
 51+ $task_assignees = $row[0];
 52+ }
 53+
 54+ // If not any row, do nothing
 55+ if ( empty( $task_assignees ) ) {
 56+ return FALSE;
 57+ }
 58+
 59+ $subject = "$pre_title $title";
 60+ $from = new MailAddress( $user->getEmail(), $user->getName() );
 61+ $link = $title->escapeFullURL();
 62+
 63+ $user_mailer = new UserMailer();
 64+
 65+ while ( $task_assignee = $task_assignees->getNextObject() ) {
 66+ $assignee_username = $task_assignee->getTitle();
 67+ $assignee_user_name = explode( ":", $assignee_username );
 68+ $assignee_name = $assignee_user_name[1];
 69+ $body = wfMsg( $message , $assignee_name , $title ) . $link;
 70+ if ( $display_text ) {
 71+ $body .= "\n \n" . wfMsg( 'semantictasks-text-message' ) . "\n" . $article->getContent() ;
 72+ }
 73+ if ( $display_diff ) {
 74+ $body .= "\n \n" . wfMsg( 'semantictasks-diff-message' ) . "\n" . self::generateDiffBodyTxt( $title );
 75+ }
 76+
 77+ // TEST: uncomment this for test mode (Writes body in testFile)
 78+ // st_WriteTestFile( $body );
 79+
 80+ $assignee = User::newFromName( $assignee_name );
 81+ // if assignee is the current user, do nothing
 82+ if ( $assignee->getID() != $user->getID() ) {
 83+ $assignee_mail = new MailAddress( $assignee->getEmail(), $assignee_name );
 84+ $user_mailer->send( $assignee_mail, $from, $subject, $body );
 85+ }
 86+ }
 87+
 88+ return TRUE;
 89+ }
 90+
 91+ /**
 92+ * Generates a diff txt
 93+ * @param Title $title
 94+ * @return string
 95+ */
 96+ function generateDiffBodyTxt( $title ) {
 97+ $revision = Revision::newFromTitle( $title, 0 );
 98+ $diff = new DifferenceEngine( $title, $revision->getId(), 'prev' );
 99+ // The getDiffBody() method generates html, so let's generate the txt diff manualy:
 100+ global $wgContLang;
 101+ $diff->loadText();
 102+ $otext = str_replace( "\r\n", "\n", $diff->mOldtext );
 103+ $ntext = str_replace( "\r\n", "\n", $diff->mNewtext );
 104+ $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
 105+ $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
 106+ // We use here the php diff engine included in MediaWiki
 107+ $diffs = new Diff( $ota, $nta );
 108+ // And we ask for a txt formatted diff
 109+ $formatter = new UnifiedDiffFormatter();
 110+ $diff_text = $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
 111+ return $diff_text;
 112+ }
 113+
 114+ /**
 115+ * This function returns to results of a certain query
 116+ * Thank you Yaron Koren for advices concerning this code
 117+ * @param $query_string String : the query
 118+ * @param $properties_to_display array(String): array of property names to display
 119+ * @param $display_title Boolean : add the page title in the result
 120+ * @return TODO
 121+*/
 122+ function getQueryResults( $query_string, $properties_to_display, $display_title ) {
 123+ // i18n
 124+ wfLoadExtensionMessages( 'SemanticTasks' );
 125+
 126+ // We use the Semantic MediaWiki Processor
 127+ global $smwgIP;
 128+ include_once( $smwgIP . "/includes/SMW_QueryProcessor.php" );
 129+
 130+ $params = array();
 131+ $inline = true;
 132+ $format = 'auto';
 133+ $printlabel = "";
 134+ $printouts = array();
 135+
 136+ // add the page name to the printouts
 137+ if ( $display_title ) {
 138+ $to_push = new SMWPrintRequest( SMWPrintRequest::PRINT_THIS, $printlabel );
 139+ array_push( $printouts, $to_push );
 140+ }
 141+
 142+ // Push the properties to display in the printout array.
 143+ foreach ( $properties_to_display as $property ) {
 144+ if ( class_exists( 'SMWPropertyValue' ) ) { // SMW 1.4
 145+ $to_push = new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, $printlabel, SMWPropertyValue::makeProperty( $property ) );
 146+ } else {
 147+ $to_push = new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, $printlabel, Title::newFromText( $property, SMW_NS_PROPERTY ) );
 148+ }
 149+ array_push( $printouts, $to_push );
 150+ }
 151+
 152+ $query = SMWQueryProcessor::createQuery( $query_string, $params, $inline, $format, $printouts );
 153+ $results = smwfGetStore()->getQueryResult( $query );
 154+
 155+ return $results;
 156+ }
 157+
 158+ function remindAssignees( $wiki_url ) {
 159+ global $wgSitename, $wgServer;
 160+
 161+ $user_mailer = new UserMailer();
 162+
 163+ $t = getdate();
 164+ $today = date( 'F d Y', $t[0] );
 165+
 166+ $query_string = "[[Reminder at::+]][[Status::New||In Progress]][[Target date::> $today]]";
 167+ $properties_to_display = array( 'Reminder at', 'Assigned to', 'Target date' );
 168+
 169+ $results = self::getQueryResults( $query_string, $properties_to_display, true );
 170+ if ( empty( $results ) ) {
 171+ return FALSE;
 172+ }
 173+
 174+ $sender = new MailAddress( "no-reply@$wgServerName", "$wgSitename" );
 175+
 176+ while ( $row = $results->getNext() ) {
 177+ $task_name = $row[0]->getNextObject()->getTitle();
 178+ $subject = '[' . $wgSitename . '] ' . wfMsg( 'semantictasks-reminder' ) . $task_name;
 179+ // The following doesn't work, maybe because we use a cron job.
 180+ // $link = $task_name->escapeFullURL();
 181+ // So let's do it manually
 182+ $link = $wiki_url . $task_name->getPartialURL();
 183+
 184+ $target_date = $row[3]->getNextObject();
 185+ $tg_date = new DateTime( $target_date->getShortHTMLText() );
 186+
 187+ while ( $reminder = $row[1]->getNextObject() ) {
 188+ $remind_me_in = $reminder->getShortHTMLText();
 189+ $date = new DateTime( $today );
 190+ $date->modify( "+$remind_me_in day" );
 191+
 192+ if ( $tg_date-> format( 'F d Y' ) == $date-> format( 'F d Y' ) ) {
 193+ global $wgLang;
 194+ while ( $task_assignee = $row[2]->getNextObject() ) {
 195+ $assignee_username = $task_assignee->getTitle();
 196+ $assignee_user_name = explode( ":", $assignee_username );
 197+ $assignee_name = $assignee_user_name[1];
 198+
 199+ $assignee = User::newFromName( $assignee_name );
 200+ $assignee_mail = new MailAddress( $assignee->getEmail(), $assignee_name );
 201+ $body = wfMsgExt( 'semantictasks-reminder-message', 'parsemag', $assignee_name, $task_name, $wgLang->formatNum( $remind_me_in ), $link );
 202+ $user_mailer->send( $assignee_mail, $sender, $subject, $body );
 203+ }
 204+ }
 205+ }
 206+ }
 207+ return TRUE;
 208+ }
 209+}
 210+
 211+/**
 212+* This function is for test mode only, it write its argument in a specific file.
 213+* This file must be writable for the system and be at the roor of your wiki installation
 214+* @param $stringData String : to write
 215+*/
 216+function st_WriteTestFile( $stringData ) {
 217+ $testFile = "/tmp/testFile.txt";
 218+ $fh = fopen( $testFile, 'a' ) or die( "can't open file" );
 219+ fwrite( $fh, $stringData );
 220+ fclose( $fh );
 221+}

Status & tagging log