Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]

Search subsystem manager.

Copyright: Prateek Sachan {@link http://prateeksachan.com}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 2023 lines (80 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 2 classes


Class: manager  - X-Ref

Search subsystem manager.

__construct($engine)   X-Ref
Constructor, use \core_search\manager::instance instead to get a class instance.

param: \core_search\base The search engine to use

instance(bool $fast = false, bool $query = false)   X-Ref
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.

return: \core_search\manager
param: bool $fast Set to true when calling on a page that requires high performance
param: bool $query Set true on a page that is used for querying

is_global_search_enabled()   X-Ref
Returns whether global search is enabled or not.

return: bool

can_replace_course_search()   X-Ref
Tests if global search is configured to be equivalent to the front page course search.

return: bool

get_course_search_url()   X-Ref
Returns the search URL for course search

return: moodle_url

is_indexing_enabled()   X-Ref
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: bool True if indexing is enabled.

search_engine_instance(bool $query = false)   X-Ref
Returns an instance of the search engine.

return: \core_search\engine
param: bool $query If true, gets the query-only search engine (where configured)

search_engine_instance_from_setting(string $setting)   X-Ref
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.

return: engine|null
param: string $setting

get_engine()   X-Ref
Returns the search engine.

return: \core_search\engine

get_area_classname($areaid)   X-Ref
Returns a search area class name.

return: string
param: string $areaid

get_search_area($areaid)   X-Ref
Returns a new area search indexer instance.

return: \core_search\base|bool False if the area is not available.
param: string $areaid

get_search_areas_list($enabled = false)   X-Ref
Return the list of available search areas.

return: \core_search\base[]
param: bool $enabled Return only the enabled ones.

get_search_area_category_by_name($name)   X-Ref
Return search area category instance by category name.

return: \core_search\area_category
param: string $name Category name. If name is not valid will return default category.

get_search_area_categories()   X-Ref
Return a list of existing search area categories.

return: \core_search\area_category[]

get_core_search_area_categories()   X-Ref
Get list of core search area categories.

return: \core_search\area_category[]

get_additional_search_area_categories()   X-Ref
Gets a list of additional search area categories.

return: \core_search\area_category[]

is_valid_area_category($areacategory)   X-Ref
Check if provided instance of area category is valid.

return: bool
param: mixed $areacategory Area category instance. Potentially could be anything.

clear_static()   X-Ref
Clears all static caches.

return: void

generate_areaid($componentname, $areaname)   X-Ref
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/.

return: void
param: string $componentname
param: string $areaname

extract_areaid_parts($areaid)   X-Ref
Returns all areaid string components (component name and area name).

return: array Component name (Frankenstyle) and area name (search area class name)
param: string $areaid

parse_areaid($areaid)   X-Ref
Parse a search area id and get plugin name and config name prefix from it.

return: array Where the first element is a plugin name and the second is config names prefix.
param: string $areaid Search area id.

get_areas_user_accesses($limitcourseids = false, $limitcontextids = false)   X-Ref
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.

return: \stdClass Object as described above
param: array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.
param: array|false $limitcontextids An array of context ids to limit the search to. False for no limiting.

paged_search(\stdClass $formdata, $pagenum)   X-Ref
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.

return: object An object with 3 properties:
param: stdClass $formdata
param: int $pagenum The 0 based page number.

search(\stdClass $formdata, $limit = 0)   X-Ref
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)

return: \core_search\document[]
param: \stdClass $formdata Query input data (usually from search form)
param: int $limit The maximum number of documents to return

search_top(\stdClass $formdata)   X-Ref
Search for top ranked result.

return: array|document[]
param: \stdClass $formdata search query data

build_limitcourseids(\stdClass $formdata)   X-Ref
Build a list of course ids to limit the search based on submitted form data.

return: array|bool
param: \stdClass $formdata Submitted search form data.

optimize_index()   X-Ref
Merge separate index segments into one.


index($fullindex = false, $timelimit = 0, \progress_trace $progress = null)   X-Ref
Index all documents.

return: bool Whether there was any updated document or not.
param: bool $fullindex Whether we should reindex everything or not.
param: float $timelimit Time limit in seconds (0 = no time limit)
param: \progress_trace|null $progress Optional class for tracking progress

index_context($context, $singleareaid = '', $timelimit = 0,\progress_trace $progress = null, $startfromarea = '', $startfromtime = 0)   X-Ref
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.

return: \stdClass Object indicating success
param: \context $context Context to restrict index.
param: string $singleareaid If specified, indexes only the given area.
param: float $timelimit Time limit in seconds (0 = no time limit)
param: \progress_trace|null $progress Optional class for tracking progress
param: string $startfromarea Area to start from
param: int $startfromtime Timestamp to start from

reset_config($areaid = false)   X-Ref
Resets areas config.

return: void
param: string $areaid

delete_index($areaid = false)   X-Ref
Deletes an area's documents or all areas documents.

return: void
param: string $areaid The area id or false for all

delete_index_by_id($id)   X-Ref
Deletes index by id.

param: int Solr Document string $id

get_areas_config($searchareas)   X-Ref
Returns search areas configuration.

return: \stdClass[] $configsettings
param: \core_search\base[] $searchareas

trigger_search_results_viewed($other)   X-Ref
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

return: \core\event\search_results_viewed
param: array $other Other info for the event.

is_search_area($classname)   X-Ref
Checks whether a classname is of an actual search area.

return: bool
param: string $classname

request_index(\context $context, $areaid = '',$priority = self::INDEX_PRIORITY_NORMAL)   X-Ref
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.

param: \context $context Context to index within
param: string $areaid Area to index, '' = all areas
param: int $priority Priority (INDEX_PRIORITY_xx constant)

process_index_requests($timelimit = 0.0, \progress_trace $progress = null)   X-Ref
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.

param: float $timelimit Time limit (0 = none)
param: \progress_trace|null $progress Optional progress indicator

get_index_requests_info()   X-Ref
Gets information about the request queue, in the form of a plain object suitable for passing
to a template for rendering.

return: \stdClass Information about queued index requests

get_current_time()   X-Ref
Gets current time for use in search system.

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

return: float Current time in seconds (with decimals)

is_search_area_categories_enabled()   X-Ref
Check if search area categories functionality is enabled.

return: bool

should_hide_all_results_category()   X-Ref
Check if all results category should be hidden.

return: bool

get_default_area_category_name()   X-Ref
Returns default search area category name.

return: string

get_all_courses($limitcourseids)   X-Ref
Get a list of all courses limited by ids if required.

return: array
param: array|false $limitcourseids An array of course ids to limit the search to. False for no limiting.

get_my_courses($allaccessible)   X-Ref
Get a list of courses as user can access.

return: array
param: bool $allaccessible Include courses user is not enrolled in, but can access.

include_all_courses()   X-Ref
Check if search all courses setting is enabled.

return: bool

clean_up_non_existing_area($areaid)   X-Ref
Cleans up non existing search area.

1. Remove all configs from {config_plugins} table.
2. Delete all related indexed documents.

param: string $areaid Search area id.

Class: to  - X-Ref

context_deleted(\context $context)   X-Ref
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.

param: \context $context Context object that has just been deleted

course_deleting_start(int $courseid)   X-Ref
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().

param: int $courseid Course id that is being deleted

course_deleting_finish(int $courseid)   X-Ref
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.

param: int $courseid Course id that no longer exists