Index: branches/wmf/1.17wmf1/includes/upload/UploadFromUrl.php |
— | — | @@ -100,41 +100,62 @@ |
101 | 101 | protected function makeTemporaryFile() { |
102 | 102 | return tempnam( wfTempDir(), 'URL' ); |
103 | 103 | } |
| 104 | + |
104 | 105 | /** |
105 | | - * Save the result of a HTTP request to the temporary file |
| 106 | + * Callback: save a chunk of the result of a HTTP request to the temporary file |
106 | 107 | * |
107 | | - * @param $req MWHttpRequest |
108 | | - * @return Status |
| 108 | + * @param $req mixed |
| 109 | + * @param $buffer string |
| 110 | + * @return int number of bytes handled |
109 | 111 | */ |
110 | | - private function saveTempFile( $req ) { |
111 | | - if ( $this->mTempPath === false ) { |
112 | | - return Status::newFatal( 'tmp-create-error' ); |
| 112 | + public function saveTempFileChunk( $req, $buffer ) { |
| 113 | + $nbytes = fwrite( $this->mTmpHandle, $buffer ); |
| 114 | + |
| 115 | + if ( $nbytes == strlen( $buffer ) ) { |
| 116 | + $this->mFileSize += $nbytes; |
| 117 | + } else { |
| 118 | + // Well... that's not good! |
| 119 | + fclose( $this->mTmpHandle ); |
| 120 | + $this->mTmpHandle = false; |
113 | 121 | } |
114 | | - if ( file_put_contents( $this->mTempPath, $req->getContent() ) === false ) { |
115 | | - return Status::newFatal( 'tmp-write-error' ); |
116 | | - } |
117 | 122 | |
118 | | - $this->mFileSize = filesize( $this->mTempPath ); |
| 123 | + return $nbytes; |
| 124 | + } |
119 | 125 | |
120 | | - return Status::newGood(); |
121 | | - } |
122 | 126 | /** |
123 | 127 | * Download the file, save it to the temporary file and update the file |
124 | 128 | * size and set $mRemoveTempFile to true. |
125 | 129 | */ |
126 | 130 | protected function reallyFetchFile() { |
| 131 | + if ( $this->mTempPath === false ) { |
| 132 | + return Status::newFatal( 'tmp-create-error' ); |
| 133 | + } |
| 134 | + |
| 135 | + // Note the temporary file should already be created by makeTemporaryFile() |
| 136 | + $this->mTmpHandle = fopen( $this->mTempPath, 'wb' ); |
| 137 | + if ( !$this->mTmpHandle ) { |
| 138 | + return Status::newFatal( 'tmp-create-error' ); |
| 139 | + } |
| 140 | + |
| 141 | + $this->mRemoveTempFile = true; |
| 142 | + $this->mFileSize = 0; |
| 143 | + |
127 | 144 | $req = MWHttpRequest::factory( $this->mUrl ); |
| 145 | + $req->setCallback( array( $this, 'saveTempFileChunk' ) ); |
128 | 146 | $status = $req->execute(); |
129 | 147 | |
130 | | - if ( !$status->isOk() ) { |
131 | | - return $status; |
| 148 | + if ( $this->mTmpHandle ) { |
| 149 | + // File got written ok... |
| 150 | + fclose( $this->mTmpHandle ); |
| 151 | + $this->mTmpHandle = null; |
| 152 | + } else { |
| 153 | + // We encountered a write error during the download... |
| 154 | + return Status::newFatal( 'tmp-write-error' ); |
132 | 155 | } |
133 | 156 | |
134 | | - $status = $this->saveTempFile( $req ); |
135 | | - if ( !$status->isGood() ) { |
| 157 | + if ( !$status->isOk() ) { |
136 | 158 | return $status; |
137 | 159 | } |
138 | | - $this->mRemoveTempFile = true; |
139 | 160 | |
140 | 161 | return $status; |
141 | 162 | } |
Index: branches/wmf/1.17wmf1/includes/HttpFunctions.php |
— | — | @@ -307,11 +307,26 @@ |
308 | 308 | } |
309 | 309 | |
310 | 310 | /** |
311 | | - * Set the callback |
| 311 | + * Set a read callback to accept data read from the HTTP request. |
| 312 | + * By default, data is appended to an internal buffer which can be |
| 313 | + * retrieved through $req->getContent(). |
312 | 314 | * |
| 315 | + * To handle data as it comes in -- especially for large files that |
| 316 | + * would not fit in memory -- you can instead set your own callback, |
| 317 | + * in the form function($resource, $buffer) where the first parameter |
| 318 | + * is the low-level resource being read (implementation specific), |
| 319 | + * and the second parameter is the data buffer. |
| 320 | + * |
| 321 | + * You MUST return the number of bytes handled in the buffer; if fewer |
| 322 | + * bytes are reported handled than were passed to you, the HTTP fetch |
| 323 | + * will be aborted. |
| 324 | + * |
313 | 325 | * @param $callback Callback |
314 | 326 | */ |
315 | 327 | public function setCallback( $callback ) { |
| 328 | + if ( !is_callable( $callback ) ) { |
| 329 | + throw new MWException( 'Invalid MwHttpRequest callback' ); |
| 330 | + } |
316 | 331 | $this->callback = $callback; |
317 | 332 | } |
318 | 333 | |
Index: branches/wmf/1.17wmf1/includes/MessageBlobStore.php |
— | — | @@ -117,49 +117,37 @@ |
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
121 | | - * Update all message blobs for a given module. |
| 121 | + * Update the message blob for a given module in a given language |
122 | 122 | * |
123 | 123 | * @param $name String: module name |
124 | 124 | * @param $module ResourceLoaderModule object |
125 | | - * @param $lang String: language code (optional) |
126 | | - * @return Mixed: if $lang is set, the new message blob for that language is |
127 | | - * returned if present. Otherwise, null is returned. |
| 125 | + * @param $lang String: language code |
| 126 | + * @return String Regenerated message blob, or null if there was no blob for the given module/language pair |
128 | 127 | */ |
129 | | - public static function updateModule( $name, ResourceLoaderModule $module, $lang = null ) { |
130 | | - $retval = null; |
131 | | - |
132 | | - // Find all existing blobs for this module |
| 128 | + public static function updateModule( $name, ResourceLoaderModule $module, $lang ) { |
133 | 129 | $dbw = wfGetDB( DB_MASTER ); |
134 | | - $res = $dbw->select( 'msg_resource', |
135 | | - array( 'mr_lang', 'mr_blob' ), |
136 | | - array( 'mr_resource' => $name ), |
| 130 | + $row = $dbw->selectRow( 'msg_resource', 'mr_blob', |
| 131 | + array( 'mr_resource' => $name, 'mr_lang' => $lang ), |
137 | 132 | __METHOD__ |
138 | 133 | ); |
139 | | - |
140 | | - // Build the new msg_resource rows |
141 | | - $newRows = array(); |
142 | | - $now = $dbw->timestamp(); |
143 | | - // Save the last-processed old and new blobs for later |
144 | | - $oldBlob = $newBlob = null; |
145 | | - |
146 | | - foreach ( $res as $row ) { |
147 | | - $oldBlob = $row->mr_blob; |
148 | | - $newBlob = self::generateMessageBlob( $module, $row->mr_lang ); |
149 | | - |
150 | | - if ( $row->mr_lang === $lang ) { |
151 | | - $retval = $newBlob; |
152 | | - } |
153 | | - $newRows[] = array( |
154 | | - 'mr_resource' => $name, |
155 | | - 'mr_lang' => $row->mr_lang, |
156 | | - 'mr_blob' => $newBlob, |
157 | | - 'mr_timestamp' => $now |
158 | | - ); |
| 134 | + if ( !$row ) { |
| 135 | + return null; |
159 | 136 | } |
160 | 137 | |
| 138 | + // Save the old and new blobs for later |
| 139 | + $oldBlob = $row->mr_blob; |
| 140 | + $newBlob = self::generateMessageBlob( $module, $lang ); |
| 141 | + |
| 142 | + $newRow = array( |
| 143 | + 'mr_resource' => $name, |
| 144 | + 'mr_lang' => $lang, |
| 145 | + 'mr_blob' => $newBlob, |
| 146 | + 'mr_timestamp' => $dbw->timestamp() |
| 147 | + ); |
| 148 | + |
161 | 149 | $dbw->replace( 'msg_resource', |
162 | 150 | array( array( 'mr_resource', 'mr_lang' ) ), |
163 | | - $newRows, __METHOD__ |
| 151 | + $newRow, __METHOD__ |
164 | 152 | ); |
165 | 153 | |
166 | 154 | // Figure out which messages were added and removed |
— | — | @@ -192,7 +180,7 @@ |
193 | 181 | ); |
194 | 182 | } |
195 | 183 | |
196 | | - return $retval; |
| 184 | + return $newBlob; |
197 | 185 | } |
198 | 186 | |
199 | 187 | /** |
Property changes on: branches/wmf/1.17wmf1/includes/MessageBlobStore.php |
___________________________________________________________________ |
Modified: svn:mergeinfo |
200 | 188 | Merged /trunk/phase3/includes/MessageBlobStore.php:r89005 |