Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

@@ -11,30 +11,59 @@
namespace IPS\core\modules\admin\discovery;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DomainException;
use Exception;
use IPS\Content\Search\Index;
use IPS\Data\Store;
use IPS\Dispatcher;
use IPS\Dispatcher\Controller;
use IPS\Helpers\Form;
use IPS\Helpers\Form\CheckboxSet;
use IPS\Helpers\Form\Codemirror;
use IPS\Helpers\Form\Custom;
use IPS\Helpers\Form\Number;
use IPS\Helpers\Form\Radio;
use IPS\Helpers\Form\Select;
use IPS\Helpers\Form\Text;
use IPS\Helpers\Form\Url as FormUrl;
use IPS\Http\Url;
use IPS\Log;
use IPS\Member;
use IPS\Member\Group;
use IPS\Output;
use IPS\Request;
use IPS\Session;
use IPS\Settings;
use IPS\Theme;
use function count;
use function defined;
use function floatval;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Search settings
*/
class _search extends \IPS\Dispatcher\Controller
class search extends Controller
{
/**
* @brief Has been CSRF-protected
*/
public static $csrfProtected = TRUE;
public static bool $csrfProtected = TRUE;
/**
* Execute
*
* @return void
*/
public function execute()
public function execute() : void
{
\IPS\Dispatcher::i()->checkAcpPermission( 'search_manage' );
Dispatcher::i()->checkAcpPermission( 'search_manage' );
parent::execute();
}
@@ -43,21 +72,21 @@ class _search extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function manage()
protected function manage() : void
{
/* Rebuild button */
\IPS\Output::i()->sidebar['actions'] = array(
Output::i()->sidebar['actions'] = array(
'rebuildIndex' => array(
'title' => 'search_rebuild_index',
'icon' => 'undo',
'link' => \IPS\Http\Url::internal( 'app=core&module=discovery&controller=search&do=queueIndexRebuild' )->csrf(),
'data' => array( 'confirm' => '', 'confirmSubMessage' => \IPS\Member::loggedIn()->language()->get('search_rebuild_index_confirm') ),
'link' => Url::internal( 'app=core&module=discovery&controller=search&do=queueIndexRebuild' )->csrf(),
'data' => array( 'confirm' => '', 'confirmSubMessage' => Member::loggedIn()->language()->get('search_rebuild_index_confirm') ),
),
);
$form = new \IPS\Helpers\Form;
$form = new Form;
$form->addHeader('search_method');
$form->add( new \IPS\Helpers\Form\Radio( 'search_method', \IPS\Settings::i()->search_method, FALSE, array(
$form->add( new Radio( 'search_method', Settings::i()->search_method, FALSE, array(
'options' => array(
'mysql' => 'search_method_mysql',
'elastic' => 'search_method_elastic'
@@ -67,29 +96,29 @@ class _search extends \IPS\Dispatcher\Controller
'elastic' => array( 'search_elastic_server', 'search_elastic_index', 'search_elastic_analyzer', 'search_decay', 'search_elastic_self_boost', 'search_index_maxresults' )
)
) ) );
$form->add( new \IPS\Helpers\Form\Url( 'search_elastic_server', \IPS\Settings::i()->search_elastic_server, NULL, array( 'placeholder' => 'http://localhost:9200' ), function( $val )
$form->add( new FormUrl( 'search_elastic_server', Settings::i()->search_elastic_server, NULL, array( 'placeholder' => 'http://localhost:9200' ), function( $val )
{
if( \IPS\Request::i()->search_method != 'elastic' )
if( Request::i()->search_method != 'elastic' )
{
return;
}
if( !( $val instanceof \IPS\Http\Url ) )
if( !( $val instanceof Url ) )
{
throw new \DomainException('form_url_error');
throw new DomainException('form_url_error');
}
try
{
$response = \IPS\Content\Search\Elastic\Index::request( $val )->get()->decodeJson();
}
catch ( \Exception $e )
catch ( Exception $e )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack('search_elastic_server_error', FALSE, array( 'sprintf' => array( $e->getMessage() ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack('search_elastic_server_error', FALSE, array( 'sprintf' => array( $e->getMessage() ) ) ) );
}
if ( !isset( $response['version']['number'] ) )
{
throw new \DomainException('search_elastic_server_no_version');
throw new DomainException('search_elastic_server_no_version');
}
/* Open search is compatible up to at least version 2 */
@@ -97,24 +126,24 @@ class _search extends \IPS\Dispatcher\Controller
{
if ( version_compare( $response['version']['number'], '2.2', '>=' ) )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack('search_opensearch_server_unsupported_version', FALSE, array( 'sprintf' => array( $response['version']['number'] ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack('search_opensearch_server_unsupported_version', FALSE, array( 'sprintf' => array( $response['version']['number'] ) ) ) );
}
}
else
{
if ( version_compare( $response['version']['number'], \IPS\Content\Search\Elastic\Index::MINIMUM_VERSION, '<' ) )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack('search_elastic_server_unsupported_version', FALSE, array( 'sprintf' => array( \IPS\Content\Search\Elastic\Index::MINIMUM_VERSION, $response['version']['number'] ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack('search_elastic_server_unsupported_version', FALSE, array( 'sprintf' => array( \IPS\Content\Search\Elastic\Index::MINIMUM_VERSION, $response['version']['number'] ) ) ) );
}
if ( version_compare( $response['version']['number'], \IPS\Content\Search\Elastic\Index::UNSUPPORTED_VERSION, '>=' ) )
{
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack('search_elastic_server_unsupported_version_high', FALSE, array( 'sprintf' => array( \IPS\Content\Search\Elastic\Index::UNSUPPORTED_VERSION, $response['version']['number'] ) ) ) );
throw new DomainException( Member::loggedIn()->language()->addToStack('search_elastic_server_unsupported_version_high', FALSE, array( 'sprintf' => array( \IPS\Content\Search\Elastic\Index::UNSUPPORTED_VERSION, $response['version']['number'] ) ) ) );
}
}
}, NULL, NULL, 'search_elastic_server' ) );
\IPS\Member::loggedIn()->language()->words['search_elastic_server_desc'] = sprintf( \IPS\Member::loggedIn()->language()->get('search_elastic_server_desc'), \IPS\Content\Search\Elastic\Index::MINIMUM_VERSION, \IPS\Content\Search\Elastic\Index::UNSUPPORTED_VERSION );
$form->add( new \IPS\Helpers\Form\Text( 'search_elastic_index', \IPS\Settings::i()->search_elastic_index, NULL, array( 'regex' => '/^[A-Z0-9_]*$/i' ), NULL, NULL, NULL, 'search_elastic_index' ) );
$form->add( new \IPS\Helpers\Form\Select( 'search_elastic_analyzer', \IPS\Settings::i()->search_elastic_analyzer, FALSE, array(
Member::loggedIn()->language()->words['search_elastic_server_desc'] = sprintf( Member::loggedIn()->language()->get('search_elastic_server_desc'), \IPS\Content\Search\Elastic\Index::MINIMUM_VERSION, \IPS\Content\Search\Elastic\Index::UNSUPPORTED_VERSION );
$form->add( new Text( 'search_elastic_index', Settings::i()->search_elastic_index, NULL, array( 'regex' => '/^[A-Z0-9_]*$/i' ), NULL, NULL, NULL, 'search_elastic_index' ) );
$form->add( new Select( 'search_elastic_analyzer', Settings::i()->search_elastic_analyzer, FALSE, array(
'options' => array(
'language' => array(
'arabic' => 'elastic_analyzer_arabic',
@@ -160,16 +189,16 @@ class _search extends \IPS\Dispatcher\Controller
'custom' => array( 'search_elastic_custom_analyzer_row' )
)
), NULL, NULL, NULL, 'search_elastic_analyzer' ) );
$form->add( new \IPS\Helpers\Form\Codemirror(
$form->add( new Codemirror(
'search_elastic_custom_analyzer',
\IPS\Settings::i()->search_elastic_custom_analyzer ?: "\t\"analyzer\": {\n\t\t\"my_custom_analyzer\": {\n\t\t\t\"type\": \"custom\",\n\t\t\t\"char_filter\": [\n\t\t\t\t\"emoticons\" \n\t\t\t],\n\t\t\t\"tokenizer\": \"punctuation\", \n\t\t\t\"filter\": [\n\t\t\t\t\"lowercase\",\n\t\t\t\t\"english_stop\" \n\t\t\t]\n\t\t}\n\t},\n\t\"tokenizer\": {\n\t\t\"punctuation\": { \n\t\t\t\"type\": \"pattern\",\n\t\t\t\"pattern\": \"[ .,!?]\"\n\t\t}\n\t},\n\t\"char_filter\": {\n\t\t\"emoticons\": { \n\t\t\t\"type\": \"mapping\",\n\t\t\t\"mappings\": [\n\t\t\t\t\":) => _happy_\",\n\t\t\t\t\":( => _sad_\"\n\t\t\t]\n\t\t}\n\t},\n\t\"filter\": {\n\t\t\"english_stop\": { \n\t\t\t\"type\": \"stop\",\n\t\t\t\"stopwords\": \"_english_\"\n\t\t}\n\t}",
Settings::i()->search_elastic_custom_analyzer ?: "\t\"analyzer\": {\n\t\t\"my_custom_analyzer\": {\n\t\t\t\"type\": \"custom\",\n\t\t\t\"char_filter\": [\n\t\t\t\t\"emoticons\" \n\t\t\t],\n\t\t\t\"tokenizer\": \"punctuation\", \n\t\t\t\"filter\": [\n\t\t\t\t\"lowercase\",\n\t\t\t\t\"english_stop\" \n\t\t\t]\n\t\t}\n\t},\n\t\"tokenizer\": {\n\t\t\"punctuation\": { \n\t\t\t\"type\": \"pattern\",\n\t\t\t\"pattern\": \"[ .,!?]\"\n\t\t}\n\t},\n\t\"char_filter\": {\n\t\t\"emoticons\": { \n\t\t\t\"type\": \"mapping\",\n\t\t\t\"mappings\": [\n\t\t\t\t\":) => _happy_\",\n\t\t\t\t\":( => _sad_\"\n\t\t\t]\n\t\t}\n\t},\n\t\"filter\": {\n\t\t\"english_stop\": { \n\t\t\t\"type\": \"stop\",\n\t\t\t\"stopwords\": \"_english_\"\n\t\t}\n\t}",
NULL,
array( 'mode' => 'javascript' ),
array( 'codeModeAllowedLanguages' => [ 'json' ] ),
function( $val ) {
$json = json_decode( '{' . $val . '}', TRUE );
if ( $json === NULL or !isset( $json['analyzer'] ) or \count( $json['analyzer'] ) !== 1 )
if ( $json === NULL or !isset( $json['analyzer'] ) or count( $json['analyzer'] ) !== 1 )
{
throw new \DomainException( 'search_elastic_custom_analyzer_error' );
throw new DomainException( 'search_elastic_custom_analyzer_error' );
}
},
'<code>"analysis": {</code>',
@@ -177,32 +206,32 @@ class _search extends \IPS\Dispatcher\Controller
'search_elastic_custom_analyzer_row'
) );
$form->addHeader('search_options');
$form->add( new \IPS\Helpers\Form\Radio( 'search_default_operator', \IPS\Settings::i()->search_default_operator, FALSE, array( 'options' => array(
$form->add( new Radio( 'search_default_operator', Settings::i()->search_default_operator, FALSE, array( 'options' => array(
'or' => 'search_default_operator_or',
'and' => 'search_default_operator_and',
) ), NULL, \IPS\Member::loggedIn()->language()->addToStack('search_default_operator_prefix') ) );
$form->add( new \IPS\Helpers\Form\Number( 'search_title_boost', \IPS\Settings::i()->search_title_boost, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'search_title_boost_unlimited' ), NULL, \IPS\Member::loggedIn()->language()->addToStack('search_title_boost_prefix'), \IPS\Member::loggedIn()->language()->addToStack('search_title_boost_suffix'), 'search_title_boost' ) );
$form->add( new \IPS\Helpers\Form\Custom( 'search_decay', array( \IPS\Settings::i()->search_decay_days, \IPS\Settings::i()->search_decay_factor ), FALSE, array(
) ), NULL, Member::loggedIn()->language()->addToStack('search_default_operator_prefix') ) );
$form->add( new Number( 'search_title_boost', Settings::i()->search_title_boost, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'search_title_boost_unlimited' ), NULL, Member::loggedIn()->language()->addToStack('search_title_boost_prefix'), Member::loggedIn()->language()->addToStack('search_title_boost_suffix'), 'search_title_boost' ) );
$form->add( new Custom( 'search_decay', array( Settings::i()->search_decay_days, Settings::i()->search_decay_factor ), FALSE, array(
'getHtml' => function( $field ) {
return \IPS\Theme::i()->getTemplate( 'settings' )->searchDecay( isset( $field->value[0] ) ? $field->value[0] : 0, isset( $field->value[1] ) ? $field->value[1] : 0 );
return Theme::i()->getTemplate( 'settings' )->searchDecay( $field->value[0] ?? 0, $field->value[1] ?? 0 );
}
), NULL, NULL, NULL, 'search_decay' ) );
$form->add( new \IPS\Helpers\Form\Number( 'search_elastic_self_boost', \IPS\Settings::i()->search_elastic_self_boost, FALSE, array( 'unlimited' => \floatval( 0 ), 'unlimitedLang' => 'do_not_boost', 'decimals' => 1 ), NULL, \IPS\Member::loggedIn()->language()->addToStack('search_elastic_self_boost_prefix'), \IPS\Member::loggedIn()->language()->addToStack('search_elastic_self_boost_suffix'), 'search_elastic_self_boost' ) );
$form->add( new \IPS\Helpers\Form\Number( 'search_index_timeframe', \IPS\Settings::i()->search_index_timeframe, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'search_index_timeframe_unlimited' ), NULL, \IPS\Member::loggedIn()->language()->addToStack('search_index_timeframe_prefix'), \IPS\Member::loggedIn()->language()->addToStack('search_index_timeframe_suffix'), 'search_index_timeframe' ) );
$form->add( new \IPS\Helpers\Form\Number( 'search_index_maxresults', \IPS\Settings::i()->search_index_maxresults, FALSE, array(), NULL, NULL, NULL, 'search_index_maxresults' ) );
$form->add( new Number( 'search_elastic_self_boost', Settings::i()->search_elastic_self_boost, FALSE, array( 'unlimited' => floatval( 0 ), 'unlimitedLang' => 'do_not_boost', 'decimals' => 1 ), NULL, Member::loggedIn()->language()->addToStack('search_elastic_self_boost_prefix'), Member::loggedIn()->language()->addToStack('search_elastic_self_boost_suffix'), 'search_elastic_self_boost' ) );
$form->add( new Number( 'search_index_timeframe', Settings::i()->search_index_timeframe, FALSE, array( 'unlimited' => 0, 'unlimitedLang' => 'search_index_timeframe_unlimited' ), NULL, Member::loggedIn()->language()->addToStack('search_index_timeframe_prefix'), Member::loggedIn()->language()->addToStack('search_index_timeframe_suffix'), 'search_index_timeframe' ) );
$form->add( new Number( 'search_index_maxresults', Settings::i()->search_index_maxresults, FALSE, array(), NULL, NULL, NULL, 'search_index_maxresults' ) );
$form->addHeader('search_logs');
$groups = array_combine( array_keys( \IPS\Member\Group::groups( TRUE, TRUE ) ), array_map( function( $_group ) { return (string) $_group; }, \IPS\Member\Group::groups( TRUE, TRUE ) ) );
$form->add( new \IPS\Helpers\Form\CheckboxSet( 'searchlog_exclude_groups', json_decode( \IPS\Settings::i()->searchlog_exclude_groups, TRUE ), FALSE, array( 'options' => $groups, 'multiple' => true ), NULL, NULL, NULL, 'searchlog_exclude_groups' ) );
$groups = array_combine( array_keys( Group::groups( TRUE, TRUE ) ), array_map( function( $_group ) { return (string) $_group; }, Group::groups( TRUE, TRUE ) ) );
$form->add( new CheckboxSet( 'searchlog_exclude_groups', json_decode( Settings::i()->searchlog_exclude_groups, TRUE ), FALSE, array( 'options' => $groups, 'multiple' => true ), NULL, NULL, NULL, 'searchlog_exclude_groups' ) );
if ( $values = $form->values() )
{
$engine = \IPS\Settings::i()->search_method;
$indexPrune = \IPS\Settings::i()->search_index_timeframe;
$analyzer = \IPS\Settings::i()->search_elastic_analyzer;
$customAnalyzer = \IPS\Settings::i()->search_elastic_custom_analyzer;
$maxResults = \IPS\Settings::i()->search_index_maxresults;
$engine = Settings::i()->search_method;
$indexPrune = Settings::i()->search_index_timeframe;
$analyzer = Settings::i()->search_elastic_analyzer;
$customAnalyzer = Settings::i()->search_elastic_custom_analyzer;
$maxResults = Settings::i()->search_index_maxresults;
if ( isset( $values['search_decay'][2] ) )
{
@@ -220,11 +249,11 @@ class _search extends \IPS\Dispatcher\Controller
{
try
{
\IPS\Content\Search\Index::i()->prune();
Index::i()->prune();
}
catch( \Exception $e )
catch( Exception $e )
{
\IPS\Log::log( $e, 'search_index_prune' );
Log::log( $e, 'search_index_prune' );
}
}
@@ -232,23 +261,23 @@ class _search extends \IPS\Dispatcher\Controller
/* Go ahead and save... */
$form->saveAsSettings( $values );
\IPS\Session::i()->log( 'acplogs__search_settings' );
Session::i()->log( 'acplogs__search_settings' );
/* And re-index if setting updated */
if( $engine != $values['search_method'] or ( $values['search_method'] == 'elastic' and ( $values['search_elastic_analyzer'] != $analyzer or ( $values['search_elastic_analyzer'] == 'custom' and $values['search_elastic_custom_analyzer'] != $customAnalyzer ) or $values['search_index_maxresults'] != $maxResults ) ) )
{
/* We pass TRUE to the i() method to ensure we get a new instance, otherwise the old instance cached from the previous prune call will be used */
\IPS\Content\Search\Index::i( TRUE )->init();
\IPS\Content\Search\Index::i()->rebuild();
Index::i( TRUE )->init();
Index::i()->rebuild();
}
elseif( $indexPrune != $values['search_index_timeframe'] )
{
\IPS\Content\Search\Index::i()->rebuild();
Index::i()->rebuild();
}
}
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('menu__core_discovery_search');
\IPS\Output::i()->output .= \IPS\Theme::i()->getTemplate( 'global' )->block( 'menu__core_discovery_search', $form );
Output::i()->title = Member::loggedIn()->language()->addToStack('menu__core_discovery_search');
Output::i()->output .= Theme::i()->getTemplate( 'global' )->block( 'menu__core_discovery_search', $form );
}
/**
@@ -256,17 +285,17 @@ class _search extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function queueIndexRebuild()
protected function queueIndexRebuild() : void
{
\IPS\Session::i()->csrfCheck();
Session::i()->csrfCheck();
/* Clear MySQL minimum word length cached value */
unset( \IPS\Data\Store::i()->mysqlMinWord );
unset( \IPS\Data\Store::i()->mysqlMaxWord );
unset( Store::i()->mysqlMinWord );
unset( Store::i()->mysqlMaxWord );
\IPS\Content\Search\Index::i()->rebuild();
Index::i()->rebuild();
\IPS\Session::i()->log( 'acplogs__queued_search_index' );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=discovery&controller=search' ), 'search_index_rebuilding' );
Session::i()->log( 'acplogs__queued_search_index' );
Output::i()->redirect( Url::internal( 'app=core&module=discovery&controller=search' ), 'search_index_rebuilding' );
}
}