Jump to content

Extension talk:SendToTwitter2

Add topic
From mediawiki.org
Latest comment: 14 years ago by Ploegvde in topic Fixed

--173.88.252.175 18:21, 18 September 2010 (UTC)--173.88.252.175 18:21, 18 September 2010 (UTC)--173.88.252.175 18:21, 18 September 2010 (UTC)--173.88.252.175 18:21, 18 September 2010 (UTC)--173.88.252.175 18:21, 18 September 2010 (UTC)for configuration help or consulting, feel free to contact me at http://www.wikiexpert.com/contactReply

--Phalseid 19:24, 8 December 2009 (UTC)Reply

I needed to change this line in the LocalSettings:

$wgSendToTwitterWikiURL = 'http://www.yourdomain.com/wiki/'; #don't forget trailing slash!

to this

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

in order for this extension to work.

--LCaruana 18:25, 9 February 2010 (UTC)Reply


probably depends if you are using prettyurls or not --Phalseid 17:58, 18 February 2010 (UTC)Reply

Same Error As "SendToTwitter"

[edit]
 PHP Fatal error:  Call to undefined function curl_init() in /var/www/htdocs/extensions/SendToTwitter2/SendToTwitter2.php on line 65

Any ideas?
Thanks
--John Thomson 02:57, 2 March 2010 (UTC)Reply

You likely don't have CURL enabled on your webspace --Frantik 15:54, 3 March 2010 (UTC)Reply
Hi Frantik - That did it! Bit of a "doh" moment there. Thanks very much, I appreciate your help!!
--John Thomson 23:27, 3 March 2010 (UTC)Reply

Send to Twitter and Captcha allows TwitterSpam

[edit]

I enabled SendToTwitter2 this evening, and within minutes I received a tweet where the summary was "Accutane", it was promptly followed by a similar tweet. Seeing nothing in my RecentChanges, I tried the following:

  1. I logged out of my wiki
  2. I attempted to edit a page and added a comment
  3. I was prompted to enter my captchas, but by that point, STT2 had already fired, and the tweet was sent.

Relevant captcha config:

// Fix the default captcha behaviour
$wgGroupPermissions['*'            ]['skipcaptcha'] = false;
$wgGroupPermissions['user'         ]['skipcaptcha'] = true;
$wgGroupPermissions['autoconfirmed']['skipcaptcha'] = true;
$wgGroupPermissions['bot'          ]['skipcaptcha'] = true; // registered bots
$wgGroupPermissions['sysop'        ]['skipcaptcha'] = true;
#
$wgCaptchaTriggers['edit']          = true;
$wgCaptchaTriggers['create']        = true;
$wgCaptchaTriggers['createaccount'] = true;

For the time being, I disabled captcha and disabled anonymous edits altogether. It's not an important feature for me anyways.
--John Thomson 23:35, 3 March 2010 (UTC)Reply

Hi John, try this solution which is mentioned on the original SendToTwitter page: Extension:SendToTwitter#Anti_Spam and let me know if it solves your problem. I'm not using a captcha on my site so I'm not sure. --Frantik 18:11, 6 March 2010 (UTC)Reply

Hide to anonymous users & YOURLS integration

[edit]

This is a great tool for my wiki. If anyone is interested, I added a few modifications. First, if you want to hide the SendToTwitter checkbox to anonymous users (which I highly recommend!) you can just change these two lines (around 111 or so) from this:

global $wgSendToTwitterUseCheckBox, $wgSendToTwitterChecked;
if ($wgSendToTwitterUseCheckBox)

to this:

global $wgSendToTwitterUseCheckBox, $wgSendToTwitterChecked, $wgUser;
if ($wgSendToTwitterUseCheckBox && $wgUser->isLoggedIn())

Next, if you have a YOURLS installation for custom short links, you can replace the line that queries the URL shortener's API (around line 67 or so) from this:

curl_setopt( $ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $wurl .  $titleurl );

to this (replace the stuff in caps to your own info):

curl_setopt( $ch, CURLOPT_URL, 'http://YOURDOMAIN/yourls-api.php?action=shorturl&url=' . $wurl .  $titleurl . '&username=USERNAME&password=PASSWORD&format=simple' );

