Extension:Mail2Facebook

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Mail2Facebook

Release status: beta

Mail2facebook Extension.jpg
Implementation Notify
Description Email info about new articles to a Facebook Page's "Mobile Email" address
Author(s) Thiemo Schuff (StarwhooperTalk)
Last version 0.8 beta build 20111227
MediaWiki 1.17, 1.18
License cc-by-sa
Download No link
Example THWiki
Facebook Page
Parameters

$wgMail2Facebook['mailto']
$wgMail2Facebook['url']
$wgMail2Facebook['addpicture']
$wgMail2Facebook['displayauthor']
$wgMail2Facebook['kindofauthorname']
$wgMail2Facebook['defaultpic']
$wgMail2Facebook['devmail']
$wgMail2Facebook['displayauthor']
$wgMail2Facebook['imgquality']
$wgMail2Facebook['lessminold']
$wgMail2Facebook['blacklist']
$wgMail2Facebook['thumbsizes']
$wgMail2Facebook['mailfrom']
$wgMail2Facebook['picturesize']
$wgMail2Facebook['events']['onlynew']

Hooks used
ArticleSaveComplete

Check usage (experimental)

Contents

[edit] What can this extension do?

When you save an article, the extension sends an email to the specified Facebook Page Mobile Email address.

Your Facebook Page's Mobile Email address is accessible to the Page's Admins on the Welcome / Get Started tab.

[edit] History

[edit] 0.2 beta build 20110614

  • Post additional Username
  • Add picture to post
  • Show Username in post

[edit] 0.3 beta build 20110623

  • Hide the IP, if the author is not logged into MediaWiki
  • Pictures from Articles with blanks may upload, too
  • Better link in Special:Version
  • Switch for configure with/without username
  • Switch between real name or username
  • Post only Articles, not other type of MediaWiki pages
  • Better text for image license

[edit] 0.4 beta build 20110729

  • Support SVG images (it will sent as png to Facebook)
  • Reports only Namespace 0 Articles (0 = Standard)
  • Send a random picture from the article

[edit] 0.5 beta build 20110731

  • New Parameter $wgMail2Facebook['devmail']
  • Send a copy of the email to Facebook with detailed information to an additional email address using $wgMail2Facebook['devmail']
  • Signs like äüö will be displayed correct
  • Only try to send follow filetypes: .png, .jpg, .jpeg, .gif and .svg
  • With $wgMail2Facebook['lessminold'] you may set how many minutes since the last edit of the article must pass before sending a new email to Facebook
  • You may set with $wgMail2Facebook['reduce'] = array('x' => 97, 'y' => 68); if you want send a smaller one image
  • All image will sent as JPEG to Facebook
  • You may change the JPEG Quality with $wgMail2Facebook['imgquality'];

[edit] 0.6 beta build 20110830

  • Calculate with true color
  • $wgMail2Facebook['reduce'] is replace by $wgMail2Facebook['picturesize']. $wgMail2Facebook['picturesize'] is the pixel width and height auf the sent image
  • Images will not strech or compress the image dimensions.
  • The mail didn't contain image information, is non image in the article
  • The size of 120 pixel is recommended for the FB Wall. The Mail2facebook extension ignore the local cache thumbs in 100px size, to use bigger one
  • Blacklist: $wgMail2Facebook['blacklist'] = array('examble.svg','test.gif');

[edit] 0.7 beta build 20111016

  • With $wgMail2Facebook['thumbsizes'] = array(120,150,180,200); the this extension to find the smallest thumb to save time for recalculation the FB Thumb. If you still use Manual:$wgThumbLimits, extension will take this sizes.
  • Use wfGetDB on 1 of 3 db selects
  • seperate code in few small files
  • $wgMail2Facebook['mailfrom'] set the Mailsender. As Example 'THWiki Facebookmail<'.$wgPasswordSender.'>'
  • remove old code that should sent gif or png files.
  • Posttext configurable over Special:AllMessages

[edit] 0.8 beta build 20111227

[edit] 0.9 beta build ...

[edit] Future versions

  • Priority B
    • Check it with other image structures
    • Fix the problem that Facebook put more post in one
  • Priority C
    • Form to choose which image to post
    • Send Mail, to inform about new Authors

[edit] Wish list for further releases

  • ...
  • ...
  • ...

[edit] Usage

[edit] Download instructions

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

[edit] Installation

To install this extension, add the following to LocalSettings.php:

