r107473 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107472‎ | r107473 | r107474 >
Date:09:35, 28 December 2011
Author:varnent
Status:deferred (Comments)
Tags:
Comment:
functional update of TweetANew extension - lots of todo's and localisation still to complete
Modified paths:
  • /trunk/extensions/TweetANew/TweetANew.body.php (added) (history)
  • /trunk/extensions/TweetANew/TweetANew.class.php (deleted) (history)
  • /trunk/extensions/TweetANew/TweetANew.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TweetANew/TweetANew.class.php
@@ -1,57 +0,0 @@
2 -<?php
3 -if (!defined('MEDIAWIKI')) die();
4 -/**
5 - * Class file for the TweetANew extension
6 - *
7 - * @addtogroup Extensions
8 - * @author Joachim De Schrijver
9 - * @license LGPL
10 - */
11 -
12 -// TweetANew
13 -class TweetANew {
14 - // SHORTEN URL TO TWEET
15 - public static function make_bitly_url($url,$login,$appkey,$version = '2.0.1') {
16 -
17 - $bitly = "http://api.bit.ly/shorten?version=".$version."&longUrl=".urlencode($url)."&login=".$login."&apiKey=".$appkey."&format=xml";
18 -
19 - //get the url
20 - $response = file_get_contents($bitly);
21 -
22 - //parse
23 - $xml = simplexml_load_string($response);
24 - return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
25 - }
26 -
27 - // TWEET
28 - public static function Tweet($article, $user, $text, $summary, $minoredit, $watchthis, $sectionanchor, $flags, $revision){
29 - global $wgConsumerKey,$wgConsumerSecret,$wgAccessToken,$wgAccessTokenSecret,$bitly_login,$bitly_api;
30 -
31 - // Make connection to Twitter
32 - require_once('tmhOAuth.php'); // include connection
33 -
34 - $connection = new tmhOAuth(array(
35 - 'consumer_key' => $wgConsumerKey,
36 - 'consumer_secret' => $wgConsumerSecret,
37 - 'user_token' => $wgAccessToken,
38 - 'user_secret' => $wgAccessTokenSecret,
39 - ));
40 -
41 - // Shorten
42 - $to_shorten = $article->getTitle()->getFullURL()."&fb_ref=FB&source=TwitterTAN";
43 - $short = self::make_bitly_url($to_shorten,$bitly_login,$bitly_api);
44 -
45 - // Make tweet message
46 - $tweet_text = "NEW ARTICLE: ".$article->getTitle()->getText()." ".$short;
47 -
48 - // Tweet if not jpg or png
49 - if ( !preg_match( "/\.(png|jpe?g)/i", $article->getTitle()->getText() ) ) {
50 - $connection->request('POST',
51 - $connection->url('1/statuses/update'),
52 - array('status' => $tweet_text)
53 - );
54 - }
55 -
56 - return true;
57 - }
58 -}
\ No newline at end of file
Index: trunk/extensions/TweetANew/TweetANew.body.php
@@ -0,0 +1,155 @@
 2+<?php
 3+if (!defined('MEDIAWIKI')) die();
 4+/**
 5+ * Class file for the TweetANew extension
 6+ *
 7+ * @addtogroup Extensions
 8+ * @author Joachim De Schrijver
 9+ * @license LGPL
 10+ * ToDo - many listed below - general removal of duplication and overall cleanup
 11+ */
 12+
 13+// TweetANew
 14+class TweetANew {
 15+ /**
 16+ * Function for connecting to and preparing bitly
 17+ *
 18+ * @param $url
 19+ * @param $login
 20+ * @param $appkey
 21+ * @return string
 22+ */
 23+ public static function make_bitly_url($url,$login,$appkey) {
 24+ global $wgTweetANewBitly;
 25+
 26+ # Check setting to enable/disable use of bit.ly
 27+ if ( $wgTweetANewBitly['Enable'] ) {
 28+ # Generate url for bitly
 29+ $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl=".urlencode($url)."&login=".$login."&apiKey=".$appkey."&format=txt";
 30+ # Get the url
 31+ $response = file_get_contents($bitly);
 32+ return $response;
 33+ }
 34+ }
 35+
 36+ # ToDo - TweetANewNewEditOption function for when auto-tweet is disabled to display checkbox on edit/create page to tweet new/edited pages
 37+
 38+ /**
 39+ * Function for tweeting new articles
 40+ *
 41+ * @param $article
 42+ * @param $user
 43+ * @param $text
 44+ * @param $summary
 45+ * @param $minoredit
 46+ * @param $watchthis
 47+ * @param $sectionanchor
 48+ * @param $flags
 49+ * @param $revision
 50+ * @return bool
 51+ */
 52+ public static function TweetANewNewArticle($article, $user, $text, $summary, $minoredit, $watchthis, $sectionanchor, $flags, $revision){
 53+ global $wgTweetANewBitly,$wgTweetANewTwitter,$wgTweetANewTweet;
 54+
 55+ # ToDo - Check if $wgTweetANewTweet['Auto'] is enabled
 56+
 57+ # Make connection to Twitter
 58+ require_once('lib/tmhOAuth.php'); // include connection
 59+ $connection = new tmhOAuth(array(
 60+ 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'],
 61+ 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'],
 62+ 'user_token' => $wgTweetANewTwitter['AccessToken'],
 63+ 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
 64+ ));
 65+
 66+ # Check setting to enable/disable use of bit.ly
 67+ if ( $wgTweetANewBitly['Enable'] ) {
 68+ # Shorten URL using bitly
 69+ $short = self::make_bitly_url($article->getTitle()->getFullURL(),$wgTweetANewBitly['Login'],$wgTweetANewBitly['API']);
 70+ }
 71+ else {
 72+ # Generate url without use of bitly
 73+ $short = $article->getTitle()->getFullURL();
 74+ }
 75+ # Make tweet message
 76+ $tweet_text = "NEW ARTICLE: ".$article->getTitle()->getText()." ".$short;
 77+ # Check if page is in content namespace
 78+ if ( MWNamespace::isContent( $article->getTitle()->getNamespace() ) ) {
 79+ $connection->request('POST',
 80+ $connection->url('1/statuses/update'),
 81+ array('status' => $tweet_text)
 82+ );
 83+ }
 84+ return true;
 85+ }
 86+
 87+ /**
 88+ * Function for tweeting edited articles
 89+ *
 90+ * @param $article
 91+ * @param $user
 92+ * @param $text
 93+ * @param $summary
 94+ * @param $minoredit
 95+ * @param $watchthis
 96+ * @param $sectionanchor
 97+ * @param $flags
 98+ * @param $revision
 99+ * @return bool
 100+ */
 101+ public static function TweetANewEditMade(&$article, &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId, &$redirect){
 102+ global $wgTweetANewBitly,$wgTweetANewTwitter,$wgTweetANewTweet,$wgArticle;
 103+
 104+ # ToDo - Check if $wgTweetANewTweet['Auto'] is enabled
 105+
 106+ # Determine the time and date of last modification - skip if newer than $wgTweetANewTweet['lessminold'] setting
 107+ # ToDo - there must be a cleaner way of doing this
 108+ $dbr = wfGetDB( DB_SLAVE );
 109+ $res = $dbr->select('revision', array( 'rev_timestamp' ), array('rev_page = '.$article->getID()), $fname = 'Database::select', $options = array( 'ORDER BY' => 'rev_id DESC' , 'LIMIT' => '2' ));
 110+ foreach($res as $row) $edittime[] = $row->rev_timestamp;
 111+ $edittimenow = mktime(substr($edittime[0],8,2),substr($edittime[0],10,2),substr($edittime[0],12),substr($edittime[0],4,2),substr($edittime[0],6,2),substr($edittime[0],0,4));
 112+ $edittimelast = mktime(substr($edittime[1],8,2),substr($edittime[1],10,2),substr($edittime[1],12),substr($edittime[1],4,2),substr($edittime[1],6,2),substr($edittime[1],0,4));
 113+ $edittimediv = $edittimenow - $edittimelast;
 114+ $mailtext['time_since_last_edit'] = $edittimediv;
 115+ if(isset($wgTweetANewTweet['lessminold'])) if ($edittimediv < ($wgTweetANewTweet['lessminold'] * 60)) return true;
 116+
 117+ # Only proceed if this is not the first edit to the article, in which case it's new and TweetANewNewArticle is used instead
 118+ if ($wgArticle->estimateRevisionCount() == 1 ) return true;
 119+
 120+ # Check $wgTweetANewTweet['SkipMinor'] setting to see if minor edits should be skipped
 121+ if ($minoredit !== 0 && $wgTweetANewTweet['SkipMinor']) return true;
 122+
 123+ # ToDo - If !$wgTweetANewTweet['SkipMinor'] and $wgTweetANewTweet['TagMinor'] add "m" to tweet
 124+
 125+ # Make connection to Twitter
 126+ require_once('lib/tmhOAuth.php'); // include connection
 127+ $connection = new tmhOAuth(array(
 128+ 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'],
 129+ 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'],
 130+ 'user_token' => $wgTweetANewTwitter['AccessToken'],
 131+ 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
 132+ ));
 133+
 134+ # Check setting to enable/disable use of bitly
 135+ if ( $wgTweetANewBitly['Enable'] ) {
 136+ # Shorten URL using bitly
 137+ $to_shorten = $article->getTitle()->getFullURL();
 138+ $short = self::make_bitly_url($to_shorten,$wgTweetANewBitly['Login'],$wgTweetANewBitly['API']);
 139+ }
 140+ else {
 141+ # Generate url without use of bitly
 142+ $short = $article->getTitle()->getFullURL();
 143+ }
 144+ # Make tweet message
 145+ # ToDo - localisation integration - pick between various defaults, a rotator and LocalSettings.php overrifde
 146+ $tweet_text = "UPDATED ARTICLE: ".$article->getTitle()->getText()." ".$short;
 147+ # Check if page is in content namespace
 148+ if ( MWNamespace::isContent( $article->getTitle()->getNamespace() ) ) {
 149+ $connection->request('POST',
 150+ $connection->url('1/statuses/update'),
 151+ array('status' => $tweet_text)
 152+ );
 153+ }
 154+ return true;
 155+ }
 156+}