And that's it! I hope that may be helpful to someone. I've recently started using Ping.fm to send certain updates to multiple social networks simultaneously, and it should be pretty simple to modify this extension to send to Ping.fm instead of Twitter. If I end up making those modifications, I'll post an example here. —Pensapedia 15:35, 19 March 2010 (UTC)Reply

for even finer control..
global $wgSendToTwitterUseCheckBox, $wgSendToTwitterChecked, $wgUser;
if ($wgSendToTwitterUseCheckBox && $wgUser->isAllowed( 'canTweet' )())
then you can specify who can tweet
$wgGroupPermissions['*']['canTweet'] = false;          // anon not allowed to tweet
$wgGroupPermissions['user']['canTweet'] = false;       // users can't tweet either
$wgGroupPermissions['myCoolGroup']['canTweet'] = true; // myCoolGroup users can tweet
$wgGroupPermissions['sysop']['canTweet'] = true;       // admins  can tweet
i will add this in the next release --Frantik 12:11, 20 March 2010 (UTC)Reply

Problem with the extension

[edit]

Copied the settings to Localsettings.php, and everything is default except for username & password. I get the following error message when I try to save a page that after editing it.

Detected bug in an extension! Hook efSendToTwitter2Save failed to return a value; should return true to continue hook processing or false to abort.

I'm not sure what then problem is since the function does indeed return a value. Any other error messages? --Frantik 15:02, 14 August 2010 (UTC)Reply

I saw the same type of error, failed to return a value, on the ConfirmAccount extension and the solution there was to upgrade it to the version that is for my Mediwiki installation version. I currently run 1.16.0 of Mediawiki and this extension is for 1.14+ so I guess that its not the problem.

Help with new OAuth

[edit]

I've recieved this in a mail from Twitter: >> Starting August 31, all applications will be required to use “OAuth” to access your Twitter account. So the extension is not working anymore. What to do? Erwin van der Ploeg 10:52, 6 September 2010 (UTC)Reply

Yeah i thought it was supposed to be sooner actually. Unfortunately, I don't have a lot of use for this extension; someone paid me to make the first set of changes from the original. If someone wants to contract me to update it for OAuth, it is more likely to be working quickly. Otherwise it will have to wait until I have some free time and interest in fixing it. If someone can give an example on how to do the OAuth stuff it would make it a lot easier to implement. --Frantik 11:23, 6 September 2010 (UTC)Reply

I was very pleased with the extension, so too bad it isn't working anymore. I'm not a developer, so i can rewrite your code. I did find some PHP code what can do the authenticating trick. Maybe you can integrate is? http://github.com/abraham/twitteroauth Erwin van der Ploeg 07:07, 7 September 2010 (UTC)Reply

that twitteroauth code will help a lot, thanks --Frantik 08:18, 7 September 2010 (UTC)Reply

Fixed

[edit]
abstract class OAuthDamnitBase {

	var $consumer_key; 
	var $consumer_secret;
	var $access_token = ;
	var $access_token_secret = ;

	abstract function http($method, $url, $data);