#Mail 2 Facebook
require_once("{$IP}/extensions/mail2facebook/mail2facebook.php");
$wgMail2Facebook['addpicture'] = true; //with picture ?
#//$wgMail2Facebook['defaultpic'] = $wgLogo; //use this picture, if the article without one (not recommend)
$wgMail2Facebook['devmail'] = 'xxx@yyy.com'; //if set, receive a copy with few imageinformation
$wgMail2Facebook['displayauthor'] = true; //display authorname in post
$wgMail2Facebook['imgquality']  = 70; //quality of send image (0 = bad ; 100 = best)
$wgMail2Facebook['kindofauthorname'] = 'nick'; //display realname or nickname
$wgMail2Facebook['lessminold'] = 5; //the time in minutes, that the last edit must be in the past, to send a new mail
$wgMail2Facebook['mailto'] = 'xxx@m.facebook.com';
$wgMail2Facebook['url'] = 'http://thwiki.org/index.php?title=';
$wgMail2Facebook['blacklist'] = array('example.svg','test.gif'); //Ignore the Images [[:File:example.svg]] and [[:File:test.gif]]
$wgMail2Facebook['thumbsizes'] = array(120,150,180,200); //help this extension to find the smallest thumb to save time for recalculation the FB Thumb. If you still use Manual:$wgThumbLimits, extension will take this sizes.
$wgMail2Facebook['mailfrom'] = 'Facebookmail<'.$wgPasswordSender.'>'; //Wrote "Facebookmail" as Sender of the Sender of the Mail
$wgMail2Facebook['picturesize'] = 120;
$wgMail2Facebook['addpicture'] = true; //with picture ?
$wgMail2Facebook['events']['onlynew'] = false; // if true, send only by new articles

[edit] old ones, that will not use in furter times

  • $wgMail2Facebook['reduce']

[edit] Configuration parameters

see comments above

[edit] User rights

[edit] Code

[edit] mail2facebook.class.php

<?php
//Website http://www.mediawiki.org/wiki/Extension:Mail2Facebook
//cc-by-sa-de 3.0 by Thiemo Schuff
include('getimage.func.php');
 
class m2f{
        static function mail2facebook(&$article, &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId, &$redirect ){
                global $wgMail2Facebook;
                global $wgPasswordSender;
                global $wgLanguageCode;
                global $mailtext;
                $boundary = strtoupper(md5(uniqid(time())));
 
//ERMITTELN
        //ermitteln der ArtikelID
                $pageid = $article->getID();
                $mailtext['pageid'] = $pageid;
 
        //ermitteln der zeit der letzten und aktuellen änderung;
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select('revision', array( 'rev_timestamp' ), array('rev_page = '.$pageid), $fname = 'Database::select', $options = array( 'ORDER BY' => 'rev_id DESC' , 'LIMIT' => '2' ));
                foreach($res as $row) $zeit[] = $row->rev_timestamp;
                $edittimenow = mktime(substr($zeit[0],8,2),substr($zeit[0],10,2),substr($zeit[0],12),substr($zeit[0],4,2),substr($zeit[0],6,2),substr($zeit[0],0,4));
                $edittimelast = mktime(substr($zeit[1],8,2),substr($zeit[1],10,2),substr($zeit[1],12),substr($zeit[1],4,2),substr($zeit[1],6,2),substr($zeit[1],0,4));
                $edittimediv = $edittimenow - $edittimelast;
                $mailtext['time_since_last_edit'] = $edittimediv;
                if(isset($wgMail2Facebook['lessminold'])) if ($edittimediv < ($wgMail2Facebook['lessminold'] * 60)) return true;
 
        //ermitteln der Version des articels
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select('revision', array( 'rev_id' ), array('rev_page = '.$pageid), $fname = 'Database::select', $options = array());
                $version = 0;
                foreach($res as $row) $version++;
                $mailtext['version'] = $version;
                if($wgMail2Facebook['events']['onlynew'] == true and $version > 1) return true;
 
        //ermitteln des Artikeltitel und namespace
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select('page', array( 'page_title', 'page_namespace' ), 'page_id = '.$pageid, $fname = 'Database::select', $options = array());
                foreach($res as $row){
                        $title = $row->page_title;
                        $namespace = $row->page_namespace;
                        break;
                }
                $mailtext['article'] = $title;
                $mailtext['namespace'] = $namespace;
                if ($namespace != 0) return true;
 
        //ermitteln des Editors
                if ($wgMail2Facebook['displayauthor'] == true){
                        if($wgMail2Facebook['kindofauthorname'] == 'real') $author = $user->getRealName();
                        if($wgMail2Facebook['kindofauthorname'] == 'nick') $author = $user->getName();
                }
                $mailtext['authorname'] = $author;
 
        //ermitteln der Vollständigen Artikel URL                              
                $url = $wgMail2Facebook['url'].str_replace(' ', '_', $title);   
                $mailtext['articleurl'] = $url;
 
        //ermitteln der Bildinformationen
                if ($wgMail2Facebook['addpicture'] == true) $datei = getimage($pageid);
 
//generieren des Post Textes 
                if (in_array(substr($author,0,1),array(1,2,3,4,5,6,7,8,9))) unset($author); //Absender ist eine IP
                if (isset($author)) $posttext = wfMsg('mail2facebook_posttext_with_author',$author,str_replace('_',' ',$title),$url);                   
                else $posttext = wfMsg('mail2facebook_posttext_without_author',str_replace('_',' ',$title),$url);                       
 
        //generieren des mailheader
                $mail_header  = 'From:'.$wgMail2Facebook['mailfrom']."\n";
                $mail_header .= "MIME-Version: 1.0\n";
                $mail_header .= 'Content-Type: multipart/mixed; boundary='.$boundary."\n";
                $mail_header .= '--'.$boundary."\n";
                $mail_header .= "Content-Type: text/plain\n";
                $mail_header .= "Content-Transfer-Encoding: 8bit\n";
                $mail_header .= "\n";
                if (isset($wgMail2Facebook['devmail'])){
                        foreach($mailtext as $key => $text){
                                $mail_header .= $key.': '.$text."\n";
                        }
                }
                if(isset($datei['pfad'])){
                        $posttext .= wfMsg('mail2facebook_posttext_license');                   
                        $mail_header .= "\n".'--'.$boundary."\n";
                        $mail_header .= 'Content-Type: image/jpeg; name="'.$datei['name'].'"'."\n";
                        $mail_header .= 'Content-Transfer-Encoding: base64'."\n";
                        $mail_header .= 'Content-Disposition: attachment; filename="'.$datei['name'].'"'."\n";
                        $mail_header .= "\n".$datei['content']."\n";
                        $mail_header .= '--'.$boundary.'--';
                }
 
//VERSENDEN     der Mail
                mail($wgMail2Facebook['mailto'],$posttext,NULL,$mail_header);
                if (isset($wgMail2Facebook['devmail'])) mail($wgMail2Facebook['devmail'],$posttext,NULL,$mail_header);
                return true;
        }
}

[edit] getimage.func.php

<?php
function getimage($pageid){
        global $mailtext;
        global $wgMail2Facebook;
        global $wgThumbLimits;
 
        $whitelist[] = 'il_from = '.$pageid;
        foreach($wgMail2Facebook['blacklist'] as $blacklist) $whitelist[] = 'il_to != "'.$blacklist.'"';
 
        $filetyps = array('png','jpg','jpeg','gif','svg');
 
        foreach($filetyps as $filetyp){
                $whitelist2 = $whitelist;
                $whitelist2[] = 'il_to LIKE "%.'.$filetyp.'"';
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select('imagelinks', array('il_to'), $whitelist2, $fname = 'Database::select',array('ORDER BY' => 'RAND()'));
                foreach($res as $row) $filelist[] = $row->il_to;
        }
        shuffle($filelist);
        $mailtext['file'] = $orgdateiname = $datei['name'] = $filelist[0];
 
        if (strlen($datei['name']) > 0){
                $foldername = $datei['name'];
                if (substr($datei['name'],-4) == '.svg') $datei['name'] .= '.png';
 
                if (isset($wgThumbLimits)) $prefixes = $wgThumbLimits;
                else $prefixes = $wgMail2Facebook['thumbsizes'];
 
                foreach($prefixes as $key => $prefix) $prefixes[$key] = $prefix.'px-';
                $prefixes[] = '';
                foreach($prefixes as $prefix){
                        $pfade[] = 'images/thumb/'.$foldername.'/'.$prefix.$datei['name'];
                        $pfade[] = 'images/thumb/'.substr(md5($foldername),0,1).'/'.substr(md5($foldername),0,2).'/'.$foldername.'/'.$prefix.$datei['name'];
                }
 
                if(isset($wgMail2Facebook['defaultpic'])) {
                        if (substr($wgMail2Facebook['defaultpic'],0,1) == '/') $wgMail2Facebook['defaultpic'] = substr($wgMail2Facebook['defaultpic'],1);
                        $pfade[] = $wgMail2Facebook['defaultpic'];
                }
 
                foreach($pfade as $pfad){
                        $mailtext['suchpfad_'.$i++] = $pfad;
                        if(file_exists($pfad)) {
                                $mailtext['pfad'] = $datei['pfad'] = $pfad;
                                $mailtext['oldmimetype'] = $datei['oldcontenttype'] = image_type_to_mime_type(exif_imagetype($pfad));
                                $datei['content'] = chunk_split(base64_encode(fread(fopen($pfad,"r"),filesize($pfad))));
 
                                if ($datei['oldcontenttype'] == 'image/jpeg') $im = ImageCreateFromJPEG($pfad);
                                if ($datei['oldcontenttype'] == 'image/gif') $im = ImageCreateFromGIF($pfad);
                                if ($datei['oldcontenttype'] == 'image/png') {
                                        $im = ImageCreateFromPNG($pfad);
                                        ImageAlphaBlending($im,true);
                                        ImageSaveAlpha($im,true); 
                                }
 
                                if(isset($wgMail2Facebook['picturesize'])){
                                        $imold = $im;
                                        unset($im);
                                        $oldx = ImageSX($imold);
                                        $oldy = ImageSY($imold);
                                        if ($oldx >= $oldy){
                                                $newx = $wgMail2Facebook['picturesize'];
                                                $newy = $oldy / $oldx * $wgMail2Facebook['picturesize'];
                                        }
                                        else{
                                                $newx = $oldx / $oldy * $wgMail2Facebook['picturesize'];
                                                $newy = $wgMail2Facebook['picturesize'];
                                        }
 
                                        $im = imagecreatetruecolor($newx,$newy);
//                                              $im = imagefill($im,0,0,imagecolorallocate($im, 255, 255, 255));
 
                                        $mailtext['old_xy'] = $oldx.'px x '.$oldy.'px';
                                        $mailtext['new_xy'] = $newx.'px x '.$newy.'px';
                                        ImageCopyResized($im,$imold,0,0,0,0,$newx,$newy,$oldx,$oldy);
                                }
 
                                ob_start();
                                imagejpeg($im,NULL,$wgMail2Facebook['imgquality']);
                                $contents = ob_get_contents();
                                ob_end_clean();
 
                                $datei['content'] = chunk_split(base64_encode($contents));
 
                                $datei['name'] = substr($orgdateiname,0,strrpos($orgdateiname,'.')).'.jpg';
 
                                break;
                        }
                }
//              $mailtext['contenttype'] = $datei['contenttype'];
        }
        return($datei);
}
?>

