FastLizard4 has submitted this change and it was merged.

Change subject: Remove globals from special page execution
......................................................................


Remove globals from special page execution

Finally, all of the globals in the CommunityTwitter special page are
gone, replaced with variables retrieved from ContextSource. With that
done, we should be able to move on to improving the extension's
functionality instead of improving what's already there.

Change-Id: Id7d2984e66bdf998bdd11249abae5824c8a23591
---
M CommunityTwitter_body.php
1 file changed, 51 insertions(+), 56 deletions(-)

Approvals:
  RAN1: Verified; Looks good to me, approved



diff --git a/CommunityTwitter_body.php b/CommunityTwitter_body.php
index 257cbd5..8c12512 100755
--- a/CommunityTwitter_body.php
+++ b/CommunityTwitter_body.php
@@ -32,33 +32,31 @@
 
 class CommunityTwitter extends SpecialPage {
 
-       function CommunityTwitter() {
-               global $wgUser;
-               global $individualAccs;
+       // Variable to keep track of whether individual accounts are used by 
each user
+       private $individualAccs;
 
-               $individualAccs = true;
+       function __construct() {
+               $this->individualAccs = true;
 
                // In case you want to change the name of the special page, you 
have to edit CommunityTwitter_body.php and CommunityTwitter.php
-               SpecialPage::SpecialPage('CommunityTwitter', 'twitter');
-               $this->skin = $wgUser->getSkin();
+               SpecialPage::__construct('CommunityTwitter', 'twitter');
        }
 
        function execute($query) {
-               global $wgOut;
-               global $wgUser;
-               global $wgScriptPath;
-               global $wgRequest;
-               global $wgDBserver;
-               global $wgDBuser;
-               global $wgDBpassword;
-               global $wgDBname;
+               // Get context variables
+               $output = $this->getOutput();
+               $user = $this->getUser();
+               $request = $this->getRequest();
+               $title = $this->getTitle();
+               $language = $this->getLanguage();
+
+               // Get extension configuration variables
                global $ctTableName;
                global $ctDefaultAccount;
                global $ctDisplayCoordinates;
                global $ctLat;
                global $ctLong;
                global $ctAllowedGroup;
-               global $individualAccs;
 
                if (!isset($ctTableName) || !isset($ctDefaultAccount) || 
!isset($ctDisplayCoordinates)) die ("Configure LocalSettings.php first.");
 
@@ -67,7 +65,7 @@
                $this->setHeaders();
 
                // Grant access to users with twitter right only. All others 
receive error pages.
-               if ( !$this->userCanExecute( $wgUser ) ) {
+               if ( !$this->userCanExecute( $user ) ) {
                        // User does not have rights or is not logged in
 
                        $tGroupRights = SpecialPage::getTitleFor( 
'ListGroupRights' );
@@ -77,10 +75,10 @@
                                array( 'User', 'makeGroupLinkWiki' ),
                                User::getGroupsWithPermission( 'twitter' )
                        );
-                       $groupsList = $this->getLanguage()->commaList( $groups 
);
+                       $groupsList = $language->commaList( $groups );
 
 
-                       if ( !$wgUser->isAnon() )
+                       if ( !$user->isAnon() )
                        {
                                // Logged in, but no rights
 
@@ -102,9 +100,9 @@
 
                        }
 
-               } else if ( $wgUser->isBlocked() ) {
+               } else if ( $user->isBlocked() ) {
                        // isBlocked is not checked in userCanExecute, make 
sure blocked users aren't tweeting.
-                       throw new UserBlockedError( $wgUser->getBlock() );
+                       throw new UserBlockedError( $user->getBlock() );
 
                } else {
                        // Presumably the user is allowed
@@ -121,16 +119,16 @@
                                // Query for individual account(s)
                                $sql_individual = sprintf("SELECT app_name, 
consumer_key, consumer_secret, access_token, access_token_secret FROM %s 
NATURAL JOIN %s ".
                                                                                
        "JOIN %s ON (user_id=ug_user) WHERE `ug_group` = '%s' AND `active` = 1 
AND (`user_id` = %d OR `user_id` = 0);",
-                                                                               
        $dbr->tableName("user"), $ctTableName, $dbr->tableName("user_groups"), 
$ctAllowedGroup, $wgUser->getId());
+                                                                               
        $dbr->tableName("user"), $ctTableName, $dbr->tableName("user_groups"), 
$ctAllowedGroup, $user->getId());
                                $res = $dbr->query($sql_individual);
                        } else {
-                               $individualAccs = false;
+                               $this->individualAccs = false;
                        }
 
                        if ($dbr->numRows($res) != 0) {
 
                                // Load CSS ResourceLoader module with 
addModuleStyles since most functionality does not require JS
-                               $wgOut->addModuleStyles( 
'ext.CommunityTwitter.styles' );
+                               $output->addModuleStyles( 
'ext.CommunityTwitter.styles' );
 
                                // Establish Twitter-connections
                                $connections = array();
@@ -139,7 +137,7 @@
                                        $connections[$row->app_name] = @new 
TwitterOAuth($row->consumer_key, $row->consumer_secret, $row->access_token, 
$row->access_token_secret);
                                }
                                if 
(!communitytwitter::checkConnections($connections)) {
-                                       $wgOut->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-connection-failure")."</strong>");
+                                       $output->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-connection-failure")."</strong>");
                                } else {
 
                                        // Get Twitter timeline from default 
account
@@ -147,22 +145,22 @@
                                        $twitterStatuses = 
array_values(array_filter(array_map(array("CommunityTwitter", "getOwnTweets"), 
$timeline)));
 
                                        // Display description template
-                                       
$wgOut->addWikiText("{{".wfMsg("communitytwitter-description-template")."}}");
+                                       
$output->addWikiText("{{".wfMsg("communitytwitter-description-template")."}}");
 
                                        // Tweet section
-                                       
$wgOut->addWikiText("==".wfMsg("communitytwitter-tweet-section-title")."==");
-                                       
$wgOut->addHTML(wfMsg("communitytwitter-logged-in-as")." 
<b>".$wgUser->getName()."</b>.\n<br/>\n".
+                                       
$output->addWikiText("==".wfMsg("communitytwitter-tweet-section-title")."==");
+                                       
$output->addHTML(wfMsg("communitytwitter-logged-in-as")." 
<b>".$user->getName()."</b>.\n<br/>\n".
                                                                                
        "<table class=\"wikitable ct-tweet-section\"><tr><td 
class=\"ct-tweet-section\">\n");
 
                                        // Process update/deletion/retweet
 
                                        // New status set by user
-                                       if ($wgRequest->getText("status") != 
"") {
+                                       if ($request->getText("status") != "") {
 
                                                // Send status message via 
OAuth to Twitter
                                                $responseIds = array();
                                                foreach($connections as 
$connection) {
-                                                       $tweetData = 
array("status" => urldecode($wgRequest->getText("status")));
+                                                       $tweetData = 
array("status" => urldecode($request->getText("status")));
                                                        // Positions get 
ignored, if latitude and/or longitude are not set
                                                        if 
($ctDisplayCoordinates && isset($ctLat) && isset($ctLong)) {
                                                                
$tweetData["lat"] = $ctLat;
@@ -175,24 +173,24 @@
                                                // If response seems to be ok, 
tell the user about success, else, error unknown
                                                if 
(communitytwitter::checkResponses($responseIds)) {
                                                        foreach($connections as 
$name => $connection) {
-                                                               
$wgOut->addWikiText("<strong 
class=\"ct-success\">".wfMsg("communitytwitter-update-success")." 
([http://twitter.com/".$name."/status/".$responseIds[$name]."; 
".wfMsg("communitytwitter-check-tweet")."]).</strong>\n");
+                                                               
$output->addWikiText("<strong 
class=\"ct-success\">".wfMsg("communitytwitter-update-success")." 
([http://twitter.com/".$name."/status/".$responseIds[$name]."; 
".wfMsg("communitytwitter-check-tweet")."]).</strong>\n");
                                                        }
 
                                                        // Get the new status 
only to display updated "last tweets" section
                                                        $newStatus = 
$connections[$ctDefaultAccount]->get("statuses/show", array("id" => 
$responseIds[$ctDefaultAccount], "include_rts" => "true"));
                                                        
array_unshift($twitterStatuses, $newStatus);
                                                } else {
-                                                       
$wgOut->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
+                                                       
$output->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
                                                }
 
-                                       } else if 
($wgRequest->getText("deleteId") != "") {
+                                       } else if 
($request->getText("deleteId") != "") {
                                                // User requests deletion of 
tweet
                                                $delIdNotFound = true;
                                                $delUnknownError = false;
 
                                                // Verify that user is author 
of to be deleted tweet
                                                for ($i = 0; $i < 
count($twitterStatuses); $i++) {
-                                                       if 
($twitterStatuses[$i]->id_str == $wgRequest->getText("deleteId")) {
+                                                       if 
($twitterStatuses[$i]->id_str == $request->getText("deleteId")) {
 
                                                                $responses = 
array();
 
@@ -202,77 +200,77 @@
                                                                        
$currTimeline = $connections[$appName]->get("statuses/user_timeline", 
array("include_rts" => "true"));
                                                                        
$currTimeline = array_values(array_filter(array_map(array("CommunityTwitter", 
"getOwnTweets"), $currTimeline)));
                                                                        $hashId 
= communitytwitter::hashIds($twitterStatuses, $currTimeline);
-                                                                       if 
(array_key_exists($wgRequest->getText("deleteId"), $hashId)) {
-                                                                               
$responses[$appName] = $connections[$appName]->post("statuses/destroy/" . 
$hashId[$wgRequest->getText("deleteId")]);
+                                                                       if 
(array_key_exists($request->getText("deleteId"), $hashId)) {
+                                                                               
$responses[$appName] = $connections[$appName]->post("statuses/destroy/" . 
$hashId[$request->getText("deleteId")]);
                                                                        }
                                                                }
 
                                                                // Delete Tweet 
via OAuth
-                                                               
$responses[$ctDefaultAccount] = 
$connections[$ctDefaultAccount]->post("statuses/destroy/" . 
$wgRequest->getText("deleteId"));
+                                                               
$responses[$ctDefaultAccount] = 
$connections[$ctDefaultAccount]->post("statuses/destroy/" . 
$request->getText("deleteId"));
 
                                                                if 
(communitytwitter::checkResponses($responses)) {
-                                                                       
$wgOut->addHTML("<strong 
class=\"ct-success\">".wfMsg("communitytwitter-deletion-success")."</strong>\n");
+                                                                       
$output->addHTML("<strong 
class=\"ct-success\">".wfMsg("communitytwitter-deletion-success")."</strong>\n");
                                                                        // 
Update timeline array to display updated "your last tweets" section
                                                                        
array_splice($twitterStatuses, $i, 1);
                                                                } else {
-                                                                       
$wgOut->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
+                                                                       
$output->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
                                                                }
 
                                                                $delIdNotFound 
= false;
                                                        }
                                                }
-                                               if ($delIdNotFound) 
$wgOut->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-deletion-tweet-not-found")."</strong>\n");
+                                               if ($delIdNotFound) 
$output->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-deletion-tweet-not-found")."</strong>\n");
 
-                                       } else if 
($wgRequest->getText("tweetid") != "" && 
is_numeric($wgRequest->getText("tweetid")) && in_array($ctAllowedGroup, 
$wgUser->getEffectiveGroups())) {
+                                       } else if ($request->getText("tweetid") 
!= "" && is_numeric($request->getText("tweetid")) && in_array($ctAllowedGroup, 
$user->getEffectiveGroups())) {
 
                                                // Retweet status message via 
OAuth on Twitter
                                                $responseIds = array();
                                                foreach($connections as 
$connection) {
-                                                       
$responseIds[array_search($connection, $connections, true)] = 
$connection->post("statuses/retweet/".$wgRequest->getText("tweetid"))->id_str;
+                                                       
$responseIds[array_search($connection, $connections, true)] = 
$connection->post("statuses/retweet/".$request->getText("tweetid"))->id_str;
                                                }
 
                                                // If response seems to be ok, 
tell the user about success, else: error unknown
                                                if 
(communitytwitter::checkResponses($responseIds)) {
-                                                       
$wgOut->addWikiText("<strong 
class=\"ct-success\">".wfMsg("communitytwitter-update-success")."</strong>\n");
+                                                       
$output->addWikiText("<strong 
class=\"ct-success\">".wfMsg("communitytwitter-update-success")."</strong>\n");
                                                } else {
-                                                       
$wgOut->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
+                                                       
$output->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
                                                }
 
                                        } else {
                                                // Tweet textarea; javascript 
ResourceLoader module enables an updating counter with button disabling if 
tweet is too long (for convenience, Twitter won't allow a >140 char post 
anyways)
-                                               $wgOut->addModules( 
'ext.CommunityTwitter.updatecount' );
-                                               $wgOut->addHTML("<form 
action=\"".$wgOut->getTitle()->getLinkURL()."\" method=\"post\" 
id=\"status_update_form\">\n".
+                                               $output->addModules( 
'ext.CommunityTwitter.updatecount' );
+                                               $output->addHTML("<form 
action=\"".$title->getLinkURL()."\" method=\"post\" 
id=\"status_update_form\">\n".
                                                                                
"<span class=\"ct-tweet\">".wfMsg("communitytwitter-remaining-chars").": 
<strong id=\"stringlength\">140</strong><br/>\n".
                                                                                
"<textarea tabindex=\"1\" autocomplete=\"off\" accesskey=\"u\" name=\"status\" 
id=\"status\" rows=\"2\" cols=\"40\" class=\"ct-tweet-input\"></textarea>\n".
                                                                                
"<br/><input type=\"submit\" id=\"tweetbutton\" value=\"  Tweet   \"></span>\n".
                                                                                
"</form><br /><br /><br /><br /><br /><br />\n".
                                                                                
"<span class=\"ct-tweet\">".
-                                                                               
"<form action=\"".$wgOut->getTitle()->getLinkURL()."\" 
method=\"post\">Tweet-ID: <input type=\"text\" name=\"tweetid\" />".
+                                                                               
"<form action=\"".$title->getLinkURL()."\" method=\"post\">Tweet-ID: <input 
type=\"text\" name=\"tweetid\" />".
                                                                                
"<br /><a 
href=\"javascript:alert('".wfMsg("communitytwitter-id-help-text")."')\">".
                                                                                
wfMsg("communitytwitter-id-help-link")."</a> <input type=\"submit\" 
value=\"Retweet\"></form></span>");
                                        }
 
-                                       
$wgOut->addHTML("</td></tr></table><table class=\"wikitable\" 
class=\"ct-last-tweets\"><tr><td>&nbsp;<b>".wfMsg("communitytwitter-your-last-tweets").":</b><br/><ul>\n");
+                                       
$output->addHTML("</td></tr></table><table class=\"wikitable\" 
class=\"ct-last-tweets\"><tr><td>&nbsp;<b>".wfMsg("communitytwitter-your-last-tweets").":</b><br/><ul>\n");
 
                                        // Display users' last tweets to give 
him the opportunity to delete his own tweets
                                        if (count($twitterStatuses) > 0) {
                                                for ($i=0; $i < 
min(5,count($twitterStatuses)); $i++) {
                                                                if 
(isset($twitterStatuses[$i]) && $twitterStatuses[$i]->text != "") {
-                                                               
$wgOut->addHTML("<form action=\"".$wgOut->getTitle()->getLinkURL()."\" 
method=\"post\"><input type=\"hidden\" name=\"deleteId\" 
value=\"".($twitterStatuses[$i]->id_str)."\" /><li>[<input class=\"submit\" 
type=\"submit\" value=\"".wfMsg("communitytwitter-delete-tweet-link-name")."\" 
title=\"".wfMsg("communitytwitter-delete-tweet-title-tag")."\" />] 
".$twitterStatuses[$i]->text."</li></form>\n");
+                                                               
$output->addHTML("<form action=\"".$title->getLinkURL()."\" 
method=\"post\"><input type=\"hidden\" name=\"deleteId\" 
value=\"".($twitterStatuses[$i]->id_str)."\" /><li>[<input class=\"submit\" 
type=\"submit\" value=\"".wfMsg("communitytwitter-delete-tweet-link-name")."\" 
title=\"".wfMsg("communitytwitter-delete-tweet-title-tag")."\" />] 
".$twitterStatuses[$i]->text."</li></form>\n");
                                                        }
                                                }
                                        } else {
-                                               $wgOut->addHTML("<span 
class=\"ct-info\">".wfMsg("communitytwitter-no-last-tweets")."</span>");
+                                               $output->addHTML("<span 
class=\"ct-info\">".wfMsg("communitytwitter-no-last-tweets")."</span>");
                                        }
 
-                                       
$wgOut->addHTML("</ul></td></tr></table>\n");
+                                       
$output->addHTML("</ul></td></tr></table>\n");
 
 
                                        // Display Twitter advices template
-                                       
$wgOut->addWikiText("{{".wfMsg("communitytwitter-advice-template")."}}");
+                                       
$output->addWikiText("{{".wfMsg("communitytwitter-advice-template")."}}");
                                }
                        } else {
-                               $wgOut->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
+                               $output->addHTML("<strong 
class=\"ct-error\">".wfMsg("communitytwitter-unknown-error-request")."</strong>\n");
                        }
                }
        }
@@ -311,10 +309,7 @@
 
        // Return only self-tweeted messages
        function getOwnTweets($timelineItem) {
-               global $wgUser;
-               global $individualAccs;
-
-               if (stripos($timelineItem->source, 
"User:".$wgUser->getName()."\"") !== false || !$individualAccs) {
+               if (stripos($timelineItem->source, 
"User:".$this->getUser()->getName()."\"") !== false || !$this->individualAccs) {
                        return $timelineItem;
                } else {
                        return null;

-- 
To view, visit https://gerrit.wikimedia.org/r/70452
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id7d2984e66bdf998bdd11249abae5824c8a23591
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CommunityTwitter
Gerrit-Branch: master
Gerrit-Owner: RAN1 <r...@fastlizard4.org>
Gerrit-Reviewer: Basti2342 <ba...@randomprojects.de>
Gerrit-Reviewer: FastLizard4 <fastlizard4-git-comm...@fastlizard4.org>
Gerrit-Reviewer: RAN1 <r...@fastlizard4.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to