2020use Composer \Autoload \ClassLoader ;
2121use Elastic \Elasticsearch \ClientBuilder ;
2222use Elastic \Elasticsearch \Exception \AuthenticationException ;
23+ use OpenSearch \SymfonyClientFactory ;
2324use phpMyFAQ \Configuration ;
2425use phpMyFAQ \Configuration \DatabaseConfiguration ;
2526use phpMyFAQ \Configuration \ElasticsearchConfiguration ;
27+ use phpMyFAQ \Configuration \OpenSearchConfiguration ;
2628use phpMyFAQ \Core \Exception ;
2729use phpMyFAQ \Database ;
2830use phpMyFAQ \Database \DatabaseDriver ;
3638use phpMyFAQ \Instance \Database \Stopwords ;
3739use phpMyFAQ \Instance \Elasticsearch ;
3840use phpMyFAQ \Instance \Main ;
41+ use phpMyFAQ \Instance \OpenSearch ;
3942use phpMyFAQ \Instance \Setup ;
4043use phpMyFAQ \Ldap ;
4144use phpMyFAQ \Link ;
@@ -729,6 +732,7 @@ public function checkInitialRewriteBasePath(Request $request): bool
729732 *
730733 * @param array|null $setup
731734 * @throws Exception|AuthenticationException
735+ * @throws \Exception
732736 */
733737 public function startInstall (array |null $ setup = null ): void
734738 {
@@ -907,7 +911,7 @@ public function startInstall(array|null $setup = null): void
907911 $ classLoader ->addPsr4 ('React \\Promise \\' , PMF_SRC_DIR . '/libs/react/promise/src ' );
908912 $ classLoader ->register ();
909913
910- // check LDAP connection
914+ // check Elasticsearch connection
911915 $ esHosts = array_values ($ esHosts ['elasticsearch_server ' ]);
912916 $ esClient = ClientBuilder::create ()->setHosts ($ esHosts )->build ();
913917
@@ -918,6 +922,48 @@ public function startInstall(array|null $setup = null): void
918922 $ esSetup = [];
919923 }
920924
925+ //
926+ // Check OpenSearch if enabled
927+ //
928+ $ openSearchEnabled = Filter::filterInput (INPUT_POST , 'opensearch_enabled ' , FILTER_SANITIZE_SPECIAL_CHARS );
929+ if (!is_null ($ openSearchEnabled )) {
930+ $ osSetup = [];
931+ $ osHostFilter = [
932+ 'opensearch_server ' => [
933+ 'filter ' => FILTER_SANITIZE_SPECIAL_CHARS ,
934+ 'flags ' => FILTER_REQUIRE_ARRAY
935+ ]
936+ ];
937+
938+ // OS hosts
939+ $ osHosts = Filter::filterInputArray (INPUT_POST , $ osHostFilter );
940+ if (is_null ($ osHosts )) {
941+ throw new Exception ('OpenSearch Installation Error: Please add at least one OpenSearch host. ' );
942+ }
943+
944+ $ osSetup ['hosts ' ] = $ osHosts ['opensearch_server ' ];
945+
946+ // OS Index name
947+ $ osSetup ['index ' ] = Filter::filterInput (INPUT_POST , 'opensearch_index ' , FILTER_SANITIZE_SPECIAL_CHARS );
948+ if (is_null ($ osSetup ['index ' ])) {
949+ throw new Exception ('OpenSearch Installation Error: Please add an OpenSearch index name. ' );
950+ }
951+
952+ // check OpenSearch connection
953+ $ osHosts = array_values ($ osHosts ['opensearch_server ' ]);
954+ $ osClient = (new SymfonyClientFactory ())->create ([
955+ 'base_uri ' => $ osHosts [0 ],
956+ 'verify_peer ' => false ,
957+ ]);
958+
959+ if (!$ osClient ) {
960+ throw new Exception ('OpenSearch Installation Error: No connection to OpenSearch. ' );
961+ }
962+ } else {
963+ $ osSetup = [];
964+ }
965+
966+
921967 // check the login name
922968 if (!isset ($ setup ['loginname ' ])) {
923969 $ loginName = Filter::filterInput (INPUT_POST , 'loginname ' , FILTER_SANITIZE_SPECIAL_CHARS );
@@ -999,6 +1045,14 @@ public function startInstall(array|null $setup = null): void
9991045 );
10001046 }
10011047
1048+ // check if OpenSearch is enabled
1049+ if (!is_null ($ openSearchEnabled ) && count ($ osSetup ) && !$ instanceSetup ->createOpenSearchFile ($ osSetup , '' )) {
1050+ self ::cleanFailedInstallationFiles ();
1051+ throw new Exception (
1052+ 'OpenSearch Installation Error: Setup cannot write to ./content/core/config/opensearch.php. '
1053+ );
1054+ }
1055+
10021056 // connect to the database using config/database.php
10031057 $ databaseConfiguration = new DatabaseConfiguration ($ rootDir . '/content/core/config/database.php ' );
10041058 try {
@@ -1134,6 +1188,23 @@ public function startInstall(array|null $setup = null): void
11341188 $ esInstance ->createIndex ();
11351189 }
11361190
1191+ // connect to OpenSearch if enabled
1192+ if (!is_null ($ openSearchEnabled ) && is_file ($ rootDir . '/config/opensearch.php ' )) {
1193+ $ osConfiguration = new OpenSearchConfiguration ($ rootDir . '/config/opensearch.php ' );
1194+
1195+ $ configuration ->setOpenSearchConfig ($ osConfiguration );
1196+
1197+ $ osClient = (new SymfonyClientFactory ())->create ([
1198+ 'base_uri ' => $ osConfiguration ->getHosts ()[0 ],
1199+ 'verify_peer ' => false ,
1200+ ]);
1201+
1202+ $ configuration ->setOpenSearch ($ osClient );
1203+
1204+ $ osInstance = new OpenSearch ($ configuration );
1205+ $ osInstance ->createIndex ();
1206+ }
1207+
11371208 // adjust RewriteBase in .htaccess
11381209 $ configurator = new EnvironmentConfigurator ($ configuration );
11391210 $ configurator ->adjustRewriteBaseHtaccess ();
0 commit comments