/
PageIndexer.php
95 lines (79 loc) · 2.52 KB
/
PageIndexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Concrete\Core\Page\Search\Index;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Page\Collection\Collection;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Search\IndexedSearch;
use Concrete\Core\Search\Index\Driver\IndexingDriverInterface;
use Concrete\Core\Search\Index\Driver\Iterator;
class PageIndexer implements IndexingDriverInterface, ApplicationAwareInterface
{
use ApplicationAwareTrait;
/**
* @var \Concrete\Core\Page\Search\IndexedSearch
*/
private $search;
/**
* DefaultPageDriver constructor.
* @param \Concrete\Core\Page\Search\IndexedSearch $search
*/
public function __construct(IndexedSearch $search)
{
$this->search = $search;
}
/**
* Add a page to the index
* @param string|int|Page $page Page to index. String is path, int is cID
* @return bool Success or fail
*/
public function index($page)
{
if ($page = $this->getPage($page)) {
if ($page->getVersionObject()) {
return $page->reindex($this->search, true);
}
}
return false;
}
/**
* Remove a page from the index
* @param string|int|Page $page. String is path, int is cID
* @return bool Success or fail
*/
public function forget($page)
{
if ($page = $this->getPage($page)) {
/** @todo Implement forgetting pages completely */
/** @var Connection $database */
$database = $this->app['database']->connection();
$database->executeQuery('DELETE FROM PageSearchIndex WHERE cID=?', [$page->getCollectionID()]);
}
return false;
}
/**
* Get a page based on criteria
* @param string|int|Page|Collection $page
* @return \Concrete\Core\Page\Page
*/
protected function getPage($page)
{
// Handle passed cID
if (is_numeric($page)) {
return Page::getByID($page);
}
// Handle passed /path/to/collection
if (is_string($page)) {
return Page::getByPath($page);
}
// If it's a page, just return the page
if ($page instanceof Page) {
return $page;
}
// If it's not a page but it's a collection, lets try getting a page by id
if ($page instanceof Collection) {
return $this->getPage($page->getCollectionID());
}
}
}