r107625 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107624‎ | r107625 | r107626 >
Date:07:31, 30 December 2011
Author:varnent
Status:deferred (Comments)
Tags:
Comment:
now beta - cleaned up code - added to credits - time comparison code still needs revamp - ready for feedback and tweaks welcome
Modified paths:
  • /trunk/extensions/TweetANew/TweetANew.body.php (modified) (history)
  • /trunk/extensions/TweetANew/TweetANew.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TweetANew/TweetANew.body.php
@@ -11,45 +11,17 @@
1212
1313 // TweetANew
1414 class TweetANew {
15 - /**
16 - * Function for connecting to and preparing bitly
17 - *
18 - * @param $longurl
19 - * @param $login
20 - * @param $appkey
21 - * @return string
22 - */
23 - public static function make_final_url( $longurl, $login, $appkey ) {
24 - global $wgTweetANewBitly;
2515
26 - # Check setting to enable/disable use of bitly
27 - if ( $wgTweetANewBitly['Enable'] ) {
28 - # Generate url for bitly
29 - $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl="
30 - . urlencode( $longurl ) . "&login=" . $login
31 - . "&apiKey=" . $appkey . "&format=txt";
32 -
33 - # Get the url
34 - $response = file_get_contents( $bitly );
35 - } else {
36 - $response = $longurl;
37 - }
38 - return $response;
39 - }
40 -
41 - # ToDo - TweetANewNewEditOption function for when auto-tweet is disabled to display checkbox on edit/create page to tweet new/edited pages
42 -
4316 /**
4417 * Function for tweeting new articles
4518 *
4619 * @param $article Article
4720 * @param $user User
48 - * @param $text
4921 * @param $summary
5022 * @return bool
5123 */
52 - public static function TweetANewNewArticle( $article, $user, $text, $summary ) {
53 - global $wgTweetANewTweet, $wgTweetANewText, $wgTweetANewTwitter, $wgTweetANewBitly, $wgRequest;
 24+ public static function TweetANewNewArticle( $article, $user, $summary ) {
 25+ global $wgTweetANewTweet, $wgTweetANewText, $wgRequest;
5426
5527 # Check if $wgTweetANewTweet['New'] is enabled or the Tweet checkbox was selected on the edit page
5628 if ( $wgRequest->getCheck( 'wpTweetANew' ) || $wgTweetANewTweet['New'] ) {
@@ -61,19 +33,9 @@
6234 return true;
6335 }
6436
65 - # Make connection to Twitter
66 - $connection = new tmhOAuth( array(
67 - 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'],
68 - 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'],
69 - 'user_token' => $wgTweetANewTwitter['AccessToken'],
70 - 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
71 - ) );
72 -
7337 # Generate final url
7438 $finalurl = self::make_final_url(
75 - $article->getTitle()->getFullURL(),
76 - $wgTweetANewBitly['Login'],
77 - $wgTweetANewBitly['API']
 39+ $article->getTitle()->getFullURL()
7840 );
7941
8042 # Generate $author based on $wgTweetANewText['RealName']
@@ -108,24 +70,13 @@
10971 if ( $summary && $wgTweetANewText['NewSummary'] ) {
11072 $tweet_text .= ' - ' . $summary;
11173 }
112 -
113 - # Calculate length of tweet factoring in longURL
114 - if ( strlen( $finalurl ) > 20 ) {
115 - $tweet_text_count = ( strlen( $finalurl ) - 20 ) + 140;
116 - } else {
117 - $tweet_text_count = 140;
118 - }
119 -
120 - # Check if length of tweet is beyond 140 characters and shorten if necessary
121 - if ( strlen( $tweet_text ) > $tweet_text_count ) {
122 - $tweet_text = substr( $tweet_text, 0, $tweet_text_count );
123 - }
124 -
125 - # Make tweet message
126 - $connection->request( 'POST',
127 - $connection->url( '1/statuses/update' ),
128 - array( 'status' => $tweet_text )
 74+
 75+ # Call to function for assembling and trimming tweet (if necessary) - then connecting and sending tweet to Twitter
 76+ self::assemble_send_tweet(
 77+ $tweet_text,
 78+ $finalurl
12979 );
 80+
13081 }
13182 return true;
13283 }
@@ -138,22 +89,30 @@
13990 * @param $text string
14091 * @param $summary string
14192 * @param $minoredit bool
 93+ * @param $watchthis
 94+ * @param $sectionanchor
 95+ * @param $flags
14296 * @param $revision
 97+ * @param $status
 98+ * @param $baseRevId
14399 * @return bool
144100 */
145 - public static function TweetANewEditMade( &$article, &$user, $text, $summary, $minoredit, $revision ) {
146 - global $wgTweetANewTweet, $wgTweetANewText, $wgTweetANewTwitter, $wgTweetANewBitly, $wgRequest;
 101+ public static function TweetANewEditMade( &$article, &$user, $text, $summary, $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId ) {
 102+ global $wgTweetANewTweet, $wgTweetANewText, $wgRequest;
147103
148104 # Check if $wgTweetANewTweet['Edit'] is enabled or the Tweet checkbox was selected on the edit page
149105 if ( $wgRequest->getCheck( 'wpTweetANewEdit' ) || $wgTweetANewTweet['Edit'] ) {
150106
151 - # Check if page is in content namespace or if the Tweet checkbox was selected on the edit page
152 - if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() )
153 - && !$wgRequest->getCheck( 'wpTweetANewEdit' )
 107+ # Unless the tweet checkbox is selected, only proceeds if page is outside content namespace and if a minor edit, checks $wgTweetANewTweet['SkipMinor']
 108+ # Also prevents new articles from processing as TweetANewNewArticle function is used instead
 109+ if ( ( !MWNamespace::isContent( $article->getTitle()->getNamespace() )
 110+ || ( $minoredit !== 0 && $wgTweetANewTweet['SkipMinor'] )
 111+ && !$wgRequest->getCheck( 'wpTweetANewEdit' ) )
 112+ || $article->estimateRevisionCount() == 1
154113 ) {
155114 return true;
156115 }
157 -
 116+
158117 # Determine the time and date of last modification - skip if newer than $wgTweetANewTweet['LessMinutesOld'] setting
159118 # ToDo - there must be a cleaner way of doing this
160119 $dbr = wfGetDB( DB_SLAVE );
@@ -167,6 +126,10 @@
168127 foreach ( $res as $row ) {
169128 $edittime[] = $row->rev_timestamp;
170129 }
 130+ $edittime = array();
 131+ foreach ( $res as $row ) {
 132+ $edittime[] = $row->rev_timestamp;
 133+ }
171134 $edittimenow = mktime( substr( $edittime[0], 8, 2 ), substr( $edittime[0], 10, 2 ),
172135 substr( $edittime[0], 12 ), substr( $edittime[0], 4, 2 ), substr( $edittime[0], 6, 2 ),
173136 substr( $edittime[0], 0, 4 )
@@ -180,27 +143,9 @@
181144 return true;
182145 }
183146
184 - # Only proceed if this is not the first edit to the article, in which case it's new and TweetANewNewArticle is used instead
185 - if ( !$article->getTitle()->exists() ) {
186 - return true;
187 - }
188 -
189 - # Check $wgTweetANewTweet['SkipMinor'] setting to see if minor edits should be skipped
190 - if ( $minoredit !== 0 && $wgTweetANewTweet['SkipMinor'] ) {
191 - return true;
192 - }
193 -
194 - # Make connection to Twitter
195 - $connection = new tmhOAuth( array(
196 - 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'],
197 - 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'],
198 - 'user_token' => $wgTweetANewTwitter['AccessToken'],
199 - 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
200 - ) );
201 -
202147 # Generate final url
203 - $finalurl = self::make_final_url( $article->getTitle()->getFullURL(), $wgTweetANewBitly['Login'],
204 - $wgTweetANewBitly['API']
 148+ $finalurl = self::make_final_url(
 149+ $article->getTitle()->getFullURL()
205150 );
206151
207152 # Generate $author based on $wgTweetANewText['RealName']
@@ -243,27 +188,78 @@
244189
245190 # Add summary if $wgTweetANewText['EditSummary'] is true and summary text is entered
246191 if ( $summary && $wgTweetANewText['EditSummary'] ) {
247 - $tweet_text .= ' - ' . $summary;
 192+ $tweet_text .= ' - ' . $edittime;
 193+ #$tweet_text .= ' - ' . $summary;
248194 }
249195
250 - # Calculate length of tweet factoring in longURL
251 - if ( strlen( $finalurl ) > 20 ) {
252 - $tweet_text_count = ( strlen( $finalurl ) - 20 ) + 140;
253 - } else {
254 - $tweet_text_count = 140;
255 - }
 196+ # Call to function for assembling and trimming tweet (if necessary) - then connecting and sending tweet to Twitter
 197+ self::assemble_send_tweet(
 198+ $tweet_text,
 199+ $finalurl
 200+ );
 201+ }
 202+ return true;
 203+ }
256204
257 - # Check if length of tweet is beyond 140 characters and shorten if necessary
258 - if ( strlen( $tweet_text ) > $tweet_text_count ) {
259 - $tweet_text = substr( $tweet_text, 0, $tweet_text_count );
260 - }
 205+ /**
 206+ * Function for connecting to and preparing url for shortening service - or leaving for t.co shortening if none are enabled
 207+ *
 208+ * @param $longurl
 209+ * @return string
 210+ */
 211+ public static function make_final_url( $longurl ) {
 212+ global $wgTweetANewBitly;
 213+
 214+ # Check setting to enable/disable use of bitly
 215+ if ( $wgTweetANewBitly['Enable'] ) {
 216+ # Generate url for bitly
 217+ $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl="
 218+ . urlencode( $longurl ) . "&login=" . $wgTweetANewBitly['Login']
 219+ . "&apiKey=" . $wgTweetANewBitly['API'] . "&format=txt";
261220
262 - # Make tweet message
263 - $connection->request( 'POST',
264 - $connection->url( '1/statuses/update' ),
265 - array( 'status' => $tweet_text )
266 - );
 221+ # Get the url
 222+ $response = file_get_contents( $bitly );
 223+ } else {
 224+ $response = $longurl;
267225 }
 226+ return $response;
 227+ }
 228+
 229+ /**
 230+ * Function for connecting to and preparing tweet for Twitter
 231+ *
 232+ * @param $tweet_text
 233+ * @param $finalurl
 234+ * @return bool
 235+ */
 236+ public static function assemble_send_tweet( $tweet_text, $finalurl ) {
 237+ global $wgTweetANewTwitter;
 238+
 239+ # Calculate length of tweet factoring in longURL
 240+ if ( strlen( $finalurl ) > 20 ) {
 241+ $tweet_text_count = ( strlen( $finalurl ) - 20 ) + 140;
 242+ } else {
 243+ $tweet_text_count = 140;
 244+ }
 245+
 246+ # Check if length of tweet is beyond 140 characters and shorten if necessary
 247+ if ( strlen( $tweet_text ) > $tweet_text_count ) {
 248+ $tweet_text = substr( $tweet_text, 0, $tweet_text_count );
 249+ }
 250+
 251+ # Make connection to Twitter
 252+ $connection = new tmhOAuth( array(
 253+ 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'],
 254+ 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'],
 255+ 'user_token' => $wgTweetANewTwitter['AccessToken'],
 256+ 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'],
 257+ ) );
 258+
 259+ # Make tweet message
 260+ $connection->request( 'POST',
 261+ $connection->url( '1/statuses/update' ),
 262+ array( 'status' => $tweet_text )
 263+ );
268264 return true;
269265 }
270266 }
Index: trunk/extensions/TweetANew/TweetANew.php
@@ -3,7 +3,7 @@
44 * MediaWiki extension to update a Twitter account each time an article is created or edited - depending on yours settings.
55 * Excludes files or content outside of article namespaces.
66 * Optional use of bit.ly to shorten and track URLs.
7 - * Installation instructions can be found on
 7+ * Installation and configuration instructions can be found on
88 * https://www.mediawiki.org/wiki/Extension:TweetANew
99 *
1010 * @addtogroup Extensions
@@ -19,8 +19,8 @@
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
24 - * Thank you to Raymond and others mentioned in TweetANew.i18n.php for translation work
 23+ * Thank you to Johnduhart, Reedy, SPQRobin and krinkle for feedback, bug reporting and cleaning up code
 24+ * Thank you to Raymond, Crochet.david, Gomoko, Toliño, Bjankuloski06, Inholland, SPQRobin and others mentioned in TweetANew.i18n.php for translation work
2525 *
2626 */
2727
@@ -48,7 +48,7 @@
4949 * Default is 5
5050 * $wgTweetANewTweet['SkipMinor']
5151 * - Skip minor edits
52 - Default is true
 52+ * Default is true
5353 * $wgTweetANewText['Minor']
5454 * - Indicate in tweet if edit is marked as minor - only applies if $wgTweetANewTweet['SkipMinor'] = false
5555 * Default is false
@@ -166,7 +166,7 @@
167167 $wgExtensionCredits['other'][] = array(
168168 'path' => __FILE__,
169169 'name' => 'TweetANew',
170 - 'version' => '1.0.20111228-experimental',
 170+ 'version' => '1.0.20111229-beta',
171171 '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',
172172 'descriptionmsg' => 'tweetanew-desc',
173173 'url' => 'https://www.mediawiki.org/wiki/Extension:TweetANew',

Follow-up revisions

RevisionCommit summaryAuthorDate
r107630improvements to code and cleanup of failed merge to svn per feedback from Nik...varnent10:01, 30 December 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:58, 30 December 2011
 			foreach ( $res as $row ) {
 				$edittime[] = $row->rev_timestamp;
 			}
+			$edittime = array();
+			foreach ( $res as $row ) {
+				$edittime[] = $row->rev_timestamp;
+			}

Looks like broken merge.

There are some very long lines in this commit.

+			$response = file_get_contents( $bitly );

We have Http::get/post for doing that. And the function name and comment seem to be outdated.


+			$tweet_text = substr( $tweet_text, 0, $tweet_text_count );

Use $language::truncate() to do it in multibyte safe way.

Status & tagging log