r107553 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107552‎ | r107553 | r107554 >
Date:06:02, 29 December 2011
Author:varnent
Status:deferred
Tags:
Comment:
SendToTwitter and wiki2twitter features integrated - localisation integration begun (not completed) - SendToTwitter2 code yet to be integrated - basically auto-tweet features work and customizations for them are mostly complete - cleanup work and editpage option are next
Modified paths:
  • /trunk/extensions/TweetANew/TweetANew.body.php (modified) (history)
  • /trunk/extensions/TweetANew/TweetANew.i18n.php (modified) (history)
  • /trunk/extensions/TweetANew/TweetANew.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TweetANew/TweetANew.body.php
@@ -14,22 +14,25 @@
1515 /**
1616 * Function for connecting to and preparing bitly
1717 *
18 - * @param $url
 18+ * @param $longurl
1919 * @param $login
2020 * @param $appkey
2121 * @return string
2222 */
23 - public static function make_bitly_url($url,$login,$appkey) {
 23+ public static function make_final_url($longurl,$login,$appkey) {
2424 global $wgTweetANewBitly;
2525
26 - # Check setting to enable/disable use of bit.ly
 26+ # Check setting to enable/disable use of bitly
2727 if ( $wgTweetANewBitly['Enable'] ) {
2828 # Generate url for bitly
29 - $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl=".urlencode($url)."&login=".$login."&apiKey=".$appkey."&format=txt";
 29+ $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl=".urlencode($longurl)."&login=".$login."&apiKey=".$appkey."&format=txt";
3030 # Get the url
3131 $response = file_get_contents($bitly);
32 - return $response;
3332 }
 33+ else {
 34+ $response = $longurl;
 35+ }
 36+ return $response;
3437 }
3538
3639 # ToDo - TweetANewNewEditOption function for when auto-tweet is disabled to display checkbox on edit/create page to tweet new/edited pages
@@ -49,10 +52,13 @@
5053 * @return bool
5154 */
5255 public static function TweetANewNewArticle($article, $user, $text, $summary, $minoredit, $watchthis, $sectionanchor, $flags, $revision){
53 - global $wgTweetANewBitly,$wgTweetANewTwitter,$wgTweetANewTweet;
 56+ global $wgTweetANewTweet,$wgTweetANewText,$wgTweetANewTwitter;
5457
5558 # ToDo - Check if $wgTweetANewTweet['Auto'] is enabled
5659
 60+ # Check if page is in content namespace
 61+ if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() ) ) return true;
 62+
5763 # Make connection to Twitter
5864 require_once('lib/tmhOAuth.php'); // include connection
5965 $connection = new tmhOAuth(array(
@@ -62,24 +68,58 @@
6369 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
6470 ));
6571
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']);
 72+ # Generate final url
 73+ $finalurl = self::make_final_url($article->getTitle()->getFullURL(),$wgTweetANewBitly['Login'],$wgTweetANewBitly['API']);
 74+
 75+ # Generate $author based on $wgTweetANewText['RealName']
 76+ if ( $wgTweetANewText['RealName'] ) {
 77+ $author = $user->getRealName();
7078 }
7179 else {
72 - # Generate url without use of bitly
73 - $short = $article->getTitle()->getFullURL();
 80+ $author = $user->getName();
7481 }
 82+
 83+ # Generate a random tweet texts based if $wgTweetANewText['NewRandom'] is true
 84+ if ( $wgTweetANewText['NewRandom'] ) {
 85+ # Setup switcher using max number set by $wgTweetANewText['NewRandomMax']
 86+ $switcher = rand( 1, $wgTweetANewText['NewRandomMax'] );
 87+ # Parse random text
 88+ $tweet_text .= wfMsg( 'tweetanew-new' . $switcher, array ( $article->getTitle()->getText(), $finalurl ) );
 89+ }
 90+ else {
 91+ # Use default tweet message format
 92+ $tweet_body = wfMsg( 'tweetanew-newdefault', array ( $article->getTitle()->getText(), $finalurl ) );
 93+ $tweet_text .= $tweet_body;
 94+ }
 95+
 96+ # Add author info if $wgTweetANewText['NewAuthor'] is true
 97+ if ( $wgTweetANewText['NewAuthor'] ) {
 98+ $tweet_text .= ' '. wfMsg( 'tweetanew-authorcredit' ) .' '. $author;
 99+ }
 100+
 101+ # Add summary if $wgTweetANewText['NewSummary'] is true and summary text is entered
 102+ if ( $summary && $wgTweetANewText['NewSummary'] ) {
 103+ $tweet_text .= ' - ' . $summary;
 104+ }
 105+
 106+ # Calculate length of tweet factoring in longURL
 107+ if (strlen($finalurl) > 20) {
 108+ $tweet_text_count = (strlen($finalurl) - 20) + 140;
 109+ }
 110+ else {
 111+ $tweet_text_count = 140;
 112+ }
 113+
 114+ # Check if length of tweet is beyond 140 characters and shorten if necessary
 115+ if (strlen($tweet_text) > $tweet_text_count) {
 116+ $tweet_text = substr($tweet_text,0, $tweet_text_count);
 117+ }
 118+
