Search Modules
A search module (requires Q2A 1.5+) enables plugins to create a custom index of the content in a Q2A site, including questions, answers, comments and custom pages. If a search module is selected by the site administrator, it can replace the default Q2A search results with its own, and optionally include pages from external websites in those results.
The PHP class for a search module may contain the following functions (all are optional):
-
index_post($postid, $type, $questionid, $parentid, $title, $content, $format, $text, $tagstring, $categoryid). If defined, this function is called when a post should be added to the search index, either because it is new, or because it has just been edited. The parameter$postidcontains thepostidfrom the database. The$typewill be'Q'for questions,'A'for answers,'C'for comments or'NOTE'for a note (currently used for question closing explanations). The$questionidis thepostidof the question to which the post belongs and the$parentidis thepostidof its immediate parent, ornullif it has none. For questions,$titlecontains the title in UTF-8 format, otherwise it isnull. The$contentand$formatparameters describe the post's content. If$formatis'', then$contentcontains plain text in UTF-8 encoding. If$formatis'html', then$contentcontains HTML with UTF-8 encoding. Other values of$formatmay be present if a non-standard editor module is installed. The$textparameter contains a UTF-8 plain text rendering of the post, which is particularly useful for building a search index. For questions,$tagstringcontains a comma-separated list of tags, otherwise it isnull. Finally,$categoryidcontains the category ID of the post, ornullif there is none. Note thatindex_post()is only called when a post has been made visible on the Q2A site - it is not called if a new post was marked for moderation. -
unindex_post($postid). If defined, this function is called when a post should be removed from the search index, because it has been hidden, deleted, or is about to be edited (in which caseindex_post()will be called immediately after). Generally, the parameter$postidmatches a value previously passed toindex_post()when the post was indexed. However in some circumstances the$postidmight never have been indexed, in which case the function should do nothing. -
move_post($postid, $categoryid). If defined, this function is called when a post's category is changed. The parameter$postidis the post'spostidfrom the database, and$categoryidis its newcategoryid. Generally, the parameter$postidmatches a value previously passed toindex_post()when the post was indexed. However in some circumstances the$postidmight never have been indexed, in which case the function should do nothing. -
index_page($pageid, $request, $title, $content, $format, $text). If defined, this function is called when a custom page should be added to the search index, because it is new or has just been edited. The parameter$pageidcontains thepageidfrom the database. The$requestparameter contains the URL fragment used to view the page - this can be passed toqa_path()to generate a URL. The$titleparameter contains the page's title in UTF-8 format. The$content,$formatand$textparameters work the same as forindex_post()above, but$formatis currently always'html'. -
unindex_page($pageid). If defined, this function is called when a custom page should be removed from the search index, because it has been delted or is about to be edited (in which caseindex_page()will be called immediately after). Generally, the parameter$pageidmatches a value previously passed toindex_page()when the page was indexed. However in some circumstances the$pageidmight never have been indexed, in which case the function should do nothing. -
process_search($query, $start, $count, $userid, $absoluteurls, $fullcontent). If defined, this function allows the search module to replace the default Q2A search results with its own, if the module is selected by the site administrator. The$queryparameter contains the entered search query in UTF-8 encoding. The parameter$userididentifies the user performing the query, ornullif no user is logged in. The$absoluteurlsand$fullcontentparameters are explained below. The function should return a nested array of up to$countsearch results for this query, starting at position$start>=0. Each element of the outer array is itself an inner array, which can contain one or more elements with the following keys:'question_postid' =>thepostidof the matching question. Use this to indicate a question page in general.'match_postid' =>thepostidof a specific matching question, answer or comment.'page_pageid' =>thepageidof a matching custom page.'title' =>the title to display for the search result.'url' =>the URL for the search result, which must be absolute if$absoluteurlsis true.
A search result can refer to an external website by using only the
'title'and'url'elements in the result's array. However, if one or both of these elements are present along with'question_postid','match_postid'or'page_pageid', then they are used to override the title or URL displayed for the question or page as appropriate.To save additional database queries, it is also possible to pass through more information about a matching question, post or page in
'question','match_type'or'page'elements. This requires knowledge of Q2A's internals - seeqa-app-search.phpfor more information on how these elements are used with the$fullcontentparameter.