r107612 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107611‎ | r107612 | r107613 >
Date:01:17, 30 December 2011
Author:johnduhart
Status:ok
Tags:
Comment:
Quick 10 minute cleanup
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
@@ -1,5 +1,7 @@
22 <?php
3 -if (!defined('MEDIAWIKI')) die();
 3+if ( !defined( 'MEDIAWIKI' ) ) {
 4+ die();
 5+}
46 /**
57 * Class file for the TweetANew extension
68 *
@@ -17,17 +19,19 @@
1820 * @param $appkey
1921 * @return string
2022 */
21 - public static function make_final_url($longurl,$login,$appkey) {
 23+ public static function make_final_url( $longurl, $login, $appkey ) {
2224 global $wgTweetANewBitly;
2325
2426 # Check setting to enable/disable use of bitly
25 - if ( $wgTweetANewBitly['Enable'] ) {
 27+ if ( $wgTweetANewBitly['Enable'] ) {
2628 # 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+
2833 # Get the url
29 - $response = file_get_contents($bitly);
30 - }
31 - else {
 34+ $response = file_get_contents( $bitly );
 35+ } else {
3236 $response = $longurl;
3337 }
3438 return $response;
@@ -44,76 +48,85 @@
4549 * @param $summary
4650 * @return bool
4751 */
48 - public static function TweetANewNewArticle($article, $user, $text, $summary){
 52+ public static function TweetANewNewArticle( $article, $user, $text, $summary ) {
4953 global $wgTweetANewTweet, $wgTweetANewText, $wgTweetANewTwitter, $wgTweetANewBitly, $wgRequest;
5054
5155 # 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+
5458 # 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+
5765 # 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'],
6169 '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+
6674 # 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+
6981 # Generate $author based on $wgTweetANewText['RealName']
7082 if ( $wgTweetANewText['RealName'] ) {
7183 $author = $user->getRealName();
72 - }
73 - else {
 84+ } else {
7485 $author = $user->getName();
7586 }
76 -
 87+
7788 # Generate a random tweet texts based if $wgTweetANewText['NewRandom'] is true
78 - if ( $wgTweetANewText['NewRandom'] ) {
 89+ if ( $wgTweetANewText['NewRandom'] ) {
7990 # Setup switcher using max number set by $wgTweetANewText['NewRandomMax']
8091 $switcher = rand( 1, $wgTweetANewText['NewRandomMax'] );
8192 # 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 {
8597 # 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+ );
87101 $tweet_text .= $tweet_body;
88102 }
89 -
 103+
90104 # Add author info if $wgTweetANewText['NewAuthor'] is true
91105 if ( $wgTweetANewText['NewAuthor'] ) {
92 - $tweet_text .= ' '. wfMsg( 'tweetanew-authorcredit' ) .' '. $author;
 106+ $tweet_text .= ' ' . wfMsg( 'tweetanew-authorcredit' ) . ' ' . $author;
93107 }
94 -
 108+
95109 # Add summary if $wgTweetANewText['NewSummary'] is true and summary text is entered
96110 if ( $summary && $wgTweetANewText['NewSummary'] ) {
97111 $tweet_text .= ' - ' . $summary;
98112 }
99 -
 113+
100114 # 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 {
105118 $tweet_text_count = 140;
106119 }
107 -
 120+
108121 # 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 );
111124 }
112 -
 125+
113126 # 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+ );
118131 }
119132 return true;
120133 }
@@ -129,51 +142,76 @@
130143 * @param $revision
131144 * @return bool
132145 */
133 - public static function TweetANewEditMade(&$article, &$user, $text, $summary, $minoredit, $revision){
 146+ public static function TweetANewEditMade( &$article, &$user, $text, $summary, $minoredit, $revision ) {
134147 global $wgTweetANewTweet, $wgTweetANewText, $wgTweetANewTwitter, $wgTweetANewBitly, $wgRequest;
135148
136149 # 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+
139152 # 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+
142159 # Determine the time and date of last modification - skip if newer than $wgTweetANewTweet['LessMinutesOld'] setting
143160 # ToDo - there must be a cleaner way of doing this
144161 $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+ );
149179 $edittimediv = $edittimenow - $edittimelast;
150 - if ($edittimediv < ($wgTweetANewTweet['LessMinutesOld'] * 60) ) return true;
151 -
 180+ if ( $edittimediv < ( $wgTweetANewTweet['LessMinutesOld'] * 60 ) ) {
 181+ return true;
 182+ }
 183+
152184 # 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+
155189 # 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+
158194 # 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'],
162199 '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+
167204 # 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+
170209 # Generate $author based on $wgTweetANewText['RealName']
171210 if ( $wgTweetANewText['RealName'] ) {
172211 $author = $user->getRealName();
173 - }
174 - else {
 212+ } else {
175213 $author = $user->getName();
176214 }
177 -
 215+
178216 # Add prefix indication that edit is minor if $wgTweetANewText['Minor'] is true and !$wgTweetANewTweet['SkipMinor'] is false
179217 if ( $minoredit !== 0 && $wgTweetANewText['Minor'] ) {
180218 $tweet_text = wfMsg( 'tweetanew-minoredit' );
@@ -182,47 +220,49 @@
183221 $tweet_text .= '&nbsp;';
184222 }
185223 }
186 -
 224+
187225 # Generate a random tweet texts based if $wgTweetANewText['EditRandom'] is true
188 - if ( $wgTweetANewText['EditRandom'] ) {
 226+ if ( $wgTweetANewText['EditRandom'] ) {
189227 # Setup switcher using max number set by $wgTweetANewText['EditRandomMax']
190228 $switcher = rand( 1, $wgTweetANewText['EditRandomMax'] );
191229 # 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 {
195234 # 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+ );
197238 $tweet_text .= $tweet_body;
198239 }
199 -
 240+
200241 # Add author info if $wgTweetANewText['EditAuthor'] is true
201242 if ( $wgTweetANewText['EditAuthor'] ) {
202 - $tweet_text .= ' '. wfMsg( 'tweetanew-authorcredit' ) .' '. $author;
 243+ $tweet_text .= ' ' . wfMsg( 'tweetanew-authorcredit' ) . ' ' . $author;
203244 }
204 -
 245+
205246 # Add summary if $wgTweetANewText['EditSummary'] is true and summary text is entered
206247 if ( $summary && $wgTweetANewText['EditSummary'] ) {
207248 $tweet_text .= ' - ' . $summary;
208249 }
209 -
 250+
210251 # 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 {
215255 $tweet_text_count = 140;
216256 }
217 -
 257+
218258 # 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 );
221261 }
222 -
 262+
223263 # 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 )
227267 );
228268 }
229269 return true;
Index: trunk/extensions/TweetANew/TweetANew.php
@@ -27,7 +27,7 @@
2828 /**
2929 * Exit if called outside of MediaWiki
3030 */
31 - if( !defined( 'MEDIAWIKI' ) ) {
 31+if( !defined( 'MEDIAWIKI' ) ) {
3232 echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
3333 die( 1 );
3434 }
@@ -201,7 +201,6 @@
202202 Xml::check( 'wpTweetANewEdit', $wgTweetANewEditpage['Checked'], $attribs ) .
203203 "&nbsp;<label for='wpTweetANewEdit' title='". wfMsg('tweetanew-edittooltip')."'>".wfMsg('tweetanew-editaction')."</label>";
204204 }
205 -
206205 # Check if article is new - if checkboxes are enabled and if auto-tweets of new articles are disabled
207206 elseif ( $wgTweetANewEditpage['Enable'] && !$wgTweetANewTweet['New'] ) {
208207 $attribs = array(

Sign-offs

UserFlagDate
Nikerabbitinspected06:35, 30 December 2011

Status & tagging log