75119 # 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 - }
 120+ $connection->request('POST',
 121+ $connection->url('1/statuses/update'),
 122+ array('status' => $tweet_text)
 123+ );
84124 return true;
85125 }
86126
@@ -98,10 +138,13 @@
99139 * @return bool
100140 */
101141 public static function TweetANewEditMade(&$article, &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId, &$redirect){
102 - global $wgTweetANewBitly,$wgTweetANewTwitter,$wgTweetANewTweet,$wgArticle;
 142+ global $wgTweetANewTweet,$wgTweetANewText,$wgTweetANewTwitter,$wgArticle;
103143
104144 # ToDo - Check if $wgTweetANewTweet['Auto'] is enabled
105145
 146+ # Check if page is in content namespace
 147+ if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() ) ) return true;
 148+
106149 # Determine the time and date of last modification - skip if newer than $wgTweetANewTweet['lessminold'] setting
107150 # ToDo - there must be a cleaner way of doing this
108151 $dbr = wfGetDB( DB_SLAVE );
@@ -111,45 +154,84 @@
112155 $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));
113156 $edittimediv = $edittimenow - $edittimelast;
114157 $mailtext['time_since_last_edit'] = $edittimediv;
115 - if(isset($wgTweetANewTweet['lessminold'])) if ($edittimediv < ($wgTweetANewTweet['lessminold'] * 60)) return true;
 158+ if( isset( $wgTweetANewTweet['LessMinutesOld'] ) ) if ($edittimediv < ($wgTweetANewTweet['LessMinutesOld'] * 60) ) return true;
116159
117160 # 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;
 161+ if ( $wgArticle->estimateRevisionCount() == 1 ) return true;
119162
120163 # Check $wgTweetANewTweet['SkipMinor'] setting to see if minor edits should be skipped
121 - if ($minoredit !== 0 && $wgTweetANewTweet['SkipMinor']) return true;
 164+ if ( $minoredit !== 0 && $wgTweetANewTweet['SkipMinor'] ) return true;
122165
123 - # ToDo - If !$wgTweetANewTweet['SkipMinor'] and $wgTweetANewTweet['TagMinor'] add "m" to tweet
124 -
125166 # Make connection to Twitter
126167 require_once('lib/tmhOAuth.php'); // include connection
127 - $connection = new tmhOAuth(array(
 168+ $connection = new tmhOAuth( array (
128169 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'],
129170 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'],
130171 'user_token' => $wgTweetANewTwitter['AccessToken'],
131172 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
132 - ));
 173+ ) );
