Difference between revisions of "Extension:EmailPage"
(expand templates in list) |
(subject and header form values) |
||
| Line 47: | Line 47: | ||
# Override SpecialPage::execute($param = '') | # Override SpecialPage::execute($param = '') | ||
function execute($param) { | function execute($param) { | ||
| − | global $wgOut,$wgUser,$wgEmailArticleContactsCat,$wgGroupPermissions; | + | global $wgOut,$wgUser,$wgEmailArticleContactsCat,$wgGroupPermissions,$wgSitename; |
# If the form has been posted, attempt to send the message | # If the form has been posted, attempt to send the message | ||
| Line 66: | Line 66: | ||
if ($t->getNamespace() == NS_CATEGORY) $cats .= "<option>{$t->getText()}</option>"; | if ($t->getNamespace() == NS_CATEGORY) $cats .= "<option>{$t->getText()}</option>"; | ||
} | } | ||
| − | if ($cats) $wgOut->addHTML("From category: <select name=\"sf_cat\">$cats</select><br>\n"); | + | if ($cats) $wgOut->addHTML("From category: <select name=\"sf_cat\">$cats</select><br />\n"); |
} | } | ||
| Line 75: | Line 75: | ||
# Addition of named list | # Addition of named list | ||
$wgOut->addWikiText(wfMsg('ea_selectlist')); | $wgOut->addWikiText(wfMsg('ea_selectlist')); | ||
| − | $wgOut->addHTML( | + | $wgOut->addHTML("<br /><textarea name=\"sf_list\" /><br />\n"); |
| + | |||
| + | # Subject | ||
| + | $wgOut->addWikiText(wfMsg('ea_subject')); | ||
| + | $subject = "\"$param\" article sent from $wgSitename"; | ||
| + | $wgOut->addHTML('<br />'.wfElement('input',array('type' => 'text', 'name' => 'sf_subject', 'value' => $subject))."<br />\n"); | ||
| + | |||
| + | # Header | ||
| + | $wgOut->addWikiText(wfMsg('ea_header')); | ||
| + | $wgOut->addHTML("<br /><textarea name=\"sf_header\" /><br />\n"); | ||
# Submit buttons | # Submit buttons | ||
| Line 191: | Line 200: | ||
. "''commas, semicolons, asterisks and newlines all act as separators''", | . "''commas, semicolons, asterisks and newlines all act as separators''", | ||
'ea_show' => "Show list", | 'ea_show' => "Show list", | ||
| − | 'ea_send' => "Send!" | + | 'ea_send' => "Send!", |
| + | 'ea_subject' => "Enter a subject line for the email", | ||
| + | 'ea_header' => "Optionally you can add an introductory wikitext message to your email" | ||
)); | )); | ||
} | } | ||
Revision as of 09:28, 6 June 2007
<?php
- Extension:EmailArticle
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:NadCategory:Extensions created with Template:SpecialPage
- - See http://www.mediawiki.org/wiki/Extension:EmailArticle for installation and usage details
- - Started: 2007-05-25 (based on http://www.organicdesign.co.nz/email-article.php from XmlWiki)
$wgShowExceptionDetails = true;
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('EMAILARTICLE_VERSION','0.0.1, 2007-06-05');
$wgEmailArticleGroup = 'postmaster'; # Users must belong to this group to send emails (empty string means anyone can send) $wgEmailArticleContactsCat = 'Contacts'; # This specifies the name of a category containing categories of contact articles $wgEmailArticleCss = 'EmailArticle.css'; # A minimal CSS article to embed in the email (eg. monobook/main.css without portlets, actions etc) $wgEmailArticleAllowRemoteAddr = array($_SERVER['SERVER_ADDR'],'127.0.0.1'); # Allow anonymous sending from these addresses $wgPhpMailerClass = dirname(__FILE__).'/phpmailer/class.phpmailer.php'; # From http://phpmailer.sourceforge.net/
if ($wgEmailArticleGroup) $wgGroupPermissions['sysop'][$wgEmailArticleGroup] = true;
$wgExtensionFunctions[] = 'wfSetupEmailArticle';
$wgExtensionCredits['specialpage'][] = array( 'name' => 'Special:EmailArticle', 'author' => 'User:Nad', 'description' => 'Send rendered article to an email address or list of addresses', 'url' => 'http://www.mediawiki.org/wiki/Extension:EmailArticle', 'version' => EMAILARTICLE_VERSION );
- If form has been posted, include the phpmailer class
if (isset($_REQUEST['sf_cat'])) require_once($wgPhpMailerClass);
- Define a new class based on the SpecialPage class
require_once("$IP/includes/SpecialPage.php"); class SpecialEmailArticle extends SpecialPage {
var $recipients = array();
# Constructor function SpecialEmailArticle() { global $wgEmailArticleGroup; SpecialPage::SpecialPage('EmailArticle',$wgEmailArticleGroup); }
# Override SpecialPage::execute($param = ) function execute($param) { global $wgOut,$wgUser,$wgEmailArticleContactsCat,$wgGroupPermissions,$wgSitename;
# If the form has been posted, attempt to send the message if (isset($_REQUEST['sf_cat'])) $this->send($param); elseif ($param == ) return $wgOut->addWikiText(wfMsg('ea_noarticle'));
$wgOut->addWikiText(wfMsg('ea_selectrecipients')); $wgOut->addHTML(wfElement('form',array('action' => $title->getLocalURL('action=submit'),'method' => 'POST'),null));
# If $wgEmailArticleContactsCat is set, create a select list of all categories
if ($wgEmailArticleContactsCat) {
$db =& wfGetDB(DB_SLAVE);
$cl = $db->tableName('categorylinks');
$cats = '<option/>';
$result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$wgEmailArticleContactsCat' ORDER BY cl_sortkey");
while ($row = mysql_fetch_row($result)) {
$t = Title::newFromID($row[0]);
if ($t->getNamespace() == NS_CATEGORY) $cats .= "<option>{$t->getText()}</option>";
}
if ($cats) $wgOut->addHTML("From category: <select name=\"sf_cat\">$cats</select>
\n");
}
# Allow selection of a group
$groups = '<option/><option>'.join('</option><option>',array_keys($wgGroupPermissions)).'</option>';
$wgOut->addHTML("From group: <select name=\"sf_group\">$groups</select>
\n");
# Addition of named list
$wgOut->addWikiText(wfMsg('ea_selectlist'));
$wgOut->addHTML("
<textarea name=\"sf_list\" />
\n");
# Subject
$wgOut->addWikiText(wfMsg('ea_subject'));
$subject = "\"$param\" article sent from $wgSitename";
$wgOut->addHTML('
'.wfElement('input',array('type' => 'text', 'name' => 'sf_subject', 'value' => $subject))."
\n");
# Header
$wgOut->addWikiText(wfMsg('ea_header'));
$wgOut->addHTML("
<textarea name=\"sf_header\" />
\n");
# Submit buttons $wgOut->addHTML(wfElement('input',array('type' => 'submit','name' => 'show','value' => wfMsg('ea_show')))); $wgOut->addHTML(wfElement('input',array('type' => 'submit','name' => 'send','value' => wfMsg('ea_send'))));
$wgOut->addHTML('</form>'); }
# Send the message to the recipients function send($title) { global $wgOut,$wgUser,$wgParser,$wgServer,$wgScript,$wgEmailArticleCss,$wgEmailArticleGroup,$wgEmailArticleAllowRemoteAddr;
# Set error and bail if user not in postmaster group, and request not from trusted address if (!in_array($wgEmailArticleGroup,$wgUser->getGroups()) && !in_array($_SERVER['REMOTE_ADDR'],$wgEmailArticleAllowRemoteAddr)) $wgOut->addWikiText(wfMsg('ea_autherror')); return false; }
$db = &wfGetDB(DB_SLAVE); $title = Title::newFromText($title); $parser = new Parser; $opt = new ParserOptions; $subject = $_REQUEST['sf_subject']; $header = $_REQUEST['sf_header']; $cat = $_REQUEST['sf_cat']; $group = $_REQUEST['sf_group']; $list = $_REQUEST['sf_list']; $textonly = $_REQUEST['sf_textonly'];
# Get contact article titles from selected cat if ($cat) { $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT cl_from FROM $cl WHERE cl_to = '$cat' ORDER BY cl_sortkey"); while ($row = mysql_fetch_row($result)) $this->addRecipient(Title::newFromID($row[0])); }
# Get email addresses from users in selected group if ($group) { $u = $db->tableName('user'); $ug = $db->tableName('user_groups'); $result = $db->query("SELECT $u.user_email FROM $u,$ug WHERE $ug.ug_user = $user.user_id AND $ug.ug_group = '$group'"); while ($row = mysql_fetch_row($result)) $this->addRecipient($row[0]); }
# Recipients from list (expand templates in wikitext) $list = $parser->preprocess($list,$title,$opt); foreach (preg_split("/[\\x00-\\x1f,;*]+/",$list) as $item) $this->addRecipient($item);
# Compose the wikitext content of the article to send $article = new Article($title); $message = $article->getContent(); if ($header) $message = "$header\n\n$message";
# Convert the message text to html unless textonly if ($textonly == ) {
# Parse the wikitext $message = $parser->parse($message,$title,$opt,true,true)->getText();
# Convert any relative URL's to absolute $message = preg_replace("|\\W$wgScript|i",$wgServer.$wgScript,$message);
# Create a simple html wrapper with embedded CSS for the message content $article = new Article(Title::newFromText($wgEmailArticleCss)); $css = $article->getContent(); $head = "<head><style type=\"text/css\">$css</style></head>"; $message = "$head
";
}
# Set up new mailer instance $mail = new PHPMailer(); $mail->From = $wgUser->isValidEmailAddr() ? $wgUser->getEmail() : "wiki@$wgServer"; $mail->FromName = $wgUser->getName(); $mail->Subject = $subject; $mail->Body = $message; $mail->IsHTML(!$textonly); foreach ($this->recipients as $recipient) $mail->AddAddress($recipient);
# Send message and redirect client back to article or raise error if ($state = $mail->Send()) $msg = wfMsg('ea_sent',$title,count($this->recipients),$mail->FromName); else $msg = wfMsg('ea_error',$title,$mail->ErrorInfo); $wgOut->addWikiText($msg); return $state; }
# Add a recipient the list # - accepts title objects for article containing email address, or string of actual address function addRecipient($recipient) { if (is_object($recipient) && $recipient->exists()) { $article = new Article($recipient); if (preg_match('/[a-z0-9_.-]+@[a-z0-9_.-]+/i',$article->getContent(),$emails)) $recipient = $emails[0]; else $recipient = ; } if ($valid = User::isValidEmailAddr($recipient)) $this->recipients[] = $recipient; return $valid; }
}
- 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' => "EmailArticle",
'ea_noarticle' => "Please specify an article to send, for example Special:EmailArticle/Main Page.",
'ea_error' => "Error sending $1: $1",
'ea_sent' => "Article $1 sent successfully to $2 recipients by $3.",
'ea_selectrecipients' => "Select the recipients"
'ea_selectlist' => "Additional recipients as article titles or email addresses
"
. "commas, semicolons, asterisks and newlines all act as separators",
'ea_show' => "Show list",
'ea_send' => "Send!",
'ea_subject' => "Enter a subject line for the email",
'ea_header' => "Optionally you can add an introductory wikitext message to your email"
));
}
# Add the specialpage to the environment SpecialPage::addPage(new SpecialEmailArticle()); } ?>



