Extension:Email Digest
From MediaWiki.org
|
Email Digest Release status: unknown |
|
|---|---|
| Implementation | Notify |
| Description | Emails a user their watchlist |
| Author(s) | MHart |
| Download | SpecialWatchList2.tgz & MailWatchlist.tgz |
Contents |
[edit] Email watchlist changes
I modified MediaWiki on some installations I administer to email daily watchlist changes as a digest.
My HTTP server is Apache 2.0.50, operating system is Mandrakelinux 10.1, mailer is sendmail.
Please forgive any primitive aspects of this code - this is the first time I've ever used PHP. :-)
This documentation is for MediaWiki version 1.3 and may need some adjustments to work with later versions.
[edit] Modify /languages/Language.php
MediaWiki very cleanly handles additional fields in the Misc settings of preferences. Find the section:
/* private */ $wgDefaultUserOptionsEn = array(
and add:
'emailwatch' => 0, 'emailplainformat' => 1 after the last entry (probably 'date' => 0). Don't forget to add a comma after the current last entry. My entry looks like this: /* private */ $wgDefaultUserOptionsEn = array( 'quickbar' => 1, 'underline' => 1, 'hover' => 1, 'cols' => 80, 'rows' => 25, 'searchlimit' => 20, 'contextlines' => 5, 'contextchars' => 50, 'skin' => $wgDefaultSkin, 'math' => 1, 'rcdays' => 7, 'rclimit' => 50, 'highlightbroken' => 1, 'stubthreshold' => 0, 'previewontop' => 1, 'editsection'=>1,'editsectiononrightclick'=>0, 'showtoc'=>1, 'showtoolbar' =>1, 'date' => 0, 'emailwatch' =>0, 'emailplainformat' =>1 );
What this does is give the emailing script some defaults - namely DON'T email the watchlist and use plain (non-HTML) format.
[edit] Version 1.3
Now find this section:
/* private */ $wgUserTogglesEn = array(
and add the description for the Misc settings to the end (probably after 'noncache' => 'Disable page caching'). Don't forget to add the comma to the last entry.
'emailwatch' => 'Email daily Watchlist changes', 'emailplainformat' => 'Email using plain text (HTML is default)'
[edit] Version 1.4.4
- Find
$wgUserTogglesEnand add
#* 'emailwatch', #* 'emailplainformat'
- Find
$wgAllMessagesEnand add
#* 'tog-emailwatch' => 'Email daily Watchlist changes', #* 'tog-emailplainformat' => 'Email using plain text (HTML is default)',
[edit] Copy and modify /includes/SpecialPage.php
First, find SpecialPage.php in the /includes folder. Copy it to SpecialPage2.php. Note that you can actually modify SpecialPage.php instead of making the copy... I just like to keep my pages relatively 'pristine', except for the changes to Languages (harder since it's referenced a lot).
Just add a single element to the end of the $wgSpecialPages array (you should be an expert at this by now...)
"Watchlist2.php" => new SpecialPage("Watchlist2")
[edit] SpecialWatchlist2.php
Download: SpecialWatchlist2.tgz
Okay, this one is a bit too long to try to post in the body, so it's posted on a site of mine.
Below are explanations on how this works, but you should just download the file and look at it.
It's a brand new page anyway, so just put it into your /includes folder. I copied the SpecialWatchlist.php page and made the following modifications:
[edit] Security
I didn't want just anyone to be able to launch this page, so it will only work if the server itself runs it:
if ($wgUser->getName() != '127.0.0.1') { $wgOut->addHTML('Not authorized to send emails'); return; }
[edit] Daily watchlist
Since this is a daily digest, I added a line to force the query to only include pages modified in the last day (just below $id = $wgRequest etc...):
$days = 1;
[edit] Looping query of users
Now for the meat of it. Just before the $sql to get the COUNT(*) of the pages, I add a SQL loop query to circle through all users, plus get the user from the DB to see if they've selected to email a watchlist.
$userSQL = "SELECT DISTINCT wl_user FROM watchlist"; $userRES = wfQuery($userSQL, DB_READ); while($userS = wfFetchObject($userRES)) { $nUSER = $userS->wl_user; $htmlmessage = ""; $sqlGetUser = "SELECT user_name,user_email,user_options FROM user WHERE user_id=$nUSER"; $resGetUser = wfQuery($sqlGetUser, DB_READ); $sGetUser = wfFetchObject($resGetUser); $mailUser = User::newFromName($sGetUser->user_name); $mailUser->decodeOptions($sGetUser->user_options); if (1 == $mailUser->getOption('emailwatch')) { # now for the normal watchlist code, mostly... starting with $wgLoadBalancer # etc... The only thing is that I commented out all $wgOut->addHTML lines and # made the text being added a variable, like: # $toadd = wfMsg("watchlistcontains",$wgLang->formatNum etc..., and # $htmlmessage .= $toadd; to copy the complete text into a variable instead # of directly outputting to $wgOut.
[edit] Mail sender
Then add code to send the email after all the watchlist generation code, right below $wgLoadBalancer->force(0);
global $wgSitename, $wgServer, $wgScriptPath; $mailedURL = $wgServer.$wgScriptPath; $fixedhtmlmessage = 'etc...' # headers and junk - I fixed the message so that it's email compatible, change the # relative URLs to absolute ones, and for plain text emailers, I run it through the # strip_tags PHP function - not pretty, but it works well. Then I send it with PHP's # sendmail, all pretty straightforward.
[edit] MailWatchlist.php
Download: MailWatchlist.tgz
This is a new page that goes in your wiki root folder - same place as index.php. It's actually a copy of index.php with a lot of stuff removed. This is the page you browse to in order to run the script that sends the emails, i.e. http://yourwebsite.com/MailWatchlist.php. As noted above, however, it only works when the server itself (localhost) is the one browsing to it.
Basically all I did was remove the big if{} and switch case stuff that figured out where to browse to and just made it default to the 'SpecialPage - but with a change that forced it to my SpecialPage2.php copy.
Just copy the file into your main wiki folder along with index.php.
[edit] Automatic mailing
I wanted the MailWatchlist.php to execute automatically on the Mandrakelinux box, so I used cron - the Linux/UNIX scheduling service.
It took me a bit of trial and error to get it to work - I was trying to browse to it using FireFox at first. I finally tried Lynx, the terminal-based linux/unix http browser, and that was the trick.
Open a terminal window and type:
crontab -e
That will open the vi text editor with your user's schedule. Press I to go into insert mode, then type:
50 23 * * * lynx http://localhost/MailWatchlist.php
or wherever your wiki is installed, like:
50 23 * * * lynx http://localhost/wiki/Mailwatchlist.php
If you have multiple wikis installed, just create a line for each one. The numbers mean at 50 minutes, hour 23 (11pm), every day, every month, every year (iirc), run that command.
Now save your crontab file by pressing ESC to exit Insert mode, then press : and wq and press enter. That will put you in command mode and execute the write and quit commands. You should get a message like "installing new crontab".
You can test your setup by saving crontab with a line like:
* * * * * lynx http://localhost/MailWatchlist.php
and then just wait a minute to see if it runs.
[edit] sendmail issues
Once sendmail is installed with its default configuration, you'll need to tell it that it's okay to let Apache use it for mailing stuff.
On my Mandrakelinux distribution, I edited /etc/mail/trusted-users and added:
apache
mailman
majordomo
uucp
(opened a terminal window, vi or gedit as su (super user/root) make the changes and save)

