From 29dc93d1cdb05047bc2fdd4e78e8d501bccf24af Mon Sep 17 00:00:00 2001 From: diml Date: Sat, 30 May 2009 22:16:28 +0000 Subject: [PATCH] MDL-19342 - small fixes from Tatsuva Shirai + UTF-8 global straighten up --- search/Zend/Search/Lucene.php | 2 +- search/documents/assignment_document.php | 3 ++ search/documents/chat_document.php | 8 ++- search/documents/data_document.php | 16 +++--- search/documents/document.php | 65 +++++++++++++++++++++++- search/documents/forum_document.php | 5 +- search/documents/label_document.php | 3 ++ search/documents/lesson_document.php | 5 +- search/documents/physical_doc.php | 3 +- search/documents/physical_odt.php | 3 +- search/documents/physical_ppt.php | 3 +- search/documents/resource_document.php | 2 + search/documents/user_document.php | 16 +++--- search/documents/wiki_document.php | 5 +- 14 files changed, 112 insertions(+), 27 deletions(-) diff --git a/search/Zend/Search/Lucene.php b/search/Zend/Search/Lucene.php index 361b85f5c6f..aaf081d3616 100644 --- a/search/Zend/Search/Lucene.php +++ b/search/Zend/Search/Lucene.php @@ -779,7 +779,7 @@ class Zend_Search_Lucene implements Zend_Search_Lucene_Interface public function find($query) { if (is_string($query)) { - $query = Zend_Search_Lucene_Search_QueryParser::parse($query); + $query = Zend_Search_Lucene_Search_QueryParser::parse($query, 'UTF-8'); } if (!$query instanceof Zend_Search_Lucene_Search_Query) { diff --git a/search/documents/assignment_document.php b/search/documents/assignment_document.php index e57f0f2231f..72ac36eb3d8 100644 --- a/search/documents/assignment_document.php +++ b/search/documents/assignment_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -301,6 +302,8 @@ function assignment_check_text_access($path, $itemtype, $this_id, $user, $group_ $context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $context->instanceid); + if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete + if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){ if (!empty($CFG->search_access_debug)) echo "search reject : hidden assignment "; return false; diff --git a/search/documents/chat_document.php b/search/documents/chat_document.php index 363b4389c54..ac56e36336e 100644 --- a/search/documents/chat_document.php +++ b/search/documents/chat_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -89,6 +90,10 @@ function chat_get_session_tracks($chat_id, $fromtime = 0, $totime = 0) { $course = get_record('course', 'id', $chat->course); $coursemodule = get_field('modules', 'id', 'name', 'data'); $cm = get_record('course_modules', 'course', $course->id, 'module', $coursemodule, 'instance', $chat->id); + if (empty($cm)) { // Shirai 20090530 + mtrace("Missing this chat: Course=".$chat->course."/ ChatID=".$chat_id); + return array(); + } $groupmode = groupmode($course, $cm); $fromtimeclause = ($fromtime) ? "AND timestamp >= {$fromtime}" : ''; @@ -260,8 +265,7 @@ function chat_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c $chat = get_record('chat', 'id', $chat_id); $context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $context->instanceid); - // $cm = get_coursemodule_from_instance('chat', $chat->id, $chat->course); - // $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){ if (!empty($CFG->search_access_debug)) echo "search reject : hidden chat "; diff --git a/search/documents/data_document.php b/search/documents/data_document.php index 07cfb283eb1..6a76b7c2ef2 100644 --- a/search/documents/data_document.php +++ b/search/documents/data_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -79,7 +80,7 @@ class DataCommentSearchDocument extends SearchDocument { //remove '(ip.ip.ip.ip)' from data record author field $doc->author = preg_replace('/\(.*?\)/', '', $comment['author']); $doc->contents = $comment['content']; - $doc->url = data_make_link($data_id, $comment['recordid']); + $doc->url = data_make_link($comment['dataid'], $comment['recordid']); // module specific information; optional $data->database = $comment['dataid']; @@ -196,7 +197,7 @@ function data_get_content_for_index(&$database) { $context = get_context_instance(CONTEXT_MODULE, $cm->id); // getting records for indexing - $records_content = data_get_records($database->id, 'text'); + $records_content = data_get_records($database->id, 'text,textarea'); if ($records_content){ foreach(array_keys($records_content) as $aRecordId) { @@ -222,7 +223,9 @@ function data_get_content_for_index(&$database) { $records_comments = data_get_comments($database->id); if ($records_comments){ foreach($records_comments as $aComment){ - $aComment->title = $recordsTitles[$aComment->recordid]; + $aComment->title = $recordTitles[$aComment->recordid]; + $authoruser = get_record('user', 'id', $aComment->userid); + $aComment->author = fullname($authoruser); $documents[] = new DataCommentSearchDocument(get_object_vars($aComment), $database->course, $context->id); } } @@ -247,7 +250,7 @@ function data_single_document($id, $itemtype) { $context = get_context_instance(CONTEXT_MODULE, $cm->id); // get text fields ids in this data (computable fields) // compute text - $recordData = data_get_records($recordMetaData->dataid, 'text', $id); + $recordData = data_get_records($recordMetaData->dataid, 'text,textarea', $id); if ($recordData){ $dataArray = array_values($recordData); $record_content = $dataArray[0]; // We cannot have more than one record here @@ -280,6 +283,8 @@ function data_single_document($id, $itemtype) { $comment->title = get_field('search_document', 'title', 'docid', $record->id, 'itemtype', 'record'); $comment->dataid = $record->dataid; $comment->groupid = $record->groupid; + $authoruser = get_record('user', 'id', $comment->userid); + $comment->author = fullname($authoruser); // make document return new DataCommentSearchDocument(get_object_vars($comment), $record_course, $context->id); } else { @@ -345,8 +350,7 @@ function data_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c $data = get_record('data', 'id', $record->dataid); $context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $context->instanceid); - // $cm = get_coursemodule_from_instance('data', $data->id, $data->course); - // $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (empty($cm)) return false; // Shirai 20093005 - MDL19342 - course module might have been delete if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) { if (!empty($CFG->search_access_debug)) echo "search reject : hidden database "; diff --git a/search/documents/document.php b/search/documents/document.php index ae3f46c7a4f..e68cfe55f13 100644 --- a/search/documents/document.php +++ b/search/documents/document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai on UTF-8 multibyte fixing * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -16,6 +17,66 @@ /** * */ +abstract class SearchDocument extends Zend_Search_Lucene_Document { + public function __construct(&$doc, &$data, $course_id, $group_id, $user_id, $path, $additional_keyset = null) { + + $encoding = 'UTF-8'; + + //document identification and indexing + $this->addField(Zend_Search_Lucene_Field::Keyword('docid', $doc->docid, $encoding)); + //document type : the name of the Moodle element that manages it + $this->addField(Zend_Search_Lucene_Field::Keyword('doctype', $doc->documenttype, $encoding)); + //allows subclassing information from complex modules. + $this->addField(Zend_Search_Lucene_Field::Keyword('itemtype', $doc->itemtype, $encoding)); + //caches the course context. + $this->addField(Zend_Search_Lucene_Field::Keyword('course_id', $course_id, $encoding)); + //caches the originator's group. + $this->addField(Zend_Search_Lucene_Field::Keyword('group_id', $group_id, $encoding)); + //caches the originator if any + $this->addField(Zend_Search_Lucene_Field::Keyword('user_id', $user_id, $encoding)); + // caches the context of this information. i-e, the context in which this information + // is being produced/attached. Speeds up the "check for access" process as context in + // which the information resides (a course, a module, a block, the site) is stable. + $this->addField(Zend_Search_Lucene_Field::UnIndexed('context_id', $doc->contextid, $encoding)); + + //data for document + $this->addField(Zend_Search_Lucene_Field::Text('title', $doc->title, $encoding)); + $this->addField(Zend_Search_Lucene_Field::Text('author', $doc->author, $encoding)); + $this->addField(Zend_Search_Lucene_Field::UnStored('contents', $doc->contents, $encoding)); + $this->addField(Zend_Search_Lucene_Field::UnIndexed('url', $doc->url, $encoding)); + $this->addField(Zend_Search_Lucene_Field::UnIndexed('date', $doc->date, $encoding)); + + //additional data added on a per-module basis + $this->addField(Zend_Search_Lucene_Field::Binary('data', serialize($data))); + + // adding a path allows the document to know where to find specific library calls + // for checking access to a module or block content. The Lucene records should only + // be responsible to bring back to that call sufficient and consistent information + // in order to perform the check. + $this->addField(Zend_Search_Lucene_Field::UnIndexed('path', $path, $encoding)); + /* + // adding a capability set required for viewing. -1 if no capability required. + // the capability required for viewing is depending on the local situation + // of the document. each module should provide this information when pushing + // out search document structure. Although capability model should be kept flat + // there is no exclusion some module or block developpers use logical combinations + // of multiple capabilities in their code. This possibility should be left open here. + $this->addField(Zend_Search_Lucene_Field::UnIndexed('capabilities', $caps)); + */ + + /* + // Additional key set allows a module to ask for extensible criteria based search + // depending on the module internal needs. + */ + if (!empty($additional_keyset)){ + foreach($additional_keyset as $keyname => $keyvalue){ + $this->addField(Zend_Search_Lucene_Field::Keyword($keyname, $keyvalue, $encoding)); + } + } + } +} + +/* abstract class SearchDocument extends Zend_Search_Lucene_Document { public function __construct(&$doc, &$data, $course_id, $group_id, $user_id, $path, $additional_keyset = null) { //document identification and indexing @@ -63,13 +124,13 @@ abstract class SearchDocument extends Zend_Search_Lucene_Document { /* // Additional key set allows a module to ask for extensible criteria based search // depending on the module internal needs. - */ + * if (!empty($additional_keyset)){ foreach($additional_keyset as $keyname => $keyvalue){ $this->addField(Zend_Search_Lucene_Field::Keyword($keyname, $keyvalue)); } } } -} +}*/ ?> \ No newline at end of file diff --git a/search/documents/forum_document.php b/search/documents/forum_document.php index 012eefc9532..6b68beaf09f 100644 --- a/search/documents/forum_document.php +++ b/search/documents/forum_document.php @@ -5,6 +5,7 @@ * @package search * @category core * @subpackage document_wrappers +* @contributor Tatsuva Shirai 20090530 * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License @@ -278,8 +279,8 @@ function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $ $discussion = get_record('forum_discussions', 'id', $post->discussion); $context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $context->instanceid); - // $cm = get_coursemodule_from_instance('forum', $discussion->forum, $discussion->course); - // $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (empty($cm)) return false; // Shirai 20093005 - MDL19342 - course module might have been delete + if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){ if (!empty($CFG->search_access_debug)) echo "search reject : hidden forum resource "; return false; diff --git a/search/documents/label_document.php b/search/documents/label_document.php index 6f4892a91d8..9ee02853e0c 100644 --- a/search/documents/label_document.php +++ b/search/documents/label_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.9 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -154,6 +155,8 @@ function label_check_text_access($path, $itemtype, $this_id, $user, $group_id, $ $r = get_record('label', 'id', $this_id); $module_context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $module_context->instanceid); + if (empty($cm)) return false; // Shirai 20093005 - MDL19342 - course module might have been delete + $course_context = get_context_instance(CONTEXT_COURSE, $r->course); //check if englobing course is visible diff --git a/search/documents/lesson_document.php b/search/documents/lesson_document.php index a18d569259a..33dad598248 100644 --- a/search/documents/lesson_document.php +++ b/search/documents/lesson_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -172,9 +173,7 @@ function lesson_check_text_access($path, $itemtype, $this_id, $user, $group_id, $lesson = get_record('lesson', 'id', $page->lessonid); $context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $context->instanceid); - // $lesson = get_record('lesson', 'id', $page->lessonid); - // $cm = get_coursemodule_from_instance('lesson', $page->lessonid, $lesson->course); - // $context = get_context_instance(CONTEXT_MODULE, $cm->id); + if (empty($cm)) return false; // Shirai 20093005 - MDL19342 - course module might have been delete if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)){ if (!empty($CFG->search_access_debug)) echo "search reject : hidden lesson "; diff --git a/search/documents/physical_doc.php b/search/documents/physical_doc.php index 2cd48cd4fae..deb7e0ccf7a 100644 --- a/search/documents/physical_doc.php +++ b/search/documents/physical_doc.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -29,7 +30,7 @@ function get_text_for_indexing_doc(&$resource, $directfile = ''){ // just call pdftotext over stdout and capture the output if (!empty($CFG->block_search_word_to_text_cmd)){ if (!file_exists("{$moodleroot}{$CFG->block_search_word_to_text_cmd}")){ - mtrace('Error with MSWord to text converter command : exectuable not found.'); + mtrace('Error with MSWord to text converter command : exectutable not found at '.$moodleroot.$CFG->block_search_word_to_text_cmd); } else { if ($directfile == ''){ $file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"); diff --git a/search/documents/physical_odt.php b/search/documents/physical_odt.php index 2ffa13fe595..3a13a61e634 100644 --- a/search/documents/physical_odt.php +++ b/search/documents/physical_odt.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -29,7 +30,7 @@ function get_text_for_indexing_odt(&$resource, $directfile = ''){ // just call pdftotext over stdout and capture the output if (!empty($CFG->block_search_odt_to_text_cmd)){ if (!file_exists("{$moodleroot}{$CFG->block_search_odt_to_text_cmd}")){ - mtrace('Error with OpenOffice ODT to text converter command : exectuable not found.'); + mtrace('Error with OpenOffice ODT to text converter command : executable not found at'.$moodleroot.$CFG->block_search_odt_to_text_cmd); } else { if ($directfile == ''){ $file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"); diff --git a/search/documents/physical_ppt.php b/search/documents/physical_ppt.php index 76b97ae7bd1..fa8bbfd4fc8 100644 --- a/search/documents/physical_ppt.php +++ b/search/documents/physical_ppt.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -88,7 +89,7 @@ function get_text_for_indexing_ppt(&$resource, $directfile = ''){ $indextext = shorten($text, $CFG->block_search_limit_index_body); } - $indextext = mb_convert_encoding($indextext, 'UTF8', 'auto'); + $indextext = mb_convert_encoding($indextext, 'UTF-8', 'auto'); // Shirai 20090530 - MDL19342 return $indextext; } ?> \ No newline at end of file diff --git a/search/documents/resource_document.php b/search/documents/resource_document.php index 3b141938ec2..ca6657b79f9 100644 --- a/search/documents/resource_document.php +++ b/search/documents/resource_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -307,6 +308,7 @@ function resource_check_text_access($path, $itemtype, $this_id, $user, $group_id $r = get_record('resource', 'id', $this_id); $module_context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $module_context->instanceid); + if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete $course_context = get_context_instance(CONTEXT_COURSE, $r->course); //check if course is visible diff --git a/search/documents/user_document.php b/search/documents/user_document.php index 83eac3abab5..e309b59fc7e 100644 --- a/search/documents/user_document.php +++ b/search/documents/user_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -48,7 +49,8 @@ class UserSearchDocument extends SearchDocument { // module specific information; optional // construct the parent class - parent::__construct($doc, $data, 0, 0, 0, PATH_FOR_SEARCH_TYPE_USER); + // Shirai : User pictures are not displayed in results of blogs (2009/05/29) MDL19341 + parent::__construct($doc, $data, 0, 0, $user_id, PATH_FOR_SEARCH_TYPE_USER);; } } @@ -71,18 +73,20 @@ class UserPostSearchDocument extends SearchDocument { $user = get_record('user', 'id', $user_id); // we cannot call userdate with relevant locale at indexing time. - $doc->title = get_string('post').': '.fullname($user); + // $doc->title = get_string('post').': '.fullname($user); + $doc->title = $post['subject']; $doc->date = $post['created']; //remove '(ip.ip.ip.ip)' from chat author list $doc->author = fullname($user); $doc->contents = $post['description']; - $doc->url = user_make_link($user_id, 'post'); + // $doc->url = user_make_link($user_id, 'post'); + $doc->url = user_make_link($post['id'], 'post'); // module specific information; optional // construct the parent class - parent::__construct($doc, $data, 0, 0, 0, PATH_FOR_SEARCH_TYPE_USER); + parent::__construct($doc, $data, 0, 0, $user_id, PATH_FOR_SEARCH_TYPE_USER); } } @@ -116,7 +120,7 @@ class UserBlogAttachmentSearchDocument extends SearchDocument { // module specific information; optional // construct the parent class - parent::__construct($doc, $data, 0, 0, 0, PATH_FOR_SEARCH_TYPE_USER); + parent::__construct($doc, $data, 0, 0, $post['userid'], PATH_FOR_SEARCH_TYPE_USER); } } @@ -134,7 +138,7 @@ function user_make_link($itemid, $itemtype) { if ($itemtype == 'user'){ return $CFG->wwwroot.'/user/view.php?id='.$itemid; } elseif ($itemtype == 'post') { - return $CFG->wwwroot.'/blog/index.php?userid='.$itemid; + return $CFG->wwwroot.'/blog/index.php?postid='.$itemid; } elseif ($itemtype == 'attachment') { $post = get_record('post', 'id', $itemid); if (!$CFG->slasharguments){ diff --git a/search/documents/wiki_document.php b/search/documents/wiki_document.php index 113dc39118b..28b183711f2 100644 --- a/search/documents/wiki_document.php +++ b/search/documents/wiki_document.php @@ -6,6 +6,7 @@ * @category core * @subpackage document_wrappers * @author Michael Campanis (mchampan) [cynnical@gmail.com], Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8 +* @contributor Tatsuva Shirai 20090530 * @date 2008/03/31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @@ -245,8 +246,8 @@ function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c $course = get_record('course', 'id', $entry->course); $context = get_record('context', 'id', $context_id); $cm = get_record('course_modules', 'id', $context->instanceid); - // $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course); - // $context = get_record('context', 'id', $cm->id); + if (empty($cm)) return false; // Shirai 20090530 - MDL19342 - course module might have been delete + if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) { if (!empty($CFG->search_access_debug)) echo "search reject : hidden wiki "; return false;