Skip to content

Commit a34dc5d

Browse files
authored
Merge branch 'v2.x' into PHPLIB-953
2 parents d4f5f59 + 0552779 commit a34dc5d

File tree

15 files changed

+113
-170
lines changed

15 files changed

+113
-170
lines changed

UPGRADE-2.0.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,29 @@ UPGRADE FROM 1.x to 2.0
22
========================
33

44
* Classes in the namespace `MongoDB\Operation\` are `final`.
5+
6+
GridFS
7+
------
8+
9+
* The `md5` is no longer calculated when a file is uploaded to GridFS.
10+
Applications that require a file digest should implement it outside GridFS
11+
and store in metadata.
12+
13+
```php
14+
$hash = hash_file('sha256', $filename);
15+
$bucket->openUploadStream($fileId, ['metadata' => ['hash' => $hash]]);
16+
```
17+
18+
* The fields `contentType` and `aliases` are no longer stored in the `files`
19+
collection. Applications that require this information should store it in
20+
metadata.
21+
22+
**Before:**
23+
```php
24+
$bucket->openUploadStream($fileId, ['contentType' => 'image/png']);
25+
```
26+
27+
**After:**
28+
```php
29+
$bucket->openUploadStream($fileId, ['metadata' => ['contentType' => 'image/png']]);
30+
```

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
],
1212
"require": {
1313
"php": "^8.1",
14-
"ext-hash": "*",
1514
"ext-json": "*",
1615
"ext-mongodb": "^1.20.0",
1716
"composer-runtime-api": "^2.0",

generator/config/expression/round.yaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ type:
88
- resolvesToLong
99
encode: array
1010
description: |
11-
Rounds a number to to a whole integer or to a specified decimal place.
11+
Rounds a number to a whole integer or to a specified decimal place.
1212
arguments:
1313
-
1414
name: number
1515
type:
16-
- resolvesToInt
17-
- resolvesToDouble
18-
- resolvesToDecimal
19-
- resolvesToLong
16+
- resolvesToNumber
2017
description: |
2118
Can be any valid expression that resolves to a number. Specifically, the expression must resolve to an integer, double, decimal, or long.
2219
$round returns an error if the expression resolves to a non-numeric data type.
@@ -38,3 +35,16 @@ tests:
3835
$round:
3936
- '$value'
4037
- 1
38+
-
39+
name: 'Round Average Rating'
40+
pipeline:
41+
-
42+
$project:
43+
roundedAverageRating:
44+
$avg:
45+
-
46+
$round:
47+
-
48+
$avg:
49+
- '$averageRating'
50+
- 2

src/Builder/Expression/FactoryTrait.php

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/RoundOperator.php

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GridFS/Bucket.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
use function get_resource_type;
4646
use function in_array;
4747
use function is_array;
48-
use function is_bool;
4948
use function is_integer;
5049
use function is_object;
5150
use function is_resource;
@@ -59,11 +58,8 @@
5958
use function stream_copy_to_stream;
6059
use function stream_get_meta_data;
6160
use function stream_get_wrappers;
62-
use function trigger_error;
6361
use function urlencode;
6462

65-
use const E_USER_DEPRECATED;
66-
6763
/**
6864
* Bucket provides a public API for interacting with the GridFS files and chunks
6965
* collections.
@@ -88,8 +84,6 @@ class Bucket
8884

8985
private string $bucketName;
9086

91-
private bool $disableMD5;
92-
9387
private int $chunkSizeBytes;
9488

9589
private ReadConcern $readConcern;
@@ -111,9 +105,6 @@ class Bucket
111105
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
112106
* 261120 (i.e. 255 KiB).
113107
*
114-
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
115-
* each stored file. Defaults to "false".
116-
*
117108
* * readConcern (MongoDB\Driver\ReadConcern): Read concern.
118109
*
119110
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
@@ -129,14 +120,9 @@ class Bucket
129120
*/
130121
public function __construct(private Manager $manager, private string $databaseName, array $options = [])
131122
{
132-
if (isset($options['disableMD5']) && $options['disableMD5'] === false) {
133-
@trigger_error('Setting GridFS "disableMD5" option to "false" is deprecated since mongodb/mongodb 1.18 and will not be supported in version 2.0.', E_USER_DEPRECATED);
134-
}
135-
136123
$options += [
137124
'bucketName' => self::DEFAULT_BUCKET_NAME,
138125
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
139-
'disableMD5' => false,
140126
];
141127

142128
if (! is_string($options['bucketName'])) {
@@ -155,10 +141,6 @@ public function __construct(private Manager $manager, private string $databaseNa
155141
throw InvalidArgumentException::invalidType('"codec" option', $options['codec'], DocumentCodec::class);
156142
}
157143

158-
if (! is_bool($options['disableMD5'])) {
159-
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
160-
}
161-
162144
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
163145
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], ReadConcern::class);
164146
}
@@ -182,7 +164,6 @@ public function __construct(private Manager $manager, private string $databaseNa
182164
$this->bucketName = $options['bucketName'];
183165
$this->chunkSizeBytes = $options['chunkSizeBytes'];
184166
$this->codec = $options['codec'] ?? null;
185-
$this->disableMD5 = $options['disableMD5'];
186167
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
187168
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
188169
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
@@ -211,7 +192,6 @@ public function __debugInfo()
211192
'bucketName' => $this->bucketName,
212193
'codec' => $this->codec,
213194
'databaseName' => $this->databaseName,
214-
'disableMD5' => $this->disableMD5,
215195
'manager' => $this->manager,
216196
'chunkSizeBytes' => $this->chunkSizeBytes,
217197
'readConcern' => $this->readConcern,
@@ -565,9 +545,6 @@ public function openDownloadStreamByName(string $filename, array $options = [])
565545
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to the
566546
* bucket's chunk size.
567547
*
568-
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
569-
* the stored file. Defaults to "false".
570-
*
571548
* * metadata (document): User data for the "metadata" field of the files
572549
* collection document.
573550
*
@@ -579,7 +556,6 @@ public function openUploadStream(string $filename, array $options = [])
579556
{
580557
$options += [
581558
'chunkSizeBytes' => $this->chunkSizeBytes,
582-
'disableMD5' => $this->disableMD5,
583559
];
584560

585561
$path = $this->createPathForUpload();
@@ -658,9 +634,6 @@ public function rename(mixed $id, string $newFilename)
658634
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to the
659635
* bucket's chunk size.
660636
*
661-
* * disableMD5 (boolean): When true, no MD5 sum will be generated for
662-
* the stored file. Defaults to "false".
663-
*
664637
* * metadata (document): User data for the "metadata" field of the files
665638
* collection document.
666639
*
@@ -792,9 +765,9 @@ private function registerStreamWrapper(): void
792765
*
793766
* @see StreamWrapper::setContextResolver()
794767
*
795-
* @param string $path The full url provided to fopen(). It contains the filename.
796-
* gridfs://database_name/collection_name.files/file_name
797-
* @param array{revision?: int, chunkSizeBytes?: int, disableMD5?: bool} $context The options provided to fopen()
768+
* @param string $path The full url provided to fopen(). It contains the filename.
769+
* gridfs://database_name/collection_name.files/file_name
770+
* @param array{revision?: int, chunkSizeBytes?: int} $context The options provided to fopen()
798771
*
799772
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}
800773
*
@@ -825,7 +798,6 @@ private function resolveStreamContext(string $path, string $mode, array $context
825798
'filename' => $filename,
826799
'options' => $context + [
827800
'chunkSizeBytes' => $this->chunkSizeBytes,
828-
'disableMD5' => $this->disableMD5,
829801
],
830802
];
831803
}

src/GridFS/WritableStream.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,15 @@
1717

1818
namespace MongoDB\GridFS;
1919

20-
use HashContext;
2120
use MongoDB\BSON\Binary;
2221
use MongoDB\BSON\ObjectId;
2322
use MongoDB\BSON\UTCDateTime;
2423
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2524
use MongoDB\Exception\InvalidArgumentException;
2625

2726
use function array_intersect_key;
28-
use function hash_final;
29-
use function hash_init;
30-
use function hash_update;
31-
use function is_bool;
3227
use function is_integer;
33-
use function is_string;
3428
use function MongoDB\is_document;
35-
use function MongoDB\is_string_array;
3629
use function sprintf;
3730
use function strlen;
3831
use function substr;
@@ -52,12 +45,8 @@ final class WritableStream
5245

5346
private int $chunkSize;
5447

55-
private bool $disableMD5;
56-
5748
private array $file;
5849

59-
private ?HashContext $hashCtx = null;
60-
6150
private bool $isClosed = false;
6251

6352
private int $length = 0;
@@ -69,19 +58,9 @@ final class WritableStream
6958
*
7059
* * _id (mixed): File document identifier. Defaults to a new ObjectId.
7160
*
72-
* * aliases (array of strings): DEPRECATED An array of aliases.
73-
* Applications wishing to store aliases should add an aliases field to
74-
* the metadata document instead.
75-
*
7661
* * chunkSizeBytes (integer): The chunk size in bytes. Defaults to
7762
* 261120 (i.e. 255 KiB).
7863
*
79-
* * disableMD5 (boolean): When true, no MD5 sum will be generated.
80-
* Defaults to "false".
81-
*
82-
* * contentType (string): DEPRECATED content type to be stored with the
83-
* file. This information should now be added to the metadata.
84-
*
8564
* * metadata (document): User data for the "metadata" field of the files
8665
* collection document.
8766
*
@@ -95,13 +74,8 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
9574
$options += [
9675
'_id' => new ObjectId(),
9776
'chunkSizeBytes' => self::DEFAULT_CHUNK_SIZE_BYTES,
98-
'disableMD5' => false,
9977
];
10078

101-
if (isset($options['aliases']) && ! is_string_array($options['aliases'])) {
102-
throw InvalidArgumentException::invalidType('"aliases" option', $options['aliases'], 'array of strings');
103-
}
104-
10579
if (! is_integer($options['chunkSizeBytes'])) {
10680
throw InvalidArgumentException::invalidType('"chunkSizeBytes" option', $options['chunkSizeBytes'], 'integer');
10781
}
@@ -110,32 +84,18 @@ public function __construct(private CollectionWrapper $collectionWrapper, string
11084
throw new InvalidArgumentException(sprintf('Expected "chunkSizeBytes" option to be >= 1, %d given', $options['chunkSizeBytes']));
11185
}
11286

113-
if (! is_bool($options['disableMD5'])) {
114-
throw InvalidArgumentException::invalidType('"disableMD5" option', $options['disableMD5'], 'boolean');
115-
}
116-
117-
if (isset($options['contentType']) && ! is_string($options['contentType'])) {
118-
throw InvalidArgumentException::invalidType('"contentType" option', $options['contentType'], 'string');
119-
}
120-
12187
if (isset($options['metadata']) && ! is_document($options['metadata'])) {
12288
throw InvalidArgumentException::expectedDocumentType('"metadata" option', $options['metadata']);
12389
}
12490

12591
$this->chunkSize = $options['chunkSizeBytes'];
126-
$this->disableMD5 = $options['disableMD5'];
127-
128-
if (! $this->disableMD5) {
129-
$this->hashCtx = hash_init('md5');
130-
}
131-
13292
$this->file = [
13393
'_id' => $options['_id'],
13494
'chunkSize' => $this->chunkSize,
13595
'filename' => $filename,
13696
'length' => null,
13797
'uploadDate' => null,
138-
] + array_intersect_key($options, ['aliases' => 1, 'contentType' => 1, 'metadata' => 1]);
98+
] + array_intersect_key($options, ['metadata' => 1]);
13999
}
140100

141101
/**
@@ -248,10 +208,6 @@ private function fileCollectionInsert(): void
248208
$this->file['length'] = $this->length;
249209
$this->file['uploadDate'] = new UTCDateTime();
250210

251-
if (! $this->disableMD5 && $this->hashCtx) {
252-
$this->file['md5'] = hash_final($this->hashCtx);
253-
}
254-
255211
try {
256212
$this->collectionWrapper->insertFile($this->file);
257213
} catch (DriverRuntimeException $e) {
@@ -276,10 +232,6 @@ private function insertChunkFromBuffer(): void
276232
'data' => new Binary($data),
277233
];
278234

279-
if (! $this->disableMD5 && $this->hashCtx) {
280-
hash_update($this->hashCtx, $data);
281-
}
282-
283235
try {
284236
$this->collectionWrapper->insertChunk($chunk);
285237
} catch (DriverRuntimeException $e) {

0 commit comments

Comments
 (0)