r100496 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100495‎ | r100496 | r100497 >
Date:04:21, 22 October 2011
Author:nelson
Status:deferred (Comments)
Tags:
Comment:
unlink all temps we create; unlink the temp that tempnam() creates.
Modified paths:
  • /trunk/extensions/SwiftMedia/SwiftMedia.body.php (modified) (history)
  • /trunk/extensions/SwiftMedia/TODO (modified) (history)

Diff [purge]

Index: trunk/extensions/SwiftMedia/SwiftMedia.body.php
@@ -91,7 +91,7 @@
9292
9393 parent::__construct( $title, $repo );
9494
95 - $this->tempPath = false; // Points to our local copy.
 95+ $this->tempPaths = array(); // Hash from rel to local copy.
9696 }
9797
9898 /** splitMime inherited */
@@ -101,33 +101,61 @@
102102 /** getViewURL inherited */
103103 /** isVisible inherited */
104104
 105+ /**
 106+ * We're re-purposing getPath() to checkout a copy of the file, if we don't already have a copy.
 107+ *
 108+ * @return string Path to a local copy of the file.
 109+ */
105110 public function getPath() {
106 - $this->tempPath = $this->repo->getLocalCopy( $this->repo->container, $this->getRel() );
107 - return $this->tempPath;
 111+ if ( !$this->TempPaths[''] ) {
 112+ $this->tempPaths[''] = $this->repo->getLocalCopy( $this->repo->container, $this->getRel(), 'getPath' );
 113+ }
 114+ return $this->tempPaths[''];
108115 }
109116
110 - /** Get the path of the archive directory, or a particular file if $suffix is specified */
111 - public function getArchivePath( $suffix = false ) {
112 - $this->tempPath = $this->repo->getLocalCopy( $this->repo->getZoneContainer( 'public' ), $this->getArchiveRel( $suffix ) );
113 - return $this->tempPath;
 117+ /**
 118+ * We're re-purposing getPath() to checkout a copy of the file, if we don't already have a copy.
 119+ * Get a local copy of a particular archived file specified by $suffix
 120+ *
 121+ * @param string suffix Specific archived copy.
 122+ * @return string Path to a local copy of the file.
 123+ */
 124+ public function getArchivePath( $suffix = false) {
 125+ if (!$suffix) {
 126+ throw new MWException( "Can't call getArchivePath without a suffix" );
 127+ }
 128+ $rel = $this->getArchiveRel( $suffix );
 129+ if ( !array_key_exists($rel, $this->tempPaths ) ) {
 130+ $this->tempPaths[$rel] = $this->repo->getLocalCopy( $this->repo->container, $rel );
 131+ }
 132+ return $this->tempPaths[$rel];
114133 }
115134
116 - /** Get the path of the thumbnail directory, or a particular file if $suffix is specified */
117 - public function getThumbPath( $suffix = false ) {
118 - $path = $this->getRel();
119 - if ( $suffix !== false ) {
120 - $path .= '/' . $suffix;
 135+ /**
 136+ * We're re-purposing getPath() to checkout a copy of the file, if we don't already have a copy.
 137+ * Get a local copy of a particular thumb specified by $suffix
 138+ *
 139+ * @param string suffix Specific thumbnail.
 140+ * @return string Path to a local copy of the file.
 141+ */
 142+ public function getThumbPath( $suffix = false) {
 143+ if (!$suffix) {
 144+ throw new MWException( "Can't call getThumbPath without a suffix" );
121145 }
122 - $this->tempPath = $this->repo->getLocalCopy( $this->repo->getZoneContainer( 'thumb' ), $path );
123 - return $this->tempPath;
124 - }
 146+ $rel = $this->getRel() . '/' . $suffix;
 147+ if ( !array_key_exists($rel, $this->tempPaths ) ) {
 148+ $this->tempPaths[$rel] = $this->repo->getLocalCopy( $this->repo->getZoneContainer( 'thumb' ), $rel );
 149+ }
 150+ return $this->tempPaths[$rel];
 151+ }
125152
126153 function __destruct() {
127 - if ( $this->tempPath ) {
 154+ foreach ( $this->tempPaths as $path) {
128155 // Clean up temporary data.
129 - unlink( $this->tempPath );
130 - $this->tempPath = null;
 156+ wfDebug( __METHOD__ . ": deleting $path\n" );
 157+ unlink( $path );
131158 }
 159+ $this->tempPaths = array();
132160 }
133161
134162 /**
@@ -146,7 +174,10 @@
147175 global $wgIgnoreImageErrors, $wgThumbnailEpoch, $wgTmpDirectory;
148176
149177 // get a temporary place to put the original.
150 - $thumbPath = tempnam( $wgTmpDirectory, 'transform_out_' ) . '.' . pathinfo( $thumbName, PATHINFO_EXTENSION );
 178+ $thumbPath = tempnam( $wgTmpDirectory, 'transform_out_' );
 179+ unlink( $thumbPath );
 180+ $thumbPath .= '.' . pathinfo( $thumbName, PATHINFO_EXTENSION );
 181+
151182
152183 if ( $this->repo && $this->repo->canTransformVia404() && !( $flags & self::RENDER_NOW ) ) {
153184 return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
@@ -912,10 +943,12 @@
913944 * SwiftFile->tempPath so it will be deleted when the object goes out of
914945 * scope.
915946 */
916 - function getLocalCopy( $container, $rel ) {
 947+ function getLocalCopy( $container, $rel, $prefix = 'swift_in_' ) {
917948
918949 // get a temporary place to put the original.
919 - $tempPath = tempnam( wfTempDir(), 'swift_in_' ) . '.' . pathinfo( $rel, PATHINFO_EXTENSION );
 950+ $tempPath = tempnam( wfTempDir(), $prefix );
 951+ unlink( $tempPath );
 952+ $tempPath .= '.' . pathinfo( $rel, PATHINFO_EXTENSION );
920953
921954 /* Fetch the image out of Swift */
922955 $conn = $this->connect();
Index: trunk/extensions/SwiftMedia/TODO
@@ -17,6 +17,7 @@
1818 Should UploadStashFile *always* (in our case) be called with a virtual URL?
1919 23) TS: "When you get around to implementing SwiftRepo::append(), it will need some sort of concurrency control to avoid having chunks overwrite each other. "
2020 24) TS: "SwiftRepo::swiftcopy() should return a Status object instead of throwing exceptions."
 21+26) Hazmat wonders what happens if two clients fetch missing thumbnails at the same time. What happens when you have overlapping writes to the same object.
2122
2223 Partially resolved:
2324

Comments

#Comment by 😂 (talk | contribs)   12:01, 22 October 2011

You seem to use a mix of $wgTmpDirectory and wfTmpDir. Is there a reason for that?

#Comment by RussNelson (talk | contribs)   20:20, 24 October 2011

I'm sure that I was just copying and pasting code. $wgTmpDirectory seems to be a filerepo-specific, and even more specifically, TempLocalFile-specific global. I'll change it to wfTmpDir.

Status & tagging log