Extension:Send2StatusNet

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

Release status: unknown

Implementation Data extraction
Description Sends updates to any StatusNet site of your choice for new articles or edits
Author(s) Erkan Yilmaz, Singlespeedfahrer, Rohit Keshwani, Sage Ross
Latest version 1.3.2 (2011-10-14)
MediaWiki 1.16+
License GPL
Download This page
Hooks used
ArticleSaveComplete

GetPreferences
ArticleInsertComplete

Translate the Send2StatusNet extension if possible

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

This extension lets users push updates to any StatusNet site of their choice (e.g. also Identi.ca) whenever they make an edit or create a new page on the wiki. The messages are customizable and urls are shortened with either is.gd or ur1.ca (currently down).

Users input their username and password in the "Misc" section of "My preferences" (see picture below) and set some configuration parameters.

Usage[edit | edit source]

Preferences

In the "Misc" tab of "My preferences", a user may input the username and password of their Identi.ca account (or any other StatusNet site) and choose whether to send updates about new articles and/or edits to existing articles, plus selecting the service to shorten the URLs with.

Other configuration parameters are:

  • Karma - only notify when you are lucky today
    • depending on the dice there will be no dent with the page name, but a general link to the wiki instead + the tags #wiki and #karma will be added
  • remove text from article titles
    • (e.g. in my Mediawiki blog all my page names start with: Blog:Observations/ + since dents have a limit on text, this is useful)

specific for identi.ca[edit | edit source]

use for the API: http://identi.ca/api/statuses/update.xml

please consider: send each edit or only wiki articles ?[edit | edit source]

If you select "Notify StatusNet site below every time you edit" you should be aware that in some cases this can "backfire" (e.g. spam, new users trying out something) + who knows you may get banned somewhere because of this :-(
I'd recommend to use first "Notify StatusNet when you start a new article".

changes to old version[edit | edit source]

This extension has following changes to Extension:SendToIdentica:

  • one can post now to any StatusNet site, not only identi.ca
  • post only when the extension is activated, can be useful in cases of spam or test edits by new users
  • remove text from article names (e.g. in my Mediawiki blog all my page names start with: Blog:Observations/ + since dents have a limit on text, this is useful)
  • configuration of the extension is easier (it's now in your wiki preferences) + also can be done without shell access
  • karma factor
    • why did I implement this? software should also be fun :-)
  • make URL shorteners also customizable in options (2 services offered)

tested versions so far[edit | edit source]

tested with current (2011-10-14) versions of:

  • MediaWiki 1.17.0
  • StatusNet 1.0.1

The extension is set up in this wiki if you want to see it in action.

change log[edit | edit source]

2011-10-14: version 1.3.2

  • see here: add a 2nd URL shortener since the existing one does not work anymore
  • start with refactoring code

Download instructions[edit | edit source]

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

Installation[edit | edit source]

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

require_once("$IP/extensions/Send2StatusNet/Send2StatusNet.php");

Send2StatusNet uses the GetPreferences hook and so requires MediaWiki 1.16 or later.

Note: This extension needs php-curl to work.

Code[edit | edit source]

Send2StatusNet.php[edit | edit source]

<?php
/**
 * Send2StatusNet extension - sends a dent to a StatusNet site of your choice (e.g. identi.ca) when a page is created and/or edited
 *
 * @file
 * @ingroup Extensions
 * @version 1.3.2
 * @author Erkan Yilmaz, Singlespeedfahrer, Rohit Keshwani, Sage Ross
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 * @link http://www.mediawiki.org/wiki/Extension:Send2StatusNet Documentation
 */
 
if ( !defined( 'MEDIAWIKI' ) )
        die( "This is not a valid entry point.\n" );
 
// Extension credits that will show up on Special:Version
$wgExtensionCredits['other'][] = array(
        'name' => 'Send2StatusNet',
        'version' => '1.3.2',
        'author' => array( '[http://iaskquestions.com Erkan Yilmaz]', 'Singlespeedfahrer', 'Rohit Keshwani', 'Sage Ross' ),
        'url' => 'http://www.mediawiki.org/wiki/Extension:Send2StatusNet',
        'description' => 'Sends a short message (a "dent") to a StatusNet site of your choice (e.g. Identi.ca) when a page is created and/or edited',
);
 
$dir = dirname(__FILE__) . '/';
 
// Configuration parameters
$wgExtensionMessagesFiles['SendToIdentica'] = $dir . 'Send2StatusNet.i18n.php';
 
$wgHooks['GetPreferences'][] = 'wfSendToIdenticaPreferences';
$wgHooks['ArticleInsertComplete'][] = 'SendDentAboutNewArticle';
$wgHooks['ArticleSaveComplete'][] = 'SendDentAboutEdit';
 
function wfSendToIdenticaPreferences ( $user, &$preferences ) {
 
        $preferences['SendToStatusNetActivate'] = array(
            'type' => 'toggle',
            'section' => 'misc',
            'label-message' => 'sendtostatusnet_activate',
        );
 
        $preferences['SendToIdenticaEdits'] = array(
            'type' => 'toggle',
            'section' => 'misc',
            'label-message' => 'sendtoidentica_edits',
        );
 
        $preferences['SendToIdenticaNewArticles'] = array(
            'type' => 'toggle',
            'section' => 'misc',
            'label-message' => 'sendtoidentica_newarticles',
        );
 
        $preferences['URLShortener1'] = array(
            'type' => 'toggle',
            'section' => 'misc',
            'label-message' => 'url_shortener1',
        );
 
        $preferences['URLShortener2'] = array(
            'type' => 'toggle',
            'section' => 'misc',
            'label-message' => 'url_shortener2',
        );
 
        $preferences['KarmaFactor'] = array(
            'type' => 'toggle',
            'section' => 'misc',
            'label-message' => 'karma_factor',
        );
 
        $preferences['WikiRootUrl'] = array(
            'type' => 'text',
            'section' => 'misc',
            'label-message' => 'wiki_root_url',
        );
 
        $preferences['SendToStatusNetSite'] = array(
            'type' => 'text',
            'section' => 'misc',
            'label-message' => 'sendtostatusnet_sitename',
        );
 
        $preferences['SendToIdenticaUsername'] = array(
            'type' => 'text',
            'section' => 'misc',
            'label-message' => 'sendtoidentica_username',
        );
 
        $preferences['SendToIdenticaPassword'] = array(
            'type' => 'text',
            'section' => 'misc',
            'label-message' => 'sendtoidentica_password',
        );
 
        $preferences['RemoveTextFromPageTitle'] = array(
            'type' => 'text',
            'section' => 'misc',
            'label-message' => 'remove_text_from_page_title',
        );
 
        return true;
}
 
function executeCurl($url, $message, $identicausername, $identicapassword){
	// Set up and execute the curl process
	$curl_handle = curl_init();
	curl_setopt( $curl_handle, CURLOPT_URL, "$url" );
	curl_setopt( $curl_handle, CURLOPT_CONNECTTIMEOUT, 2 );
	curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $curl_handle, CURLOPT_POST, 1 );
	curl_setopt( $curl_handle, CURLOPT_POSTFIELDS, "status=$message" );
	curl_setopt( $curl_handle, CURLOPT_USERPWD, "$identicausername:$identicapassword" );
	$buffer = curl_exec( $curl_handle );
	curl_close( $curl_handle );
 
	// check for success or failure
        if( empty( $buffer ) ) {
		return false;
		$status = 0;
	} else {
		return true;
		$status = 1;
	}
 
	return true;
}
 
function ShortenLongUrlWithUr1dotca ( $articleurl ) {
        // create a short url of the article link ( $shortlink ) using the http:// ur1.ca service
        $ch = curl_init();
        $timeout = 5;
        $shortenerurl = 'http://ur1.ca/';
        curl_setopt( $ch, CURLOPT_URL, $shortenerurl );
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "longurl=$articleurl" );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        $rawshortlinkoutput = curl_exec( $ch );
        curl_close( $ch );
 
        // extract the short url from the ur1.ca html output.  It's the link within the first <p> in the body.
        $shortlinkxml = @simplexml_load_string($rawshortlinkoutput);
        $shortlinkarray = $shortlinkxml->body->p[0]->a->attributes();
 
        return strval($shortlinkarray['href']);
}
 