133174
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']);
 175+ # Generate final url
 176+ $finalurl = self::make_final_url($article->getTitle()->getFullURL(),$wgTweetANewBitly['Login'],$wgTweetANewBitly['API']);
 177+
 178+ # Generate $author based on $wgTweetANewText['RealName']
 179+ if ( $wgTweetANewText['RealName'] ) {
 180+ $author = $user->getRealName();
139181 }
140182 else {
141 - # Generate url without use of bitly
142 - $short = $article->getTitle()->getFullURL();
 183+ $author = $user->getName();
143184 }
 185+
 186+ # Add prefix indication that edit is minor if $wgTweetANewText['Minor'] is true and !$wgTweetANewTweet['SkipMinor'] is false
 187+ if ( $minoredit !== 0 && $wgTweetANewText['Minor'] ) {
 188+ $tweet_text = wfMsg( 'tweetanew-minoredit' );
 189+ # Add a space after the indicator if $wgTweetANewText['MinorSpace'] is true
 190+ if ( $minoredit !== 0 && $wgTweetANewText['MinorSpace'] ) {
 191+ $tweet_text .= '&nbsp;';
 192+ }
 193+ }
 194+
 195+ # Generate a random tweet texts based if $wgTweetANewText['EditRandom'] is true
 196+ if ( $wgTweetANewText['EditRandom'] ) {
 197+ # Setup switcher using max number set by $wgTweetANewText['EditRandomMax']
 198+ $switcher = rand( 1, $wgTweetANewText['EditRandomMax'] );
 199+ # Parse random text
 200+ $tweet_text .= wfMsg( 'tweetanew-edit' . $switcher, array ( $article->getTitle()->getText(), $finalurl ) );
 201+ }
 202+ else {
 203+ # Use default tweet message format
 204+ $tweet_body = wfMsg( 'tweetanew-editdefault', array ( $article->getTitle()->getText(), $finalurl ) );
 205+ $tweet_text .= $tweet_body;
 206+ }
 207+
 208+ # Add author info if $wgTweetANewText['EditAuthor'] is true
 209+ if ( $wgTweetANewText['EditAuthor'] ) {
 210+ $tweet_text .= ' '. wfMsg( 'tweetanew-authorcredit' ) .' '. $author;
 211+ }
 212+
 213+ # Add summary if $wgTweetANewText['EditSummary'] is true and summary text is entered
 214+ if ( $summary && $wgTweetANewText['EditSummary'] ) {
 215+ $tweet_text .= ' - ' . $summary;
 216+ }
 217+
 218+ # Calculate length of tweet factoring in longURL
 219+ if (strlen($finalurl) > 20) {
 220+ $tweet_text_count = (strlen($finalurl) - 20) + 140;
 221+ }
 222+ else {
 223+ $tweet_text_count = 140;
 224+ }
 225+
 226+ # Check if length of tweet is beyond 140 characters and shorten if necessary
 227+ if (strlen($tweet_text) > $tweet_text_count) {
 228+ $tweet_text = substr($tweet_text,0, $tweet_text_count);
 229+ }
 230+
144231 # 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 - }
 232+ $connection->request('POST',
 233+ $connection->url('1/statuses/update'),
 234+ array('status' => $tweet_text)
 235+ );
154236 return true;
155237 }
156 -}
 238+}
\ No newline at end of file
Index: trunk/extensions/TweetANew/TweetANew.i18n.php
@@ -7,12 +7,60 @@
88 */
99
1010 $messages = array();
 11+
 12+/** English
 13+ * @author Gregory Varnum
 14+ */
