[MediaWiki-commits] [Gerrit] Various small code cleanups - change (mediawiki...Parsoid)

2013-05-30 Thread GWicke (Code Review)
GWicke has submitted this change and it was merged.

Change subject: Various small code cleanups
..


Various small code cleanups

* Use JobQueueGroup::push() in updateTitle().
* Made CurlMultiClient::request() return an array if no urls are given.
  Also cleaned up the options and error checking code a bit.
* Code style formatting.
* Added some type hints and make use of the 'self' keyword.
* Added some TODO/FIXME comments.

Change-Id: Iad4de0f0017b3f079971d76523d3a5a5487e9155
---
M php/CurlMultiClient.php
M php/Parsoid.hooks.php
M php/Parsoid.php
M php/ParsoidCacheUpdateJob.php
4 files changed, 100 insertions(+), 92 deletions(-)

Approvals:
  GWicke: Verified; Looks good to me, approved



diff --git a/php/CurlMultiClient.php b/php/CurlMultiClient.php
index 0ad2619..582a419 100644
--- a/php/CurlMultiClient.php
+++ b/php/CurlMultiClient.php
@@ -1,6 +1,8 @@
 ?php
+
 /**
  * A simple parallel CURL client helper class
+ * @TODO: name this ParsoidCurlMultiClient or move to core
  */
 class CurlMultiClient {
 
@@ -10,10 +12,10 @@
 * @static
 * @returns array default options
 */
-   public static function getDefaultOptions () {
+   public static function getDefaultOptions() {
return array(
-   CURLOPT_HEADER=0,
-   CURLOPT_RETURNTRANSFER=1
+   CURLOPT_HEADER = 0,
+   CURLOPT_RETURNTRANSFER = 1
);
}
 
@@ -23,11 +25,11 @@
 *
 * @static
 * @param $requests array requests, each with an url and an optional
-*  'headers' member:
-*array(
-*  'url' = 'http://server.com/foo',
-*  'headers' = array( 'X-Foo: Bar' )
-*)
+*  'headers' member:
+*array(
+*  'url' = 'http://server.com/foo',
+*  'headers' = array( 'X-Foo: Bar' )
+*)
 * @param $options array curl options used for each request, default
 * {CurlMultiClient::getDefaultOptions}.
 * @returns array An array of arrays containing 'error' and 'data'
@@ -35,45 +37,44 @@
 * errors, the error member will be null and data will contain the
 * response data as a string.
 */
-   public static function request($requests, $options=){
-
-
-   if( !count( $requests ) ) return false;
+   public static function request( $requests, array $options = null ) {
+   if ( !count( $requests ) ) {
+   return array();
+   }
 
$handles = array();
 
-   if( !$options ) // add default options
+   if ( $options === null ) { // add default options
$options = CurlMultiClient::getDefaultOptions();
+   }
 
// add curl options to each handle
-   foreach( $requests as $k = $row ){
+   foreach ( $requests as $k = $row ) {
$handle = curl_init();
-   $options[CURLOPT_URL] = $row['url'];
-   wfDebug(adding url:  . $row['url']);
-   if ( array_key_exists( 'headers', $row ) ) {
-   $options[CURLOPT_HTTPHEADER] = $row['headers'];
-   } else if ( array_key_exists( CURLOPT_HTTPHEADER, 
$options ) ) {
-   unset( $options[CURLOPT_HTTPHEADER] );
+   $reqOptions = array( CURLOPT_URL = $row['url'] ) + 
$options;
+   wfDebug( adding url:  . $row['url'] );
+   if ( isset( $row['headers'] ) ) {
+   $reqOptions[CURLOPT_HTTPHEADER] = 
$row['headers'];
}
-   curl_setopt_array($handle, $options);
+   curl_setopt_array( $handle, $reqOptions );
 
$handles[$k] = $handle;
}
 
$mh = curl_multi_init();
 
-   foreach( $handles as $handle ){
-   curl_multi_add_handle($mh,$handle);
+   foreach ( $handles as $handle ) {
+   curl_multi_add_handle( $mh, $handle );
}
 
$running_handles = null;
//execute the handles
do {
-   $status_cme = curl_multi_exec($mh, $running_handles);
+   $status_cme = curl_multi_exec( $mh, $running_handles );
} while ( $status_cme == CURLM_CALL_MULTI_PERFORM );
 
while ( $running_handles  $status_cme == CURLM_OK ) {
-   if ( curl_multi_select($mh) != -1 ) {
+   if ( curl_multi_select( $mh ) != -1 ) {
  

[MediaWiki-commits] [Gerrit] Various small code cleanups: - change (mediawiki...Parsoid)

2013-05-28 Thread Aaron Schulz (Code Review)
Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/65869


Change subject: Various small code cleanups:
..

Various small code cleanups:

* Use JobQueueGroup::push() in updateTitle().
* Made CurlMultiClient::request() return an array if no urls are given.
  Also cleaned up the options and error checking code a bit.
* Code style formatting.
* Added some type hints and make use of the 'self' keyword.
* Added some TODO/FIXME comments.

Change-Id: Iad4de0f0017b3f079971d76523d3a5a5487e9155
---
M php/CurlMultiClient.php
M php/Parsoid.hooks.php
M php/Parsoid.php
M php/ParsoidCacheUpdateJob.php
4 files changed, 100 insertions(+), 92 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Parsoid 
refs/changes/69/65869/1

diff --git a/php/CurlMultiClient.php b/php/CurlMultiClient.php
index 0ad2619..582a419 100644
--- a/php/CurlMultiClient.php
+++ b/php/CurlMultiClient.php
@@ -1,6 +1,8 @@
 ?php
+
 /**
  * A simple parallel CURL client helper class
+ * @TODO: name this ParsoidCurlMultiClient or move to core
  */
 class CurlMultiClient {
 
@@ -10,10 +12,10 @@
 * @static
 * @returns array default options
 */
-   public static function getDefaultOptions () {
+   public static function getDefaultOptions() {
return array(
-   CURLOPT_HEADER=0,
-   CURLOPT_RETURNTRANSFER=1
+   CURLOPT_HEADER = 0,
+   CURLOPT_RETURNTRANSFER = 1
);
}
 
@@ -23,11 +25,11 @@
 *
 * @static
 * @param $requests array requests, each with an url and an optional
-*  'headers' member:
-*array(
-*  'url' = 'http://server.com/foo',
-*  'headers' = array( 'X-Foo: Bar' )
-*)
+*  'headers' member:
+*array(
+*  'url' = 'http://server.com/foo',
+*  'headers' = array( 'X-Foo: Bar' )
+*)
 * @param $options array curl options used for each request, default
 * {CurlMultiClient::getDefaultOptions}.
 * @returns array An array of arrays containing 'error' and 'data'
@@ -35,45 +37,44 @@
 * errors, the error member will be null and data will contain the
 * response data as a string.
 */
-   public static function request($requests, $options=){
-
-
-   if( !count( $requests ) ) return false;
+   public static function request( $requests, array $options = null ) {
+   if ( !count( $requests ) ) {
+   return array();
+   }
 
$handles = array();
 
-   if( !$options ) // add default options
+   if ( $options === null ) { // add default options
$options = CurlMultiClient::getDefaultOptions();
+   }
 
// add curl options to each handle
-   foreach( $requests as $k = $row ){
+   foreach ( $requests as $k = $row ) {
$handle = curl_init();
-   $options[CURLOPT_URL] = $row['url'];
-   wfDebug(adding url:  . $row['url']);
-   if ( array_key_exists( 'headers', $row ) ) {
-   $options[CURLOPT_HTTPHEADER] = $row['headers'];
-   } else if ( array_key_exists( CURLOPT_HTTPHEADER, 
$options ) ) {
-   unset( $options[CURLOPT_HTTPHEADER] );
+   $reqOptions = array( CURLOPT_URL = $row['url'] ) + 
$options;
+   wfDebug( adding url:  . $row['url'] );
+   if ( isset( $row['headers'] ) ) {
+   $reqOptions[CURLOPT_HTTPHEADER] = 
$row['headers'];
}
-   curl_setopt_array($handle, $options);
+   curl_setopt_array( $handle, $reqOptions );
 
$handles[$k] = $handle;
}
 
$mh = curl_multi_init();
 
-   foreach( $handles as $handle ){
-   curl_multi_add_handle($mh,$handle);
+   foreach ( $handles as $handle ) {
+   curl_multi_add_handle( $mh, $handle );
}
 
$running_handles = null;
//execute the handles
do {
-   $status_cme = curl_multi_exec($mh, $running_handles);
+   $status_cme = curl_multi_exec( $mh, $running_handles );
} while ( $status_cme == CURLM_CALL_MULTI_PERFORM );
 
while ( $running_handles  $status_cme == CURLM_OK ) {
-   if ( curl_multi_select($mh) != -1 ) {
+