function ShortenLongUrlWithisgd ( $articleurl ) {
        // create a short url of the article link ( $shortlink ) using the http://is.gd service
        $ch = curl_init();
        $timeout = 5;
        $shortenerurl = 'http://is.gd/create.php?format=simple&url=';
        curl_setopt( $ch, CURLOPT_URL, $shortenerurl . $articleurl );
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT RETURN HTTP HEADERS
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // RETURN THE CONTENTS OF THE CALL
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        $rawshortlinkoutput = curl_exec( $ch );
        curl_close( $ch );
 
        if (preg_match('/^http/', $rawshortlinkoutput)) {
                return strval($rawshortlinkoutput);
                }
}
 
 
function SendDentAboutNewArticle ( &$q ) {
        global $wgTitle, $wgArticle, $wgUser;
 
        // only proceed if extension was activated AND user has selected to send dents about new articles
       	if ( $wgUser->getBoolOption('SendToStatusNetActivate') && $wgUser->getBoolOption('SendToIdenticaNewArticles') ){
                $username = $wgUser->getName();
                $title = str_replace($wgUser->getOption('RemoveTextFromPageTitle'),"", $wgTitle);
                $article = $wgArticle;
                $wurl = $wgUser->getOption('WikiRootUrl'); // root url of your wiki 
                $articleurl = $wurl . urlencode( $title );
 
                // call up for the specific StatusNet site the user's username and password from the Preferences
                $identicausername = $wgUser->getOption('SendToIdenticaUsername');
                $identicapassword = $wgUser->getOption('SendToIdenticaPassword');
 
                $test2 = explode( "\n", $article->getContent() );
 
                // create a short url of the article link ( $shortlink ) with 1) http:// ur1.ca or  2) http://is.gd
	       	if ( $wgUser->getBoolOption('URLShortener1') ){
	                $shortlink       = ShortenLongUrlWithUr1dotca ( $articleurl );
	                $shortlink_karma = ShortenLongUrlWithUr1dotca ( $wurl );
		}
	       	if ( $wgUser->getBoolOption('URLShortener2') ){
	                $shortlink       = ShortenLongUrlWithisgd ( $articleurl );
	                $shortlink_karma = ShortenLongUrlWithisgd ( $wurl );
		}
 
                // take the shortened article link and the article title and use them to create a message     
		if ($wgUser->getOption('KarmaFactor')) {$switcher = rand( 1, 4 );}
		else {$switcher = rand( 1, 3 );}
 
                switch( $switcher ) {
                        case 1:
                                $message = 'looks like ' . $username . ' created a new article on ' . $title . ' at ' . $shortlink;
                                break;
                        case 2:
                                $message = $title . ' was just started by ' . $username . ': ' . $shortlink . ' Check it out!';
                                break;
                        case 3:
                                $message = 'Check out ' . $title . ': ' . $shortlink . ' It is a new article by ' . $username . '.';
                                break;
                        case 4:
	                        $message = $username . ' had no lucky today, find out more in #wiki ' . $shortlink_karma . '#karma';
                                break;
 
                }
 
                // The API address of the StatusNet site
		$url = $wgUser->getOption('SendToStatusNetSite');
 
	        // Set up and execute the curl process
		executeCurl($url, $message, $identicausername, $identicapassword);
 
	}
        return true;
}
 
function SendDentAboutEdit ( &$q ) {
        global $wgTitle, $wgArticle, $wgUser;
 
        // only proceed if extension was activated AND if the user has selected to send dents about edits
       	if ( $wgUser->getBoolOption('SendToStatusNetActivate') && $wgUser->getBoolOption('SendToIdenticaEdits') ){
 
        // only proceed if this is not the first edit to the article, in which case it's new and SendDentAboutNewArticle is used instead
        if ($wgArticle->estimateRevisionCount() > 1 ) {
                $username = $wgUser->getName();
                $title = str_replace($wgUser->getOption('RemoveTextFromPageTitle'),"", $wgTitle);
                $article = $wgArticle;
                $wurl = $wgUser->getOption('WikiRootUrl'); // root url of your wiki 
                $articleurl = $wurl . urlencode( $title );
 
                // call up the user's Identi.ca username and password from the Preferences
                $identicausername = $wgUser->getOption('SendToIdenticaUsername');
                $identicapassword = $wgUser->getOption('SendToIdenticaPassword');
 
                $test2 = explode( "\n", $article->getContent() );
 
                // create a short url of the article link ( $shortlink ) with 1) http:// ur1.ca or 2) http://is.gd
	       	if ( $wgUser->getBoolOption('URLShortener1') ){
	                $shortlink       = ShortenLongUrlWithUr1dotca ( $articleurl );
	                $shortlink_karma = ShortenLongUrlWithUr1dotca ( $wurl );
		}
	       	if ( $wgUser->getBoolOption('URLShortener2') ){
	                $shortlink       = ShortenLongUrlWithisgd ( $articleurl );
	                $shortlink_karma = ShortenLongUrlWithisgd ( $wurl );
		}
 
                // take the shortened article link and the article title and use them to create a message     
		if ($wgUser->getOption('KarmaFactor')) {$switcher = rand( 1, 4 );}
		else {$switcher = rand( 1, 3 );}
 
                switch( $switcher ) {
                        case 1:
                                $message = 'looks like ' . $username . ' updated ' . $title . ' at ' . $shortlink;
                                break;
                        case 2:
                                $message = $title . ' was recently changed by ' . $username . ': ' . $shortlink . ' Check it out!';
                                break;
                        case 3:
                                $message = 'Check out ' . $title . ': ' . $shortlink . ' It got edited by ' . $username . '.';
                                break;
                        case 4:
	                        $message = $username . ' had no lucky today, find out more in #wiki ' . $shortlink_karma . '#karma';
                                break;
                }
 
                // The API address of the StatusNet site
		$url = $wgUser->getOption('SendToStatusNetSite');
 
	        // Set up and execute the curl process
		executeCurl($url, $message, $identicausername, $identicapassword);
 
        }
	}
        return true;
}

