Greg Sabino Mullane has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/340274 )
Change subject: Initial checkin of version 1.8 of the EmailDiff extension ...................................................................... Initial checkin of version 1.8 of the EmailDiff extension Change-Id: I6131ac952819d45c2ab05f5ad3b797f711a5d833 --- A EmailDiff_body.php A README A extension.json A i18n/en.json A i18n/qqq.json 5 files changed, 361 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/EmailDiff refs/changes/74/340274/1 diff --git a/EmailDiff_body.php b/EmailDiff_body.php new file mode 100644 index 0000000..3b8cafb --- /dev/null +++ b/EmailDiff_body.php @@ -0,0 +1,147 @@ +<?php +/** + * Extension that allows textual diffs of page changes to be emailed + * + * @file + * @ingroup Extensions + * @author Greg Sabino Mullane <g...@endpoint.com> + * @link http://www.mediawiki.org/wiki/Extension:EmailDiff Documentation + * + * @copyright Copyright © 2015-2017, Greg Sabino Mullane + * @license The MIT License http://opensource.org/licenses/mit-license.php + */ + +class EmailDiff { + + static private $emaildiff_text, $emaildiff_subject_original; + + static private $emaildiff_hasdiff = false; + + /** + * SendNotificationEmail hook + * + * Allows adding text diff to the body of the outgoing email + * + * @param User $watchingUser current user (null if impersonal) + * @param integer $oldid the old revision id + * @param Title $title current article title + * @param string $header Additional headers - not used by this extension + * @param string $subject Subject line + * @param string $body Actual email body to be sent + * @return bool + */ + public static function SendNotificationEmailDiff( $watchingUser, $oldid, $title, &$header, &$subject, &$body ) { + + global $wgEmailDiffCommand, $wgEmailDiffSubjectSuffix; + + // Store the original subject line the first time we are called + if ( self::$emaildiff_subject_original === null ) { + self::$emaildiff_subject_original = $subject; + } + else { + // Reset the subject line in case we changed it last time + $subject = self::$emaildiff_subject_original; + } + + // Only show diffs if the user has set in in their preferences + // This will appear in the "Email options" section + // watchingUser can be null if this is called via sendImpersonal + if ( $watchingUser === null || $watchingUser->getOption( 'enotifshowdiff' ) ) { + + // The goal is to only generate the diff once, no matter how many users we are emailing + if ( self::$emaildiff_text === null ) { + + // Find the revision of the new page + $newrev = Revision::newFromTitle( $title ); + if ( !$newrev ) { + // This can happen is the page is an image, for example + self::$emaildiff_text = ''; + } + else { + // Get the actual text of the new page + $newtext = $newrev->getText(); + + // The page may be new (or unchanged?) + if ( !$oldid ) { + self::$emaildiff_text = wfMessage( "emaildiff-newpage", $newtext )->plain(); + } + else { + // Begin generating the actual diff + $tempdir = wfTempDir(); + + // Put the new page text into a temporary file: + $new_file = tempnam( $tempdir, "mediawikiemaildiffnew" ); + $fh = fopen( $new_file, "w" ) or die( "Could not open file $new_file" ); + fputs( $fh, "$newtext\n" ); + fclose( $fh ); + + // Put the old page text into a different temporary file: + $oldrev = Revision::newFromId( $oldid ); + $oldtext = $oldrev->getText(); + $old_file = tempnam( $tempdir, "mediawikiemaildiffold" ); + $fh = fopen( $old_file, "w" ) or die( "Could not open file $old_file" ); + fputs( $fh, "$oldtext\n" ); + fclose( $fh ); + + // Create a destination file, then run the diff command + $diff_file = tempnam( $tempdir, "mediawikiemaildiff" ); + $diffcom = str_replace( + array( "OLDFILE", "NEWFILE", "DIFFFILE" ), + array( "$old_file", "$new_file", "$diff_file" ), + $wgEmailDiffCommand); + system( $diffcom ); + + // Put the generated diff into our variable + self::$emaildiff_text = wfMessage( "emaildiff-intro", file_get_contents( $diff_file ) )->plain(); + + // Clean up our temporary files: + unlink( $old_file ); + unlink( $new_file ); + unlink( $diff_file ); + + self::$emaildiff_hasdiff = true; + + } // end generating a diff + + } // end if we have a new revision + + } // end if we have not created a diff yet + + // Possibly modify the subject line + if ( self::$emaildiff_hasdiff && strlen ( $wgEmailDiffSubjectSuffix ) ) { + $subject .= $wgEmailDiffSubjectSuffix; + } + + } // end if this user wants a diff + + // Replace the $PAGEDIFF placeholder with our generated diff + // If there is no diff, simply remove the placeholder + // This requires you edit MediaWiki:Enotif_body - see the documentation + $body = str_replace( '$PAGEDIFF', (self::$emaildiff_text === null ? '' : self::$emaildiff_text), $body ); + + return true; + + } + + + /** + * GetPreferences hook + * + * Puts a new preference in the "User profile / Email options" section + * + * @param User $user current user + * @param array $prefs list of default user preference controls + * @return bool + */ + public static function SetEmailDiffPref( $user, &$prefs ) { + + $prefs['enotifshowdiff'] = array( + 'type' => 'toggle', + 'section' => 'personal/email', + 'label-message' => 'tog-emaildiff' + ); + + return true; + } + +} diff --git a/README b/README new file mode 100644 index 0000000..463abe7 --- /dev/null +++ b/README @@ -0,0 +1,161 @@ +This is the EmailDiff extension for MediaWiki. It will allow +sending of text-based diffs in the emails MediaWiki sends when a +page changes. + +Written by Greg Sabino Mullane, End Point Corporation <g...@endpoint.com> + +The full documentation can be found at: + +http://www.mediawiki.org/wiki/Extension:EmailDiff + +This extension will only work on MediaWiki version 1.25 or better. + +EmailDiff is very useful on small wikis when used with $wgUsersNotifiedOnAllChanges + + +INSTALLATION +============ + +1. Install the files + +Unpack the tarball to your MediaWiki 'extensions' folder as usual. + + +2. Add hooks to EmailNotification.php + +A new hook may need to be added in two places inside the file includes/mail/EmailNotification.php + +Inside the function sendPersonalized, right before the "return" line, add: + +Hooks::run( 'SendNotificationEmail', + [ $watchingUser, $this->oldid, $this->title, &$headers, &$this->subject, &$body ] ); + +Inside the function sendImpersonal, right before the "return" line, add: + +$headers = []; +Hooks::run( 'SendNotificationEmail', + [ null, $this->oldid, $this->title, &$headers, &$this->subject, &$body ] ); + + +3. Modify LocalSettings.php + +Next, add 'EmailDiff' to your list of modules to load inside your LocalSettings.php +file via wfLoadExtension: + +wfLoadExtension( 'EmailDiff' ); + +If you need to change any of the configuration values, do so right +beneath the wfLoadExtension line. There are currently two +configuration values: + +* $wgEmailDiffSubjectSuffix + +When a diff is included in the email, this is a string that will be added +to the end of the subject line. The default is " (diff)". It can be set to +an empty string to prevent the subject from being modified. + +* $wgEmailDiffCommand + +This is the system command that is run to generate the actual diff. +The default value should work on most systems and looks like this: + +"/usr/bin/diff -u OLDFILE NEWFILE | /usr/bin/tail --lines=+3 > DIFFFILE" + +If you need to change it, make sure you keep the UPPERCASE items, which +are replaced before the command is run with temporary files. + + +4. Modify the outgoing email template + +One final change is needed to make this extension work: you must +add the string "$PAGEDIFF" (without the quotes) to the template +for outgoing notification emails. This can be done by visiting +this page on your wiki: MediaWiki:Enotif_body + +The default value for this page is very wordy: it is recommended that you +trim it a little bit and add $PAGEDIFF towards the bottom. Here is +one possible setting: + +Page: $PAGETITLE +Summary: $PAGESUMMARY $PAGEMINOREDIT +User: $PAGEEDITOR Time: $PAGEEDITDATE +$PAGEDIFF + +$NEWPAGE + + +5. Set users to view diffs + +If you are using "impersonal" emails by setting $wgEnotifImpersonal true in your LocalSettings.php, +then everyone will receive a diff. + +Otherwise, users must set their preferences to receive email diffs. Go to Preferences, then +find the "Email options" section and select "Send a diff of changes" + +Alternatively, you can simpy turn this on for all users by adding the +following line to your LocalSettings.php file: + +$wgDefaultUserOptions['enotifshowdiff'] = true; + + +ADVANCED OPTIONS +================ + +* Receive email about your own edits + +If you wish to get an email for all edits, even those made by yourself, do the following: + +Add this to your LocalSettings.php file: + +$wgEnotifSelfEdits = true; + +Edit the file includes/mail/EmailNotification.php. Find the function actuallyNotifyOnPageChange, +then add $wgEnotifSelfEdits to the list of global variables at the top of the function. It should +look similar to this after the change: + + global $wgEnotifMinorEdits, $wgEnotifUserTalk, $wgEnotifSelfEdits; + +Search for the string 'editor->getName', and change this block of code: + + if ( $editor->getName() == $name ) { + // No point notifying the user that actually made the change! + continue; + } + +into this: + + if ( $editor->getName() == $name && ! $wgEnotifSelfEdits) { + // No point notifying the user that actually made the change! + continue; + } + +You should now receive emails on every change, even those done yourself. + + +* Do not receive emails when a bot makes an edit + +It can be a pain if your username is inside $wgUsersNotifiedOnAllChanges and +a bot comes along and updates hundreds of pages at once. The best way to +prevent this is to restrict email to non-bots only. + +Add this to your LocalSettings.php file: + +$wgEnotifBotEdits = false; + +Edit the file includes/mail/EmailNotification.php. Find the function notifyOnPageChange, +then add $wgEnotifBotEdits to the list of global variables at the top of the function. It should +look similar to this after the change: + + global $wgEnotifMinorEdits, $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk, $wgEnotifBotEdits; + + +Find the line that has "$sendEmail = true;" and add this block of code before that line: + + + // May not want to get emails when a bot edits a page + if ( $editor->isAllowed( 'bot' ) && ! $wgEnotifBotEdits ) { + return; + } + +Once that is done, bot edits should not trigger email notifications. + diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..14046a3 --- /dev/null +++ b/extension.json @@ -0,0 +1,35 @@ +{ + "manifest_version": 1, + "name": "EmailDiff", + "type": "other", + "author": [ + "Greg Sabino Mullane" + ], + "version": "1.8", + "url": "https://www.mediawiki.org/wiki/Extension:EmailDiff", + "descriptionmsg": "emaildiff-desc", + "license-name": "MIT", + "requires" : { + "MediaWiki": ">= 1.25.0" + }, + "AutoloadClasses": { + "EmailDiff": "EmailDiff_body.php" + }, + "Hooks": { + "SendNotificationEmail" : [ + "EmailDiff::SendNotificationEmailDiff" + ], + "GetPreferences" : [ + "EmailDiff::SetEmailDiffPref" + ] + }, + "MessagesDirs": { + "EmailDiff": [ + "i18n" + ] + }, + "config": { + "EmailDiffCommand": "/usr/bin/diff -u OLDFILE NEWFILE | /usr/bin/tail --lines=+3 > DIFFFILE", + "EmailDiffSubjectSuffix": " (diff)" + } +} diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..ae8d894 --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,7 @@ +{ + "emaildiff-desc" : "Allow sending of text diffs in notification emails", + "tog-emaildiff" : "Send a diff of changes", + "emaildiff-newpage" : "This is a new page:\n\n$1\n", + "emaildiff-intro" : "\nVersion differences:\n$1\n" +} + diff --git a/i18n/qqq.json b/i18n/qqq.json new file mode 100644 index 0000000..4b98a0b --- /dev/null +++ b/i18n/qqq.json @@ -0,0 +1,11 @@ +{ + "@metadata": { + "authors": [ + "Greg Sabino Mullane", + ] + }, + "emaildiff-desc" : "Description of what the extension does; appears in Special:Version", + "tog-emaildiff" : "Message for the user preferences section", + "emaildiff-newpage" : "Message inside email when the page has no differences because it is new", + "emaildiff-intro" : "String to put in front of the diff" +} -- To view, visit https://gerrit.wikimedia.org/r/340274 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6131ac952819d45c2ab05f5ad3b797f711a5d833 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/EmailDiff Gerrit-Branch: master Gerrit-Owner: Greg Sabino Mullane <g...@turnstep.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits