1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8use Search\Federated\ManifoldCfIndex;
9
10class Services_Search_ManifoldController
11{
12	function setUp()
13	{
14		Services_Exception_Disabled::check('feature_search');
15		Services_Exception_Denied::checkGlobal('tiki_p_admin');
16	}
17
18	function action_check($input)
19	{
20		$lib = TikiLib::lib('federatedsearch');
21		$unified = TikiLib::lib('unifiedsearch');
22
23		$instances = [];
24		$indices = $lib->getIndices();
25
26		foreach ($indices as $indexName => $index) {
27			if ($index instanceof ManifoldCfIndex) {
28				$valid = false;
29				$type = $index->getType();
30				$mapping = $unified->getElasticIndexInfo($indexName);
31				$properties = false;
32
33				if ($mapping) {
34					$first = key($mapping);
35					if (isset($mapping->$first->mappings->$type->properties)) {
36						$properties = $mapping->$first->mappings->$type->properties;
37					}
38				}
39
40				if (isset($properties->file->type)) {
41					$valid = 'attachment' === $properties->file->type;
42				}
43
44				$instances[] = [
45					'name' => $indexName,
46					'type' => $type,
47					'indexExists' => ! empty($mapping),
48					'typeExists' => ! empty($properties),
49					'valid' => $valid,
50				];
51			}
52		}
53
54		return [
55			'title' => tr('ManifoldCF Configuration Check'),
56			'instances' => $instances,
57		];
58	}
59
60	function action_create_index($input)
61	{
62		global $prefs;
63
64		$index = $input->index->word();
65		$type = $input->type->word();
66		$location = $input->location->url() ?: $prefs['unified_elastic_url'];
67
68		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
69			$lib = TikiLib::lib('federatedsearch');
70			try {
71				$lib->createIndex($location, $index, $type, [
72					'file' => [
73						'type' => 'attachment',
74					],
75				]);
76
77				return [
78					'FORWARD' => [
79						'action' => 'check',
80					],
81				];
82			} catch (Search_Elastic_MappingException $e) {
83				if ($e->getType() == 'attachment') {
84					throw new Services_Exception_NotAvailable('Attachment field plugin not installed on Elasticsearch server.');
85				} else {
86					throw $e;
87				}
88			}
89		}
90
91		return [
92			'title' => tr('Create ManifoldCF Index'),
93			'location' => $location,
94			'index' => $index,
95			'type' => $type,
96		];
97	}
98}
99