Send2StatusNet.i18n.php[edit | edit source]

<?php
/**
 * Internationalisation file for Send2StatusNet extension.
 *
*/
 
$messages = array();
 
$messages['en'] = array(
	'sendtostatusnet_activate' => 'activate Send2StatusNet extension (e.g. good for test edits (by new users))',
        'sendtoidentica_edits' => 'Notify StatusNet site below every time you edit',
        'sendtoidentica_newarticles' => 'Notify StatusNet when you start a new article',
	'url_shortener1' => 'URL shortner: use ur1.ca OR',
	'url_shortener2' => 'URL shortner: use is.gd',
	'karma_factor' => 'Karma - only notify when you are lucky today',
	'wiki_root_url' => 'root URL of your wiki',
	'sendtostatusnet_sitename' => 'API of StatusNet site',
        'sendtoidentica_username' => 'StatusNet username',
        'sendtoidentica_password' => 'StatusNet password',
	'remove_text_from_page_title' => 'remove following text from article titles',
);
 
/** French (Français)
 * @author lionelr, Erkan Yilmaz
 */
$messages['fr'] = array(
    'sendtostatusnet_activate' => 'Activer Send2StatusNet extension',
    'sendtoidentica_edits' => 'Notifier StatusNet à chaque modification',
    'sendtoidentica_newarticles' => 'Notifier le StatusNet site web (cf. ci-dessous) à la création d\'une nouvelle page',
    'url_shortener1' => "Réduction d'URL: activer ur1.ca OU",
    'url_shortener2' => "Réduction d'URL: activer is.gd",
    'karma_factor' => 'Karma - notifier seulement, si tu as de la chance',
    'wiki_root_url' => 'adresse URL de ton wiki',
    'sendtostatusnet_sitename' => 'StatusNet site web API',
    'sendtoidentica_username' => 'Login StatusNet',
    'sendtoidentica_password' => 'Mot de passe StatusNet',
    'remove_text_from_page_title' => 'éliminer suivant texte dans les titres des articles',
);
 
/** German (Deutsch)
 * @author Erkan Yilmaz
 */
$messages['de'] = array(
    'sendtostatusnet_activate' => 'aktiviere die Send2StatusNet Extension (hilft z.B. bei Test-Edits (neuer Benutzer))',
    'sendtoidentica_edits' => 'Benachrichtige StatusNet bei jeder Änderung',
    'sendtoidentica_newarticles' => 'Benachrichtige untere StatusNet Seite, wenn ein neuer Artikel begonnen wird',
    'url_shortener1' => 'Kurz-URL-Dienst: benutze ur1.ca ODER',
    'url_shortener2' => 'Kurz-URL-Dienst: benutze is.gd',
    'karma_factor' => 'Karma - benachrichtige nur, wenn du heute Glück hast',
    'wiki_root_url' => 'die root URL deines wikis',
    'sendtostatusnet_sitename' => 'API der StatusNet Seite',
    'sendtoidentica_username' => 'StatusNet Benutzername',
    'sendtoidentica_password' => 'StatusNet Passwort',
    'remove_text_from_page_title' => 'entferne folgenden Text von Artikelnamen',
);