Extension:SendToTwitter

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
SendToTwitter

Release status: experimental

Implementation  Data extraction
Description Sends a twitter update to twitter every time an article is updated on your wiki.
Author(s)  Rohit Keshwani
License GPL
Download This page

check usage (experimental)

Contents

[edit] What can this extension do?

This extension posts a tweet to twitter of any url that is updated in your wiki. The messages are customizable and urls are automatically shortened through TinyUrl.

[edit] Website examples

WikiCity Twitter Account: WikiCity

Note: This website uses a more advanced version of this extension due to specific content.


Wetland Research

Twitter Account: Wetland Research

[edit] Usage

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/SendToTwitter/SendToTwitter.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:

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

Modify the username/password in the extension file to match your twitter account.

[edit] Configuration parameters

$wgSendToTwitterUsername = 'TWITTERUSERNAME'; // Twitter Username
$wgSendToTwitterPassword = 'TWITTERPASSWORD'; // Twitter Password
$wgSendToTwitterWikiURL = 'ROOT URL TO YOUR WIKI'; // The root url to your wiki like http://www.mediawiki.org/w

[edit] Code

<?php
/**
 * SendToTwitter extension - sends twitter a tweet when a page is changed
 *
 * @file
 * @ingroup Extensions
 * @version 1.0
 * @author Rohit Keshwani
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 * @link http://www.mediawiki.org/wiki/Extension:SendToTwitter 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' => 'SendToTwitter',
	'version' => '1.0',
	'author' => 'Rohit Keshwani',
	'url' => 'http://www.mediawiki.org/wiki/Extension:SendToTwitter',
	'description' => 'Extension to send twitter a tweet when a page is changed.',
);
 
#Configuration parameters
$wgSendToTwitterUsername = 'TWITTERUSERNAME';
$wgSendToTwitterPassword = 'TWITTERPASSWORD';
$wgSendToTwitterWikiURL = 'http://www.example.com/w';
 
$wgHooks['EditPage::attemptSave'][] = 'QueryTwitter';
function QueryTwitter( &$q ) {
	global $wgTitle, $wgArticle;
	global $wgSendToTwitterUsername, $wgSendToTwitterPassword, $wgSendToTwitterWikiURL;
 
	$title = $wgTitle;
	$article = $wgArticle;
	$wurl = $wgSendToTwitterWikiURL;
 
	$test2 = explode( "\n", $article->getContent() );
 
	// The message you want to send
	$ch = curl_init();
	$timeout = 5;
	curl_setopt( $ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $wurl . urlencode( $title ) );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
	curl_setopt( $ch, CURLOPT_HEADER, 0);
	$test = curl_exec( $ch );
	curl_close( $ch );
 
	$switcher = rand( 1, 3 );
	switch( $switcher ) {
		case 1:
			$message = 'looks like somebody updated ' . $title . ' at ' . $test;
			break;
		case 2:
			$message = $title . ' was recently changed: ' . $test . ' Check it out!';
			break;
		case 3:
			$message = 'Check out ' . $test . ' it has some new content on ' . $title;
			break;
	}
 
	// The twitter API address
	$url = 'http://twitter.com/statuses/update.xml';
	// Alternative JSON version
	// $url = 'http://twitter.com/statuses/update.json';
	// 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, "$wgSendToTwitterUsername:$wgSendToTwitterPassword" );
	$buffer = curl_exec( $curl_handle );
	curl_close( $curl_handle );
	// check for success or failure
	if( empty( $buffer ) ) {
		return false;
	} else {
		return true;
	}
}

[edit] Anti Spam

When using any mediawiki anti spam measures that checks the input prior to a save, change

$wgHooks['EditPage::attemptSave'][] = 'QueryTwitter';

to

$wgHooks['ArticleSaveComplete'][] = 'QueryTwitter';


In the current setup, any change is broadcasted to twitter, on an attempted save. If the edits aren't saved due to anti spam measures, they are still are successfully twittered as changes. The modification above will only send the update to twitter if it has successfully been reviewed and saved. A quick way to check if this is an issue for you is to compare your Recent Changes to your twitter account, they should be identical. For Mediawiki versions prior to 1.4.0, you may need to use a different variable.

[edit] Troubleshooting

I had some issues with the URL not being correct when being sent to Twitter, I believe because I am using a stock Mediawiki install, and not using the Mediawiki URL rewrite option.

I changed $wgSendToTwitterWikiURL = 'http://www.mysamplesite.com/wiki/;

to

$wgSendToTwitterWikiURL = 'http://www.mysamplesite.com/wiki/index.php?title=';

and it is now working again.