\ No newline at end of file
Property changes on: trunk/extensions/TweetANew/TweetANew.body.php
___________________________________________________________________
Added: svn:eol-style
1157 + native
Index: trunk/extensions/TweetANew/TweetANew.php
@@ -1,14 +1,26 @@
22 <?php
33 /**
4 - * MediaWiki extension to update a twitter account each time a new article is created (not when images are uploaded)
5 - * Using bit.ly to shorten URLs
 4+ * MediaWiki extension to update a Twitter account each time an article is created or edited - depending on yours settings.
 5+ * Excludes files or content outside of article namespaces.
 6+ * Optional use of bit.ly to shorten and track URLs.
67 * Installation instructions can be found on
7 - * http://www.mediawiki.org/wiki/Extension:TweetANew
 8+ * https://www.mediawiki.org/wiki/Extension:TweetANew
89 *
910 * @addtogroup Extensions
10 - * @author Joachim De Schrijver
11 - * @license LGPL
 11+ * @author Gregory Varnum merging extensions by Joachim De Schrijver, Andrew Fitzgerald, Wendell Gaudencio, and Rohit Keshwani
 12+ * @license GPL
1213 *
 14+ * Version 1.0 and above based on merging extensions TweetANew v0.2 by Joachim De Schrijver, Wiki2twitter by Wendell Gaudencio,
 15+ * SendToTwitter by Rohit Keshwani and SendToTwitter2 by Rohit Keshwani, Andrew Fitzgerald.
 16+ *
 17+ * Some code borrowed from the Mail2Facebook extension by Thiemo Schuff
 18+ *
 19+ * Use Matt Harris' OAuth library to make the connection
 20+ * This lives at: https://github.com/themattharris/tmhOAuth
 21+ * The most recent edition (as of this version's publish date) is included with this extension.
 22+ *
 23+ * Thank you to Raymond and others mentioned in TweetANew.i18n.php for translation work
 24+ *
1325 */
1426
1527 /**
@@ -20,27 +32,82 @@
2133 }
2234
2335 /**
24 - * Settings
 36+ * SETTINGS
 37+ * --------
 38+ * The following variables may be reset in your LocalSettings.php file.
2539 *
26 - * Use Matt Harris' OAuth library to make the connection
27 - * This lives at: https://github.com/themattharris/tmhOAuth
28 - * The 4 keys should be set @ LocalSettings.php
29 - *
 40+ * $wgTweetANewTweet['New']
 41+ * - Tweet about new articles
 42+ * Default is true
 43+ * $wgTweetANewTweet['Edit']
 44+ * - Tweet about articles when edited
 45+ * Default is true
 46+ * $wgTweetANewTweet['lessminold']
 47+ * - Minutes since last edit to wait before tweeting about a new edit
 48+ * Default is 5
 49+ * $wgTweetANewTweet['SkipMinor']
 50+ * - Skip minor edits
 51+ Default is true
 52+ * $wgTweetANewTwitter['ConsumerKey']
 53+ * - Consumer key provided at https://dev.twitter.com/apps - be sure to have write and read permissions
 54+ * $wgTweetANewTwitter['ConsumerSecret']
 55+ * - Consumer secret provided at https://dev.twitter.com/apps - be sure to have write and read permissions
 56+ * $wgTweetANewTwitter['AccessToken']
 57+ * - Access token provided by the OAuth tool at https://dev.twitter.com/apps - be sure to have write and read permissions
 58+ * $wgTweetANewTwitter['AccessTokenSecret']
 59+ * - Access token secret provided by the OAuth tool at https://dev.twitter.com/apps - be sure to have write and read permissions
 60+ * $wgTweetANewBitly['Enable']
 61+ * - Display URL as bitly link - allowing you to track usage via your bitly account
 62+ * Default is false
 63+ * $wgTweetANewBitly['Login']
 64+ * - If bitly link display is enabled, enter your bitly user account - signup at: http://bitly.com/a/sign_up
 65+ * $wgTweetANewBitly['API']
 66+ * - If bitly link display is enabled, enter your bitly API key - find your API key at: http://bitly.com/a/your_api_key
 67+ * $wgTweetANewText['New']
 68+ * - Text used for tweets about new articles
 69+ * Default is set by TweetANew.i18n.php
 70+ * English example = ''
 71+ * $wgTweetANewText['Edit']
 72+ * - Text used for tweets about edited articles
 73+ * Default is set by TweetANew.i18n.php
 74+ * English example = ''
 75+ *
3076 */
31 - $wgConsumerKey = "";
32 - $wgConsumerSecret = "";
33 - $wgAccessToken = "";
34 - $wgAccessTokenSecret= "";
35 - $bitly_api = "";
36 - $bitly_login = "";
3777
 78+ $wgTweetANewTweet = array(
 79+ 'New' => true,
 80+ 'Edit' => true,
 81+ 'lessminold' => 5,
 82+ 'SkipMinor' => true,
 83+ 'Auto' => true,
 84+ );
 85+
 86+ $wgTweetANewTwitter = array(
 87+ 'ConsumerKey' => '',
 88+ 'ConsumerSecret' => '',
 89+ 'AccessToken' => '',
 90+ 'AccessTokenSecret' => '',
 91+ );
 92+
 93+ $wgTweetANewBitly = array(
 94+ 'Enable' => false,
 95+ 'Login' => '',
 96+ 'API' => '',
 97+ );
 98+
 99+$wgTweetANewText = array(
 100+ 'New' => '',
 101+ 'Edit' => '',
 102+ );
 103+
