Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
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. More...
 
 delete_index ($areaid=false)
 Deletes an area's documents or all areas documents. More...
 
 delete_index_by_id ($id)
 Deletes index by id. More...
 
 get_areas_config ($searchareas)
 Returns search areas configuration. More...
 
 get_engine ()
 Returns the search engine. More...
 
 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. More...
 
 index ($fullindex=false, $timelimit=0, progress_trace $progress=null)
 Index all documents. More...
 
 index_context ($context, $singleareaid='', $timelimit=0, progress_trace $progress=null, $startfromarea='', $startfromtime=0)
 Indexes or reindexes a specific context of the system, e.g. More...
 
 optimize_index ()
 Merge separate index segments into one.
 
 paged_search (\stdClass $formdata, $pagenum)
 Returns requested page of documents plus additional information for paging. More...
 
 process_index_requests ($timelimit=0.0, progress_trace $progress=null)
 Processes outstanding index requests. More...
 
 reset_config ($areaid=false)
 Resets areas config. More...
 
 search (\stdClass $formdata, $limit=0)
 Returns documents from the engine based on the data provided. More...
 

Static Public Member Functions

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

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. More...
 
 get_all_courses ($limitcourseids)
 Get a list of all courses limited by ids if required. More...
 
 get_areas_user_accesses ($limitcourseids=false, $limitcontextids=false)
 Returns information about the areas which the user can access. More...
 
 get_my_courses ($allaccessible)
 Get a list of courses as user can access. More...
 

Static Protected Member Functions

static get_additional_search_area_categories ()
 Gets a list of additional search area categories. More...
 
static get_area_classname ($areaid)
 Returns a search area class name. More...
 
static get_core_search_area_categories ()
 Get list of core search area categories. More...
 
static is_search_area ($classname)
 Checks whether a classname is of an actual search area. More...
 
static is_valid_area_category ($areacategory)
 Check if provided instance of area category is valid. More...
 

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). More...
 
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

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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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 (   $fast = 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.

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
Exceptions
core_search

◆ 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

◆ search_engine_instance()

static core_search\manager::search_engine_instance ( )
static

Returns an instance of the search engine.

Return values
core_search

◆ 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

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: