-
Notifications
You must be signed in to change notification settings - Fork 264
PHPLIB-1324 Implement rename
for GridFS stream wrapper
#1207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,15 @@ | |
use MongoDB\GridFS\Exception\FileNotFoundException; | ||
use MongoDB\GridFS\Exception\LogicException; | ||
|
||
use function array_slice; | ||
use function assert; | ||
use function explode; | ||
use function implode; | ||
use function in_array; | ||
use function is_array; | ||
use function is_integer; | ||
use function is_resource; | ||
use function str_starts_with; | ||
use function stream_context_get_options; | ||
use function stream_get_wrappers; | ||
use function stream_wrapper_register; | ||
|
@@ -90,6 +93,30 @@ public static function register(string $protocol = 'gridfs'): void | |
stream_wrapper_register($protocol, static::class, STREAM_IS_URL); | ||
} | ||
|
||
/** | ||
* Rename all revisions of a filename. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this only supports the bucket alias API, since we still only have a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if Drupal needs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a rename from stream wrapper only. If you do |
||
* | ||
* @return bool True on success or false on failure. | ||
*/ | ||
public function rename(string $fromPath, string $toPath): bool | ||
{ | ||
$prefix = implode('/', array_slice(explode('/', $fromPath, 4), 0, 3)) . '/'; | ||
if (! str_starts_with($toPath, $prefix)) { | ||
throw LogicException::renamePathMismatch($fromPath, $toPath); | ||
} | ||
|
||
try { | ||
$this->stream_open($fromPath, 'r', 0, $openedPath); | ||
} catch (FileNotFoundException $e) { | ||
return false; | ||
} | ||
|
||
$newName = explode('/', $toPath, 4)[3] ?? ''; | ||
assert($this->stream instanceof ReadableStream); | ||
|
||
return $this->stream->rename($newName); | ||
} | ||
|
||
/** | ||
* @see Bucket::resolveStreamContext() | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this warrants a spec change to Renaming Stored Files before doing something wildly different in PHPLIB. I don't recall why
filename
matching was not advised in the original spec, but selecting IDs in advance allows for error checking (see below).As I suggested in a previous comment, I think the least controversial spec change would be to use
updateMany
on the list of collected IDs. If we can't wait for that, though, sticking with the spec's existing, inefficient approach would seem the right course of action.With regard to the discussion of successful return values in #1207 (comment), you could assert that
updateMany
modifies exactly as many documents as the size of the ID list being updated and raise an exception if the write result reports an unexpected modified count. Users can decide for themselves if that's an error, but I reckon it'd only come up if a revision was deleted in another process while the rename was executing (between ID collection and issuing theupdateMany
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sincerely, I don't see the difference between collecting IDs first then modifying filenames, vs making 1 single request that finds and modifies filenames.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack - I think since renaming files by filename is not part of the spec and the spec only contains a suggested implementation for users, we should be fine adding this implementation. I'd suggest we amend the spec to add APIs to work on all revisions of a file (i.e. remove and rename by filename instead of by ID).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alcaeus: Good point. I overlooked that the instructions were telling users to run the
rename()
operation for each ID, instead of discussing how a driver should implement filename-based renames.