1115 $messages['en'] = array(
12 - 'action-sendtotwitter' => 'Tweet about this edit',
13 - 'accesskey-sendtotwitter' => 'e',
14 - 'tooltip-sendtotwitter' => 'Send information about this edit to Twitter [alt-e]',
15 - 'sendtotwitter-message1' => 'Looks like somebody updated $1 at $2 - $3',
16 - 'sendtotwitter-message2' => '$1 was recently changed: $2 - $3',
17 - 'sendtotwitter-message3' => 'Check out $2 it has some new content on $1 - $3',
18 -
 16+ 'tweetanew-desc' => 'Tweets when an article is created or edited. Depending on preferences set for the entire wiki, either automatically or from the edit page.',
 17+ 'action-sendtotwitter' => 'Tweet about this edit',
 18+ 'accesskey-sendtotwitter' => 'e',
 19+ 'tooltip-sendtotwitter' => 'Send information about this edit to Twitter [alt-e]',
 20+ 'tweetanew-minoredit' => 'm',
 21+ 'tweetanew-authorcredit' => 'by',
 22+ 'tweetanew-newdefault' => 'NEW ARTICLE: $1 - $2',
 23+ 'tweetanew-new1' => 'Looks like $1 was created at $2',
 24+ 'tweetanew-new2' => '$1 was recently created at $2',
 25+ 'tweetanew-new3' => 'Check out $2 - it has a new article on $1',
 26+ 'tweetanew-editdefault' => 'UPDATED ARTICLE: $1 - $2',
 27+ 'tweetanew-edit1' => 'Looks like $1 was updated at $2',
 28+ 'tweetanew-edit2' => '$1 was recently changed at $2',
 29+ 'tweetanew-edit3' => 'Check out $2 - it has some new content on $1',
 30+);
 31+
 32+
 33+/** Message documentation (Message documentation)
 34+ * @author Gregory Varnum
 35+ */
 36+$messages['qqq'] = array(
 37+ 'tweetanew-desc' => '{{desc}}',
 38+ 'action-sendtotwitter' => 'Used in editpage as description for option to tweet, if auto-tweet is disabled for that type of article (edit or new)',
 39+ 'accesskey-sendtotwitter' => 'Access key used for option to tweet from editpage, if otherwise enabled',
 40+ 'tooltip-sendtotwitter' => 'Tooltip describing option to tweet form editpage, if otherwise enabled',
 41+ 'tweetanew-minoredit' => 'Indicator used when edit is marked as minor, if minor edits are not already skipped - skip following indicator can be removed using MinorSpace setting',
 42+ 'tweetanew-authorcredit' => 'Used to provide credit to author of edit or new article',
 43+ 'tweetanew-newdefault' => 'Default tweet message used for new articles, if random messages are disabled. Parameters:
 44+* $1 is title of the new article
 45+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 46+ 'tweetanew-new1' => 'First random tweet message used for new articles, if random messages are enabled. Parameters:
 47+* $1 is title of the new article
 48+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 49+ 'tweetanew-new2' => 'Second random tweet message used for new articles, if random messages are enabled. Parameters:
 50+* $1 is title of the new article
 51+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 52+ 'tweetanew-new3' => 'Third random tweet message used for new articles, if random messages are enabled. Parameters:
 53+* $1 is title of the new article
 54+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 55+ 'tweetanew-editdefault' => 'Default tweet message used for edited articles, if random messages are disabled. Parameters:
 56+* $1 is title of the new article
 57+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 58+ 'tweetanew-edit1' => 'First random tweet message used for edited articles, if random messages are enabled. Parameters:
 59+* $1 is title of the new article
 60+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 61+ 'tweetanew-edit2' => 'Second random tweet message used for edited articles, if random messages are enabled. Parameters:
 62+* $1 is title of the new article
 63+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
 64+ 'tweetanew-edit3' => 'Third random tweet message used for edited articles, if random messages are enabled. Parameters:
 65+* $1 is title of the new article
 66+* $2 is the final URL of the new article - shortened if a service is enabled via this extension',
1967 );
\ No newline at end of file
Index: trunk/extensions/TweetANew/TweetANew.php
@@ -13,12 +13,13 @@
1414 * Version 1.0 and above based on merging extensions TweetANew v0.2 by Joachim De Schrijver, Wiki2twitter by Wendell Gaudencio,
1515 * SendToTwitter by Rohit Keshwani and SendToTwitter2 by Rohit Keshwani, Andrew Fitzgerald.
1616 *
17 - * Some code borrowed from the Mail2Facebook extension by Thiemo Schuff
 17+ * Some code inspired by the Mail2Facebook extension by Thiemo Schuff
1818 *
1919 * Use Matt Harris' OAuth library to make the connection
2020 * This lives at: https://github.com/themattharris/tmhOAuth
2121 * The most recent edition (as of this version's publish date) is included with this extension.
2222 *
 23+ * Thank you to Johnduhart, Reedy, and SPQRobin for feedback, bug reporting and cleaning up code
2324 * Thank you to Raymond and others mentioned in TweetANew.i18n.php for translation work
2425 *
2526 */
@@ -37,12 +38,12 @@
3839 * The following variables may be reset in your LocalSettings.php file.
3940 *
4041 * $wgTweetANewTweet['New']
41 - * - Tweet about new articles
 42+ * - Automatically tweet about new articles
4243 * Default is true
4344 * $wgTweetANewTweet['Edit']
44 - * - Tweet about articles when edited
 45+ * - Automatically tweet about articles when edited
4546 * Default is true
46 - * $wgTweetANewTweet['lessminold']
 47+ * $wgTweetANewTweet['LessMinutesOld']
4748 * - Minutes since last edit to wait before tweeting about a new edit
4849 * Default is 5
4950 * $wgTweetANewTweet['SkipMinor']
@@ -63,22 +64,47 @@
6465 * - If bitly link display is enabled, enter your bitly user account - signup at: http://bitly.com/a/sign_up
6566 * $wgTweetANewBitly['API']
6667 * - 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 = ''
 68+ * $wgTweetANewText['Minor']
 69+ * - Indicate in tweet if edit is marked as minor - only applies if $wgTweetANewTweet['SkipMinor'] = false
 70+ * Default is false
 71+ * $wgTweetANewText['MinorSpace']
 72+ * - Include a space after minor edit indicator - only applies if $wgTweetANewTweet['SkipMinor'] = false and $wgTweetANewText['Minor'] = true
 73+ * Default is true
 74+ * $wgTweetANewText['NewRandom']
 75+ * - Use a mix of random messages in body of tweets about new articles
 76+ * Default is true
 77+ * $wgTweetANewText['NewRandomMax']
 78+ * - Maximum number of random messages to use - set any additional (beyond 3) using [[MediaWiki:Tweetanew-new4]], [[MediaWiki:Tweetanew-new5]], etc.
 79+ * Default is 3
 80+ * $wgTweetANewText['NewAuthor']
 81+ * - Display the author of the new article
 82+ * Default is false
 83+ * $wgTweetANewText['NewSummary']
 84+ * - Display content entered into new article's summary box
 85+ * Default is false
 86+ * $wgTweetANewText['EditRandom']
 87+ * - Use a mix of random messages in body of tweets about article edits
 88+ * Default is true
 89+ * $wgTweetANewText['EditRandomMax']
 90+ * - Maximum number of random messages to use - set any additional (beyond 3) using [[MediaWiki:Tweetanew-edit4]], [[MediaWiki:Tweetanew-edit5]], etc.
 91+ * Default is 3
 92+ * $wgTweetANewText['EditAuthor']
 93+ * - Display the author of the edit
 94+ * Default is false
 95+ * $wgTweetANewText['EditSummary']
 96+ * - Display content entered into edit's summary box
 97+ * Default is false
 98+ * $wgTweetANewText['RealName']
 99+ * - Determine if user's real name will be displayed instead of their username
 100+ * Default is false
75101 *
76102 */
77103
78104 $wgTweetANewTweet = array(
79105 'New' => true,
80106 'Edit' => true,
81 - 'lessminold' => 5,
82 - 'SkipMinor' => true,
 107+ 'LessMinutesOld' => 5,
 108+ 'SkipMinor' => false,
83109 'Auto' => true,
84110 );
85111
@@ -96,8 +122,17 @@
97123 );
98124
99125 $wgTweetANewText = array(
100 - 'New' => '',
101 - 'Edit' => '',
 126+ 'Minor' => true, // Only applies if $wgTweetANewTweet['SkipMinor'] = false
 127+ 'MinorSpace' => true, // Only applies if $wgTweetANewTweet['SkipMinor'] = false and $wgTweetANewTweet['Minor'] = true
 128+ 'NewRandom' => true,
 129+ 'NewRandomMax' => 3,
 130+ 'NewAuthor' => true,
 131+ 'NewSummary' => true,
 132+ 'EditRandom' => true,
 133+ 'EditRandomMax' => 3,
 134+ 'EditAuthor' => true,
 135+ 'EditSummary' => true,
 136+ 'RealName' => true,
102137 );
103138
104139 /**
@@ -116,9 +151,9 @@
117152
118153 $wgExtensionCredits['other'][] = array(
119154 'name' => '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.',
 155+ 'version' => '1.0.20111228-experimental',
 156+ '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',
 157+ 'description' => 'Tweets when an article is created or edited. Depending on preferences set for the entire wiki, either automatically or from the edit page.',
123158 'url' => 'https://www.mediawiki.org/wiki/Extension:TweetANew',
124159 );
125160
@@ -128,4 +163,4 @@
129164 */
130165
131166 $wgHooks['ArticleInsertComplete'][] = 'TweetANew::TweetANewNewArticle';
132 -$wgHooks['ArticleSaveComplete'][] = 'TweetANew::TweetANewEditMade';
 167+$wgHooks['ArticleSaveComplete'][] = 'TweetANew::TweetANewEditMade';
\ No newline at end of file

Status & tagging log