r77876 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77875‎ | r77876 | r77877 >
Date:14:37, 6 December 2010
Author:ashley
Status:deferred
Tags:
Comment:
Sudo:
*coding style tweaks
**removed m prefix from member variables
**added braces where appropriate
**spacing tweaks
*tweaked/added documentation
*addWikiText( wfMsg() ) -> addWikiMsg()
*removed an unused global from showErrors()
*fixed a typo in message key (nonexistant -> nonexistent)
*trimmed trailing whitespace
Modified paths:
  • /trunk/extensions/Sudo/SpecialSudo.php (modified) (history)
  • /trunk/extensions/Sudo/Sudo.i18n.php (modified) (history)
  • /trunk/extensions/Sudo/Sudo.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Sudo/Sudo.php
@@ -1,62 +1,76 @@
22 <?php
33 /**
44 * Sudo
5 - * @package Sudo
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ * @version 0.2
69 * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name>
710 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 - *
 11+ * @link http://www.mediawiki.org/wiki/Extension:Sudo Documentation
 12+ *
913 * This program is free software; you can redistribute it and/or
1014 * modify it under the terms of the GNU General Public License
1115 * as published by the Free Software Foundation; either version 2
1216 * of the License, or (at your option) any later version.
13 - *
 17+ *
1418 * This program is distributed in the hope that it will be useful,
1519 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1620 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1721 * GNU General Public License for more details.
18 - *
 22+ *
1923 * You should have received a copy of the GNU General Public License
2024 * along with this program; if not, write to the Free Software
2125 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2226 */
2327
24 -if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
 28+if( !defined( 'MEDIAWIKI' ) ) {
 29+ die( 'This is an extension to the MediaWiki package and cannot be run standalone.' );
 30+}
