Index: trunk/extensions/TweetANew/TweetANew.body.php |
— | — | @@ -1,5 +1,7 @@ |
2 | 2 | <?php |
3 | | -if (!defined('MEDIAWIKI')) die(); |
| 3 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 4 | + die(); |
| 5 | +} |
4 | 6 | /** |
5 | 7 | * Class file for the TweetANew extension |
6 | 8 | * |
— | — | @@ -17,17 +19,19 @@ |
18 | 20 | * @param $appkey |
19 | 21 | * @return string |
20 | 22 | */ |
21 | | - public static function make_final_url($longurl,$login,$appkey) { |
| 23 | + public static function make_final_url( $longurl, $login, $appkey ) { |
22 | 24 | global $wgTweetANewBitly; |
23 | 25 | |
24 | 26 | # Check setting to enable/disable use of bitly |
25 | | - if ( $wgTweetANewBitly['Enable'] ) { |
| 27 | + if ( $wgTweetANewBitly['Enable'] ) { |
26 | 28 | # Generate url for bitly |
27 | | - $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl=".urlencode($longurl)."&login=".$login."&apiKey=".$appkey."&format=txt"; |
| 29 | + $bitly = "https://api-ssl.bitly.com/v3/shorten?longUrl=" |
| 30 | + . urlencode( $longurl ) . "&login=" . $login |
| 31 | + . "&apiKey=" . $appkey . "&format=txt"; |
| 32 | + |
28 | 33 | # Get the url |
29 | | - $response = file_get_contents($bitly); |
30 | | - } |
31 | | - else { |
| 34 | + $response = file_get_contents( $bitly ); |
| 35 | + } else { |
32 | 36 | $response = $longurl; |
33 | 37 | } |
34 | 38 | return $response; |
— | — | @@ -44,76 +48,85 @@ |
45 | 49 | * @param $summary |
46 | 50 | * @return bool |
47 | 51 | */ |
48 | | - public static function TweetANewNewArticle($article, $user, $text, $summary){ |
| 52 | + public static function TweetANewNewArticle( $article, $user, $text, $summary ) { |
49 | 53 | global $wgTweetANewTweet, $wgTweetANewText, $wgTweetANewTwitter, $wgTweetANewBitly, $wgRequest; |
50 | 54 | |
51 | 55 | # Check if $wgTweetANewTweet['New'] is enabled or the Tweet checkbox was selected on the edit page |
52 | | - if ( $wgRequest->getCheck('wpTweetANew') || $wgTweetANewTweet['New'] ) { |
53 | | - |
| 56 | + if ( $wgRequest->getCheck( 'wpTweetANew' ) || $wgTweetANewTweet['New'] ) { |
| 57 | + |
54 | 58 | # Check if page is in content namespace or if the Tweet checkbox was selected on the edit page |
55 | | - if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() ) && !$wgRequest->getCheck('wpTweetANew') ) return true; |
56 | | - |
| 59 | + if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() ) |
| 60 | + && !$wgRequest->getCheck( 'wpTweetANew' ) |
| 61 | + ) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
57 | 65 | # Make connection to Twitter |
58 | | - require_once('lib/tmhOAuth.php'); // include connection |
59 | | - $connection = new tmhOAuth(array( |
60 | | - 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'], |
| 66 | + require_once( 'lib/tmhOAuth.php' ); // include connection |
| 67 | + $connection = new tmhOAuth( array( |
| 68 | + 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'], |
61 | 69 | 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'], |
62 | | - 'user_token' => $wgTweetANewTwitter['AccessToken'], |
63 | | - 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'], |
64 | | - )); |
65 | | - |
| 70 | + 'user_token' => $wgTweetANewTwitter['AccessToken'], |
| 71 | + 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'], |
| 72 | + ) ); |
| 73 | + |
66 | 74 | # Generate final url |
67 | | - $finalurl = self::make_final_url($article->getTitle()->getFullURL(),$wgTweetANewBitly['Login'],$wgTweetANewBitly['API']); |
68 | | - |
| 75 | + $finalurl = self::make_final_url( |
| 76 | + $article->getTitle()->getFullURL(), |
| 77 | + $wgTweetANewBitly['Login'], |
| 78 | + $wgTweetANewBitly['API'] |
| 79 | + ); |
| 80 | + |
69 | 81 | # Generate $author based on $wgTweetANewText['RealName'] |
70 | 82 | if ( $wgTweetANewText['RealName'] ) { |
71 | 83 | $author = $user->getRealName(); |
72 | | - } |
73 | | - else { |
| 84 | + } else { |
74 | 85 | $author = $user->getName(); |
75 | 86 | } |
76 | | - |
| 87 | + |
77 | 88 | # Generate a random tweet texts based if $wgTweetANewText['NewRandom'] is true |
78 | | - if ( $wgTweetANewText['NewRandom'] ) { |
| 89 | + if ( $wgTweetANewText['NewRandom'] ) { |
79 | 90 | # Setup switcher using max number set by $wgTweetANewText['NewRandomMax'] |
80 | 91 | $switcher = rand( 1, $wgTweetANewText['NewRandomMax'] ); |
81 | 92 | # Parse random text |
82 | | - $tweet_text .= wfMsg( 'tweetanew-new' . $switcher, array ( $article->getTitle()->getText(), $finalurl ) ); |
83 | | - } |
84 | | - else { |
| 93 | + $tweet_text .= wfMsg( 'tweetanew-new' . $switcher, |
| 94 | + array( $article->getTitle()->getText(), $finalurl ) |
| 95 | + ); |
| 96 | + } else { |
85 | 97 | # Use default tweet message format |
86 | | - $tweet_body = wfMsg( 'tweetanew-newdefault', array ( $article->getTitle()->getText(), $finalurl ) ); |
| 98 | + $tweet_body = wfMsg( 'tweetanew-newdefault', |
| 99 | + array( $article->getTitle()->getText(), $finalurl ) |
| 100 | + ); |
87 | 101 | $tweet_text .= $tweet_body; |
88 | 102 | } |
89 | | - |
| 103 | + |
90 | 104 | # Add author info if $wgTweetANewText['NewAuthor'] is true |
91 | 105 | if ( $wgTweetANewText['NewAuthor'] ) { |
92 | | - $tweet_text .= ' '. wfMsg( 'tweetanew-authorcredit' ) .' '. $author; |
| 106 | + $tweet_text .= ' ' . wfMsg( 'tweetanew-authorcredit' ) . ' ' . $author; |
93 | 107 | } |
94 | | - |
| 108 | + |
95 | 109 | # Add summary if $wgTweetANewText['NewSummary'] is true and summary text is entered |
96 | 110 | if ( $summary && $wgTweetANewText['NewSummary'] ) { |
97 | 111 | $tweet_text .= ' - ' . $summary; |
98 | 112 | } |
99 | | - |
| 113 | + |
100 | 114 | # Calculate length of tweet factoring in longURL |
101 | | - if (strlen($finalurl) > 20) { |
102 | | - $tweet_text_count = (strlen($finalurl) - 20) + 140; |
103 | | - } |
104 | | - else { |
| 115 | + if ( strlen( $finalurl ) > 20 ) { |
| 116 | + $tweet_text_count = ( strlen( $finalurl ) - 20 ) + 140; |
| 117 | + } else { |
105 | 118 | $tweet_text_count = 140; |
106 | 119 | } |
107 | | - |
| 120 | + |
108 | 121 | # Check if length of tweet is beyond 140 characters and shorten if necessary |
109 | | - if (strlen($tweet_text) > $tweet_text_count) { |
110 | | - $tweet_text = substr($tweet_text,0, $tweet_text_count); |
| 122 | + if ( strlen( $tweet_text ) > $tweet_text_count ) { |
| 123 | + $tweet_text = substr( $tweet_text, 0, $tweet_text_count ); |
111 | 124 | } |
112 | | - |
| 125 | + |
113 | 126 | # Make tweet message |
114 | | - $connection->request('POST', |
115 | | - $connection->url('1/statuses/update'), |
116 | | - array('status' => $tweet_text) |
117 | | - ); |
| 127 | + $connection->request( 'POST', |
| 128 | + $connection->url( '1/statuses/update' ), |
| 129 | + array( 'status' => $tweet_text ) |
| 130 | + ); |
118 | 131 | } |
119 | 132 | return true; |
120 | 133 | } |
— | — | @@ -129,51 +142,76 @@ |
130 | 143 | * @param $revision |
131 | 144 | * @return bool |
132 | 145 | */ |
133 | | - public static function TweetANewEditMade(&$article, &$user, $text, $summary, $minoredit, $revision){ |
| 146 | + public static function TweetANewEditMade( &$article, &$user, $text, $summary, $minoredit, $revision ) { |
134 | 147 | global $wgTweetANewTweet, $wgTweetANewText, $wgTweetANewTwitter, $wgTweetANewBitly, $wgRequest; |
135 | 148 | |
136 | 149 | # Check if $wgTweetANewTweet['Edit'] is enabled or the Tweet checkbox was selected on the edit page |
137 | | - if ( $wgRequest->getCheck('wpTweetANewEdit') || $wgTweetANewTweet['Edit'] ) { |
138 | | - |
| 150 | + if ( $wgRequest->getCheck( 'wpTweetANewEdit' ) || $wgTweetANewTweet['Edit'] ) { |
| 151 | + |
139 | 152 | # Check if page is in content namespace or if the Tweet checkbox was selected on the edit page |
140 | | - if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() ) && !$wgRequest->getCheck('wpTweetANewEdit') ) return true; |
141 | | - |
| 153 | + if ( !MWNamespace::isContent( $article->getTitle()->getNamespace() ) |
| 154 | + && !$wgRequest->getCheck( 'wpTweetANewEdit' ) |
| 155 | + ) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
142 | 159 | # Determine the time and date of last modification - skip if newer than $wgTweetANewTweet['LessMinutesOld'] setting |
143 | 160 | # ToDo - there must be a cleaner way of doing this |
144 | 161 | $dbr = wfGetDB( DB_SLAVE ); |
145 | | - $res = $dbr->select('revision', array( 'rev_timestamp' ), array('rev_page = '.$article->getID()), $fname = 'Database::select', $options = array( 'ORDER BY' => 'rev_id DESC' , 'LIMIT' => '2' )); |
146 | | - foreach($res as $row) $edittime[] = $row->rev_timestamp; |
147 | | - $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)); |
148 | | - $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)); |
| 162 | + $res = $dbr->select( 'revision', |
| 163 | + array( 'rev_timestamp' ), |
| 164 | + array( 'rev_page' => $article->getID() ), |
| 165 | + __METHOD__, |
| 166 | + array( 'ORDER BY' => 'rev_id DESC', 'LIMIT' => '2' ) |
| 167 | + ); |
| 168 | + foreach ( $res as $row ) { |
| 169 | + $edittime[] = $row->rev_timestamp; |
| 170 | + } |
| 171 | + $edittimenow = mktime( substr( $edittime[0], 8, 2 ), substr( $edittime[0], 10, 2 ), |
| 172 | + substr( $edittime[0], 12 ), substr( $edittime[0], 4, 2 ), substr( $edittime[0], 6, 2 ), |
| 173 | + substr( $edittime[0], 0, 4 ) |
| 174 | + ); |
| 175 | + $edittimelast = mktime( substr( $edittime[1], 8, 2 ), substr( $edittime[1], 10, 2 ), |
| 176 | + substr( $edittime[1], 12 ), substr( $edittime[1], 4, 2 ), substr( $edittime[1], 6, 2 ), |
| 177 | + substr( $edittime[1], 0, 4 ) |
| 178 | + ); |
149 | 179 | $edittimediv = $edittimenow - $edittimelast; |
150 | | - if ($edittimediv < ($wgTweetANewTweet['LessMinutesOld'] * 60) ) return true; |
151 | | - |
| 180 | + if ( $edittimediv < ( $wgTweetANewTweet['LessMinutesOld'] * 60 ) ) { |
| 181 | + return true; |
| 182 | + } |
| 183 | + |
152 | 184 | # Only proceed if this is not the first edit to the article, in which case it's new and TweetANewNewArticle is used instead |
153 | | - if ( !$article->mTitle->exists() ) return true; |
154 | | - |
| 185 | + if ( !$article->mTitle->exists() ) { |
| 186 | + return true; |
| 187 | + } |
| 188 | + |
155 | 189 | # Check $wgTweetANewTweet['SkipMinor'] setting to see if minor edits should be skipped |
156 | | - if ( $minoredit !== 0 && $wgTweetANewTweet['SkipMinor'] ) return true; |
157 | | - |
| 190 | + if ( $minoredit !== 0 && $wgTweetANewTweet['SkipMinor'] ) { |
| 191 | + return true; |
| 192 | + } |
| 193 | + |
158 | 194 | # Make connection to Twitter |
159 | | - require_once('lib/tmhOAuth.php'); // include connection |
160 | | - $connection = new tmhOAuth( array ( |
161 | | - 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'], |
| 195 | + # TODO: Can this be autoloaded? |
| 196 | + require_once( 'lib/tmhOAuth.php' ); // include connection |
| 197 | + $connection = new tmhOAuth( array( |
| 198 | + 'consumer_key' => $wgTweetANewTwitter['ConsumerKey'], |
162 | 199 | 'consumer_secret' => $wgTweetANewTwitter['ConsumerSecret'], |
163 | | - 'user_token' => $wgTweetANewTwitter['AccessToken'], |
164 | | - 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'], |
165 | | - ) ); |
166 | | - |
| 200 | + 'user_token' => $wgTweetANewTwitter['AccessToken'], |
| 201 | + 'user_secret' => $wgTweetANewTwitter['AccessTokenSecret'], |
| 202 | + ) ); |
| 203 | + |
167 | 204 | # Generate final url |
168 | | - $finalurl = self::make_final_url($article->getTitle()->getFullURL(),$wgTweetANewBitly['Login'],$wgTweetANewBitly['API']); |
169 | | - |
| 205 | + $finalurl = self::make_final_url( $article->getTitle()->getFullURL(), $wgTweetANewBitly['Login'], |
| 206 | + $wgTweetANewBitly['API'] |
| 207 | + ); |
| 208 | + |
170 | 209 | # Generate $author based on $wgTweetANewText['RealName'] |
171 | 210 | if ( $wgTweetANewText['RealName'] ) { |
172 | 211 | $author = $user->getRealName(); |
173 | | - } |
174 | | - else { |
| 212 | + } else { |
175 | 213 | $author = $user->getName(); |
176 | 214 | } |
177 | | - |
| 215 | + |
178 | 216 | # Add prefix indication that edit is minor if $wgTweetANewText['Minor'] is true and !$wgTweetANewTweet['SkipMinor'] is false |
179 | 217 | if ( $minoredit !== 0 && $wgTweetANewText['Minor'] ) { |
180 | 218 | $tweet_text = wfMsg( 'tweetanew-minoredit' ); |
— | — | @@ -182,47 +220,49 @@ |
183 | 221 | $tweet_text .= ' '; |
184 | 222 | } |
185 | 223 | } |
186 | | - |
| 224 | + |
187 | 225 | # Generate a random tweet texts based if $wgTweetANewText['EditRandom'] is true |
188 | | - if ( $wgTweetANewText['EditRandom'] ) { |
| 226 | + if ( $wgTweetANewText['EditRandom'] ) { |
189 | 227 | # Setup switcher using max number set by $wgTweetANewText['EditRandomMax'] |
190 | 228 | $switcher = rand( 1, $wgTweetANewText['EditRandomMax'] ); |
191 | 229 | # Parse random text |
192 | | - $tweet_text .= wfMsg( 'tweetanew-edit' . $switcher, array ( $article->getTitle()->getText(), $finalurl ) ); |
193 | | - } |
194 | | - else { |
| 230 | + $tweet_text .= wfMsg( 'tweetanew-edit' . $switcher, |
| 231 | + array( $article->getTitle()->getText(), $finalurl ) |
| 232 | + ); |
| 233 | + } else { |
195 | 234 | # Use default tweet message format |
196 | | - $tweet_body = wfMsg( 'tweetanew-editdefault', array ( $article->getTitle()->getText(), $finalurl ) ); |
| 235 | + $tweet_body = wfMsg( 'tweetanew-editdefault', |
| 236 | + array( $article->getTitle()->getText(), $finalurl ) |
| 237 | + ); |
197 | 238 | $tweet_text .= $tweet_body; |
198 | 239 | } |
199 | | - |
| 240 | + |
200 | 241 | # Add author info if $wgTweetANewText['EditAuthor'] is true |
201 | 242 | if ( $wgTweetANewText['EditAuthor'] ) { |
202 | | - $tweet_text .= ' '. wfMsg( 'tweetanew-authorcredit' ) .' '. $author; |
| 243 | + $tweet_text .= ' ' . wfMsg( 'tweetanew-authorcredit' ) . ' ' . $author; |
203 | 244 | } |
204 | | - |
| 245 | + |
205 | 246 | # Add summary if $wgTweetANewText['EditSummary'] is true and summary text is entered |
206 | 247 | if ( $summary && $wgTweetANewText['EditSummary'] ) { |
207 | 248 | $tweet_text .= ' - ' . $summary; |
208 | 249 | } |
209 | | - |
| 250 | + |
210 | 251 | # Calculate length of tweet factoring in longURL |
211 | | - if (strlen($finalurl) > 20) { |
212 | | - $tweet_text_count = (strlen($finalurl) - 20) + 140; |
213 | | - } |
214 | | - else { |
| 252 | + if ( strlen( $finalurl ) > 20 ) { |
| 253 | + $tweet_text_count = ( strlen( $finalurl ) - 20 ) + 140; |
| 254 | + } else { |
215 | 255 | $tweet_text_count = 140; |
216 | 256 | } |
217 | | - |
| 257 | + |
218 | 258 | # Check if length of tweet is beyond 140 characters and shorten if necessary |
219 | | - if (strlen($tweet_text) > $tweet_text_count) { |
220 | | - $tweet_text = substr($tweet_text,0, $tweet_text_count); |
| 259 | + if ( strlen( $tweet_text ) > $tweet_text_count ) { |
| 260 | + $tweet_text = substr( $tweet_text, 0, $tweet_text_count ); |
221 | 261 | } |
222 | | - |
| 262 | + |
223 | 263 | # Make tweet message |
224 | | - $connection->request('POST', |
225 | | - $connection->url('1/statuses/update'), |
226 | | - array('status' => $tweet_text) |
| 264 | + $connection->request( 'POST', |
| 265 | + $connection->url( '1/statuses/update' ), |
| 266 | + array( 'status' => $tweet_text ) |
227 | 267 | ); |
228 | 268 | } |
229 | 269 | return true; |
Index: trunk/extensions/TweetANew/TweetANew.php |
— | — | @@ -27,7 +27,7 @@ |
28 | 28 | /** |
29 | 29 | * Exit if called outside of MediaWiki |
30 | 30 | */ |
31 | | - if( !defined( 'MEDIAWIKI' ) ) { |
| 31 | +if( !defined( 'MEDIAWIKI' ) ) { |
32 | 32 | echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" ); |
33 | 33 | die( 1 ); |
34 | 34 | } |
— | — | @@ -201,7 +201,6 @@ |
202 | 202 | Xml::check( 'wpTweetANewEdit', $wgTweetANewEditpage['Checked'], $attribs ) . |
203 | 203 | " <label for='wpTweetANewEdit' title='". wfMsg('tweetanew-edittooltip')."'>".wfMsg('tweetanew-editaction')."</label>"; |
204 | 204 | } |
205 | | - |
206 | 205 | # Check if article is new - if checkboxes are enabled and if auto-tweets of new articles are disabled |
207 | 206 | elseif ( $wgTweetANewEditpage['Enable'] && !$wgTweetANewTweet['New'] ) { |
208 | 207 | $attribs = array( |