Difference between revisions of "Extension:EmailPage"

From Organic Design wiki
(may as well get this one back in the pipeline too)
 
m (Method)
 
(112 intermediate revisions by the same user not shown)
Line 1: Line 1:
<?php
+
{{svn|extensions|MediaWiki/EmailPage/}}<br />
# Extension:EmailArticle{{Category:Extensions}}{{#Security:*|dev}}{{#Security:view|*}}{{php}}
+
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
+
== Using an SMW query result as the recipient list ==
# - Author: [http://www.organicdesign.co.nz/nad User:Nad]{{Category:Extensions created with Template:SpecialPage}}
+
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.
+
<source>
if (!defined('MEDIAWIKI')) die('Not an entry point.');
+
{{#ask:[[User:+]][[Species::~*{!duck!}*]][[Cur::!N]]
+
|?Email
define('EMAILARTICLE_VERSION','0.0.0, 2007-05-25');
+
}}
+
</source>
$wgExtensionFunctions[] = 'wfSetupEmailArticle';
+
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.
+
 
$wgExtensionCredits['specialpage'][] = array(
+
=== Method ===
'name'        => 'Special:EmailArticle',
+
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.
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
+
 
'description' => 'Send rendered article to an email address or list of 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.
'url'        => 'http://www.organicdesign.co.nz/Extension:SpecialPage',
+
 
'version'     => EMAILARTICLE_VERSION
+
For example, the HTML part of this mechanism might be as follows:
);
+
<source lang="html">
+
<form id="send-email-form">
require_once "$IP/includes/SpecialPage.php";
+
 
+
<!-- This is the parameter that will be part of the SMW query -->
# Define a new class based on the SpecialPage class
+
<input id="send-email-param" />
class SpecialEmailArticle extends SpecialPage {
+
 
+
<!-- This will execute the JavaScript that populates the following two hidden inputs -->
# Constructor
+
<input type="button" id="send-email-button" value="Send" />
function SpecialSearchLog() {
+
 
SpecialPage::SpecialPage(
+
<!-- Special:EmailPage will use this as the page to send to the recipients -->
'EmailArticle',     # name as seen in links etc
+
<input type="hidden" id="send-email-page" name="ea-title" />
'sysop',       # user rights required
+
 
true,         # listed in special:specialpages
+
<!-- Special:EmailPage will use this as the recipient list -->
false,         # function called by execute() - defaults to wfSpecial{$name}
+
<input type="hidden" id="send-email-to" name="ea-to" />
false,        # file included by execute() - defaults to Special{$name}.php, only used if no function
+
</form>
false          # includable
+
</source>
);
+
 
 +
The corresponding JavaScript to match the above HTML and the example SMW query might be as follows.
 +
<source lang="js">
 +
// 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();
 
}
 
}
+
});
# Override SpecialPage::execute()
+
});
wfSpecialEmailArticle($param,&$specialpage) {
+
</source>
global $wgOut;
+
 
$title = Title::makeTitle(NS_SPECIAL,'EmailArticle');
+
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.
$wgOut->addWikiText(wfMsg('example-message','exampleParameter'));
+
<source lang="js">
$wgOut->addHTML(
+
$('{!body.page-Sandbox h1.firstHeading!}').after(
wfElement('form',array('action' => $title->getLocalURL('action=submit'),'method' => 'post'),null)
+
'<form id="send-email-form">'
. '<textarea name="target" cols=25 rows=10></textarea>'
+
+ '<input id="send-email-param" value="JavaScript" />'
. wfElement('input',array('type' => 'submit'))
+
+ '<input type="button" id="send-email-button" value="Send" />'
. '</form>'
+
+ '<input type="hidden" id="send-email-page" name="ea-title" />'
);
+
+ '<input type="hidden" id="send-email-to" name="ea-to" />'
}
+
+ '</form>'
+
);
}
+
</source>
+
[[Category:Extensions|EmailPage]]
# Called from $wgExtensionFunctions array when initialising extensions
 
function wfSetupEmailArticle() {
 
global $wgLanguageCode,$wgMessageCache;
 
 
# Add the messages used by the specialpage
 
if ($wgLanguageCode == 'en') {
 
$wgMessageCache->addMessages(array(
 
'emailarticle' => 'Example Specialpage',        # The friendly page title
 
'exampleMessage' => "Example message: <tt>$1</tt>",
 
));
 
}
 
 
# Add the specialpage to the environment
 
SpecialPage::addPage(new SpecialEmailArticle());
 
}
 
?>
 

Latest revision as of 20:10, 8 February 2018

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>'
);