2531
26 -$wgExtensionCredits['other'][] = array (
 32+// Extension credits that will show up on Special:Version
 33+$wgExtensionCredits['other'][] = array(
2734 'name' => 'Sudo',
28 - 'url' => 'http://mediawiki.org/wiki/Extension:Sudo',
2935 'version' => '0.2',
30 - 'author' => "[http://mediawiki.org/wiki/User:Dantman Daniel Friesen] [mailto:Daniel%20Friesen%20%3Cmediawiki@danielfriesen.name%3E <mediawiki@danielfriesen.name>]",
31 - 'description' => "Allows sudoers to login as other users."
 36+ 'author' => '[http://www.mediawiki.org/wiki/User:Dantman Daniel Friesen] [mailto:Daniel%20Friesen%20%3Cmediawiki@danielfriesen.name%3E <mediawiki@danielfriesen.name>]',
 37+ 'description' => 'Allows sudoers to login as other users.',
 38+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Sudo',
3239 );
3340
34 -$wgAvailableRights[] = 'sudo';
35 -$wgExtensionMessagesFiles['Sudo'] = dirname(__FILE__).'/Sudo.i18n.php';
36 -$wgAutoloadClasses['SpecialSudo'] = dirname(__FILE__).'/SpecialSudo.php';
 41+// Set up i18n and the new special page
 42+$dir = dirname( __FILE__ ) . '/';
 43+$wgExtensionMessagesFiles['Sudo'] = $dir . 'Sudo.i18n.php';
 44+$wgAutoloadClasses['SpecialSudo'] = $dir . 'SpecialSudo.php';
3745 $wgSpecialPages['Sudo'] = 'SpecialSudo';
3846 $wgSpecialPageGroups['Sudo'] = 'users';
3947
 48+// New user right, required to use Special:Sudo
 49+$wgAvailableRights[] = 'sudo';
 50+
 51+// New log type, all sudo actions are logged to this log (Special:Log/sudo)
4052 $wgLogTypes[] = 'sudo';
4153 $wgLogNames['sudo'] = 'sudo-logpagename';
4254 $wgLogHeaders['sudo'] = 'sudo-logpagetext';
4355 $wgLogActions['sudo/sudo'] = 'sudo-logentry';
4456
45 -$wgHooks['UserLogoutComplete'][] = 'efSudoLogout';
46 -$wgHooks['PersonalUrls'][] = 'efSudoPersonalUrls';
 57+// Hooked functions
 58+$wgHooks['UserLogoutComplete'][] = 'wfSudoLogout';
 59+$wgHooks['PersonalUrls'][] = 'wfSudoPersonalUrls';
4760
48 -function efSudoLogout( &$user, &$inject_html ) {
49 - ## Unset wsSudoId when we logout. We don't want to be in a sudo login while logged out.
50 - unset($_SESSION['wsSudoId']);
 61+function wfSudoLogout( &$user, &$inject_html ) {
 62+ // Unset wsSudoId when we logout.
 63+ // We don't want to be in a sudo login while logged out.
 64+ unset( $_SESSION['wsSudoId'] );
5165 return true;
5266 }
5367
54 -function efSudoPersonalUrls( &$personal_urls, &$wgTitle ) {
55 - ## Replace logout link with a unsudo link while in a sudo login.
56 - if( isset($_SESSION['wsSudoId']) && $_SESSION['wsSudoId'] > 0 ) {
 68+function wfSudoPersonalUrls( &$personal_urls, &$wgTitle ) {
 69+ // Replace logout link with a unsudo link while in a sudo login.
 70+ if( isset( $_SESSION['wsSudoId'] ) && $_SESSION['wsSudoId'] > 0 ) {
5771 $personal_urls['logout'] = array(
58 - 'text' => wfMsg( 'sudo-personal-unsudo' ),
59 - 'href' => Skin::makeSpecialUrl( 'Sudo', 'mode=unsudo' ),
60 - 'active' => false
 72+ 'text' => wfMsg( 'sudo-personal-unsudo' ),
 73+ 'href' => Skin::makeSpecialUrl( 'Sudo', 'mode=unsudo' ),
 74+ 'active' => false
6175 );
6276 }
6377 return true;
Index: trunk/extensions/Sudo/SpecialSudo.php
@@ -1,115 +1,140 @@
22 <?php
33 /**
44 * Sudo
5 - * @package Sudo
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
68 * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name>
79 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 - *
 10+ *
911 * This program is free software; you can redistribute it and/or
1012 * modify it under the terms of the GNU General Public License
1113 * as published by the Free Software Foundation; either version 2
1214 * of the License, or (at your option) any later version.
13 - *
 15+ *
1416 * This program is distributed in the hope that it will be useful,
1517 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1618 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1719 * GNU General Public License for more details.
18 - *
 20+ *
1921 * You should have received a copy of the GNU General Public License
2022 * along with this program; if not, write to the Free Software
2123 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2224 */
2325
24 -if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
 26+if( !defined( 'MEDIAWIKI' ) ) {
 27+ die( 'This is an extension to the MediaWiki package and cannot be run standalone.' );
 28+}
2529
2630 class SpecialSudo extends SpecialPage {
27 - protected $mMode, $mSkin, $mTarget, $mReason, $mErrors;
28 -
29 - function __construct() {
 31+ protected $mode, $skin, $target, $reason, $errors;
 32+
 33+ /**
 34+ * Constructor -- set up the new special page
 35+ */
 36+ public function __construct() {
3037 parent::__construct( 'Sudo', 'sudo' );
31 - wfLoadExtensionMessages('Sudo');
 38+ wfLoadExtensionMessages( 'Sudo' );
3239 }
33 -
 40+
3441 /**
35 - * Execute
 42+ * Show the special page
 43+ *
 44+ * @param $par String: name of the user to sudo into
3645 */
37 - function execute( $par = '' ) {
 46+ public function execute( $par ) {
3847 global $wgOut, $wgRequest, $wgUser;
39 -
40 - $this->mMode = $wgRequest->getText('mode');
41 - if( $this->mMode == 'success' ) return $this->showSuccessPage();
42 - if( $this->mMode == 'unsudo' ) return $this->showUnsudoPage();
43 -
 48+
 49+ $this->mode = $wgRequest->getText( 'mode' );
 50+ if( $this->mode == 'success' ) {
 51+ return $this->showSuccessPage();
 52+ }
 53+ if( $this->mode == 'unsudo' ) {
 54+ return $this->showUnsudoPage();
 55+ }
 56+
 57+ // Check that the user is allowed to access this special page...
4458 if( !$wgUser->isAllowed( 'sudo' ) ) {
4559 $wgOut->permissionRequired( 'sudo' );
4660 return;
4761 }
 62+
 63+ // ...and that the user isn't blocked
4864 if( $wgUser->isBlocked() ) {
4965 $wgOut->blockedPage();
5066 return;
5167 }
 68+
 69+ // ...and that the database is not in read-only mode.
5270 if( wfReadOnly() ) {
5371 $wgOut->readOnlyPage();
5472 return;
5573 }
56 -
 74+
 75+ // Set page title and other stuff
5776 $this->setHeaders();
58 -
59 - $this->mSkin = $wgUser->getSkin();
60 - $this->mTarget = $wgRequest->getText('target', $par);
61 - $this->mReason = $wgRequest->getText('reason', '');
62 - $this->mErrors = array();
63 -
 77+
 78+ $this->skin = $wgUser->getSkin();
 79+ $this->target = $wgRequest->getText( 'target', $par );
 80+ $this->reason = $wgRequest->getText( 'reason', '' );
 81+ $this->errors = array();
 82+
6483 $this->showSudoForm();
65 - if( $this->mTarget != '' && $wgRequest->wasPosted() ) $this->doSudo();
 84+ if( $this->target != '' && $wgRequest->wasPosted() ) {
 85+ $this->doSudo();
 86+ }
6687 $this->showErrors();
6788 }
68 -
 89+
6990 function showSuccessPage() {
7091 global $wgOut, $wgUser;
71 - if( !isset($_SESSION['wsSudoId']) || $_SESSION['wsSudoId'] < 0 ) {
72 - $this->showError('sudo-error-nosudo');
 92+ if( !isset( $_SESSION['wsSudoId'] ) || $_SESSION['wsSudoId'] < 0 ) {
 93+ $this->showError( 'sudo-error-nosudo' );
7394 } else {
7495 $this->setHeaders();
7596 $s = $wgUser->getSkin();
76 - $suUser = User::newFromId($_SESSION['wsSudoId']);
 97+ $suUser = User::newFromId( $_SESSION['wsSudoId'] );
7798 $wgOut->addWikiMsgArray( 'sudo-success', array(
7899 $s->makeLinkObj( $suUser->getUserPage(), $suUser->getName() ),
79100 $s->makeLinkObj( $wgUser->getUserPage(), $wgUser->getName() ) ),
80 - array('replaceafter') );
 101+ array( 'replaceafter' )
 102+ );
81103 }
82104 }
83 -
 105+
84106 function showUnsudoPage() {
85107 global $wgOut, $wgUser, $wgRequest;
86 - if( !isset($_SESSION['wsSudoId']) || $_SESSION['wsSudoId'] < 0 ) {
87 - $this->showError('sudo-error-nosudo');
 108+ if( !isset( $_SESSION['wsSudoId'] ) || $_SESSION['wsSudoId'] < 0 ) {
 109+ $this->showError( 'sudo-error-nosudo' );
88110 } else {
89 - $suUser = User::newFromId($_SESSION['wsSudoId']);
 111+ $suUser = User::newFromId( $_SESSION['wsSudoId'] );
90112 if( $wgRequest->wasPosted() ) {
91 - unset($_SESSION['wsSudoId']);
 113+ unset( $_SESSION['wsSudoId'] );
92114 $suUser->setCookies();
93 - $wgOut->redirect($this->getTitle()->getFullURL());
 115+ $wgOut->redirect( $this->getTitle()->getFullURL() );
94116 return;
95117 }
96118 $this->setHeaders();
97 - $wgOut->setPageTitle(wfMsg('unsudo'));
 119+ $wgOut->setPageTitle( wfMsg( 'unsudo' ) );
98120 $s = $wgUser->getSkin();
99 -
 121+
100122 $wgOut->addHTML(
101 - Xml::openElement('form', array( 'method' => 'post',
102 - 'action' => $this->getTitle()->getFullURL('mode=unsudo') )).
103 - Xml::hidden('title', $this->getTitle()->getPrefixedText()) );
 123+ Xml::openElement( 'form', array( 'method' => 'post',
 124+ 'action' => $this->getTitle()->getFullURL( 'mode=unsudo' ) ) ) .
 125+ Xml::hidden( 'title', $this->getTitle()->getPrefixedText() )
 126+ );
104127 $wgOut->addWikiMsgArray( 'sudo-unsudo', array(
105128 $s->makeLinkObj( $suUser->getUserPage(), $suUser->getName() ),
106129 $s->makeLinkObj( $wgUser->getUserPage(), $wgUser->getName() ) ),
107 - array('replaceafter') );
 130+ array( 'replaceafter' )
 131+ );
108132 $wgOut->addHTML(
109 - Xml::submitButton(wfMsg('sudo-unsudo-submit')).
110 - Xml::closeElement('form'));
 133+ Xml::submitButton( wfMsg( 'sudo-unsudo-submit' ) ) .
 134+ Xml::closeElement( 'form' )
 135+ );
111136 }
112137 }
113 -
 138+
114139 function showSudoForm() {
115140 global $wgOut;
116141 $wgOut->addHTML(
@@ -118,49 +143,59 @@
119144 Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
120145 Xml::openElement( 'fieldset' ) .
121146 Xml::element( 'legend', array(), wfMsg( 'sudo-form' ) ) .
122 - Xml::inputLabel( wfMsg( 'sudo-user' ), 'target', 'sudo-user', 20, $this->mTarget ) . ' ' .
123 - Xml::inputLabel( wfMsg( 'sudo-reason' ), 'reason', 'sudo-reason', 45, $this->mReason ) . ' ' .
 147+ Xml::inputLabel( wfMsg( 'sudo-user' ), 'target', 'sudo-user', 20, $this->target ) . ' ' .
 148+ Xml::inputLabel( wfMsg( 'sudo-reason' ), 'reason', 'sudo-reason', 45, $this->reason ) . ' ' .
124149 Xml::submitButton( wfMsg( 'sudo-submit' ) ) .
125150 Xml::closeElement( 'fieldset' ) .
126151 Xml::closeElement( 'form' ) . "\n"
127152 );
128153 }
129 -
 154+
130155 function addError( $error = '' ) {
131 - $this->mErrors[] = $error;
 156+ $this->errors[] = $error;
132157 return;
133158 }
134 -
 159+
135160 function showError( $error ) {
136161 global $wgOut;
137 - $wgOut->addHTML( Xml::openElement('div', array( 'class' => 'sudo-error' )) );
138 - $wgOut->addWikiText( wfMsg('sudo-error',wfMsg($error)) );
139 - $wgOut->addHTML( Xml::closeElement('div') );
 162+ $wgOut->addHTML( Xml::openElement( 'div', array( 'class' => 'sudo-error' ) ) );
 163+ $wgOut->addWikiMsg( 'sudo-error', wfMsg( $error ) );
 164+ $wgOut->addHTML( Xml::closeElement( 'div' ) );
140165 }
141 -
 166+
142167 function showErrors() {
143 - global $wgOut;
144 - foreach( $this->mErrors as $error ) {
 168+ foreach( $this->errors as $error ) {
145169 $this->showError( $error );
146170 }
147171 }
148 -
 172+
149173 function doSudo() {
150174 global $wgOut, $wgUser;
151 - $u = User::newFromName( $this->mTarget );
152 - if( is_null($u) ) return $this->addError('sudo-error-sudo-invaliduser');
153 - if( User::isIP($u->getName()) ) return $this->addError('sudo-error-ip');
154 - if( $u->isAnon() ) return $this->addError('sudo-error-sudo-nonexistant');
155 - if( $u->getName() === $wgUser->getName() ) return $this->addError('sudo-error-sudo-self');
156 -
 175+
 176+ $u = User::newFromName( $this->target );
 177+ if( is_null( $u ) ) {
 178+ return $this->addError( 'sudo-error-sudo-invaliduser' );
 179+ }
 180+ if( User::isIP( $u->getName() ) ) {
 181+ return $this->addError( 'sudo-error-ip' );
 182+ }
 183+ if( $u->isAnon() ) {
 184+ return $this->addError( 'sudo-error-sudo-nonexistent' );
 185+ }
 186+ if( $u->getName() === $wgUser->getName() ) {
 187+ return $this->addError( 'sudo-error-sudo-self' );
 188+ }
 189+
157190 $s = $wgUser->getSkin();
158191 $log = new LogPage( 'sudo' );
159 - $log->addEntry( 'sudo', $wgUser->getUserPage(), $this->mReason,
 192+ $log->addEntry( 'sudo', $wgUser->getUserPage(), $this->reason,
160193 array( $s->makeLinkObj( $u->getUserPage(), $u->getName() ) ) );
161 -
162 - if( !isset($_SESSION['wsSudoId']) || $_SESSION['wsSudoId'] < 0 ) $_SESSION['wsSudoId'] = $wgUser->getId();
 194+
 195+ if( !isset( $_SESSION['wsSudoId'] ) || $_SESSION['wsSudoId'] < 0 ) {
 196+ $_SESSION['wsSudoId'] = $wgUser->getId();
 197+ }
163198 $u->setCookies();
164 -
 199+
165200 $wgOut->redirect( $this->getTitle()->getFullURL( 'mode=success' ) );
166201 }
167202 }
Index: trunk/extensions/Sudo/Sudo.i18n.php
@@ -1,27 +1,27 @@
22 <?php
33 /**
44 * Sudo
5 - * @package Sudo
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
68 * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name>
79 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 - *
 10+ *
911 * This program is free software; you can redistribute it and/or
1012 * modify it under the terms of the GNU General Public License
1113 * as published by the Free Software Foundation; either version 2
1214 * of the License, or (at your option) any later version.
13 - *
 15+ *
1416 * This program is distributed in the hope that it will be useful,
1517 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1618 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1719 * GNU General Public License for more details.
18 - *
 20+ *
1921 * You should have received a copy of the GNU General Public License
2022 * along with this program; if not, write to the Free Software
2123 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2224 */
2325
24 -if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." );
25 -
2626 $messages = array();
2727
2828 /** English
@@ -38,10 +38,10 @@
3939 'sudo-unsudo' => 'Welcome $1, you are currently logged into the wiki as $2, just submit the form to return to your normal login.',
4040 'sudo-unsudo-submit' => 'Return',
4141 'sudo-success' => 'Welcome $1, you are now logged into the wiki as $2.',
42 - 'sudo-error' => 'Sudo Error: $1',
43 - 'sudo-error-sudo-invaliduser' => 'Invalid Username',
 42+ 'sudo-error' => 'Sudo error: $1',
 43+ 'sudo-error-sudo-invaliduser' => 'Invalid username',
4444 'sudo-error-sudo-ip' => 'Cannot login to an IP',
45 - 'sudo-error-sudo-nonexistant' => 'That user does not exist',
 45+ 'sudo-error-sudo-nonexistent' => 'That user does not exist',
4646 'sudo-error-sudo-self' => 'Cannot sudo into yourself',
4747 'sudo-error-nosudo' => 'You do not appear to be inside of a sudo login',
4848 'sudo-logpagename' => 'Sudo log',

Status & tagging log