r46551 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46550‎ | r46551 | r46552 >
Date:21:09, 29 January 2009
Author:aaron
Status:ok
Tags:
Comment:
Make userCan() short getUserPermissionsErrorsInternal()
Modified paths:
  • /trunk/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Title.php
@@ -1056,7 +1056,7 @@
10571057 */
10581058 public function userCan( $action, $doExpensiveQueries = true ) {
10591059 global $wgUser;
1060 - return ( $this->getUserPermissionsErrorsInternal( $action, $wgUser, $doExpensiveQueries ) === array());
 1060+ return ($this->getUserPermissionsErrorsInternal( $action, $wgUser, $doExpensiveQueries, true ) === array());
10611061 }
10621062
10631063 /**
@@ -1158,9 +1158,10 @@
11591159 * @param $action \type{\string} action that permission needs to be checked for
11601160 * @param $user \type{User} user to check
11611161 * @param $doExpensiveQueries \type{\bool} Set this to false to avoid doing unnecessary queries.
 1162+ * @param $short \type{\bool} Set this to true to stop after the first permission error.
11621163 * @return \type{\array} Array of arrays of the arguments to wfMsg to explain permissions problems.
11631164 */
1164 - private function getUserPermissionsErrorsInternal( $action, $user, $doExpensiveQueries = true ) {
 1165+ private function getUserPermissionsErrorsInternal( $action, $user, $doExpensiveQueries=true, $short=false ) {
11651166 wfProfileIn( __METHOD__ );
11661167
11671168 $errors = array();
@@ -1170,7 +1171,7 @@
11711172 wfProfileOut( __METHOD__ );
11721173 return $result ? array() : array( array( 'badaccess-group0' ) );
11731174 }
1174 -
 1175+ // Check getUserPermissionsErrors hook
11751176 if( !wfRunHooks( 'getUserPermissionsErrors', array(&$this,&$user,$action,&$result) ) ) {
11761177 if( is_array($result) && count($result) && !is_array($result[0]) )
11771178 $errors[] = $result; # A single array representing an error
@@ -1181,6 +1182,12 @@
11821183 else if( $result === false )
11831184 $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
11841185 }
 1186+ # Short-circuit point
 1187+ if( $short && count($errors) > 0 ) {
 1188+ wfProfileOut( __METHOD__ );
 1189+ return $errors;
 1190+ }
 1191+ // Check getUserPermissionsErrorsExpensive hook
11851192 if( $doExpensiveQueries && !wfRunHooks( 'getUserPermissionsErrorsExpensive', array(&$this,&$user,$action,&$result) ) ) {
11861193 if( is_array($result) && count($result) && !is_array($result[0]) )
11871194 $errors[] = $result; # A single array representing an error
@@ -1191,6 +1198,11 @@
11921199 else if( $result === false )
11931200 $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
11941201 }
 1202+ # Short-circuit point
 1203+ if( $short && count($errors) > 0 ) {
 1204+ wfProfileOut( __METHOD__ );
 1205+ return $errors;
 1206+ }
11951207
11961208 // TODO: document
11971209 $specialOKActions = array( 'createaccount', 'execute' );
@@ -1236,6 +1248,11 @@
12371249 }
12381250 }
12391251 }
 1252+ # Short-circuit point
 1253+ if( $short && count($errors) > 0 ) {
 1254+ wfProfileOut( __METHOD__ );
 1255+ return $errors;
 1256+ }
12401257
12411258 foreach( $this->getRestrictions($action) as $right ) {
12421259 // Backwards compatibility, rewrite sysop -> protect
@@ -1249,14 +1266,17 @@
12501267 // with cascading option turned on.
12511268 if( $this->mCascadeRestriction ) {
12521269 $errors[] = array( 'protectedpagetext', $right );
1253 - } else {
1254 - // Nothing, user can edit!
12551270 }
12561271 } else {
12571272 $errors[] = array( 'protectedpagetext', $right );
12581273 }
12591274 }
12601275 }
 1276+ # Short-circuit point
 1277+ if( $short && count($errors) > 0 ) {
 1278+ wfProfileOut( __METHOD__ );
 1279+ return $errors;
 1280+ }
12611281
12621282 if( $action == 'protect' ) {
12631283 if( $this->getUserPermissionsErrors('edit', $user) != array() ) {

Status & tagging log