	function __construct($consumer_key, $consumer_secret, $access_token = , $access_token_secret = ) {
		$this->consumer_key = $consumer_key;
		$this->consumer_secret = $consumer_secret;
		$this->access_token = $access_token;
		$this->access_token_secret = $access_token_secret;
	}
	function get($url, $params = array()) {
		return $this->request('GET', $url, $params);
	}
	function post($url, $params = array()) {
		return $this->request('POST', $url, $params);
	}
	function request($method, $url, $params = array()) {
		$signed_params = $this->sign($method, $url, $params);
		return $this->http($method, $url, $this->build_query($signed_params));
	}
	function sign($method, $url, $params = array()) {
		$params['oauth_consumer_key'] = $this->consumer_key;
		$params['oauth_nonce'] = $this->nonce();
		$params['oauth_signature_method'] = 'HMAC-SHA1';
		$params['oauth_timestamp'] = time();
		$params['oauth_version'] = '1.0';
		if (!empty($this->access_token)) {
			$params['oauth_token'] = $this->access_token;
		}
		$params['oauth_signature'] = $this->signature($method, $url, $params);
		return $params;
	}
	function signature($method, $url, $params = array()) {
		$query = $this->build_query($params);
		$base = $method . '&' . $this->encode($url) . '&' . $this->encode($query);
		$key = $this->consumer_secret . '&';
		if (!empty($this->access_token_secret)) {
			$key .= $this->access_token_secret;
		}
		return base64_encode($this->hmac_sha1($key, $base));
	}
	function build_query($params) {
		$output = array();
		foreach ($params as $k => $v) {
			if (is_array($v)) {
				foreach ($v as $vv) {
					$output[] = $this->encode($k) . '=' . $this->encode($vv);
				}
			} else {
				$output[] = $this->encode($k) . '=' . $this->encode($v);
			}
		}
		sort ($output, SORT_STRING);
		return implode('&', $output);
	}
	function hmac_sha1($key, $message) {
		if (function_exists('hash_hmac')) {
			return hash_hmac('sha1', $message, $key, true);
		}
		if (strlen($key) > 64) {
			$key = sha1($key, true);
		}
		$key = str_pad($key, 64, "\0");
		$key1 = $key2 = ;
		for ($i = 0; $i < 64; $i ++) {
			$key1 .= chr(ord($key[$i]) ^ 0x5C);
			$key2 .= chr(ord($key[$i]) ^ 0x36);
		}
		return sha1($key1 . sha1($key2 . $message, true), true);
	}
	function nonce() {
		return bin2hex(md5(microtime(), true) . sha1(microtime(), true));
	}
	function encode($string) {
		return str_replace('%7E', '~', rawurlencode($string));
	}
 
}
 
class OAuthDamnit extends OAuthDamnitBase {
	
	var $ch;
	function curl_set_options() {
		curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt ($this->ch, CURLOPT_USERAGENT, 'OAuth Damnit v0.1');
	}
	function curl_execute() {
		return curl_exec($this->ch);
	}
	function http($method, $url, $data) {
		if (!isset($this->ch)) {
			$this->ch = curl_init();
			$this->curl_set_options();
		}
		if ($method == 'POST') {
			curl_setopt ($this->ch, CURLOPT_URL, $url);
			curl_setopt ($this->ch, CURLOPT_POST, true);
			curl_setopt ($this->ch, CURLOPT_POSTFIELDS, $data);
		} else {
			$append = strpos($url, '?') === false ? '?' : '&';
			curl_setopt ($this->ch, CURLOPT_URL, $url . $append . $data);
		}
		return $this->curl_execute();
	}

} 


if ( !defined( 'MEDIAWIKI' ) )
	die( "This is not a valid entry point.\n" );

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

function efSendToTwitter2Save( &$q ) {

	global $wgTitle;
	$title = $wgTitle;
	$titleurl = str_replace(' ','_',$wgTitle);
	$wurl = 'YOUR MEDIA WIKI URL';

	$ch = curl_init();
	$timeout = 5;
	curl_setopt( $ch, CURLOPT_URL, 'TINYURL URL/api-create.php?url=' . $wurl .  $titleurl );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
	curl_setopt( $ch, CURLOPT_HEADER, 0);
	$link = curl_exec( $ch );
	curl_close( $ch );

	$message = $title . ' was recently changed. ' . $link;
 
	if (strlen($message) > 140){
		$message = substr($message,0, 140);
	}
 
define ('ACCESS_TOKEN', 'YOUR TWITTER ACCESS TOKEN');
define ('ACCESS_TOKEN_SECRET', 'YOUR TWITTER ACCESS TOKEN SECRET');
define('CONSUMER_KEY', 'YOUR TWITTER CONSUMER KEY');
define('CONSUMER_SECRET', 'YOUR TWITTER CONSUMER SECRET');
$api = new OAuthDamnit(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$raw = $api->post('http://api.twitter.com/1/statuses/update.json', array('status' => $message));

return true;	 

}

Erwin van der Ploeg 07:52, 20 September 2010 (UTC) Is this the code that repleces all the code in SendToTwitter2.php? Just replace all?Reply

Yes and no. All of this code replaces all of the old code, however you'll need to make some changes. You can email me at sruchris@gmail.com if you want more info on how this code works.

Alternative solution

[edit]
  • I found a blog post [1] that I could easily use to send to Twitter with MediaWiki. It uses twitterOAuth for talking OAuth.