r64875 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64874‎ | r64875 | r64876 >
Date:13:33, 10 April 2010
Author:reedy
Status:ok
Tags:
Comment:
Refactor getTokenSalt to use caching getUser/getTitle in ApiRollback and ApiUserrights
Modified paths:
  • /trunk/phase3/includes/api/ApiRollback.php (modified) (history)
  • /trunk/phase3/includes/api/ApiUserrights.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiRollback.php
@@ -36,27 +36,27 @@
3737 parent::__construct( $main, $action );
3838 }
3939
40 - private $mTitleObj = null;
 40+ private $mTitleObj = null, $mUser = null;
4141
4242 public function execute() {
4343 $params = $this->extractRequestParams();
4444
4545 // User and title already validated in call to getTokenSalt from Main
46 -
47 - $articleObj = new Article( $this->mTitleObj );
 46+ $titleObj = $this->getTitle();
 47+ $articleObj = new Article( $titleObj );
4848 $summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
4949 $details = null;
50 - $retval = $articleObj->doRollback( $this->username, $summary, $params['token'], $params['markbot'], $details );
 50+ $retval = $articleObj->doRollback( $this->getUser(), $summary, $params['token'], $params['markbot'], $details );
5151
5252 if ( $retval ) {
5353 // We don't care about multiple errors, just report one of them
5454 $this->dieUsageMsg( reset( $retval ) );
5555 }
5656
57 - $this->setWatch( $params['watchlist'], $this->mTitleObj );
 57+ $this->setWatch( $params['watchlist'], $titleObj );
5858
5959 $info = array(
60 - 'title' => $this->mTitleObj->getPrefixedText(),
 60+ 'title' => $titleObj->getPrefixedText(),
6161 'pageid' => intval( $details['current']->getPage() ),
6262 'summary' => $details['summary'],
6363 'revid' => intval( $details['newid'] ),
@@ -123,25 +123,43 @@
124124 }
125125
126126 public function getTokenSalt() {
 127+ return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
 128+ }
 129+
 130+ private function getUser() {
 131+ if ( $this->mUser !== null ) {
 132+ return $this->mUser;
 133+ }
 134+
127135 $params = $this->extractRequestParams();
128 -
 136+
129137 if ( !isset( $params['user'] ) ) {
130138 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
131139 }
132140
133141 // We need to be able to revert IPs, but getCanonicalName rejects them
134 - $this->username = User::isIP( $params['user'] )
 142+ $this->mUser = User::isIP( $params['user'] )
135143 ? $params['user']
136144 : User::getCanonicalName( $params['user'] );
137 - if ( !$this->username ) {
 145+ if ( !$this->mUser ) {
138146 $this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
139147 }
140 -
 148+
 149+ return $this->mUser;
 150+ }
 151+
 152+ private function getTitle() {
 153+ if ( $this->mTitleObj !== null ) {
 154+ return $this->mTitleObj;
 155+ }
 156+
 157+ $params = $this->extractRequestParams();
141158 if ( !isset( $params['title'] ) ) {
142 - $this->dieUsageMsg( array( 'missingparam', 'title' ) );
 159+ $this->dieUsageMsg( array( 'missingparam', 'title' ) );
143160 }
144 -
 161+
145162 $this->mTitleObj = Title::newFromText( $params['title'] );
 163+
146164 if ( !$this->mTitleObj ) {
147165 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
148166 }
@@ -149,7 +167,7 @@
150168 $this->dieUsageMsg( array( 'notanarticle' ) );
151169 }
152170
153 - return array( $this->mTitleObj->getPrefixedText(), $this->username );
 171+ return $this->mTitleObj;
154172 }
155173
156174 protected function getExamples() {
Index: trunk/phase3/includes/api/ApiUserrights.php
@@ -36,12 +36,12 @@
3737 parent::__construct( $main, $action );
3838 }
3939
 40+ private $mUser = null;
 41+
4042 public function execute() {
4143 $params = $this->extractRequestParams();
4244
43 - // User already validated in call to getTokenSalt from Main
44 - $form = new UserrightsPage;
45 - $user = $form->fetchUser( $params['user'] );
 45+ $user = $this->getUser();
4646
4747 $r['user'] = $user->getName();
4848 list( $r['added'], $r['removed'] ) =
@@ -103,6 +103,14 @@
104104 }
105105
106106 public function getTokenSalt() {
 107+ return $this->getUser()->getName();
 108+ }
 109+
 110+ private function getUser() {
 111+ if ( $this->mUser !== null ) {
 112+ return $this->mUser;
 113+ }
 114+
107115 $params = $this->extractRequestParams();
108116 if ( is_null( $params['user'] ) ) {
109117 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
@@ -115,7 +123,8 @@
116124 (array)$user->getMessageKey(), $user->getMessageArgs() ) );
117125 }
118126
119 - return $user->getName();
 127+ $this->mUser = $user;
 128+ return $user;
120129 }
121130
122131 protected function getExamples() {

Status & tagging log