r78242 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78241‎ | r78242 | r78243 >
Date:12:40, 12 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Implemented remote authentication via the api
Modified paths:
  • /trunk/extensions/Push/Push_Settings.php (modified) (history)
  • /trunk/extensions/Push/api/ApiPush.php (modified) (history)
  • /trunk/extensions/Push/includes/Push_Tab.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Push/Push_Settings.php
@@ -29,8 +29,8 @@
3030 $egPushIncTemplates = false;
3131
3232 $egPushAllowLogin = true;
33 -$egPushLodignUser = '';
34 -$egPushLodignPass = '';
 33+$egPushLoginUser = '';
 34+$egPushLoginPass = '';
3535
3636 $egPushBulkWorkers = 3;
3737 $egPushBatchSize = 3;
Index: trunk/extensions/Push/includes/Push_Tab.php
@@ -294,7 +294,7 @@
295295 $wgOut->addHTML(
296296 Html::rawElement(
297297 'div',
298 - array( 'id' => 'divIncTemplates' ),
 298+ array( 'id' => 'divIncTemplates', 'style' => 'display: table-cell' ),
299299 Xml::check( 'checkIncTemplates', $egPushIncTemplates, array( 'id' => 'checkIncTemplates' ) ) .
300300 Html::element(
301301 'label',
Index: trunk/extensions/Push/api/ApiPush.php
@@ -14,6 +14,16 @@
1515
1616 protected $editResponses = array();
1717
 18+ /**
 19+ * Associative array containing CookieJar objects (values) to be passed in
 20+ * order to autenticate to the targets (keys).
 21+ *
 22+ * @since 0.4
 23+ *
 24+ * @var array
 25+ */
 26+ protected $cookieJars = array();
 27+
1828 public function __construct( $main, $action ) {
1929 parent::__construct( $main, $action );
2030 }
@@ -29,6 +39,17 @@
3040 $this->dieUsageMsg( array( 'missingparam', 'targets' ) );
3141 }
3242
 43+ foreach ( $params['targets'] as &$target ) {
 44+ if ( substr( $target, -1 ) !== '/' ) {
 45+ $target .= '/';
 46+ }
 47+
 48+ $target .= 'api.php';
 49+ }
 50+
 51+ global $egPushLoginUser, $egPushLoginPass;
 52+ $this->doLogin( $egPushLoginUser, $egPushLoginPass, $params['targets'] );
 53+
3354 foreach ( $params['page'] as $page ) {
3455 $title = Title::newFromText( $page );
3556
@@ -68,6 +89,70 @@
6990 }
7091
7192 /**
 93+ * Logs in into the target wiki using the provided username and password.
 94+ *
 95+ * @since 0.4
 96+ *
 97+ * @param string $user
 98+ * @param string $password
 99+ * @param array $targets
 100+ * @param string $token
 101+ * @param CookieJar $cookie
 102+ */
 103+ protected function doLogin( $user, $password, array $targets, $token = null, $cookieJar = null ) {
 104+ $requestData = array(
 105+ 'action' => 'login',
 106+ 'format' => 'json',
 107+ 'lgname' => $user,
 108+ 'lgpassword' => $password
 109+ );
 110+
 111+ if ( !is_null( $token ) ) {
 112+ $requestData['lgtoken'] = $token;
 113+ }
 114+
 115+ foreach ( $targets as $target ) {
 116+ $req = MWHttpRequest::factory( $target,
 117+ array(
 118+ 'postData' => $requestData,
 119+ 'method' => 'POST',
 120+ 'timeout' => 'default'
 121+ )
 122+ );
 123+
 124+ if ( !is_null( $cookieJar ) ) {
 125+ $req->setCookieJar( $cookieJar );
 126+ }
 127+
 128+ $status = $req->execute();
 129+
 130+ if ( $status->isOK() ) {
 131+ $response = FormatJson::decode( $req->getContent() );
 132+
 133+ if ( property_exists( $response, 'login' )
 134+ && property_exists( $response->login, 'result' ) ) {
 135+
 136+ if ( $response->login->result == 'NeedToken' ) {
 137+ $this->doLogin( $user, $password, array( $target ), $response->login->token, $req->getCookieJar() );
 138+ }
 139+ else if ( $response->login->result == 'Success' ) {
 140+ $this->cookieJars[$target] = $req->getCookieJar();
 141+ }
 142+ else {
 143+ var_dump($response->login);exit;
 144+ }
 145+ }
 146+ else {
 147+ // TODO
 148+ }
 149+ }
 150+ else {
 151+ // TODO
 152+ }
 153+ }
 154+ }
 155+
 156+ /**
72157 * Makes an internal request to the API to get the needed revision.
73158 *
74159 * @since 0.3
@@ -130,13 +215,7 @@
131216 * @param array $targets
132217 */
133218 protected function doPush( Title $title, array $revision, array $targets ) {
134 - foreach ( $targets as $target ) {
135 - if ( substr( $target, -1 ) !== '/' ) {
136 - $target .= '/';
137 - }
138 -
139 - $target .= 'api.php';
140 -
 219+ foreach ( $targets as $target ) {
141220 $token = $this->getEditToken( $title, $target );
142221
143222 if ( $token !== false ) {
@@ -171,7 +250,21 @@
172251 $parts[] = $key . '=' . urlencode( $value );
173252 }
174253
175 - $response = FormatJson::decode( Http::get( $target . '?' . implode( '&', $parts ) ) );
 254+ $req = MWHttpRequest::factory( $target . '?' . implode( '&', $parts ),
 255+ array(
 256+ 'method' => 'GET',
 257+ 'timeout' => 'default'
 258+ )
 259+ );
 260+
 261+ if ( array_key_exists( $target, $this->cookieJars ) ) {
 262+ $req->setCookieJar( $this->cookieJars[$target] );
 263+ }
 264+
 265+ $status = $req->execute();
 266+
 267+ $response = $status->isOK() ? FormatJson::decode( $req->getContent() ) : null;
 268+
176269 $token = false;
177270
178271 if ( !is_null( $response )
@@ -231,12 +324,27 @@
232325 'token' => $token,
233326 );
234327
235 - $response = Http::post( $target, array( 'postData' => $requestData ) );
236 -
237 - if ( $response !== false ) {
238 - $this->editResponses[] = $response;
 328+ $req = MWHttpRequest::factory( $target,
 329+ array(
 330+ 'method' => 'POST',
 331+ 'timeout' => 'default',
 332+ 'postData' => $requestData
 333+ )
 334+ );
 335+
 336+ if ( array_key_exists( $target, $this->cookieJars ) ) {
 337+ $req->setCookieJar( $this->cookieJars[$target] );
239338 }
240339 else {
 340+ var_dump($this->cookieJars);exit;
 341+ }
 342+
 343+ $status = $req->execute();
 344+
 345+ if ( $status->isOK() ) {
 346+ $this->editResponses[] = $req->getContent();
 347+ }
 348+ else {
241349 $this->dieUsage( wfMsg( 'push-special-err-push-failed' ), 'page-push-failed' );
242350 }
243351 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r78243Follow up to r78242 - removed debug statements, added check for username and ...jeroendedauw13:07, 12 December 2010

Status & tagging log