Skip to content

Commit b033d93

Browse files
committed
Add parameter for view/edit in Disdcovery->getWopiClientURL().
1 parent 68baf87 commit b033d93

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/Discovery/Discovery.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ public function __construct(
3232
/**
3333
* {@inheritdoc}
3434
*/
35-
public function getWopiClientURL(string $mimetype = 'text/plain'): ?string {
36-
$result = $this->parsedXml->xpath(sprintf('/wopi-discovery/net-zone/app[@name=\'%s\']/action', $mimetype));
35+
public function getWopiClientURL(string $mimetype = 'text/plain', ?bool $edit = NULL): ?string {
36+
$result = $this->parsedXml->xpath(sprintf(
37+
'/wopi-discovery/net-zone/app[@name=\'%s\']/action%s',
38+
$mimetype,
39+
match ($edit) {
40+
TRUE => "[@name='edit']",
41+
FALSE => "[@name='view']",
42+
NULL => '',
43+
}
44+
));
3745
if (empty($result[0]['urlsrc'][0])) {
3846
return NULL;
3947
}

src/Discovery/DiscoveryInterface.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ interface DiscoveryInterface {
2525
* @param string $mimetype
2626
* Mime type for which to get the WOPI client URL.
2727
* This refers to config entries in the discovery.xml file.
28+
* @param bool|null $edit
29+
* TRUE to get the edit url, FALSE to get the view url, NULL to get
30+
* whichever url comes first, no matter if it has 'view' or 'edit' or none.
31+
* In Collabora, this parameter makes little difference.
2832
*
2933
* @return string|null
30-
* The WOPI client URL, or NULL if none provided for the MIME type.
34+
* The WOPI client URL, or NULL if none provided for the MIME type and
35+
* operation.
3136
*/
32-
public function getWopiClientURL(string $mimetype = 'text/plain'): ?string;
37+
public function getWopiClientURL(string $mimetype = 'text/plain', ?bool $edit = NULL): ?string;
3338

3439
/**
3540
* Gets the public key used for proofing.

tests/fixtures/discovery.mimetypes.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
<app name="text/csv">
88
<action default="true" ext="" name="edit" urlsrc="http://csv.collabora.test:9980/browser/61cf2b4/cool.html?"/>
99
</app>
10+
<app name="text/csv">
11+
<action default="true" ext="" name="view" urlsrc="http://view.csv.collabora.test:9980/browser/61cf2b4/cool.html?"/>
12+
</app>
1013
<app name="text/plain">
11-
<action default="true" ext="" name="edit" urlsrc="http://collabora.test:9980/browser/61cf2b4/cool.html?"/>
14+
<action default="true" ext="" name="view" urlsrc="http://collabora.test:9980/browser/61cf2b4/cool.html?"/>
15+
</app>
16+
<app name="image/png">
17+
<action urlsrc="http://png.collabora.test:9980/browser/61cf2b4/cool.html?"/>
1218
</app>
1319
</net-zone>
1420
</wopi-discovery>

tests/src/Unit/CollaboraDiscoveryTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public function testWopiClientUrl(): void {
3434
$this->assertNull(
3535
$discovery->getWopiClientURL('text/unknown'),
3636
);
37+
$this->assertSame(
38+
'http://csv.collabora.test:9980/browser/61cf2b4/cool.html?',
39+
$discovery->getWopiClientURL('text/csv', TRUE),
40+
);
41+
$this->assertSame(
42+
'http://view.csv.collabora.test:9980/browser/61cf2b4/cool.html?',
43+
$discovery->getWopiClientURL('text/csv', FALSE),
44+
);
45+
// Test the default MIME type 'text/plain' which has only 'edit' action in
46+
// the example file, but no 'view' action.
47+
$this->assertNull($discovery->getWopiClientURL(edit: TRUE));
48+
$this->assertNotNull($discovery->getWopiClientURL(edit: FALSE));
49+
// Test a MIME type with no action name specified.
50+
// This does not occur in the known discovery.xml, but we still want a
51+
// well-defined behavior in that case.
52+
$this->assertNull($discovery->getWopiClientURL('image/png', TRUE));
53+
$this->assertNull($discovery->getWopiClientURL('image/png', FALSE));
54+
$this->assertNotNull($discovery->getWopiClientURL('image/png'));
3755
}
3856

3957
/**

0 commit comments

Comments
 (0)