-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[stable10] Search api stable10 #31946
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
apps/dav/lib/Connector/Sabre/FilesSearchReportPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
/** | ||
* @author Juan Pablo Villafañez <jvillafanez@solidgear.es> | ||
* | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\Connector\Sabre; | ||
|
||
use Sabre\DAV\Server as DavServer; | ||
use Sabre\DAV\ServerPlugin; | ||
use Sabre\DAV\PropFind; | ||
use Sabre\DAV\Exception\BadRequest; | ||
use Sabre\DAV\Exception\NotImplemented; | ||
use OCA\DAV\Files\Xml\SearchRequest; | ||
use OCP\ISearch; | ||
use OC\Search\Result\File as FileResult; | ||
|
||
class FilesSearchReportPlugin extends ServerPlugin { | ||
// namespace | ||
const NS_OWNCLOUD = 'http://owncloud.org/ns'; | ||
const REPORT_NAME = '{http://owncloud.org/ns}search-files'; | ||
|
||
/** | ||
* Reference to main server object | ||
* | ||
* @var DavServer | ||
*/ | ||
private $server; | ||
|
||
/** @var ISearch */ | ||
private $searchService; | ||
|
||
public function __construct(ISearch $searchService) { | ||
$this->searchService = $searchService; | ||
} | ||
|
||
/** | ||
* This initializes the plugin. | ||
* | ||
* This function is called by \Sabre\DAV\Server, after | ||
* addPlugin is called. | ||
* | ||
* This method should set up the required event subscriptions. | ||
* | ||
* @param DavServer $server | ||
* @return void | ||
*/ | ||
public function initialize(DavServer $server) { | ||
$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; | ||
|
||
$server->xml->elementMap[self::REPORT_NAME] = SearchRequest::class; | ||
|
||
$this->server = $server; | ||
$this->server->on('report', [$this, 'onReport']); | ||
} | ||
|
||
/** | ||
* Returns a list of reports this plugin supports. | ||
* | ||
* This will be used in the {DAV:}supported-report-set property. | ||
* | ||
* @param string $uri | ||
* @return array | ||
*/ | ||
public function getSupportedReportSet($uri) { | ||
$reportTargetNode = $this->server->tree->getNodeForPath($uri); | ||
if ($reportTargetNode instanceof Directory && $reportTargetNode->getPath() === '/') { | ||
return [self::REPORT_NAME]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
/** | ||
* REPORT operations to look for files | ||
* | ||
* @param string $reportName | ||
* @param mixed $report | ||
* @param string $uri | ||
* @return bool | ||
* @throws BadRequest | ||
* @throws NotImplemented | ||
* @internal param $ [] $report | ||
*/ | ||
public function onReport($reportName, $report, $uri) { | ||
$reportTargetNode = $this->server->tree->getNodeForPath($uri); | ||
if (!$reportTargetNode instanceof Directory || | ||
$reportName !== self::REPORT_NAME) { | ||
return; | ||
} | ||
|
||
if ($reportTargetNode->getPath() !== '/') { | ||
throw new NotImplemented('Search report only available in the root folder of the user'); | ||
} | ||
|
||
$requestedProps = $report->properties; | ||
$searchInfo = $report->searchInfo; | ||
|
||
if (!isset($searchInfo['pattern'])) { | ||
throw new BadRequest('Search pattern cannot be empty'); | ||
} | ||
|
||
$limit = 30; | ||
if (isset($searchInfo['limit'])) { | ||
$limit = $searchInfo['limit']; | ||
} | ||
|
||
$searchResults = $this->searchService->searchPaged( | ||
$searchInfo['pattern'], | ||
['files'], | ||
1, | ||
$limit | ||
); | ||
|
||
$filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath()); | ||
|
||
$xml = $this->server->generateMultiStatus( | ||
$this->getSearchResultIterator($filesUri, $searchResults, $requestedProps) | ||
); | ||
$this->server->httpResponse->setStatus(207); | ||
$this->server->httpResponse->setHeader( | ||
'Content-Type', | ||
'application/xml; charset=utf-8' | ||
); | ||
$this->server->httpResponse->setBody($xml); | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param string $filesUri the base uri for this user's files directory, | ||
* usually /files/username | ||
* @param File[] $searchResults the results coming from the search service, | ||
* within the files app | ||
* @param array $requestedProps the list of requested webDAV properties | ||
* @return \Generator a generator to traverse over the properties of the | ||
* search result, suitable for server's multistatus response | ||
*/ | ||
private function getSearchResultIterator($filesUri, $searchResults, $requestedProps) { | ||
$paths = \array_map(function ($searchResult) use ($filesUri) { | ||
return $filesUri . $searchResult->path; | ||
}, $searchResults); | ||
|
||
$nodes = $this->server->tree->getMultipleNodes($paths); | ||
|
||
$propFindType = $requestedProps ? PropFind::NORMAL : PropFind::ALLPROPS; | ||
|
||
foreach ($nodes as $path => $node) { | ||
$propFind = new PropFind( | ||
$path, | ||
$requestedProps, | ||
0, | ||
$propFindType | ||
); | ||
$this->server->getPropertiesByNode($propFind, $node); | ||
|
||
$result = $propFind->getResultForMultiStatus(); | ||
$result['href'] = $propFind->getPath(); | ||
yield $result; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the base uri of the files root by removing | ||
* the subpath from the URI | ||
* | ||
* @param string $uri URI from this request | ||
* @param string $subPath subpath to remove from the URI | ||
* | ||
* @return string files base uri | ||
*/ | ||
private function getFilesBaseUri($uri, $subPath) { | ||
$uri = \trim($uri, '/'); | ||
$subPath = \trim($subPath, '/'); | ||
if ($subPath === '') { | ||
$filesUri = $uri; | ||
} else { | ||
$filesUri = \substr($uri, 0, \strlen($uri) - \strlen($subPath)); | ||
} | ||
$filesUri = \trim($filesUri, '/'); | ||
if ($filesUri === '') { | ||
return ''; | ||
} | ||
return '/' . $filesUri; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* @author Juan Pablo Villafañez <jvillafanez@solidgear.es> | ||
* | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\Files\Xml; | ||
|
||
use Sabre\Xml\Element\Base; | ||
use Sabre\Xml\Element\KeyValue; | ||
use Sabre\Xml\Reader; | ||
use Sabre\Xml\XmlDeserializable; | ||
|
||
class SearchRequest implements XmlDeserializable { | ||
/** | ||
* An array with requested properties. | ||
* | ||
* @var array | ||
*/ | ||
public $properties; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $searchInfo; | ||
|
||
public static function xmlDeserialize(Reader $reader) { | ||
$newProps = [ | ||
'properties' => [], | ||
'searchInfo' => null, | ||
]; | ||
|
||
$elems = (array)$reader->parseInnerTree([ | ||
'{DAV:}prop' => KeyValue::class, | ||
'{http://owncloud.org/ns}search' => KeyValue::class, | ||
]); | ||
|
||
if (!\is_array($elems)) { | ||
$elems = []; | ||
} | ||
|
||
foreach ($elems as $elem) { | ||
switch ($elem['name']) { | ||
case '{DAV:}prop': | ||
$newProps['properties'] = \array_keys($elem['value']); | ||
break; | ||
case '{http://owncloud.org/ns}search': | ||
$value = $elem['value']; | ||
if (isset($value['{http://owncloud.org/ns}pattern'])) { | ||
$newProps['searchInfo']['pattern'] = $value['{http://owncloud.org/ns}pattern']; | ||
} | ||
if (isset($value['{http://owncloud.org/ns}limit'])) { | ||
$newProps['searchInfo']['limit'] = (int)$value['{http://owncloud.org/ns}limit']; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
$obj = new self(); | ||
foreach ($newProps as $key => $value) { | ||
$obj->$key = $value; | ||
} | ||
|
||
return $obj; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did we backport zsync?
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.
looks like a conflict resolution mistake. zsync is OC 11 only.
@jvillafanez can you fix ?
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.
Send a fix via #32603
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.
ok already done in #32603