Difference between revisions of "Extension:EmailPage"

From Organic Design wiki
(only list subcats)
(more structure)
Line 41: Line 41:
 
global $wgOut,$wgUser,$wgEmailArticleContactsCat;
 
global $wgOut,$wgUser,$wgEmailArticleContactsCat;
 
$title = Title::makeTitle(NS_SPECIAL,'EmailArticle');
 
$title = Title::makeTitle(NS_SPECIAL,'EmailArticle');
if ($param == '') return $wgOut->addWikiText(wfMsg('ea_noarticle'));
+
 
 +
# 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->addWikiText(wfMsg('ea_selectrecipients'));
Line 64: Line 67:
 
. '</form>'
 
. '</form>'
 
);
 
);
 +
}
 +
 +
# Send the message to the recipients
 +
function send($title) {
 +
global $wgOut,$wgUser,$wgServer;
 +
 +
# get the info from the posted form
 +
$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 the wikitext content of the article to send
 +
$title = Title::newFromText($title);
 +
$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
 +
$parser = new Parser;
 +
$opt = new ParserOptions;
 +
$message = $parser->parse($message,$title,$opt,true,true)->getText();
 +
 +
# Add CSS and add margin:8px to body tag
 +
xwReplaceTokens($article, true);
 +
 +
# Absoluterise relative urls Absolute url of wiki
 +
# str_replace('"/wiki/', '"http://'.$_SERVER['HTTP_HOST'].'/wiki/', $message);
 +
}
 +
 +
# Set up new mailer instance
 +
$mail = new PHPMailer();
 +
$mail->From = $wgUser->isValidEmailAddr() ? $wgUser->getEmail() : "wiki@$wgServer";
 +
$mail->FromName = $GLOBALS['xwUserName'];
 +
$mail->IsHTML(!$textonly);
 +
foreach ( $GLOBALS['sa-email'] as $i ) $mail->AddAddress( trim($i) );
 +
$mail->Subject = $title;
 +
$mail->Body = $article;
 +
 +
# Send message and redirect client back to article or raise error
 +
$title = preg_replace('/^(?=Category:)/i',':',$title);
 +
if ($mail->Send()) {
 +
global $xwUserName;
 +
$msg = count($GLOBALS['sa-email']).' recipient(s)';
 +
xwLog("[[$title]] sent to $msg by [[$xwUserName]]");
 +
} else xwLog($msg = "Error sending [[$title]] for [[$xwUserName]]: ".$mail->ErrorInfo);
 
}
 
}
  
Line 79: Line 134:
 
'ea_error'            => "Error: '''$1'''",
 
'ea_error'            => "Error: '''$1'''",
 
'ea_ok'              => "Article [[$1]] sent successfully to '''$2''' recipient$3",
 
'ea_ok'              => "Article [[$1]] sent successfully to '''$2''' recipient$3",
 +
'ea_selectrecipients' => "Select the recipients"
 
'ea_selectrecipients' => "Select the recipients"
 
'ea_selectrecipients' => "Select the recipients"
 
));
 
));

Revision as of 07:24, 30 May 2007

<?php

  1. Extension:EmailArticle
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:NadCategory:Extensions created with Template:SpecialPage
  3. - See http://www.mediawiki.org/wiki/Extension:EmailArticle for installation and usage details

$wgShowExceptionDetails = true;

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('EMAILARTICLE_VERSION','0.0.0, 2007-05-25');

$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

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

require_once "$IP/includes/SpecialPage.php";

  1. Define a new class based on the SpecialPage class

class SpecialEmailArticle extends SpecialPage {

# Constructor function SpecialEmailArticle() { global $wgEmailArticleGroup; SpecialPage::SpecialPage('EmailArticle',$wgEmailArticleGroup); }

# Override SpecialPage::execute($param = ) function execute($param) { global $wgOut,$wgUser,$wgEmailArticleContactsCat; $title = Title::makeTitle(NS_SPECIAL,'EmailArticle');

# 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 = ; $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>"); }

$wgOut->addHTML( '<textarea name="sf_list" cols=25 rows=10></textarea>' . wfElement('input',array('type' => 'submit')) . '</form>' ); }

# Send the message to the recipients function send($title) { global $wgOut,$wgUser,$wgServer;

# get the info from the posted form $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 the wikitext content of the article to send $title = Title::newFromText($title); $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 $parser = new Parser; $opt = new ParserOptions; $message = $parser->parse($message,$title,$opt,true,true)->getText();

# Add CSS and add margin:8px to body tag xwReplaceTokens($article, true);

# Absoluterise relative urls Absolute url of wiki # str_replace('"/wiki/', '"http://'.$_SERVER['HTTP_HOST'].'/wiki/', $message); }

# Set up new mailer instance $mail = new PHPMailer(); $mail->From = $wgUser->isValidEmailAddr() ? $wgUser->getEmail() : "wiki@$wgServer"; $mail->FromName = $GLOBALS['xwUserName']; $mail->IsHTML(!$textonly); foreach ( $GLOBALS['sa-email'] as $i ) $mail->AddAddress( trim($i) ); $mail->Subject = $title; $mail->Body = $article;

# Send message and redirect client back to article or raise error $title = preg_replace('/^(?=Category:)/i',':',$title); if ($mail->Send()) { global $xwUserName; $msg = count($GLOBALS['sa-email']).' recipient(s)'; xwLog("$title sent to $msg by $xwUserName"); } else xwLog($msg = "Error sending $title for $xwUserName: ".$mail->ErrorInfo); }

}

  1. 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: $1", 'ea_ok' => "Article $1 sent successfully to $2 recipient$3", 'ea_selectrecipients' => "Select the recipients" 'ea_selectrecipients' => "Select the recipients" )); }

# Add the specialpage to the environment SpecialPage::addPage(new SpecialEmailArticle()); } ?>