38104 /**
39105 * Class and localisation
40106 *
41107 */
42108
43109 $dir = dirname(__FILE__) . '/';
44 - $wgAutoloadClasses['TweetANew'] = $dir . 'TweetANew.class.php';
 110+ $wgAutoloadClasses['TweetANew'] = $dir . 'TweetANew.body.php';
 111+ $wgExtensionMessagesFiles['TweetANew'] = $dir . 'TweetANew.i18n.php';
45112
46113 /**
47114 * Credits
@@ -49,10 +116,10 @@
50117
51118 $wgExtensionCredits['other'][] = array(
52119 'name' => 'TweetANew',
53 - 'version' => '0.2',
54 - 'author' => '[http://www.mediawiki.org/wiki/User:Joa_ds Joachim De Schrijver]',
55 - 'description' => 'Tweets whenever a new article is created',
56 - 'url' => 'http://www.mediawiki.org/wiki/Extension:TweetANew',
 120+ 'version' => '1.0.20111227-experimental',
 121+ 'author' => '[https:www.mediawiki.org/wiki/User:Varnent Gregory Varnum] merging extensions by [https://www.mediawiki.org/wiki/User:Joa_ds Joachim De Schrijver], Andrew Fitzgerald, Wendell Gaudencio, and Rohit Keshwani',
 122+ 'description' => 'Tweets when an article is created or edited - based on your settings.',
 123+ 'url' => 'https://www.mediawiki.org/wiki/Extension:TweetANew',
57124 );
58125
59126 /**
@@ -60,4 +127,5 @@
61128 *
62129 */
63130
64 -$wgHooks['ArticleInsertComplete'][] = 'TweetANew::Tweet';
\ No newline at end of file
 131+$wgHooks['ArticleInsertComplete'][] = 'TweetANew::TweetANewNewArticle';
 132+$wgHooks['ArticleSaveComplete'][] = 'TweetANew::TweetANewEditMade';
\ No newline at end of file

Comments

#Comment by SPQRobin (talk | contribs)   02:52, 29 December 2011

The author link won't work, it is missing // after https:.

Status & tagging log