Extension:EmailPage

From Organic Design wiki
Info.svg This code is in our Git repository here.

Note: If there is no information in this page about this code and it's a MediaWiki extension, there may be something at mediawiki.org.


Using an SMW query result as the recipient list

The idea here is that we'd like to have a form with an input box that allows us to enter a value to be used for part of a query, and the result of the query will be a list of email addresses. For example we may have a query structure that results in the following query when the term "duck" is entered into the form.

{{#ask:[[User:+]][[Species::~*duck*]][[Cur::!N]]
|?Email
}}

When the form is submitted, this query is executed and we're taken to the EmailPage special page with the recipient list automatically populated with the results from the query.

Method

All of the fields in the EmailPage special page can be populated from the query-string or POST data. The ea-title variable is set to the page title that will be sent to all the recipients, and the ea-to variable can be set to the list of recipient's email addresses.

We can have some JavaScript which is executed on the clicking of the form's button that builds the query and then sends it to the wiki's API to be executed, and then the results entered into a hidden ea-to value in the form and then the form is submitted to the EmailPage special page.

For example, the HTML part of this mechanism might be as follows:

<form id="send-email-form">

	<!-- This is the parameter that will be part of the SMW query -->
	<input id="send-email-param" />

	<!-- This will execute the JavaScript that populates the following two hidden inputs -->
	<input type="button" id="send-email-button" value="Send" />

	<!-- Special:EmailPage will use this as the page to send to the recipients -->
	<input type="hidden" id="send-email-page" name="ea-title" />

	<!-- Special:EmailPage will use this as the recipient list -->
	<input type="hidden" id="send-email-to" name="ea-to" />
</form>

The corresponding JavaScript to match the above HTML and the example SMW query might be as follows.

// When our button is clicked, do the following...
$('#send-email-button').click(function() {

	// Get the parameter from the form input and force to lowercase
	var param = $('#send-email-param').val().toLowerCase();

	// Construct our SMW query with the parameter in it
	var query = '{{#ask:[[User:+]][[Species::~*' + param + '*]][[Cur::!N]]|?Email}}';

	// Send the query to the MediaWiki API to be parsed
	$.ajax({
		type: 'post',
		url: mw.util.wikiScript('api'),
		dataType: 'json',
		data: {
			action: 'parse',
			text: query,
			contentmodel: 'wikitext',
			disablelimitreport: true,
			disabletidy: true,
			format: 'json'
		},

		// When the API response arrives...
		success: function(json) {

			// Extract the raw email addresses from the parsed result
			var result = json.parse.text['*'].match(/(\S+@[^,]+)/g).join(';');

			// Tell EmailPage to use the query result as the recipient list
			$('#send-email-to').val(result);

			// Tell EmailPage to use the current page to send
			$('#send-email-page').val(mw.config.get('wgPageName'));

			// Set the forms submission URL to Special:EmailPage
			$('#send-email-form').attr('action', mw.util.wikiScript() + '?title=Special:EmailPage');

			// Best POST it since the email list might be long
			$('#send-email-form').attr('method', 'post');

			// Submit the form to Special:EmailPage
			$('#send-email-form').submit();
		}
	});
});

Note that if you don't want to have raw HTML enabled in your wiki, you can use JavaScript to add the form. The code should go before the click handler code since the click handler refers to the form which needs to already exist. You could add the form to some part of the skin on every page, or you could add it to the content of a specific page. In the following example I'm using a jQuery CSS-selector (highlighted) to add the form to the "Sandbox" article just after the page heading.

$('body.page-Sandbox h1.firstHeading').after(
	'<form id="send-email-form">'
	+ '<input id="send-email-param" value="JavaScript" />'
	+ '<input type="button" id="send-email-button" value="Send" />'
	+ '<input type="hidden" id="send-email-page" name="ea-title" />'
	+ '<input type="hidden" id="send-email-to" name="ea-to" />'
	+ '</form>'
);