jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/274764 )
Change subject: Initial import ...................................................................... Initial import Import EmailDiff version 1.7 Change-Id: I7120980f5f6423d74bda03cfe522660b074b5949 --- A EmailDiff_body.php A README A extension.json A i18n/en.json A i18n/qqq.json 5 files changed, 257 insertions(+), 0 deletions(-) Approvals: Umherirrender: Looks good to me, approved jenkins-bot: Verified diff --git a/EmailDiff_body.php b/EmailDiff_body.php new file mode 100644 index 0000000..f4e11ce --- /dev/null +++ b/EmailDiff_body.php @@ -0,0 +1,135 @@ +<?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-2016, 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; + + /** + * SendPersonalizedNotificationEmail hook + * + * Allows adding text diff to the body of the outgoing email + * + * @param User $watchingUser current user + * @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 SendPersonalizedNotificationEmailDiff( $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 + if ( $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() . "\n"; + } 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" ); + fwrite( $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" ); + fwrite( $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 = "\n" . wfMessage( 'emaildiff-intro', file_get_contents( $diff_file ) )->plain() . "\n"; + + // 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..382a930 --- /dev/null +++ b/README @@ -0,0 +1,69 @@ +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 + +Note that this extension may have serious performance issues on +very large sites, where "large" means thousands of users making +near-constant edits. + +INSTALLATION +============ + +This extension will only work on MediaWiki version 1.25 or better. + +Unpack the tarball to your MediaWiki 'extensions' folder as usual. + +A new hook will need to be added to the file includes/mail/EmailNotification.php + +Inside the function sendPersonalized, right before the "return" line, add: + +Hooks::run( 'SendPersonalizedNotificationEmail', + [ $watchingUser, $this->oldid, $this->title, &$headers, &$this->subject, &$body ] ); + +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 three +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. + +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 + diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..8047d86 --- /dev/null +++ b/extension.json @@ -0,0 +1,35 @@ +{ + "manifest_version": 1, + "name": "EmailDiff", + "type": "other", + "author": [ + "Greg Sabino Mullane" + ], + "version": "1.7", + "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": { + "SendPersonalizedNotificationEmail": [ + "EmailDiff::SendPersonalizedNotificationEmailDiff" + ], + "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..8fce42e --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,7 @@ +{ + "@metadata": {}, + "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", + "emaildiff-intro": "Version differences:\n$1" +} diff --git a/i18n/qqq.json b/i18n/qqq.json new file mode 100644 index 0000000..4e765ae --- /dev/null +++ b/i18n/qqq.json @@ -0,0 +1,11 @@ +{ + "@metadata": { + "authors": [ + "Greg Sabino Mullane" + ] + }, + "emaildiff-desc": "{{desc|name=EmailDiff|url=https://www.mediawiki.org/wiki/Extension:EmailDiff}}", + "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/274764 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I7120980f5f6423d74bda03cfe522660b074b5949 Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/EmailDiff Gerrit-Branch: master Gerrit-Owner: Greg Sabino Mullane <g...@turnstep.com> Gerrit-Reviewer: Greg Sabino Mullane <g...@turnstep.com> Gerrit-Reviewer: Siebrand <siebr...@kitano.nl> Gerrit-Reviewer: Umherirrender <umherirrender_de...@web.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits