r69002 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69001‎ | r69002 | r69003 >
Date:15:00, 4 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Work on porting WP filesystem abstraction classes
Modified paths:
  • /trunk/extensions/Deployment/includes/Filesystem.php (modified) (history)
  • /trunk/extensions/Deployment/includes/filesystems/DirectFilesystem.php (modified) (history)
  • /trunk/extensions/Deployment/includes/filesystems/FtpFilesystem.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Deployment/includes/filesystems/FtpFilesystem.php
@@ -488,8 +488,29 @@
489489 /**
490490 * @see Filesystem::writeToFile
491491 */
492 - public function writeToFile( $file, $contents ) {
 492+ public function writeToFile( $file, $contents, $mode = false ) {
 493+ $tempfile = wp_tempnam( $file );
 494+ $temp = fopen( $tempfile, 'w+' );
493495
 496+ if ( !$temp ) {
 497+ return false;
 498+ }
 499+
 500+ fwrite( $temp, $contents );
 501+
 502+ // Skip back to the start of the file being written to
 503+ fseek( $temp, 0 );
 504+
 505+ wfSuppressWarnings();
 506+ $ret = ftp_fput( $this->connection, $file, $temp, $this->isBinary( $contents ) ? FTP_BINARY : FTP_ASCII );
 507+ wfRestoreWarnings();
 508+
 509+ fclose( $temp );
 510+ unlink( $tempfile );
 511+
 512+ $this->chmod( $file, $mode );
 513+
 514+ return $ret;
494515 }
495516
496517 /**
@@ -497,20 +518,23 @@
498519 */
499520 protected function parseListing( $line ) {
500521 static $is_windows;
501 - if ( is_null($is_windows) )
502 - $is_windows = stripos( ftp_systype($this->link), 'win') !== false;
 522+
 523+ if ( is_null( $is_windows ) ) {
 524+ $is_windows = stripos( ftp_systype( $this->connection ), 'win' ) !== false;
 525+ }
503526
504 - if ( $is_windows && preg_match('/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer) ) {
 527+ if ( $is_windows && preg_match( '/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer ) ) {
505528 $b = array();
506 - if ( $lucifer[3] < 70 )
507 - $lucifer[3] +=2000;
508 - else
509 - $lucifer[3] += 1900; // 4digit year fix
510 - $b['isdir'] = ( $lucifer[7] == '<DIR>');
511 - if ( $b['isdir'] )
512 - $b['type'] = 'd';
513 - else
514 - $b['type'] = 'f';
 529+
 530+ if ( $lucifer[3] < 70 ) {
 531+ $lucifer[3] += 2000;
 532+ }
 533+ else {
 534+ $lucifer[3] += 1900; // 4 digit year fix
 535+ }
 536+
 537+ $b['isdir'] = ( $lucifer[7] == '<DIR>' );
 538+ $b['type'] = $b['isdir'] ? 'd' : 'f';
515539 $b['size'] = $lucifer[7];
516540 $b['month'] = $lucifer[1];
517541 $b['day'] = $lucifer[2];
@@ -520,35 +544,46 @@
521545 $b['time'] = @mktime($lucifer[4] + (strcasecmp($lucifer[6], "PM") == 0 ? 12 : 0), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3]);
522546 $b['am/pm'] = $lucifer[6];
523547 $b['name'] = $lucifer[8];
524 - } elseif ( !$is_windows && $lucifer = preg_split('/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY)) {
 548+ } elseif ( !$is_windows && $lucifer = preg_split( '/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY ) ) {
525549 //echo $line."\n";
 550+
526551 $lcount = count($lucifer);
527 - if ( $lcount < 8 )
 552+
 553+ if ( $lcount < 8 ) {
528554 return '';
 555+ }
 556+
529557 $b = array();
530558 $b['isdir'] = $lucifer[0]{0} === 'd';
531559 $b['islink'] = $lucifer[0]{0} === 'l';
532 - if ( $b['isdir'] )
 560+ if ( $b['isdir'] ) {
533561 $b['type'] = 'd';
534 - elseif ( $b['islink'] )
 562+ }
 563+ elseif ( $b['islink'] ) {
535564 $b['type'] = 'l';
536 - else
 565+ }
 566+ else {
537567 $b['type'] = 'f';
 568+ }
538569 $b['perms'] = $lucifer[0];
539570 $b['number'] = $lucifer[1];
540571 $b['owner'] = $lucifer[2];
541572 $b['group'] = $lucifer[3];
542573 $b['size'] = $lucifer[4];
 574+
543575 if ( $lcount == 8 ) {
544 - sscanf($lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day']);
545 - sscanf($lucifer[6], '%d:%d', $b['hour'], $b['minute']);
546 - $b['time'] = @mktime($b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year']);
 576+ sscanf( $lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day'] );
 577+ sscanf( $lucifer[6], '%d:%d', $b['hour'], $b['minute'] );
 578+ wfSuppressWarnings();
 579+ $b['time'] = mktime( $b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year'] );
 580+ wfRestoreWarnings();
547581 $b['name'] = $lucifer[7];
548582 } else {
549583 $b['month'] = $lucifer[5];
550584 $b['day'] = $lucifer[6];
551 - if ( preg_match('/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2) ) {
552 - $b['year'] = date("Y");
 585+
 586+ if ( preg_match( '/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2 ) ) {
 587+ $b['year'] = date( 'Y' );
553588 $b['hour'] = $l2[1];
554589 $b['minute'] = $l2[2];
555590 } else {
@@ -556,7 +591,8 @@
557592 $b['hour'] = 0;
558593 $b['minute'] = 0;
559594 }
560 - $b['time'] = strtotime( sprintf('%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute']) );
 595+
 596+ $b['time'] = strtotime( sprintf( '%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute'] ) );
561597 $b['name'] = $lucifer[8];
562598 }
563599 }
Index: trunk/extensions/Deployment/includes/filesystems/DirectFilesystem.php
@@ -472,8 +472,23 @@
473473 /**
474474 * @see Filesystem::writeToFile
475475 */
476 - public function writeToFile( $file, $contents ) {
 476+ public function writeToFile( $file, $contents, $mode = false ) {
 477+ wfSuppressWarnings();
 478+ $fopen = fopen( $file, 'w' );
 479+ wfRestoreWarnings();
477480
 481+ if ( !( $fp = $fopen ) ) {
 482+ return false;
 483+ }
 484+
 485+ wfSuppressWarnings();
 486+ fwrite( $fp, $contents );
 487+ fclose( $fp );
 488+ wfRestoreWarnings();
 489+
 490+ $this->chmod( $file, $mode );
 491+
 492+ return true;
478493 }
479494
480495 }
\ No newline at end of file
Index: trunk/extensions/Deployment/includes/Filesystem.php
@@ -53,12 +53,13 @@
5454 /**
5555 * Writes a string to a file.
5656 *
57 - * @param string $file Path to the file.
58 - * @param string $contents
 57+ * @param $file String: Path to the file.
 58+ * @param $contents String
 59+ * @param $mode Boolean
5960 *
6061 * @return boolean Success indicator
6162 */
62 - public abstract function writeToFile( $file, $contents );
 63+ public abstract function writeToFile( $file, $contents, $mode = false );
6364
6465 /**
6566 * Gets the current working directory.
@@ -356,4 +357,15 @@
357358 wfDebug( $error );
358359 }
359360
 361+ /**
 362+ * Determines if the string provided contains binary characters.
 363+ *
 364+ * @param $text String: the contents to test against
 365+ *
 366+ * @return Boolean: true if string is binary, false otherwise
 367+ */
 368+ protected function isBinary( $text ) {
 369+ return (bool)preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
 370+ }
 371+
360372 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r69004Follow up to r69002jeroendedauw15:06, 4 July 2010

Status & tagging log