From d51a8d2e4039a7380d2c6e47f450d1d15323ae89 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Fri, 1 Dec 2017 15:14:55 +0000 Subject: [PATCH] MDL-60943 core_search: Improve exception for preg_replace errors --- lang/en/search.php | 1 + search/classes/document.php | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lang/en/search.php b/lang/en/search.php index 096722d5a91d8..ebd0f5a32e7b4 100644 --- a/lang/en/search.php +++ b/lang/en/search.php @@ -51,6 +51,7 @@ $string['enginenotselected'] = 'You have not selected any search engine.'; $string['engineserverstatus'] = 'The search engine is not available. Please contact your administrator.'; $string['enteryoursearchquery'] = 'Enter your search query'; +$string['error_indexing'] = 'An error occurred while indexing'; $string['errors'] = 'Errors'; $string['errorareanotavailable'] = '{$a} search area is not available.'; $string['filesinindexdirectory'] = 'Files in index directory'; diff --git a/search/classes/document.php b/search/classes/document.php index 97cdc7b93969c..64831b189c9b3 100644 --- a/search/classes/document.php +++ b/search/classes/document.php @@ -280,6 +280,16 @@ public function set($fieldname, $value) { } else { // Replace all groups of line breaks and spaces by single spaces. $this->data[$fieldname] = preg_replace("/\s+/u", " ", $value); + if ($this->data[$fieldname] === null) { + if (isset($this->data['id'])) { + $docid = $this->data['id']; + } else { + $docid = '(unknown)'; + } + throw new \moodle_exception('error_indexing', 'search', '', null, '"' . $fieldname . + '" value causes preg_replace error (may be caused by unusual characters) ' . + 'in document with id "' . $docid . '"'); + } } return $this->data[$fieldname];