r96486 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96485‎ | r96486 | r96487 >
Date:20:21, 7 September 2011
Author:ariel
Status:resolved (Comments)
Tags:
Comment:
get rid of duplication, remove unused function rename(), add documentation as per comments on r95260
Modified paths:
  • /trunk/phase3/includes/Export.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Export.php
@@ -709,17 +709,21 @@
710710 return;
711711 }
712712
713 - // TODO: document
714 - function closeAndRename( $newname ) {
 713+ /**
 714+ * Close the old file, and move it to a specified name.
 715+ * Use this for the last piece of a file written out
 716+ * at specified checkpoints (e.g. every n hours).
 717+ * @param $newname mixed File name. May be a string or an array with one element
 718+ * @param $open bool If true, a new file with the old filename will be opened again for writing (default: false)
 719+ */
 720+ function closeAndRename( $newname, $open = false ) {
715721 return;
716722 }
717723
718 - // TODO: document
719 - function rename( $newname ) {
720 - return;
721 - }
722 -
723 - // TODO: document
 724+ /**
 725+ * Returns the name of the file or files which are
 726+ * being written to, if there are any.
 727+ */
724728 function getFilename() {
725729 return NULL;
726730 }
@@ -742,21 +746,10 @@
743747 }
744748
745749 function closeRenameAndReopen( $newname ) {
746 - if ( is_array( $newname ) ) {
747 - if ( count( $newname ) > 1 ) {
748 - throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
749 - } else {
750 - $newname = $newname[0];
751 - }
752 - }
753 - if ( $newname ) {
754 - fclose( $this->handle );
755 - rename( $this->filename, $newname );
756 - $this->handle = fopen( $this->filename, "wt" );
757 - }
 750+ $this->closeAndRename( $newname, true );
758751 }
759752
760 - function closeAndRename( $newname ) {
 753+ function closeAndRename( $newname, $open = false ) {
761754 if ( is_array( $newname ) ) {
762755 if ( count( $newname ) > 1 ) {
763756 throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
@@ -767,20 +760,10 @@
768761 if ( $newname ) {
769762 fclose( $this->handle );
770763 rename( $this->filename, $newname );
771 - }
772 - }
773 -
774 - function rename( $newname ) {
775 - if ( is_array( $newname ) ) {
776 - if ( count( $newname ) > 1 ) {
777 - throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
778 - } else {
779 - $newname = $newname[0];
 764+ if ( $open ) {
 765+ $this->handle = fopen( $this->filename, "wt" );
780766 }
781767 }
782 - if ( $newname ) {
783 - rename( $this->filename, $newname );
784 - }
785768 }
786769
787770 function getFilename() {
@@ -816,29 +799,11 @@
817800 $this->handle = $pipes[0];
818801 }
819802
820 - /**
821 - * Close the old file, move it to a specified name,
822 - * and reopen new file with the old name.
823 - */
824803 function closeRenameAndReopen( $newname ) {
825 - if ( is_array( $newname ) ) {
826 - if ( count( $newname ) > 1 ) {
827 - throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
828 - } else {
829 - $newname = $newname[0];
830 - }
831 - }
832 - if ( $newname ) {
833 - fclose( $this->handle );
834 - proc_close( $this->procOpenResource );
835 - rename( $this->filename, $newname );
836 - $command = $this->command;
837 - $command .= " > " . wfEscapeShellArg( $this->filename );
838 - $this->startCommand( $command );
839 - }
 804+ $this->closeAndRename( $newname, true );
840805 }
841806
842 - function closeAndRename( $newname ) {
 807+ function closeAndRename( $newname, $open = false ) {
843808 if ( is_array( $newname ) ) {
844809 if ( count( $newname ) > 1 ) {
845810 throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
@@ -847,25 +812,17 @@
848813 }
849814 }
850815 if ( $newname ) {
851 -# pclose( $this->handle );
852816 fclose( $this->handle );
853817 proc_close( $this->procOpenResource );
854818 rename( $this->filename, $newname );
855 - }
856 - }
857 -
858 - function rename( $newname ) {
859 - if ( is_array( $newname ) ) {
860 - if ( count( $newname ) > 1 ) {
861 - throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
862 - } else {
863 - $newname = $newname[0];
 819+ if ( $open ) {
 820+ $command = $this->command;
 821+ $command .= " > " . wfEscapeShellArg( $this->filename );
 822+ $this->startCommand( $command );
864823 }
865824 }
866 - if ( $newname ) {
867 - rename( $this->filename, $newname );
868 - }
869825 }
 826+
870827 }
871828
872829 /**
@@ -896,34 +853,16 @@
897854 protected $filename;
898855
899856 function __construct( $file ) {
900 - $command = "7za a -bd -si " . wfEscapeShellArg( $file );
901 - // Suppress annoying useless crap from p7zip
902 - // Unfortunately this could suppress real error messages too
903 - $command .= ' >' . wfGetNull() . ' 2>&1';
 857+ $command = setup7zCommand( $file );
904858 parent::__construct( $command );
905859 $this->filename = $file;
906860 }
907861
908862 function closeRenameAndReopen( $newname ) {
909 - if ( is_array( $newname ) ) {
910 - if ( count( $newname ) > 1 ) {
911 - throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
912 - }
913 - else {
914 - $newname = $newname[0];
915 - }
916 - }
917 - if ( $newname ) {
918 - fclose( $this->handle );
919 - proc_close( $this->procOpenResource );
920 - rename( $this->filename, $newname );
921 - $command = "7za a -bd -si " . wfEscapeShellArg( $file );
922 - $command .= ' >' . wfGetNull() . ' 2>&1';
923 - $this->startCommand( $command );
924 - }
 863+ $this->closeAndRename( $newname, true );
925864 }
926865
927 - function closeAndRename( $newname ) {
 866+ function closeAndRename( $newname, $open = false ) {
928867 if ( is_array( $newname ) ) {
929868 if ( count( $newname ) > 1 ) {
930869 throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
@@ -935,21 +874,14 @@
936875 fclose( $this->handle );
937876 proc_close( $this->procOpenResource );
938877 rename( $this->filename, $newname );
939 - }
940 - }
941 -
942 - function rename( $newname ) {
943 - if ( is_array( $newname ) ) {
944 - if ( count( $newname ) > 1 ) {
945 - throw new MWException( __METHOD__ . ": passed multiple arguments for rename of single file\n" );
 878+ if ( $open ) {
 879+ $command = "7za a -bd -si " . wfEscapeShellArg( $file );
 880+ // Suppress annoying useless crap from p7zip
 881+ // Unfortunately this could suppress real error messages too
 882+ $command .= ' >' . wfGetNull() . ' 2>&1';
 883+ $this->startCommand( $command );
946884 }
947 - else {
948 - $newname = $newname[0];
949 - }
950885 }
951 - if ( $newname ) {
952 - rename( $this->filename, $newname );
953 - }
954886 }
955887 }
956888
@@ -1002,14 +934,10 @@
1003935 $this->sink->closeRenameAndReopen( $newname );
1004936 }
1005937
1006 - function closeAndRename( $newname ) {
1007 - $this->sink->closeAndRename( $newname );
 938+ function closeAndRename( $newname, $open = false ) {
 939+ $this->sink->closeAndRename( $newname, $open );
1008940 }
1009941
1010 - function rename( $newname ) {
1011 - $this->sink->rename( $newname );
1012 - }
1013 -
1014942 function getFilename() {
1015943 return $this->sink->getFilename();
1016944 }
@@ -1163,21 +1091,14 @@
11641092 }
11651093
11661094 function closeRenameAndReopen( $newnames ) {
1167 - for ( $i = 0; $i < $this->count; $i++ ) {
1168 - $this->sinks[$i]->closeRenameAndReopen( $newnames[$i] );
1169 - }
 1095+ $this->closeAndRename( $newnames, true );
11701096 }
11711097
1172 - function closeAndRename( $newname ) {
 1098+ function closeAndRename( $newnames, $open = false ) {
11731099 for ( $i = 0; $i < $this->count; $i++ ) {
1174 - $this->sinks[$i]->closeAndRename( $newnames[$i] );
 1100+ $this->sinks[$i]->closeAndRename( $newnames[$i], $open );
11751101 }
11761102 }
1177 - function rename( $newnames ) {
1178 - for ( $i = 0; $i < $this->count; $i++ ) {
1179 - $this->sinks[$i]->rename( $newnames[$i] );
1180 - }
1181 - }
11821103
11831104 function getFilename() {
11841105 $filenames = array();

Follow-up revisions

RevisionCommit summaryAuthorDate
r101606corrections for fixme in r96486ariel09:55, 2 November 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r95260add functions that support close and rename of output files as they are being...ariel22:01, 22 August 2011

Comments

#Comment by Platonides (talk | contribs)   21:04, 17 October 2011

Where's setup7zCommand()?

#Comment by ArielGlenn (talk | contribs)   11:16, 2 November 2011

reset to new after followup

Status & tagging log