Refactor getTokenSalt to use caching getUser/getTitle in ApiRollback and ApiUserrights

This commit is contained in:
Sam Reed 2010-04-10 13:33:24 +00:00
parent 410736b38f
commit 659a0b2912
2 changed files with 45 additions and 18 deletions

View file

@ -36,27 +36,27 @@ class ApiRollback extends ApiBase {
parent::__construct( $main, $action );
}
private $mTitleObj = null;
private $mTitleObj = null, $mUser = null;
public function execute() {
$params = $this->extractRequestParams();
// User and title already validated in call to getTokenSalt from Main
$articleObj = new Article( $this->mTitleObj );
$titleObj = $this->getTitle();
$articleObj = new Article( $titleObj );
$summary = ( isset( $params['summary'] ) ? $params['summary'] : '' );
$details = null;
$retval = $articleObj->doRollback( $this->username, $summary, $params['token'], $params['markbot'], $details );
$retval = $articleObj->doRollback( $this->getUser(), $summary, $params['token'], $params['markbot'], $details );
if ( $retval ) {
// We don't care about multiple errors, just report one of them
$this->dieUsageMsg( reset( $retval ) );
}
$this->setWatch( $params['watchlist'], $this->mTitleObj );
$this->setWatch( $params['watchlist'], $titleObj );
$info = array(
'title' => $this->mTitleObj->getPrefixedText(),
'title' => $titleObj->getPrefixedText(),
'pageid' => intval( $details['current']->getPage() ),
'summary' => $details['summary'],
'revid' => intval( $details['newid'] ),
@ -123,25 +123,43 @@ class ApiRollback extends ApiBase {
}
public function getTokenSalt() {
return array( $this->getTitle()->getPrefixedText(), $this->getUser() );
}
private function getUser() {
if ( $this->mUser !== null ) {
return $this->mUser;
}
$params = $this->extractRequestParams();
if ( !isset( $params['user'] ) ) {
$this->dieUsageMsg( array( 'missingparam', 'user' ) );
}
// We need to be able to revert IPs, but getCanonicalName rejects them
$this->username = User::isIP( $params['user'] )
$this->mUser = User::isIP( $params['user'] )
? $params['user']
: User::getCanonicalName( $params['user'] );
if ( !$this->username ) {
if ( !$this->mUser ) {
$this->dieUsageMsg( array( 'invaliduser', $params['user'] ) );
}
if ( !isset( $params['title'] ) ) {
$this->dieUsageMsg( array( 'missingparam', 'title' ) );
return $this->mUser;
}
private function getTitle() {
if ( $this->mTitleObj !== null ) {
return $this->mTitleObj;
}
$params = $this->extractRequestParams();
if ( !isset( $params['title'] ) ) {
$this->dieUsageMsg( array( 'missingparam', 'title' ) );
}
$this->mTitleObj = Title::newFromText( $params['title'] );
if ( !$this->mTitleObj ) {
$this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
}
@ -149,7 +167,7 @@ class ApiRollback extends ApiBase {
$this->dieUsageMsg( array( 'notanarticle' ) );
}
return array( $this->mTitleObj->getPrefixedText(), $this->username );
return $this->mTitleObj;
}
protected function getExamples() {

View file

@ -36,12 +36,12 @@ class ApiUserrights extends ApiBase {
parent::__construct( $main, $action );
}
private $mUser = null;
public function execute() {
$params = $this->extractRequestParams();
// User already validated in call to getTokenSalt from Main
$form = new UserrightsPage;
$user = $form->fetchUser( $params['user'] );
$user = $this->getUser();
$r['user'] = $user->getName();
list( $r['added'], $r['removed'] ) =
@ -103,6 +103,14 @@ class ApiUserrights extends ApiBase {
}
public function getTokenSalt() {
return $this->getUser()->getName();
}
private function getUser() {
if ( $this->mUser !== null ) {
return $this->mUser;
}
$params = $this->extractRequestParams();
if ( is_null( $params['user'] ) ) {
$this->dieUsageMsg( array( 'missingparam', 'user' ) );
@ -115,7 +123,8 @@ class ApiUserrights extends ApiBase {
(array)$user->getMessageKey(), $user->getMessageArgs() ) );
}
return $user->getName();
$this->mUser = $user;
return $user;
}
protected function getExamples() {