r76473 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76472‎ | r76473 | r76474 >
Date:21:21, 10 November 2010
Author:demon
Status:deferred
Tags:
Comment:
Use FormatJson::encode() and decode() rather than a DIY wrapper doing the same thing FormatJson already does
Modified paths:
  • /trunk/extensions/WikiSync/WikiSync.php (modified) (history)
  • /trunk/extensions/WikiSync/WikiSyncClient.php (modified) (history)
  • /trunk/extensions/WikiSync/pear (deleted) (history)

Diff [purge]

Index: trunk/extensions/WikiSync/WikiSyncClient.php
@@ -63,7 +63,7 @@
6464 */
6565 function setContext( $rc ) {
6666 if ( is_string( $rc ) ) {
67 - $rc = json_decode( $rc, true );
 67+ $rc = FormatJson::decode( $rc, true );
6868 }
6969 if ( !isset( $rc['wikiroot'] ) ) {
7070 throw new MWException( 'wikiroot is undefined in ' . __METHOD__ );
@@ -173,7 +173,7 @@
174174 function getResult( $code = null, $msg = null ) {
175175 $this->setCode( $code, $msg );
176176 if ( $this->encodeResult ) {
177 - return json_encode( $this->jr );
 177+ return FormatJson::encode( $this->jr );
178178 } else {
179179 return $this->jr;
180180 }
@@ -297,7 +297,7 @@
298298 if ( $snoopy->error != '' ) {
299299 return $json_result->getResult( 'http', $snoopy->error );
300300 }
301 - $response = json_decode( $snoopy->results );
 301+ $response = FormatJson::decode( $snoopy->results );
302302 # proxy returned html instead of json ?
303303 if ( $response === null ) {
304304 return $json_result->getResult( 'http' );
@@ -325,7 +325,7 @@
326326 if ( $snoopy->error != '' ) {
327327 return $json_result->getResult( 'http', $snoopy->error );
328328 }
329 - $response = json_decode( $snoopy->results );
 329+ $response = FormatJson::decode( $snoopy->results );
330330 # proxy returned html instead of json ?
331331 if ( $response === null ) {
332332 return $json_result->getResult( 'http' );
@@ -354,7 +354,7 @@
355355 */
356356 static function localAPIwrap( $api_params ) {
357357 if ( is_string( $api_params ) ) {
358 - $api_params = json_decode( $api_params, true );
 358+ $api_params = FormatJson::decode( $api_params, true );
359359 }
360360 $req = new FauxRequest( $api_params );
361361 $api = new ApiMain( $req );
@@ -385,7 +385,7 @@
386386 # not enough priviledges to run this method
387387 return $json_result->getResult( 'noaccess', $iu );
388388 }
389 - $api_params = is_array( $args[0] ) ? $args[0] : json_decode( $args[0], true );
 389+ $api_params = is_array( $args[0] ) ? $args[0] : FormatJson::decode( $args[0], true );
390390 try {
391391 $response = self::localAPIwrap( $api_params );
392392 } catch ( Exception $e ) {
@@ -434,7 +434,7 @@
435435 return $json_result->getResult( 'noaccess', $iu );
436436 }
437437 # snoopy api_params are associative array
438 - $api_params = is_array( $args[1] ) ? $args[1] : json_decode( $args[1], true );
 438+ $api_params = is_array( $args[1] ) ? $args[1] : FormatJson::decode( $args[1], true );
439439 $snoopy = new WikiSnoopy();
440440 $snoopy->setContext( $args[0] );
441441 # we always use POST method because it's less often cached by proxies
@@ -453,7 +453,7 @@
454454 if ( $resultEncoding == self::RESULT_SNOOPY ) {
455455 return $snoopy;
456456 }
457 - $response = json_decode( $snoopy->results, true );
 457+ $response = FormatJson::decode( $snoopy->results, true );
458458 # proxy returned html instead of json ?
459459 if ( $response === null ) {
460460 return $json_result->getResult( 'http' );
@@ -514,7 +514,7 @@
515515 }
516516 # remote context; used for remote API calls
517517 self::$remoteContextJSON = $args[0];
518 - self::$client_params = json_decode( $args[1], true );
 518+ self::$client_params = FormatJson::decode( $args[1], true );
519519 if ( ($check_result = self::checkClientParameters( $client_name )) !== true ) {
520520 self::$json_result->setCode( 'init_client', $check_result );
521521 return false;
@@ -631,7 +631,7 @@
632632 $result = self::sourceAPIget( $APIparams );
633633 if ( $result['ws_status'] === '0' ) {
634634 $result['ws_msg'] = 'source: ' . $result['ws_msg'] . ' (' . __METHOD__ . ')';
635 - return json_encode( $result );
 635+ return FormatJson::encode( $result );
636636 }
637637 # collect the file titles existed in current chunk's revisions
638638 $files = array();
@@ -649,7 +649,7 @@
650650 $result = self::importXML( $client_params['dst_import_token'], $result['query']['exportxml'] );
651651 if ( $result['ws_status'] === '0' ) {
652652 $result['ws_msg'] = 'destination: ' . $result['ws_msg'] . ' (' . __METHOD__ . ')';
653 - return json_encode( $result );
 653+ return FormatJson::encode( $result );
654654 }
655655 $json_result->setStatus( '1' ); // API success
656656 return $json_result->getResult();
@@ -700,7 +700,7 @@
701701 !isset( $src_result['query'] ) ||
702702 !isset( $src_result['query']['pages'] ) ) {
703703 $src_result['ws_msg'] = 'source: ' . $src_result['ws_msg'] . ' (' . __METHOD__ . ')';
704 - return json_encode( $src_result );
 704+ return FormatJson::encode( $src_result );
705705 }
706706 $src_result = self::transformImageInfoResult( $src_result );
707707 $dst_result = self::destinationAPIget( $APIparams );
@@ -708,7 +708,7 @@
709709 !isset( $dst_result['query'] ) ||
710710 !isset( $dst_result['query']['pages'] ) ) {
711711 $dst_result['ws_msg'] = 'destination: ' . $dst_result['ws_msg'] . ' (' . __METHOD__ . ')';
712 - return json_encode( $dst_result );
 712+ return FormatJson::encode( $dst_result );
713713 }
714714 $dst_result = self::transformImageInfoResult( $dst_result );
715715 $new_files = array();
Index: trunk/extensions/WikiSync/WikiSync.php
@@ -54,24 +54,6 @@
5555
5656 WikiSyncSetup::init();
5757
58 -if ( !function_exists( 'json_decode' ) ) {
59 - function json_decode( $content, $assoc = false ) {
60 - if ( $assoc ) {
61 - $json = new Services_JSON( SERVICES_JSON_LOOSE_TYPE );
62 - } else {
63 - $json = new Services_JSON;
64 - }
65 - return $json->decode( $content );
66 - }
67 -}
68 -
69 -if ( !function_exists( 'json_encode' ) ) {
70 - function json_encode( $content ) {
71 - $json = new Services_JSON;
72 - return $json->encode($content);
73 - }
74 -}
75 -
7658 class WikiSyncSetup {
7759
7860 const COOKIE_EXPIRE_TIME = 2592000; // 60 * 60 * 24 * 30; see also WikiSync.js, WikiSync.cookieExpireTime
@@ -135,7 +117,6 @@
136118 $wgAutoloadClasses['_QXML'] = self::$ExtDir . '/WikiSyncBasic.php';
137119 }
138120 $wgAutoloadClasses['Snoopy'] = self::$ExtDir . '/Snoopy/Snoopy.class.php';
139 - $wgAutoloadClasses['Services_JSON'] = self::$ExtDir . '/pear/JSON.php';
140121 $wgAutoloadClasses['WikiSyncSetup'] = self::$ExtDir . '/WikiSync.php';
141122 $wgAutoloadClasses['WikiSnoopy'] =
142123 $wgAutoloadClasses['WikiSyncJSONresult'] =
@@ -234,7 +215,7 @@
235216 $arg = self::JS_MSG_PREFIX . $arg;
236217 $result[$arg] = wfMsg( $arg );
237218 }
238 - return json_encode( $result );
 219+ return FormatJson::encode( $result );
239220 }
240221
241222 static function checkUserMembership( $groups ) {

Status & tagging log