Jump to content

Extension:Email Digest: Difference between revisions

From mediawiki.org
Content deleted Content added
Johnduhart (talk | contribs)
Decollapsing
Johnduhart (talk | contribs)
Replaced content with "{{Archived Extension|478284}}"
 
Line 1: Line 1:
{{Extension
{{Archived Extension|478284}}
|name = Email Digest
|status = unknown
|type = notify
|author = [[m:user:MHart|MHart]]
|version =
|update =
|mediawiki = 1.4?
|download = [http://blackbeltvb.com/SpecialWatchlist2.tgz SpecialWatchList2.tgz] & [http://blackbeltvb.com/MailWatchlist.tgz MailWatchlist.tgz]
|readme =
|changelog =
|description = Emails a user their watchlist
|parameters =
|rights =
|example =
}}
== 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.

=== Modify /languages/Language.php ===
MediaWiki very cleanly handles additional fields in the Misc settings of preferences. Find the section:
<source lang = "php">
/* private */ $wgDefaultUserOptionsEn = array(
</source>
and add:
<source lang= "php">
'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
);
</source>
What this does is give the emailing script some defaults - namely DON'T email the watchlist and use plain (non-HTML) format.

==== Version 1.3 ====
Now find this section:
<source lang="php">
/* private */ $wgUserTogglesEn = array(
</source>
and add the description for the Misc settings to the end (probably after <code>'noncache' => 'Disable page caching')</code>. Don't forget to add the comma to the last entry.
<source lang="php">
'emailwatch' => 'Email daily Watchlist changes',
'emailplainformat' => 'Email using plain text (HTML is default)'
</source>
==== Version 1.4.4 ====
# Find <code>$wgUserTogglesEn</code> and add
<source lang="php">
#* 'emailwatch',
#* 'emailplainformat'
</source>
# Find <code>$wgAllMessagesEn</code> and add
<source lang="php">
#* 'tog-emailwatch' => 'Email daily Watchlist changes',
#* 'tog-emailplainformat' => 'Email using plain text (HTML is default)',
</source>
==== Version 1.15 ====
Please take a look at [[Extension:MailDigest]]. It is based upon this code, yet supports the newer mediawiki database format and is installable as a regular Extension, without the need to replace original mw-files.

=== 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 <code>$wgSpecialPages</code> array (you should be an expert at this by now...)
<source lang="php">
"Watchlist2.php" => new SpecialPage("Watchlist2")
</source>

=== SpecialWatchlist2.php ===
'''Download:''' ''<span class="plainlinks">[http://blackbeltvb.com/SpecialWatchlist2.tgz SpecialWatchlist2.tgz]</span>''

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:

==== 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:
<source lang="php">
if ($wgUser->getName() != '127.0.0.1') {
$wgOut->addHTML('Not authorized to send emails');
return;
}
</source>
==== 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...):
<source lang="php">
$days = 1;
</source>
==== 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.
<source lang="php">
$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.
</source>
==== Mail sender ====
Then add code to send the email after all the watchlist generation code, right below $wgLoadBalancer->force(0);
<source lang="php">
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.
</source>

=== MailWatchlist.php ===
'''Download:''' ''<span class="plainlinks">[http://blackbeltvb.com/MailWatchlist.tgz MailWatchlist.tgz]</span>''

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. <code><nowiki>http://yourwebsite.com/MailWatchlist.php</nowiki></code>. 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 <code>if{}</code> and <code>switch case</code> 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.

=== 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:

<code>crontab -e</code>

That will open the '''vi''' text editor with your user's schedule. Press '''I''' to go into insert mode, then type:

<code>50 23 * * * lynx http://localhost/MailWatchlist.php</code>

or wherever your wiki is installed, like:

<code>50 23 * * * lynx http://localhost/wiki/Mailwatchlist.php</code>

or by using wget you also may use:

<code>50 23 * * * wget --delete-after http://localhost/wiki/Mailwatchlist.php</code>

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 '''w'''rite and '''q'''uit commands. You should get a message like "installing new crontab".

You can test your setup by saving crontab with a line like:

<code>* * * * * lynx http://localhost/MailWatchlist.php</code>

and then just wait a minute to see if it runs.

=== 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:

<code>apache</code><br />
<code>mailman</code><br />
<code>majordomo</code><br />
<code>uucp</code><br />

(opened a terminal window, vi or [[w:gedit|gedit]] as su (super user/root) make the changes and save)
= See also =
[[Extension:MailDigest]]

Latest revision as of 18:46, 2 January 2012