r60009 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60008‎ | r60009 | r60010 >
Date:17:57, 13 December 2009
Author:ashley
Status:ok
Tags:
Comment:
*coding style cleanup
*marked public functions as such in memcached-client.php
Modified paths:
  • /trunk/phase3/includes/JobQueue.php (modified) (history)
  • /trunk/phase3/includes/memcached-client.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/JobQueue.php
@@ -46,16 +46,20 @@
4747 * actually find a job; it may be adversely affected by concurrent job
4848 * runners.
4949 */
50 - static function pop_type($type) {
 50+ static function pop_type( $type ) {
5151 wfProfilein( __METHOD__ );
5252
5353 $dbw = wfGetDB( DB_MASTER );
5454
 55+ $row = $dbw->selectRow(
 56+ 'job',
 57+ '*',
 58+ array( 'job_cmd' => $type ),
 59+ __METHOD__,
 60+ array( 'LIMIT' => 1 )
 61+ );
5562
56 - $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__,
57 - array( 'LIMIT' => 1 ));
58 -
59 - if ($row === false) {
 63+ if ( $row === false ) {
6064 wfProfileOut( __METHOD__ );
6165 return false;
6266 }
@@ -64,7 +68,7 @@
6569 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
6670 $affected = $dbw->affectedRows();
6771
68 - if ($affected == 0) {
 72+ if ( $affected == 0 ) {
6973 wfProfileOut( __METHOD__ );
7074 return false;
7175 }
@@ -75,7 +79,7 @@
7680 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
7781
7882 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
79 - $dbw->immediateCommit();
 83+ $dbw->commit();
8084
8185 wfProfileOut( __METHOD__ );
8286 return $job;
@@ -84,10 +88,10 @@
8589 /**
8690 * Pop a job off the front of the queue
8791 *
88 - * @param $offset Number of jobs to skip
 92+ * @param $offset Integer: Number of jobs to skip
8993 * @return Job or false if there's no jobs
9094 */
91 - static function pop($offset=0) {
 95+ static function pop( $offset = 0 ) {
9296 wfProfileIn( __METHOD__ );
9397
9498 $dbr = wfGetDB( DB_SLAVE );
@@ -100,17 +104,18 @@
101105 */
102106
103107 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
104 - array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
 108+ array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
105109
106110 // Refetching without offset is needed as some of job IDs could have had delayed commits
107111 // and have lower IDs than jobs already executed, blame concurrency :)
108112 //
109 - if ( $row === false) {
110 - if ($offset!=0)
 113+ if ( $row === false ) {
 114+ if ( $offset != 0 ) {
111115 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
112 - array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
 116+ array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
 117+ }
113118
114 - if ($row === false ) {
 119+ if ( $row === false ) {
115120 wfProfileOut( __METHOD__ );
116121 return false;
117122 }
@@ -121,7 +126,7 @@
122127 $dbw = wfGetDB( DB_MASTER );
123128 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
124129 $affected = $dbw->affectedRows();
125 - $dbw->immediateCommit();
 130+ $dbw->commit();
126131
127132 if ( !$affected ) {
128133 // Failed, someone else beat us to it
@@ -135,7 +140,7 @@
136141 }
137142 // Get the random row
138143 $row = $dbw->selectRow( 'job', '*',
139 - 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
 144+ 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
140145 if ( $row === false ) {
141146 // Random job gone before we got the chance to select it
142147 // Give up
@@ -145,7 +150,7 @@
146151 // Delete the random row
147152 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
148153 $affected = $dbw->affectedRows();
149 - $dbw->immediateCommit();
 154+ $dbw->commit();
150155
151156 if ( !$affected ) {
152157 // Random job gone before we exclusively deleted it
Index: trunk/phase3/includes/memcached-client.php
@@ -70,10 +70,9 @@
7171 * @author Ryan T. Dean <rtdean@cytherianage.net>
7272 * @ingroup Cache
7373 */
74 -class MWMemcached
75 -{
76 - // {{{ properties
77 - // {{{ public
 74+class MWMemcached {
 75+ // {{{ properties
 76+ // {{{ public
7877
7978 // {{{ constants
8079 // {{{ flags
@@ -243,17 +242,15 @@
244243 * @param array $args Associative array of settings
245244 *
246245 * @return mixed
247 - * @access public
248246 */
249 - function __construct ($args)
250 - {
251 - $this->set_servers(@$args['servers']);
 247+ public function __construct( $args ) {
 248+ $this->set_servers( @$args['servers'] );
252249 $this->_debug = @$args['debug'];
253250 $this->stats = array();
254251 $this->_compress_threshold = @$args['compress_threshold'];
255 - $this->_persistant = array_key_exists('persistant', $args) ? (@$args['persistant']) : false;
 252+ $this->_persistant = array_key_exists( 'persistant', $args ) ? ( @$args['persistant'] ) : false;
256253 $this->_compress_enable = true;
257 - $this->_have_zlib = function_exists("gzcompress");
 254+ $this->_have_zlib = function_exists( 'gzcompress' );
258255
259256 $this->_cache_sock = array();
260257 $this->_host_dead = array();
@@ -277,11 +274,9 @@
278275 * @param integer $exp (optional) Time to expire data at
279276 *
280277 * @return boolean
281 - * @access public
282278 */
283 - function add ($key, $val, $exp = 0)
284 - {
285 - return $this->_set('add', $key, $val, $exp);
 279+ public function add( $key, $val, $exp = 0 ) {
 280+ return $this->_set( 'add', $key, $val, $exp );
286281 }
287282
288283 // }}}
@@ -294,11 +289,9 @@
295290 * @param integer $amt (optional) Amount to decriment
296291 *
297292 * @return mixed FALSE on failure, value on success
298 - * @access public
299293 */
300 - function decr ($key, $amt=1)
301 - {
302 - return $this->_incrdecr('decr', $key, $amt);
 294+ public function decr( $key, $amt = 1 ) {
 295+ return $this->_incrdecr( 'decr', $key, $amt );
303296 }
304297
305298 // }}}
@@ -311,33 +304,34 @@
312305 * @param integer $time (optional) How long to wait before deleting
313306 *
314307 * @return boolean TRUE on success, FALSE on failure
315 - * @access public
316308 */
317 - function delete ($key, $time = 0)
318 - {
319 - if (!$this->_active)
 309+ public function delete( $key, $time = 0 ) {
 310+ if ( !$this->_active ) {
320311 return false;
 312+ }
321313
322 - $sock = $this->get_sock($key);
323 - if (!is_resource($sock))
 314+ $sock = $this->get_sock( $key );
 315+ if ( !is_resource( $sock ) ) {
324316 return false;
 317+ }
325318
326 - $key = is_array($key) ? $key[1] : $key;
 319+ $key = is_array( $key ) ? $key[1] : $key;
327320
328321 @$this->stats['delete']++;
329322 $cmd = "delete $key $time\r\n";
330 - if(!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
331 - {
332 - $this->_dead_sock($sock);
 323+ if( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
 324+ $this->_dead_sock( $sock );
333325 return false;
334326 }
335 - $res = trim(fgets($sock));
 327+ $res = trim( fgets( $sock ) );
336328
337 - if ($this->_debug)
338 - $this->_debugprint(sprintf("MemCache: delete %s (%s)\n", $key, $res));
 329+ if ( $this->_debug ) {
 330+ $this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
 331+ }
339332
340 - if ($res == "DELETED")
 333+ if ( $res == "DELETED" ) {
341334 return true;
 335+ }
342336 return false;
343337 }
344338
@@ -346,13 +340,11 @@
347341
348342 /**
349343 * Disconnects all connected sockets
350 - *
351 - * @access public
352344 */
353 - function disconnect_all ()
354 - {
355 - foreach ($this->_cache_sock as $sock)
356 - fclose($sock);
 345+ public function disconnect_all() {
 346+ foreach ( $this->_cache_sock as $sock ) {
 347+ fclose( $sock );
 348+ }
357349
358350 $this->_cache_sock = array();
359351 }
@@ -364,11 +356,8 @@
365357 * Enable / Disable compression
366358 *
367359 * @param boolean $enable TRUE to enable, FALSE to disable
368 - *
369 - * @access public
370360 */
371 - function enable_compress ($enable)
372 - {
 361+ public function enable_compress( $enable ) {
373362 $this->_compress_enable = $enable;
374363 }
375364
@@ -377,11 +366,8 @@
378367
379368 /**
380369 * Forget about all of the dead hosts
381 - *
382 - * @access public
383370 */
384 - function forget_dead_hosts ()
385 - {
 371+ public function forget_dead_hosts() {
386372 $this->_host_dead = array();
387373 }
388374
@@ -394,24 +380,22 @@
395381 * @param string $key Key to retrieve
396382 *
397383 * @return mixed
398 - * @access public
399384 */
400 - function get ($key)
401 - {
 385+ public function get( $key ) {
402386 wfProfileIn( __METHOD__ );
403387
404388 if ( $this->_debug ) {
405389 $this->_debugprint( "get($key)\n" );
406390 }
407391
408 - if (!$this->_active) {
 392+ if ( !$this->_active ) {
409393 wfProfileOut( __METHOD__ );
410394 return false;
411395 }
412396
413 - $sock = $this->get_sock($key);
 397+ $sock = $this->get_sock( $key );
414398
415 - if (!is_resource($sock)) {
 399+ if ( !is_resource( $sock ) ) {
416400 wfProfileOut( __METHOD__ );
417401 return false;
418402 }
@@ -419,19 +403,20 @@
420404 @$this->stats['get']++;
421405
422406 $cmd = "get $key\r\n";
423 - if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
424 - {
425 - $this->_dead_sock($sock);
 407+ if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
 408+ $this->_dead_sock( $sock );
426409 wfProfileOut( __METHOD__ );
427410 return false;
428411 }
429412
430413 $val = array();
431 - $this->_load_items($sock, $val);
 414+ $this->_load_items( $sock, $val );
432415
433 - if ($this->_debug)
434 - foreach ($val as $k => $v)
435 - $this->_debugprint(sprintf("MemCache: sock %s got %s\n", serialize($sock), $k));
 416+ if ( $this->_debug ) {
 417+ foreach ( $val as $k => $v ) {
 418+ $this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
 419+ }
 420+ }
436421
437422 wfProfileOut( __METHOD__ );
438423 return @$val[$key];
@@ -446,23 +431,22 @@
447432 * @param array $keys Keys to retrieve
448433 *
449434 * @return array
450 - * @access public
451435 */
452 - function get_multi ($keys)
453 - {
454 - if (!$this->_active)
 436+ public function get_multi( $keys ) {
 437+ if ( !$this->_active ) {
455438 return false;
 439+ }
456440
457441 @$this->stats['get_multi']++;
458442 $sock_keys = array();
459443
460 - foreach ($keys as $key)
461 - {
462 - $sock = $this->get_sock($key);
463 - if (!is_resource($sock)) continue;
464 - $key = is_array($key) ? $key[1] : $key;
465 - if (!isset($sock_keys[$sock]))
466 - {
 444+ foreach ( $keys as $key ) {
 445+ $sock = $this->get_sock( $key );
 446+ if ( !is_resource( $sock ) ) {
 447+ continue;
 448+ }
 449+ $key = is_array( $key ) ? $key[1] : $key;
 450+ if ( !isset( $sock_keys[$sock] ) ) {
467451 $sock_keys[$sock] = array();
468452 $socks[] = $sock;
469453 }
@@ -470,34 +454,31 @@
471455 }
472456
473457 // Send out the requests
474 - foreach ($socks as $sock)
475 - {
476 - $cmd = "get";
477 - foreach ($sock_keys[$sock] as $key)
478 - {
479 - $cmd .= " ". $key;
 458+ foreach ( $socks as $sock ) {
 459+ $cmd = 'get';
 460+ foreach ( $sock_keys[$sock] as $key ) {
 461+ $cmd .= ' ' . $key;
480462 }
481463 $cmd .= "\r\n";
482464
483 - if ($this->_safe_fwrite($sock, $cmd, strlen($cmd)))
484 - {
 465+ if ( $this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
485466 $gather[] = $sock;
486 - } else
487 - {
488 - $this->_dead_sock($sock);
 467+ } else {
 468+ $this->_dead_sock( $sock );
489469 }
490470 }
491471
492472 // Parse responses
493473 $val = array();
494 - foreach ($gather as $sock)
495 - {
496 - $this->_load_items($sock, $val);
 474+ foreach ( $gather as $sock ) {
 475+ $this->_load_items( $sock, $val );
497476 }
498477
499 - if ($this->_debug)
500 - foreach ($val as $k => $v)
501 - $this->_debugprint(sprintf("MemCache: got %s\n", $k));
 478+ if ( $this->_debug ) {
 479+ foreach ( $val as $k => $v ) {
 480+ $this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
 481+ }
 482+ }
502483
503484 return $val;
504485 }
@@ -512,11 +493,9 @@
513494 * @param integer $amt (optional) amount to increment
514495 *
515496 * @return integer New key value?
516 - * @access public
517497 */
518 - function incr ($key, $amt=1)
519 - {
520 - return $this->_incrdecr('incr', $key, $amt);
 498+ public function incr( $key, $amt = 1 ) {
 499+ return $this->_incrdecr( 'incr', $key, $amt );
521500 }
522501
523502 // }}}
@@ -530,11 +509,9 @@
531510 * @param integer $exp (optional) Experiation time
532511 *
533512 * @return boolean
534 - * @access public
535513 */
536 - function replace ($key, $value, $exp=0)
537 - {
538 - return $this->_set('replace', $key, $value, $exp);
 514+ public function replace( $key, $value, $exp = 0 ) {
 515+ return $this->_set( 'replace', $key, $value, $exp );
539516 }
540517
541518 // }}}
@@ -556,22 +533,24 @@
557534 * @return array Output array
558535 * @access public
559536 */
560 - function run_command ($sock, $cmd)
561 - {
562 - if (!is_resource($sock))
 537+ function run_command( $sock, $cmd ) {
 538+ if ( !is_resource( $sock ) ) {
563539 return array();
 540+ }
564541
565 - if (!$this->_safe_fwrite($sock, $cmd, strlen($cmd)))
 542+ if ( !$this->_safe_fwrite( $sock, $cmd, strlen( $cmd ) ) ) {
566543 return array();
 544+ }
567545
568 - while (true)
569 - {
570 - $res = fgets($sock);
 546+ while ( true ) {
 547+ $res = fgets( $sock );
571548 $ret[] = $res;
572 - if (preg_match('/^END/', $res))
 549+ if ( preg_match( '/^END/', $res ) ) {
573550 break;
574 - if (strlen($res) == 0)
 551+ }
 552+ if ( strlen( $res ) == 0 ) {
575553 break;
 554+ }
576555 }
577556 return $ret;
578557 }
@@ -588,11 +567,9 @@
589568 * @param integer $exp (optional) Experiation time
590569 *
591570 * @return boolean TRUE on success
592 - * @access public
593571 */
594 - function set ($key, $value, $exp=0)
595 - {
596 - return $this->_set('set', $key, $value, $exp);
 572+ public function set( $key, $value, $exp = 0 ) {
 573+ return $this->_set( 'set', $key, $value, $exp );
597574 }
598575
599576 // }}}
@@ -602,11 +579,8 @@
603580 * Sets the compression threshold
604581 *
605582 * @param integer $thresh Threshold to compress if larger than
606 - *
607 - * @access public
608583 */
609 - function set_compress_threshold ($thresh)
610 - {
 584+ public function set_compress_threshold( $thresh ) {
611585 $this->_compress_threshold = $thresh;
612586 }
613587
@@ -618,12 +592,9 @@
619593 *
620594 * @param boolean $dbg TRUE for debugging, FALSE otherwise
621595 *
622 - * @access public
623 - *
624596 * @see MWMemcached::__construct
625597 */
626 - function set_debug ($dbg)
627 - {
 598+ public function set_debug( $dbg ) {
628599 $this->_debug = $dbg;
629600 }
630601
@@ -635,20 +606,18 @@
636607 *
637608 * @param array $list Array of servers to connect to
638609 *
639 - * @access public
640 - *
641610 * @see MWMemcached::__construct()
642611 */
643 - function set_servers ($list)
644 - {
 612+ public function set_servers( $list ) {
645613 $this->_servers = $list;
646 - $this->_active = count($list);
 614+ $this->_active = count( $list );
647615 $this->_buckets = null;
648616 $this->_bucketcount = 0;
649617
650618 $this->_single_sock = null;
651 - if ($this->_active == 1)
 619+ if ( $this->_active == 1 ) {
652620 $this->_single_sock = $this->_servers[0];
 621+ }
653622 }
654623
655624 /**
@@ -656,11 +625,8 @@
657626 *
658627 * @param integer $seconds Number of seconds
659628 * @param integer $microseconds Number of microseconds
660 - *
661 - * @access public
662629 */
663 - function set_timeout ($seconds, $microseconds)
664 - {
 630+ public function set_timeout( $seconds, $microseconds ) {
665631 $this->_timeout_seconds = $seconds;
666632 $this->_timeout_microseconds = $microseconds;
667633 }
@@ -677,11 +643,10 @@
678644 *
679645 * @access private
680646 */
681 - function _close_sock ($sock)
682 - {
683 - $host = array_search($sock, $this->_cache_sock);
684 - fclose($this->_cache_sock[$host]);
685 - unset($this->_cache_sock[$host]);
 647+ function _close_sock( $sock ) {
 648+ $host = array_search( $sock, $this->_cache_sock );
 649+ fclose( $this->_cache_sock[$host] );
 650+ unset( $this->_cache_sock[$host] );
686651 }
687652
688653 // }}}
@@ -696,29 +661,27 @@
697662 * @return boolean
698663 * @access private
699664 */
700 - function _connect_sock (&$sock, $host)
701 - {
702 - list ($ip, $port) = explode(":", $host);
 665+ function _connect_sock( &$sock, $host ) {
 666+ list( $ip, $port ) = explode( ':', $host );
703667 $sock = false;
704668 $timeout = $this->_connect_timeout;
705669 $errno = $errstr = null;
706 - for ($i = 0; !$sock && $i < $this->_connect_attempts; $i++) {
707 - if ($this->_persistant == 1)
708 - {
709 - $sock = @pfsockopen($ip, $port, $errno, $errstr, $timeout);
710 - } else
711 - {
712 - $sock = @fsockopen($ip, $port, $errno, $errstr, $timeout);
 670+ for( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
 671+ if ( $this->_persistant == 1 ) {
 672+ $sock = @pfsockopen( $ip, $port, $errno, $errstr, $timeout );
 673+ } else {
 674+ $sock = @fsockopen( $ip, $port, $errno, $errstr, $timeout );
713675 }
714676 }
715 - if (!$sock) {
716 - if ($this->_debug)
 677+ if ( !$sock ) {
 678+ if ( $this->_debug ) {
717679 $this->_debugprint( "Error connecting to $host: $errstr\n" );
 680+ }
718681 return false;
719682 }
720683
721684 // Initialise timeout
722 - stream_set_timeout($sock, $this->_timeout_seconds, $this->_timeout_microseconds);
 685+ stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
723686
724687 return true;
725688 }
@@ -733,18 +696,16 @@
734697 *
735698 * @access private
736699 */
737 - function _dead_sock ($sock)
738 - {
739 - $host = array_search($sock, $this->_cache_sock);
740 - $this->_dead_host($host);
 700+ function _dead_sock( $sock ) {
 701+ $host = array_search( $sock, $this->_cache_sock );
 702+ $this->_dead_host( $host );
741703 }
742704
743 - function _dead_host ($host)
744 - {
745 - @list ($ip, /* $port */) = explode(":", $host);
746 - $this->_host_dead[$ip] = time() + 30 + intval(rand(0, 10));
 705+ function _dead_host( $host ) {
 706+ @list( $ip, /* $port */) = explode( ':', $host );
 707+ $this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
747708 $this->_host_dead[$host] = $this->_host_dead[$ip];
748 - unset($this->_cache_sock[$host]);
 709+ unset( $this->_cache_sock[$host] );
749710 }
750711
751712 // }}}
@@ -758,44 +719,40 @@
759720 * @return mixed resource on success, false on failure
760721 * @access private
761722 */
762 - function get_sock ($key)
763 - {
764 - if (!$this->_active)
 723+ function get_sock( $key ) {
 724+ if ( !$this->_active ) {
765725 return false;
 726+ }
766727
767 - if ($this->_single_sock !== null) {
768 - $this->_flush_read_buffer($this->_single_sock);
769 - return $this->sock_to_host($this->_single_sock);
 728+ if ( $this->_single_sock !== null ) {
 729+ $this->_flush_read_buffer( $this->_single_sock );
 730+ return $this->sock_to_host( $this->_single_sock );
770731 }
771732
772 - $hv = is_array($key) ? intval($key[0]) : $this->_hashfunc($key);
 733+ $hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
773734
774 - if ($this->_buckets === null)
775 - {
776 - foreach ($this->_servers as $v)
777 - {
778 - if (is_array($v))
779 - {
780 - for ($i=0; $i<$v[1]; $i++)
 735+ if ( $this->_buckets === null ) {
 736+ foreach ( $this->_servers as $v ) {
 737+ if ( is_array( $v ) ) {
 738+ for( $i = 0; $i < $v[1]; $i++ ) {
781739 $bu[] = $v[0];
782 - } else
783 - {
 740+ }
 741+ } else {
784742 $bu[] = $v;
785743 }
786744 }
787745 $this->_buckets = $bu;
788 - $this->_bucketcount = count($bu);
 746+ $this->_bucketcount = count( $bu );
789747 }
790748
791 - $realkey = is_array($key) ? $key[1] : $key;
792 - for ($tries = 0; $tries<20; $tries++)
793 - {
 749+ $realkey = is_array( $key ) ? $key[1] : $key;
 750+ for( $tries = 0; $tries < 20; $tries++ ) {
794751 $host = $this->_buckets[$hv % $this->_bucketcount];
795 - $sock = $this->sock_to_host($host);
796 - if (is_resource($sock)) {
797 - $this->_flush_read_buffer($sock);
 752+ $sock = $this->sock_to_host( $host );
 753+ if ( is_resource( $sock ) ) {
 754+ $this->_flush_read_buffer( $sock );
798755 return $sock;
799 - }
 756+ }
800757 $hv = $this->_hashfunc( $hv . $realkey );
801758 }
802759
@@ -813,12 +770,11 @@
814771 * @return integer Hash value
815772 * @access private
816773 */
817 - function _hashfunc ($key)
818 - {
 774+ function _hashfunc( $key ) {
819775 # Hash function must on [0,0x7ffffff]
820776 # We take the first 31 bits of the MD5 hash, which unlike the hash
821777 # function used in a previous version of this client, works
822 - return hexdec(substr(md5($key),0,8)) & 0x7fffffff;
 778+ return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
823779 }
824780
825781 // }}}
@@ -834,24 +790,27 @@
835791 * @return integer New value of $key
836792 * @access private
837793 */
838 - function _incrdecr ($cmd, $key, $amt=1)
839 - {
840 - if (!$this->_active)
 794+ function _incrdecr( $cmd, $key, $amt = 1 ) {
 795+ if ( !$this->_active ) {
841796 return null;
 797+ }
842798
843 - $sock = $this->get_sock($key);
844 - if (!is_resource($sock))
 799+ $sock = $this->get_sock( $key );
 800+ if ( !is_resource( $sock ) ) {
845801 return null;
 802+ }
846803
847 - $key = is_array($key) ? $key[1] : $key;
 804+ $key = is_array( $key ) ? $key[1] : $key;
848805 @$this->stats[$cmd]++;
849 - if (!$this->_safe_fwrite($sock, "$cmd $key $amt\r\n"))
850 - return $this->_dead_sock($sock);
 806+ if ( !$this->_safe_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
 807+ return $this->_dead_sock( $sock );
 808+ }
851809
852 - $line = fgets($sock);
 810+ $line = fgets( $sock );
853811 $match = array();
854 - if (!preg_match('/^(\d+)/', $line, $match))
 812+ if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
855813 return null;
 814+ }
856815 return $match[1];
857816 }
858817
@@ -866,53 +825,50 @@
867826 *
868827 * @access private
869828 */
870 - function _load_items ($sock, &$ret)
871 - {
872 - while (1)
873 - {
874 - $decl = fgets($sock);
875 - if ($decl == "END\r\n")
876 - {
 829+ function _load_items( $sock, &$ret ) {
 830+ while ( 1 ) {
 831+ $decl = fgets( $sock );
 832+ if ( $decl == "END\r\n" ) {
877833 return true;
878 - } elseif (preg_match('/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match))
879 - {
880 - list($rkey, $flags, $len) = array($match[1], $match[2], $match[3]);
881 - $bneed = $len+2;
 834+ } elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+)\r\n$/', $decl, $match ) ) {
 835+ list( $rkey, $flags, $len ) = array( $match[1], $match[2], $match[3] );
 836+ $bneed = $len + 2;
882837 $offset = 0;
883838
884 - while ($bneed > 0)
885 - {
886 - $data = fread($sock, $bneed);
887 - $n = strlen($data);
888 - if ($n == 0)
 839+ while ( $bneed > 0 ) {
 840+ $data = fread( $sock, $bneed );
 841+ $n = strlen( $data );
 842+ if ( $n == 0 ) {
889843 break;
 844+ }
890845 $offset += $n;
891846 $bneed -= $n;
892847 @$ret[$rkey] .= $data;
893848 }
894849
895 - if ($offset != $len+2)
896 - {
 850+ if ( $offset != $len + 2 ) {
897851 // Something is borked!
898 - if ($this->_debug)
899 - $this->_debugprint(sprintf("Something is borked! key %s expecting %d got %d length\n", $rkey, $len+2, $offset));
 852+ if ( $this->_debug ) {
 853+ $this->_debugprint( sprintf( "Something is borked! key %s expecting %d got %d length\n", $rkey, $len + 2, $offset ) );
 854+ }
900855
901 - unset($ret[$rkey]);
902 - $this->_close_sock($sock);
 856+ unset( $ret[$rkey] );
 857+ $this->_close_sock( $sock );
903858 return false;
904859 }
905860
906 - if ($this->_have_zlib && $flags & self::COMPRESSED)
907 - $ret[$rkey] = gzuncompress($ret[$rkey]);
 861+ if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
 862+ $ret[$rkey] = gzuncompress( $ret[$rkey] );
 863+ }
908864
909 - $ret[$rkey] = rtrim($ret[$rkey]);
 865+ $ret[$rkey] = rtrim( $ret[$rkey] );
910866
911 - if ($flags & self::SERIALIZED)
912 - $ret[$rkey] = unserialize($ret[$rkey]);
 867+ if ( $flags & self::SERIALIZED ) {
 868+ $ret[$rkey] = unserialize( $ret[$rkey] );
 869+ }
913870
914 - } else
915 - {
916 - $this->_debugprint("Error parsing memcached response\n");
 871+ } else {
 872+ $this->_debugprint( "Error parsing memcached response\n" );
917873 return 0;
918874 }
919875 }
@@ -932,55 +888,57 @@
933889 * @return boolean
934890 * @access private
935891 */
936 - function _set ($cmd, $key, $val, $exp)
937 - {
938 - if (!$this->_active)
 892+ function _set( $cmd, $key, $val, $exp ) {
 893+ if ( !$this->_active ) {
939894 return false;
 895+ }
940896
941 - $sock = $this->get_sock($key);
942 - if (!is_resource($sock))
 897+ $sock = $this->get_sock( $key );
 898+ if ( !is_resource( $sock ) ) {
943899 return false;
 900+ }
944901
945902 @$this->stats[$cmd]++;
946903
947904 $flags = 0;
948905
949 - if (!is_scalar($val))
950 - {
951 - $val = serialize($val);
 906+ if ( !is_scalar( $val ) ) {
 907+ $val = serialize( $val );
952908 $flags |= self::SERIALIZED;
953 - if ($this->_debug)
954 - $this->_debugprint(sprintf("client: serializing data as it is not scalar\n"));
 909+ if ( $this->_debug ) {
 910+ $this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
 911+ }
955912 }
956913
957 - $len = strlen($val);
 914+ $len = strlen( $val );
958915
959 - if ($this->_have_zlib && $this->_compress_enable &&
960 - $this->_compress_threshold && $len >= $this->_compress_threshold)
 916+ if ( $this->_have_zlib && $this->_compress_enable &&
 917+ $this->_compress_threshold && $len >= $this->_compress_threshold )
961918 {
962 - $c_val = gzcompress($val, 9);
963 - $c_len = strlen($c_val);
 919+ $c_val = gzcompress( $val, 9 );
 920+ $c_len = strlen( $c_val );
964921
965 - if ($c_len < $len*(1 - self::COMPRESSION_SAVINGS))
966 - {
967 - if ($this->_debug)
968 - $this->_debugprint(sprintf("client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len));
 922+ if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
 923+ if ( $this->_debug ) {
 924+ $this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
 925+ }
969926 $val = $c_val;
970927 $len = $c_len;
971928 $flags |= self::COMPRESSED;
972929 }
973930 }
974 - if (!$this->_safe_fwrite($sock, "$cmd $key $flags $exp $len\r\n$val\r\n"))
975 - return $this->_dead_sock($sock);
 931+ if ( !$this->_safe_fwrite( $sock, "$cmd $key $flags $exp $len\r\n$val\r\n" ) ) {
 932+ return $this->_dead_sock( $sock );
 933+ }
976934
977 - $line = trim(fgets($sock));
 935+ $line = trim( fgets( $sock ) );
978936
979 - if ($this->_debug)
980 - {
981 - $this->_debugprint(sprintf("%s %s (%s)\n", $cmd, $key, $line));
 937+ if ( $this->_debug ) {
 938+ $this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
982939 }
983 - if ($line == "STORED")
 940+ if ( $line == "STORED" ) {
984941 return true;
 942+ }
985943 return false;
986944 }
987945
@@ -995,31 +953,34 @@
996954 * @return mixed IO Stream or false
997955 * @access private
998956 */
999 - function sock_to_host ($host)
1000 - {
1001 - if (isset($this->_cache_sock[$host]))
 957+ function sock_to_host( $host ) {
 958+ if ( isset( $this->_cache_sock[$host] ) ) {
1002959 return $this->_cache_sock[$host];
 960+ }
1003961
1004962 $sock = null;
1005963 $now = time();
1006 - list ($ip, /* $port */) = explode (":", $host);
1007 - if (isset($this->_host_dead[$host]) && $this->_host_dead[$host] > $now ||
1008 - isset($this->_host_dead[$ip]) && $this->_host_dead[$ip] > $now)
 964+ list( $ip, /* $port */) = explode( ':', $host );
 965+ if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
 966+ isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
 967+ ) {
1009968 return null;
 969+ }
1010970
1011 - if (!$this->_connect_sock($sock, $host))
1012 - return $this->_dead_host($host);
 971+ if ( !$this->_connect_sock( $sock, $host ) ) {
 972+ return $this->_dead_host( $host );
 973+ }
1013974
1014975 // Do not buffer writes
1015 - stream_set_write_buffer($sock, 0);
 976+ stream_set_write_buffer( $sock, 0 );
1016977
1017978 $this->_cache_sock[$host] = $sock;
1018979
1019980 return $this->_cache_sock[$host];
1020981 }
1021982
1022 - function _debugprint($str){
1023 - print($str);
 983+ function _debugprint( $str ) {
 984+ print( $str );
1024985 }
1025986
1026987 /**
@@ -1028,21 +989,21 @@
1029990 * @return bool false on failure, true on success
1030991 */
1031992 /*
1032 - function _safe_fwrite($f, $buf, $len = false) {
1033 - stream_set_blocking($f, 0);
 993+ function _safe_fwrite( $f, $buf, $len = false ) {
 994+ stream_set_blocking( $f, 0 );
1034995
1035 - if ($len === false) {
1036 - wfDebug("Writing " . strlen( $buf ) . " bytes\n");
1037 - $bytesWritten = fwrite($f, $buf);
 996+ if ( $len === false ) {
 997+ wfDebug( "Writing " . strlen( $buf ) . " bytes\n" );
 998+ $bytesWritten = fwrite( $f, $buf );
1038999 } else {
1039 - wfDebug("Writing $len bytes\n");
1040 - $bytesWritten = fwrite($f, $buf, $len);
 1000+ wfDebug( "Writing $len bytes\n" );
 1001+ $bytesWritten = fwrite( $f, $buf, $len );
10411002 }
1042 - $n = stream_select($r=NULL, $w = array($f), $e = NULL, 10, 0);
1043 - # $this->_timeout_seconds, $this->_timeout_microseconds);
 1003+ $n = stream_select( $r = null, $w = array( $f ), $e = null, 10, 0 );
 1004+ # $this->_timeout_seconds, $this->_timeout_microseconds );
10441005
1045 - wfDebug("stream_select returned $n\n");
1046 - stream_set_blocking($f, 1);
 1006+ wfDebug( "stream_select returned $n\n" );
 1007+ stream_set_blocking( $f, 1 );
10471008 return $n == 1;
10481009 return $bytesWritten;
10491010 }*/
@@ -1050,11 +1011,11 @@
10511012 /**
10521013 * Original behaviour
10531014 */
1054 - function _safe_fwrite($f, $buf, $len = false) {
1055 - if ($len === false) {
1056 - $bytesWritten = fwrite($f, $buf);
 1015+ function _safe_fwrite( $f, $buf, $len = false ) {
 1016+ if ( $len === false ) {
 1017+ $bytesWritten = fwrite( $f, $buf );
10571018 } else {
1058 - $bytesWritten = fwrite($f, $buf, $len);
 1019+ $bytesWritten = fwrite( $f, $buf, $len );
10591020 }
10601021 return $bytesWritten;
10611022 }
@@ -1062,14 +1023,14 @@
10631024 /**
10641025 * Flush the read buffer of a stream
10651026 */
1066 - function _flush_read_buffer($f) {
1067 - if (!is_resource($f)) {
 1027+ function _flush_read_buffer( $f ) {
 1028+ if ( !is_resource( $f ) ) {
10681029 return;
10691030 }
1070 - $n = stream_select($r=array($f), $w = null, $e = null, 0, 0);
1071 - while ($n == 1 && !feof($f)) {
1072 - fread($f, 1024);
1073 - $n = stream_select($r=array($f), $w = null, $e = null, 0, 0);
 1031+ $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
 1032+ while ( $n == 1 && !feof( $f ) ) {
 1033+ fread( $f, 1024 );
 1034+ $n = stream_select( $r = array( $f ), $w = null, $e = null, 0, 0 );
10741035 }
10751036 }
10761037

Status & tagging log