Index: trunk/extensions/TweetANew/TweetANew.body.php |
— | — | @@ -11,45 +11,17 @@ |
12 | 12 | |
13 | 13 | // TweetANew |
14 | 14 | 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; |
25 | 15 | |
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 | | - |
43 | 16 | /** |
44 | 17 | * Function for tweeting new articles |
45 | 18 | * |
46 | 19 | * @param $article Article |
47 | 20 | * @param $user User |
48 | | - * @param $text |
49 | 21 | * @param $summary |
50 | 22 | * @return bool |
51 | 23 | */ |
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; |
54 | 26 | |
55 | 27 | # Check if $wgTweetANewTweet['New'] is enabled or the Tweet checkbox was selected on the edit page |
56 | 28 | if ( $wgRequest->getCheck( 'wpTweetANew' ) || $wgTweetANewTweet['New'] ) { |
— | — | @@ -61,19 +33,9 @@ |
62 | 34 | return true; |
63 | 35 | } |
64 | 36 | |
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 | | - |
73 | 37 | # Generate final url |
74 | 38 | $finalurl = self::make_final_url( |
75 | | - $article->getTitle()->getFullURL(), |
76 | | - $wgTweetANewBitly['Login'], |
77 | | - $wgTweetANewBitly['API'] |
| 39 | + $article->getTitle()->getFullURL() |
78 | 40 | ); |
79 | 41 | |
80 | 42 | # Generate $author based on $wgTweetANewText['RealName'] |
— | — | @@ -108,24 +70,13 @@ |
109 | 71 | if ( $summary && $wgTweetANewText['NewSummary'] ) { |
110 | 72 | $tweet_text .= ' - ' . $summary; |
111 | 73 | } |
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 |
129 | 79 | ); |
| 80 | + |
130 | 81 | } |
131 | 82 | return true; |
132 | 83 | } |
— | — | @@ -138,22 +89,30 @@ |
139 | 90 | * @param $text string |
140 | 91 | * @param $summary string |
141 | 92 | * @param $minoredit bool |
| 93 | + * @param $watchthis |
| 94 | + * @param $sectionanchor |
| 95 | + * @param $flags |
142 | 96 | * @param $revision |
| 97 | + * @param $status |
| 98 | + * @param $baseRevId |
143 | 99 | * @return bool |
144 | 100 | */ |
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; |
147 | 103 | |
148 | 104 | # Check if $wgTweetANewTweet['Edit'] is enabled or the Tweet checkbox was selected on the edit page |
149 | 105 | if ( $wgRequest->getCheck( 'wpTweetANewEdit' ) || $wgTweetANewTweet['Edit'] ) { |
150 | 106 | |
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 |
154 | 113 | ) { |
155 | 114 | return true; |
156 | 115 | } |
157 | | - |
| 116 | + |
158 | 117 | # Determine the time and date of last modification - skip if newer than $wgTweetANewTweet['LessMinutesOld'] setting |
159 | 118 | # ToDo - there must be a cleaner way of doing this |
160 | 119 | $dbr = wfGetDB( DB_SLAVE ); |
— | — | @@ -167,6 +126,10 @@ |
168 | 127 | foreach ( $res as $row ) { |
169 | 128 | $edittime[] = $row->rev_timestamp; |
170 | 129 | } |
| 130 | + $edittime = array(); |
| 131 | + foreach ( $res as $row ) { |
| 132 | + $edittime[] = $row->rev_timestamp; |
| 133 | + } |
171 | 134 | $edittimenow = mktime( substr( $edittime[0], 8, 2 ), substr( $edittime[0], 10, 2 ), |
172 | 135 | substr( $edittime[0], 12 ), substr( $edittime[0], 4, 2 ), substr( $edittime[0], 6, 2 ), |
173 | 136 | substr( $edittime[0], 0, 4 ) |
— | — | @@ -180,27 +143,9 @@ |
181 | 144 | return true; |
182 | 145 | } |
183 | 146 | |
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 | | - |
202 | 147 | # 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() |
205 | 150 | ); |
206 | 151 | |
207 | 152 | # Generate $author based on $wgTweetANewText['RealName'] |
— | — | @@ -243,27 +188,78 @@ |
244 | 189 | |
245 | 190 | # Add summary if $wgTweetANewText['EditSummary'] is true and summary text is entered |
246 | 191 | if ( $summary && $wgTweetANewText['EditSummary'] ) { |
247 | | - $tweet_text .= ' - ' . $summary; |
| 192 | + $tweet_text .= ' - ' . $edittime; |
| 193 | + #$tweet_text .= ' - ' . $summary; |
248 | 194 | } |
249 | 195 | |
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 | + } |
256 | 204 | |
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"; |
261 | 220 | |
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; |
267 | 225 | } |
| 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 | + ); |
268 | 264 | return true; |
269 | 265 | } |
270 | 266 | } |
Index: trunk/extensions/TweetANew/TweetANew.php |
— | — | @@ -3,7 +3,7 @@ |
4 | 4 | * MediaWiki extension to update a Twitter account each time an article is created or edited - depending on yours settings. |
5 | 5 | * Excludes files or content outside of article namespaces. |
6 | 6 | * 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 |
8 | 8 | * https://www.mediawiki.org/wiki/Extension:TweetANew |
9 | 9 | * |
10 | 10 | * @addtogroup Extensions |
— | — | @@ -19,8 +19,8 @@ |
20 | 20 | * This lives at: https://github.com/themattharris/tmhOAuth |
21 | 21 | * The most recent edition (as of this version's publish date) is included with this extension. |
22 | 22 | * |
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 |
25 | 25 | * |
26 | 26 | */ |
27 | 27 | |
— | — | @@ -48,7 +48,7 @@ |
49 | 49 | * Default is 5 |
50 | 50 | * $wgTweetANewTweet['SkipMinor'] |
51 | 51 | * - Skip minor edits |
52 | | - Default is true |
| 52 | + * Default is true |
53 | 53 | * $wgTweetANewText['Minor'] |
54 | 54 | * - Indicate in tweet if edit is marked as minor - only applies if $wgTweetANewTweet['SkipMinor'] = false |
55 | 55 | * Default is false |
— | — | @@ -166,7 +166,7 @@ |
167 | 167 | $wgExtensionCredits['other'][] = array( |
168 | 168 | 'path' => __FILE__, |
169 | 169 | 'name' => 'TweetANew', |
170 | | - 'version' => '1.0.20111228-experimental', |
| 170 | + 'version' => '1.0.20111229-beta', |
171 | 171 | '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', |
172 | 172 | 'descriptionmsg' => 'tweetanew-desc', |
173 | 173 | 'url' => 'https://www.mediawiki.org/wiki/Extension:TweetANew', |