[edit] mail2facebook.i18n.php

<?php
//Website http://www.mediawiki.org/wiki/Extension:Mail2Facebook
//cc-by-sa-de 3.0 by Thiemo Schuff
$messages = array();
 
//English
$messages['en'] = array(
        'mail2facebook_posttext_with_author' => 'News from author $1 in article "$2" to show under "$3".',
        'mail2facebook_posttext_without_author' => 'News in article "$1" to show under "$2".',
        'mail2facebook_posttext_license' => ' (Picture in high resolution and copyrightinformation may you get over the URL)',
);
 
//German
$messages['de'] = array(
        'mail2facebook_posttext_with_author' => 'Neues von Author $1 im Artikel "$2" zu lesen unter "$3".',
        'mail2facebook_posttext_without_author' => 'Neues im Artikel "$1" zu lesen unter "$2".',
        'mail2facebook_posttext_license' => ' (Bild in voller Aufl&ouml;sung und entsprechende Lizenzangaben k&ouml;nnen &uuml;ber die URL eingesehen werden)',
);
 
$messages['de_ch'] = $messages['de']; 
$messages['de_at'] = $messages['de']; 
$messages['de_formal'] = $messages['de'];

[edit] mail2facebook.php

<?php
//Website http://www.mediawiki.org/wiki/Extension:Mail2Facebook
//cc-by-sa-de 3.0 by Thiemo Schuff
 
if (!defined('MEDIAWIKI')) die('This file is an extension to the MediaWiki software and cannot be used standalone.');
 
$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['m2f']        = $dir . 'mail2facebook.i18n.php';
$wgAutoloadClasses['m2f']                               = $dir . 'mail2facebook.class.php';
 
$wgHooks['ArticleSaveComplete'][]                                       = 'm2f::mail2facebook';
 
$wgExtensionCredits['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'Mail2Facebook',
        'description'    => 'Send e-mail over Mobile-Upload by new articelversions to Facebook, to post a new text and picture on the wall',
        'descriptionmsg' => 'mail2facebook',
        'author'         => 'Thiemo Schuff',
        'url'            => 'http://www.mediawiki.org/wiki/Extension:Mail2Facebook',
        'version'        => '0.8 beta build 20111227',
);

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox