Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
core_search\manager Class Reference

Search subsystem manager. More...

Public Member Functions

 __construct ($engine)
 Constructor, use core_search\manager\instance instead to get a class instance.
 
 delete_index ($areaid=false)
 Deletes an area's documents or all areas documents.
 
 delete_index_by_id ($id)
 Deletes index by id.
 
 get_areas_config ($searchareas)
 Returns search areas configuration.
 
 get_engine ()
 Returns the search engine.
 
 get_index_requests_info ()
 Gets information about the request queue, in the form of a plain object suitable for passing to a template for rendering.
 
 index ($fullindex=false, $timelimit=0, progress_trace $progress=null)
 Index all documents.
 
 index_context ($context, $singleareaid='', $timelimit=0, progress_trace $progress=null, $startfromarea='', $startfromtime=0)
 Indexes or reindexes a specific context of the system, e.g.
 
 optimize_index ()
 Merge separate index segments into one.
 
 paged_search (\stdClass $formdata, $pagenum)
 Returns requested page of documents plus additional information for paging.
 
 process_index_requests ($timelimit=0.0, progress_trace $progress=null)
 Processes outstanding index requests.
 
 reset_config ($areaid=false)
 Resets areas config.
 
 search (\stdClass $formdata, $limit=0)
 Returns documents from the engine based on the data provided.
 
 search_top (\stdClass $formdata)
 Search for top ranked result.
 

Static Public Member Functions

static can_replace_course_search ()
 Tests if global search is configured to be equivalent to the front page course search.
 
static clean_up_non_existing_area ($areaid)
 Cleans up non existing search area.
 
static clear_static ()
 Clears all static caches.
 
static context_deleted (\context $context)
 Informs the search system that a context has been deleted.
 
static course_deleting_finish (int $courseid)
 Informs the search engine that a course has now been deleted.
 
static course_deleting_start (int $courseid)
 Informs the search system that a course is about to be deleted.
 
static extract_areaid_parts ($areaid)
 Returns all areaid string components (component name and area name).
 
static generate_areaid ($componentname, $areaname)
 Generates an area id from the componentname and the area name.
 
static get_course_search_url ()
 Returns the search URL for course search.
 
static get_current_time ()
 Gets current time for use in search system.
 
static get_default_area_category_name ()
 Returns default search area category name.
 
static get_search_area ($areaid)
 Returns a new area search indexer instance.
 
static get_search_area_categories ()
 Return a list of existing search area categories.
 
static get_search_area_category_by_name ($name)
 Return search area category instance by category name.
 
static get_search_areas_list ($enabled=false)
 Return the list of available search areas.
 
static include_all_courses ()
 Check if search all courses setting is enabled.
 
static instance (bool $fast=false, bool $query=false)
 Returns an initialised core_search instance.
 
static is_global_search_enabled ()
 Returns whether global search is enabled or not.
 
static is_indexing_enabled ()
 Returns whether indexing is enabled or not (you can enable indexing even when search is not enabled at the moment, so as to have it ready for students).
 
static is_search_area_categories_enabled ()
 Check if search area categories functionality is enabled.
 
static parse_areaid ($areaid)
 Parse a search area id and get plugin name and config name prefix from it.
 
static request_index (\context $context, $areaid='', $priority=self::INDEX_PRIORITY_NORMAL)
 Requests that a specific context is indexed by the scheduled task.
 
static search_engine_instance (bool $query=false)
 Returns an instance of the search engine.
 
static should_hide_all_results_category ()
 Check if all results category should be hidden.
 
static trigger_search_results_viewed ($other)
 Triggers search_results_viewed event.
 

Public Attributes

int const ACCESS_DELETED = 2
 The document was deleted.
 
int const ACCESS_DENIED = 0
 User can not access the document.
 
int const ACCESS_GRANTED = 1
 User can access the document.
 
float const DISPLAY_INDEXING_PROGRESS_EVERY = 30.0
 Adds indexing progress within one search area to cron log every N seconds.
 
float const DISPLAY_LONG_QUERY_TIME = 5.0
 If initial query takes longer than N seconds, this will be shown in cron log.
 
int const DISPLAY_RESULTS_PER_PAGE = 10
 Number of results per page.
 
int const INDEX_PRIORITY_NORMAL = 100
 Context indexing: normal priority.
 
int const INDEX_PRIORITY_REINDEXING = 50
 Context indexing: low priority for reindexing.
 
int const MAX_RESULTS = 100
 Maximum number of results that will be retrieved from the search engine.
 
int const NO_OWNER_ID = 0
 The id to be placed in owneruserid when there is no owner.
 
int const SCHEMA_CHECK_REQUIRED_EVERY = 4 * 3600
 Require a new schema check at least every 4 hours.
 
int const SCHEMA_CHECK_TRACKING_DELAY = 10 * 60
 Record time of each successful schema check, but not more than once per 10 minutes.
 
string const SEARCH_AREA_CATEGORY_ALL = 'core-all'
 Core search area category for all results.
 
string const SEARCH_AREA_CATEGORY_COURSE_CONTENT = 'core-course-content'
 Core search area category for course content.
 
string const SEARCH_AREA_CATEGORY_COURSES = 'core-courses'
 Core search area category for courses.
 
string const SEARCH_AREA_CATEGORY_OTHER = 'core-other'
 Core search area category for results that do not fit into any of existing categories.
 
string const SEARCH_AREA_CATEGORY_USERS = 'core-users'
 Core search area category for users.
 
int const TYPE_FILE = 2
 File contents.
 
int const TYPE_TEXT = 1
 Text contents.
 

Protected Member Functions

 build_limitcourseids (\stdClass $formdata)
 Build a list of course ids to limit the search based on submitted form data.
 
 get_all_courses ($limitcourseids)
 Get a list of all courses limited by ids if required.
 
 get_areas_user_accesses ($limitcourseids=false, $limitcontextids=false)
 Returns information about the areas which the user can access.
 
 get_my_courses ($allaccessible)
 Get a list of courses as user can access.
 

Static Protected Member Functions

static get_additional_search_area_categories ()
 Gets a list of additional search area categories.
 
static get_area_classname ($areaid)
 Returns a search area class name.
 
static get_core_search_area_categories ()
 Get list of core search area categories.
 
static is_search_area ($classname)
 Checks whether a classname is of an actual search area.
 
static is_valid_area_category ($areacategory)
 Check if provided instance of area category is valid.
 
static search_engine_instance_from_setting (string $setting)
 Loads a search engine based on the name given in settings, which can optionally include '-alternate' to indicate that an alternate version should be used.
 

Protected Attributes

int $behatresultcount = 0
 Result count when used with mock results for Behat tests.
 
core_search engine $engine = null
 

Static Protected Attributes

static core_search base[] $allsearchareas = null
 All system search areas.
 
static array $coursedeleting = []
 IDs (as keys) of course deletions in progress in this requuest, if any.
 
static core_search base[] $enabledsearchareas = null
 Enabled search areas.
 
static core_search manager $instance = null
 
static float $phpunitfaketime = 0
 Note: This should be removed once possible (see MDL-60644).
 
static core_search area_category[] $searchareacategories = null
 A list of search area categories.
 

Detailed Description

Search subsystem manager.

License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

core_search\manager::__construct ( $engine)

Constructor, use core_search\manager\instance instead to get a class instance.

Parameters
core_search\baseThe search engine to use

Member Function Documentation

◆ build_limitcourseids()

core_search\manager::build_limitcourseids ( \stdClass $formdata)
protected

Build a list of course ids to limit the search based on submitted form data.

Parameters
stdClass$formdataSubmitted search form data.
Return values
array|bool

◆ can_replace_course_search()

static core_search\manager::can_replace_course_search ( )
static

Tests if global search is configured to be equivalent to the front page course search.

Return values
bool

◆ clean_up_non_existing_area()

static core_search\manager::clean_up_non_existing_area ( $areaid)
static

Cleans up non existing search area.

  1. Remove all configs from {config_plugins} table.
  2. Delete all related indexed documents.
Parameters
string$areaidSearch area id.

◆ clear_static()

static core_search\manager::clear_static ( )
static

Clears all static caches.

Return values
void

◆ context_deleted()

static core_search\manager::context_deleted ( \context $context)
static

Informs the search system that a context has been deleted.

This will clear the data from the search index, where the search engine supports that.

This function does not usually throw an exception (so as not to get in the way of the context deletion finishing).

This is called for all types of context deletion.

Parameters
context$contextContext object that has just been deleted

◆ course_deleting_finish()

static core_search\manager::course_deleting_finish ( int $courseid)
static

Informs the search engine that a course has now been deleted.

This causes the search engine to actually delete the index for the whole course.

Parameters
int$courseidCourse id that no longer exists

◆ course_deleting_start()

static core_search\manager::course_deleting_start ( int $courseid)
static

Informs the search system that a course is about to be deleted.

This prevents it from sending hundreds of 'delete context' updates for all the individual contexts that are deleted.

If you call this, you must call course_deleting_finish().

Parameters
int$courseidCourse id that is being deleted

◆ delete_index()

core_search\manager::delete_index ( $areaid = false)

Deletes an area's documents or all areas documents.

Parameters
string$areaidThe area id or false for all
Return values
void

◆ delete_index_by_id()

core_search\manager::delete_index_by_id ( $id)

Deletes index by id.

Parameters
intSolr Document string $id

◆ extract_areaid_parts()

static core_search\manager::extract_areaid_parts ( $areaid)
static

Returns all areaid string components (component name and area name).

Parameters
string$areaid
Return values
arrayComponent name (Frankenstyle) and area name (search area class name)

◆ generate_areaid()

static core_search\manager::generate_areaid ( $componentname,
$areaname )
static

Generates an area id from the componentname and the area name.

There should not be any naming conflict as the area name is the class name in component/classes/search/.

Parameters
string$componentname
string$areaname
Return values
void

◆ get_additional_search_area_categories()

static core_search\manager::get_additional_search_area_categories ( )
staticprotected

Gets a list of additional search area categories.

Return values
core_search\area_category[]

◆ get_all_courses()

core_search\manager::get_all_courses ( $limitcourseids)
protected

Get a list of all courses limited by ids if required.

Parameters
array | false$limitcourseidsAn array of course ids to limit the search to. False for no limiting.
Return values
array

◆ get_area_classname()

static core_search\manager::get_area_classname ( $areaid)
staticprotected

Returns a search area class name.

Parameters
string$areaid
Return values
string

◆ get_areas_config()

core_search\manager::get_areas_config ( $searchareas)

Returns search areas configuration.

Parameters
core_search\base[]$searchareas
Return values
stdClass[]\$configsettings

◆ get_areas_user_accesses()

core_search\manager::get_areas_user_accesses ( $limitcourseids = false,
$limitcontextids = false )
protected

Returns information about the areas which the user can access.

The returned value is a stdClass object with the following fields:

  • everything (bool, true for admin only)
  • usercontexts (indexed by area identifier then context
  • separategroupscontexts (contexts within which group restrictions apply)
  • visiblegroupscontextsareas (overrides to the above when the same contexts also have 'visible groups' for certain search area ids - hopefully rare)
  • usergroups (groups which the current user belongs to)

The areas can be limited by course id and context id. If specifying context ids, results are limited to the exact context ids specified and not their children (for example, giving the course context id would result in including search items with the course context id, and not anything from a context inside the course). For performance, you should also specify course id(s) when using context ids.

Parameters
array | false$limitcourseidsAn array of course ids to limit the search to. False for no limiting.
array | false$limitcontextidsAn array of context ids to limit the search to. False for no limiting.
Return values
stdClassObject as described above

◆ get_core_search_area_categories()

static core_search\manager::get_core_search_area_categories ( )
staticprotected

Get list of core search area categories.

Return values
core_search\area_category[]

◆ get_course_search_url()

static core_search\manager::get_course_search_url ( )
static

Returns the search URL for course search.

Return values
moodle_url

◆ get_current_time()

static core_search\manager::get_current_time ( )
static

Gets current time for use in search system.

Note: This should be replaced with generic core functionality once possible (see MDL-60644).

Return values
floatCurrent time in seconds (with decimals)

◆ get_default_area_category_name()

static core_search\manager::get_default_area_category_name ( )
static

Returns default search area category name.

Return values
string

◆ get_engine()

core_search\manager::get_engine ( )

Returns the search engine.

Return values
core_search\engine

◆ get_index_requests_info()

core_search\manager::get_index_requests_info ( )

Gets information about the request queue, in the form of a plain object suitable for passing to a template for rendering.

Return values
stdClassInformation about queued index requests

◆ get_my_courses()

core_search\manager::get_my_courses ( $allaccessible)
protected

Get a list of courses as user can access.

Parameters
bool$allaccessibleInclude courses user is not enrolled in, but can access.
Return values
array

◆ get_search_area()

static core_search\manager::get_search_area ( $areaid)
static

Returns a new area search indexer instance.

Parameters
string$areaid
Return values
core_search\base|boolFalse if the area is not available.

◆ get_search_area_categories()

static core_search\manager::get_search_area_categories ( )
static

Return a list of existing search area categories.

Return values
core_search\area_category[]

◆ get_search_area_category_by_name()

static core_search\manager::get_search_area_category_by_name ( $name)
static

Return search area category instance by category name.

Parameters
string$nameCategory name. If name is not valid will return default category.
Return values
core_search\area_category

◆ get_search_areas_list()

static core_search\manager::get_search_areas_list ( $enabled = false)
static

Return the list of available search areas.

Parameters
bool$enabledReturn only the enabled ones.
Return values
core_search\base[]

◆ include_all_courses()

static core_search\manager::include_all_courses ( )
static

Check if search all courses setting is enabled.

Return values
bool

◆ index()

core_search\manager::index ( $fullindex = false,
$timelimit = 0,
progress_trace $progress = null )

Index all documents.

Parameters
bool$fullindexWhether we should reindex everything or not.
float$timelimitTime limit in seconds (0 = no time limit)
progress_trace | null$progressOptional class for tracking progress
Exceptions
moodle_exception
Return values
boolWhether there was any updated document or not.

◆ index_context()

core_search\manager::index_context ( $context,
$singleareaid = '',
$timelimit = 0,
progress_trace $progress = null,
$startfromarea = '',
$startfromtime = 0 )

Indexes or reindexes a specific context of the system, e.g.

one course.

The function returns an object with field 'complete' (true or false).

This function supports partial indexing via the time limit parameter. If the time limit expires, it will return values for $startfromarea and $startfromtime which can be passed next time to continue indexing.

Parameters
context$contextContext to restrict index.
string$singleareaidIf specified, indexes only the given area.
float$timelimitTime limit in seconds (0 = no time limit)
progress_trace | null$progressOptional class for tracking progress
string$startfromareaArea to start from
int$startfromtimeTimestamp to start from
Return values
stdClassObject indicating success

◆ instance()

static core_search\manager::instance ( bool $fast = false,
bool $query = false )
static

Returns an initialised core_search instance.

While constructing the instance, checks on the search schema may be carried out. The $fast parameter provides a way to skip those checks on pages which are used frequently. It has no effect if an instance has already been constructed in this request.

The $query parameter indicates that the page is used for queries rather than indexing. If configured, this will cause the query-only search engine to be used instead of the 'normal' one.

See also
core_search\engine\is_installed
core_search\engine\is_server_ready
Parameters
bool$fastSet to true when calling on a page that requires high performance
bool$querySet true on a page that is used for querying
Exceptions
core_search\engine_exception
Return values
core_search\manager

◆ is_global_search_enabled()

static core_search\manager::is_global_search_enabled ( )
static

Returns whether global search is enabled or not.

Return values
bool

◆ is_indexing_enabled()

static core_search\manager::is_indexing_enabled ( )
static

Returns whether indexing is enabled or not (you can enable indexing even when search is not enabled at the moment, so as to have it ready for students).

Return values
boolTrue if indexing is enabled.

◆ is_search_area()

static core_search\manager::is_search_area ( $classname)
staticprotected

Checks whether a classname is of an actual search area.

Parameters
string$classname
Return values
bool

◆ is_search_area_categories_enabled()

static core_search\manager::is_search_area_categories_enabled ( )
static

Check if search area categories functionality is enabled.

Return values
bool

◆ is_valid_area_category()

static core_search\manager::is_valid_area_category ( $areacategory)
staticprotected

Check if provided instance of area category is valid.

Parameters
mixed$areacategoryArea category instance. Potentially could be anything.
Return values
bool

◆ paged_search()

core_search\manager::paged_search ( \stdClass $formdata,
$pagenum )

Returns requested page of documents plus additional information for paging.

This function does not perform any kind of security checking for access, the caller code should check that the current user have moodle/search:query capability.

If a page is requested that is beyond the last result, the last valid page is returned in results, and actualpage indicates which page was returned.

Parameters
stdClass$formdata
int$pagenumThe 0 based page number.
Return values
objectAn object with 3 properties: results => An array of core_search\documents for the actual page. totalcount => Number of records that are possibly available, to base paging on. actualpage => The actual page returned.

◆ parse_areaid()

static core_search\manager::parse_areaid ( $areaid)
static

Parse a search area id and get plugin name and config name prefix from it.

Parameters
string$areaidSearch area id.
Return values
arrayWhere the first element is a plugin name and the second is config names prefix.

◆ process_index_requests()

core_search\manager::process_index_requests ( $timelimit = 0.0,
progress_trace $progress = null )

Processes outstanding index requests.

This will take the first item from the queue (taking account the indexing priority) and process it, continuing until an optional time limit is reached.

If there are no index requests, the function will do nothing.

Parameters
float$timelimitTime limit (0 = none)
progress_trace | null$progressOptional progress indicator

◆ request_index()

static core_search\manager::request_index ( \context $context,
$areaid = '',
$priority = self::INDEX_PRIORITY_NORMAL )
static

Requests that a specific context is indexed by the scheduled task.

The context will be added to a queue which is processed by the task.

This is used after a restore to ensure that restored items are indexed, even though their modified time will be older than the latest indexed. It is also used by the 'Gradual reindex' admin feature from the search areas screen.

Parameters
context$contextContext to index within
string$areaidArea to index, '' = all areas
int$priorityPriority (INDEX_PRIORITY_xx constant)

◆ reset_config()

core_search\manager::reset_config ( $areaid = false)

Resets areas config.

Exceptions
moodle_exception
Parameters
string$areaid
Return values
void

◆ search()

core_search\manager::search ( \stdClass $formdata,
$limit = 0 )

Returns documents from the engine based on the data provided.

This function does not perform any kind of security checking, the caller code should check that the current user have moodle/search:query capability.

It might return the results from the cache instead.

Valid formdata options include:

  • q (query text)
  • courseids (optional list of course ids to restrict)
  • contextids (optional list of context ids to restrict)
  • context (Moodle context object for location user searched from)
  • order (optional ordering, one of the types supported by the search engine e.g. 'relevance')
  • userids (optional list of user ids to restrict)
Parameters
stdClass$formdataQuery input data (usually from search form)
int$limitThe maximum number of documents to return
Return values
core_search\document[]

◆ search_engine_instance()

static core_search\manager::search_engine_instance ( bool $query = false)
static

Returns an instance of the search engine.

Parameters
bool$queryIf true, gets the query-only search engine (where configured)
Return values
core_search\engine

◆ search_engine_instance_from_setting()

static core_search\manager::search_engine_instance_from_setting ( string $setting)
staticprotected

Loads a search engine based on the name given in settings, which can optionally include '-alternate' to indicate that an alternate version should be used.

Parameters
string$setting
Return values
engine|null

◆ search_top()

core_search\manager::search_top ( \stdClass $formdata)

Search for top ranked result.

Parameters
stdClass$formdatasearch query data
Return values
array|document[]

◆ should_hide_all_results_category()

static core_search\manager::should_hide_all_results_category ( )
static

Check if all results category should be hidden.

Return values
bool

◆ trigger_search_results_viewed()

static core_search\manager::trigger_search_results_viewed ( $other)
static

Triggers search_results_viewed event.

Other data required:

  • q: The query string
  • page: The page number
  • title: Title filter
  • areaids: Search areas filter
  • courseids: Courses filter
  • timestart: Time start filter
  • timeend: Time end filter
Since
Moodle 3.2
Parameters
array$otherOther info for the event.
Return values
core\event\search_results_viewed

Member Data Documentation

◆ $phpunitfaketime

float core_search\manager::$phpunitfaketime = 0
staticprotected

Note: This should be removed once possible (see MDL-60644).

Fake current time for use in PHPunit tests


The documentation for this class was generated from the following file: