3 * Global Search Engine for Moodle
7 * @subpackage document_wrappers
8 * @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
10 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 * @version revised for Moodle 2.0
13 * this is a format handler for getting text out of a proprietary binary format
14 * so it can be indexed by Lucene search engine
19 * @param object $resource
20 * @param string $directfile if the resource is given as a direct file path, use it as reference to the file
23 function get_text_for_indexing_doc(&$resource, $directfile = ''){
26 // SECURITY : do not allow non admin execute anything on system !!
27 if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) return;
29 // adds moodle root switch if none was defined
30 if (!isset($CFG->block_search_usemoodleroot
)){
31 set_config('block_search_usemoodleroot', 1);
34 $moodleroot = ($CFG->block_search_usemoodleroot
) ?
"{$CFG->dirroot}/" : '' ;
36 // just call pdftotext over stdout and capture the output
37 if (!empty($CFG->block_search_word_to_text_cmd
)){
38 if (!file_exists("{$moodleroot}{$CFG->block_search_word_to_text_cmd}")){
39 mtrace('Error with MSWord to text converter command : executable not found at '.$moodleroot.$CFG->block_search_word_to_text_cmd
);
41 if ($directfile == ''){
42 $file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
44 $file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
46 $command = trim($CFG->block_search_word_to_text_cmd
);
47 $text_converter_cmd = "{$moodleroot}{$command} -m UTF-8.txt $file";
48 if ($CFG->block_search_word_to_text_env
){
49 putenv($CFG->block_search_word_to_text_env
);
51 mtrace("Executing : $text_converter_cmd");
52 $result = shell_exec($text_converter_cmd);
54 return mb_convert_encoding($result, 'UTF-8', 'auto');
56 mtrace('Error with MSWord to text converter command : execution failed. ');
61 mtrace('Error with MSWord to text converter command : command not set up. Execute once search block configuration.');