Extension:WatchEmailOptional

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
WatchEmailOptional

Release status:Extension status beta

ImplementationTemplate:Extension#type User interface, Notify
DescriptionTemplate:Extension#description Enable users to configure watched-article email notification on a per-article basis.
Author(s)Template:Extension#username Jlernertalk
Latest versionTemplate:Extension#version 0.1
MediaWikiTemplate:Extension#mediawiki 1.13
LicenseTemplate:Extension#license GPL
Download see below
ExampleTemplate:Extension#example http://wiki.name.com/
Hooks usedTemplate:Extension#hook
WatchArticleCompleteManual:Hooks/WatchArticleComplete

Translate the WatchEmailOptional extension if it is available at translatewiki.net

Check usage and version matrix.

WatchEmailOptional enables users to set email notifications on watched articles on a per-article basis.

Questions welcome, but please be careful hacking up your local copy of MediaWiki and altering your database. Always make backups first before attempting such changes. You have been warned.

The extension is geared to give a solution for Bugzilla 2555 Secondary watchlist adds checkboxes for watched pages: watching with or without E-mail notification (enotif). If there is sufficient interest, I'll add features to this extension and/or try to get it added to MediaWiki itself.

Usage[edit]

Watch some articles, then visit your [[Special:Watchlist/edit]] page.

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/ExtensionName/ExtensionName.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit]

Be careful hacking up your local copy of MediaWiki and altering your database. Always make backups first before attempting such changes.

To install this extension:

  • Patch the files listed below (UserMailer.php and WatchlistEditor):
require_once("$IP/extensions/WatchEmailOptional/WatchEmailOptional.php");

WatchEmailOptional.php[edit]

<?php
if ( ! defined( 'MEDIAWIKI' ) )
    die();

$wgHooks['WatchArticleComplete'][] = 'setEnotifWatchlist';

function setEnotifWatchlist( &$user, &$article ) {
	global $wgUser;

	if( !$wgUser->getBoolOption( 'enotifwatchlistdefault' ) ) {
		$dbw =& wfGetDB( DB_MASTER );
		$title = $article->getTitle();
		$res = $dbw->update( 'watchlist', 
			array( 'wl_enotif' => 0 ),
			array( 'wl_user' => $wgUser->getId(), 'wl_title' => $title->getPartialURL(), 'wl_namespace' => $title->getNamespace() ),
                        __METHOD__ );
	}

	return true;
}

function getEnotif( $title ) {
	global $wgUser;
	$dbr = wfGetDB( DB_SLAVE );
	$res = $dbr->select( 'watchlist', 1, array( 'wl_user' => $wgUser->getID(), 'wl_namespace' => $title->getNamespace(),
		'wl_title' => $title->getPartialURL(), 'wl_enotif' => 1 ), __METHOD__ );
	return ($dbr->numRows( $res ) > 0) ? true : false;
}

function setEnotif( $list ) {
	global $wgUser;
	$dbw = wfGetDB( DB_MASTER );

	if( !is_array( $list ) ) {
		$list = explode( "\n", trim( $list ) );
		if( !is_array( $list ) )
			return;
	}

	foreach( $list as $key => $value ) {
		$value = trim( $value );
		if( !strlen( $value ) )
			continue;

		$text = trim( $key );
		if( strlen( $key ) ) {
			$title = Title::newFromText( $key );
			if( $title instanceof Title && $title->isWatchable() ) {
				if( $value == 'enotif' ) {
					$dbw->update( 'watchlist', array( 'wl_enotif' => 1 ),
						array( 'wl_user' => $wgUser->getID(), 'wl_namespace' => $title->getNamespace(),
						'wl_title' => $title->getPartialURL() ), __METHOD__ );
				}
				elseif( $value == 'noenotif' ) {
					$dbw->update( 'watchlist', array( 'wl_enotif' => 0 ),
						array( 'wl_user' => $wgUser->getID(), 'wl_namespace' => $title->getNamespace(),
						'wl_title' => $title->getPartialURL() ), __METHOD__ );
				}
			}
		}
	}
	return true;
}

Change to UserMailer.php[edit]

Add after line 376:

'wl_enotif' => 1,

Change to WatchlistEditor.php[edit]

Add after line 69:

setEnotif( $request->getValues() );

Add after line 394 :

. wfRadio( $title, getEnotif( $title ) ? '' : 'enotif', getEnotif( $title ) ) .' (email) '
. wfRadio( $title, getEnotif( $title ) ? 'noenotif' : '', !getEnotif( $title ) ) .' (no email) '

watchlist.sql[edit]

/* use wiki prefix for your setup, if any */
ALTER TABLE watchlist
	ADD COLUMN wl_enotif bool NOT NULL default 1;