r92289 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92288‎ | r92289 | r92290 >
Date:19:28, 15 July 2011
Author:reedy
Status:ok
Tags:
Comment:
1.17wmf1: Redoing r92287, merging r89003, r89005 into 1.17wmf1
Modified paths:
  • /branches/wmf/1.17wmf1/includes/HttpFunctions.php (modified) (history)
  • /branches/wmf/1.17wmf1/includes/MessageBlobStore.php (modified) (history)
  • /branches/wmf/1.17wmf1/includes/upload/UploadFromUrl.php (modified) (history)

Diff [purge]

Index: branches/wmf/1.17wmf1/includes/upload/UploadFromUrl.php
@@ -100,41 +100,62 @@
101101 protected function makeTemporaryFile() {
102102 return tempnam( wfTempDir(), 'URL' );
103103 }
 104+
104105 /**
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
106107 *
107 - * @param $req MWHttpRequest
108 - * @return Status
 108+ * @param $req mixed
 109+ * @param $buffer string
 110+ * @return int number of bytes handled
109111 */
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;
113121 }
114 - if ( file_put_contents( $this->mTempPath, $req->getContent() ) === false ) {
115 - return Status::newFatal( 'tmp-write-error' );
116 - }
117122
118 - $this->mFileSize = filesize( $this->mTempPath );
 123+ return $nbytes;
 124+ }
119125
120 - return Status::newGood();
121 - }
122126 /**
123127 * Download the file, save it to the temporary file and update the file
124128 * size and set $mRemoveTempFile to true.
125129 */
126130 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+
127144 $req = MWHttpRequest::factory( $this->mUrl );
 145+ $req->setCallback( array( $this, 'saveTempFileChunk' ) );
128146 $status = $req->execute();
129147
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' );
132155 }
133156
134 - $status = $this->saveTempFile( $req );
135 - if ( !$status->isGood() ) {
 157+ if ( !$status->isOk() ) {
136158 return $status;
137159 }
138 - $this->mRemoveTempFile = true;
139160
140161 return $status;
141162 }
Index: branches/wmf/1.17wmf1/includes/HttpFunctions.php
@@ -307,11 +307,26 @@
308308 }
309309
310310 /**
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().
312314 *
 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+ *
313325 * @param $callback Callback
314326 */
315327 public function setCallback( $callback ) {
 328+ if ( !is_callable( $callback ) ) {
 329+ throw new MWException( 'Invalid MwHttpRequest callback' );
 330+ }
316331 $this->callback = $callback;
317332 }
318333
Index: branches/wmf/1.17wmf1/includes/MessageBlobStore.php
@@ -117,49 +117,37 @@
118118 }
119119
120120 /**
121 - * Update all message blobs for a given module.
 121+ * Update the message blob for a given module in a given language
122122 *
123123 * @param $name String: module name
124124 * @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
128127 */
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 ) {
133129 $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 ),
137132 __METHOD__
138133 );
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;
159136 }
160137
 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+
161149 $dbw->replace( 'msg_resource',
162150 array( array( 'mr_resource', 'mr_lang' ) ),
163 - $newRows, __METHOD__
 151+ $newRow, __METHOD__
164152 );
165153
166154 // Figure out which messages were added and removed
@@ -192,7 +180,7 @@
193181 );
194182 }
195183
196 - return $retval;
 184+ return $newBlob;
197185 }
198186
199187 /**
Property changes on: branches/wmf/1.17wmf1/includes/MessageBlobStore.php
___________________________________________________________________
Modified: svn:mergeinfo
200188 Merged /trunk/phase3/includes/MessageBlobStore.php:r89005

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r89003* (bug 29174) Fix regression in upload-by-URL: files larger than PHP memory l...brion22:31, 27 May 2011
r89005Refactor MessageBlobStore::updateModule() to remove the multi-language update...catrope22:42, 27 May 2011
r922871.17wmf1 MFT r89003, r89005, r90217reedy19:24, 15 July 2011

Status & tagging log