support query splitting and quoting and search content and tags
[grauphel.git] / lib / search / provider.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/grauphel.htm
13  */
14 namespace OCA\Grauphel\Search;
15
16 use \OCA\Grauphel\Lib\NoteStorage;
17
18 /**
19  * Hook for the site-wide owncloud search.
20  *
21  * @category  Tools
22  * @package   Grauphel
23  * @author    Christian Weiske <cweiske@cweiske.de>
24  * @copyright 2014 Christian Weiske
25  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
26  * @version   Release: @package_version@
27  * @link      http://cweiske.de/grauphel.htm
28  */
29 class Provider extends \OCP\Search\Provider
30 {
31         /**
32          * Search for notes
33          *
34          * @param string $query
35      *
36          * @return array list of \OCA\Grauphel\Search\Note
37          */
38         public function search($query)
39     {
40         $urlGen = \OC::$server->getURLGenerator();
41         $notes  = new NoteStorage($urlGen);
42         $notes->setUsername(\OC_User::getUser());
43         $rows = $notes->search($this->parseQuery($query));
44
45         $results = array();
46         foreach ($rows as $row) {
47             $res = new Note();
48             $res->id   = $row['note_guid'];
49             $res->name = htmlspecialchars_decode($row['note_title']);
50             $res->link = $urlGen->linkToRoute(
51                 'grauphel.gui.note', array('guid' => $row['note_guid'])
52             );
53             $results[] = $res;
54         }
55         return $results;
56     }
57
58     /**
59      * Splits the user's query string up into several keywords
60      * that all have to be within the note (AND).
61      *
62      * Split by space, quotes are supported:
63      * - foo bar
64      *   -> searches for notes that contain "foo" and "bar"
65      * - foo "bar baz"
66      *   -> searches for notes that contain "foo" and "bar baz"
67      *
68      * @param string $query User-given query string
69      *
70      * @return array Array of keywords
71      */
72     protected function parseQuery($query)
73     {
74         $keywords = explode(' ', $query);
75         array_map('trim', $keywords);
76         $loop = 0;
77         do {
78             $changed = false;
79             foreach ($keywords as $key => &$keyword) {
80                 if ($keyword{0} != '"') {
81                     continue;
82                 }
83                 if (substr($keyword, -1) == '"') {
84                     // "foo"
85                     $keyword = trim($keyword, '"');
86                     continue;
87                 }
88                 if ($key < count($keywords) -1) {
89                     //not at the end
90                     $keyword .= ' ' . $keywords[$key + 1];
91                     unset($keywords[$key + 1]);
92                     $changed = true;
93                     break;
94                 }
95             }
96         } while ($changed && ++$loop < 20);
97
98         return $keywords;
99     }
100 }
101 ?>