MDL-14978 removed obsoleted isadmin()

This commit is contained in:
skodak 2008-05-25 10:08:05 +00:00
parent 72f563b91c
commit e3c7f155d8
23 changed files with 432 additions and 263 deletions

View file

@ -79,7 +79,7 @@
}
}
} else { // if upgrading from 1.6 or below
if (isadmin() && moodle_needs_upgrading()) {
if (is_siteadmin() && moodle_needs_upgrading()) {
redirect($CFG->wwwroot .'/'. $CFG->admin .'/index.php');
}
}

View file

@ -514,7 +514,7 @@ function has_any_capability($capabilities, $context, $userid=NULL, $doanything=t
* - moodle/site:doanything
*
* @param int $userid
* @returns bool $isadmin
* @returns bool true is user can administer server settings
*/
function is_siteadmin($userid) {
global $CFG, $DB;
@ -532,8 +532,7 @@ function is_siteadmin($userid) {
HAVING SUM(rc.permission) > 0";
$params = array($userid, 'moodle/site:config', 'moodle/legacy:admin', 'moodle/site:doanything');
$isadmin = $DB->record_exists_sql($sql, $params);
return $isadmin;
return $DB->record_exists_sql($sql, $params);
}
function get_course_from_path ($path) {

View file

@ -35,38 +35,6 @@
* @package moodlecore
*/
/**
* Determines if a user an admin
*
* @uses $USER
* @param int $userid The id of the user as is found in the 'user' table
* @staticvar array $admins List of users who have been found to be admins by user id
* @staticvar array $nonadmins List of users who have been found not to be admins by user id
* @return bool
*/
function isadmin($userid=0) {
global $USER, $CFG;
if (empty($CFG->rolesactive)) { // Then the user is likely to be upgrading NOW
if (!$userid) {
if (empty($USER->id)) {
return false;
}
if (!empty($USER->admin)) {
return true;
}
$userid = $USER->id;
}
return record_exists('user_admins', 'userid', $userid);
}
$context = get_context_instance(CONTEXT_SYSTEM);
return has_capability('moodle/legacy:admin', $context, $userid, false);
}
/**
* Determines if a user is a teacher (or better)
*

View file

@ -1477,7 +1477,7 @@ function feedback_get_groupid($course, $cm) {
$groupmode = groupmode($course, $cm);
//get groupid
if($groupmode > 0 && !isadmin()) {
if($groupmode > 0 && !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
if($mygroupid = mygroupid($course->id)) {
return $mygroupid[0]; //get the first groupid
}

View file

@ -114,7 +114,7 @@
//get students in conjunction with groupmode
if($groupmode > 0) {
if($SESSION->feedback->lstgroupid == -2) {
if(isadmin()) {
if(has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
$mygroupid = false;
$SESSION->feedback->lstgroupid = false;
}else{

View file

@ -34,7 +34,7 @@
error(get_string('globalsearchdisabled', 'search'));
}
if (!isadmin()) {
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
}

View file

@ -31,7 +31,7 @@
error(get_string('globalsearchdisabled', 'search'));
}
if (!isadmin()) {
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
} //if

View file

@ -166,7 +166,8 @@ function forum_get_discussions_fast($forum_id) {
$timelimit='';
if (!empty($CFG->forum_enabletimedposts)) {
if (!((isadmin() and !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum_id)))) {
if (!((has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))
and !empty($CFG->admineditalways)) || isteacher(get_field('forum', 'course', 'id', $forum_id)))) {
$now = time();
$timelimit = " AND ((d.timestart = 0 OR d.timestart <= '$now') AND (d.timeend = 0 OR d.timeend > '$now')";
if (!empty($USER->id)) {
@ -302,7 +303,8 @@ function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $
* @param string $title
*/
function forum_link_post_processing($title){
return mb_convert_encoding($title, 'UTF-8', 'auto');
// return mb_convert_encoding($title, 'UTF-8', 'auto');
return mb_convert_encoding($title, 'auto', 'UTF-8');
}
?>

View file

@ -18,11 +18,11 @@
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_doc(&$resource){
function get_text_for_indexing_doc(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return;
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
$moodleroot = (@$CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
@ -30,11 +30,14 @@ function get_text_for_indexing_doc(&$resource){
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.');
}
else{
$file = escapeshellarg($CFG->dataroot.'/'.$resource->course.'/'.$resource->reference);
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
}
$command = trim($CFG->block_search_word_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} $file";
$text_converter_cmd = "{$moodleroot}{$command} -m UTF-8.txt $file";
if ($CFG->block_search_word_to_text_env){
putenv($CFG->block_search_word_to_text_env);
}
@ -42,14 +45,12 @@ function get_text_for_indexing_doc(&$resource){
$result = shell_exec($text_converter_cmd);
if ($result){
return mb_convert_encoding($result, 'UTF8', 'auto');
}
else{
} else {
mtrace('Error with MSWord to text converter command : execution failed. ');
return '';
}
}
}
else {
} else {
mtrace('Error with MSWord to text converter command : command not set up. Execute once search block configuration.');
return '';
}

View file

@ -17,14 +17,18 @@
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_htm(&$resource){
function get_text_for_indexing_htm(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return;
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// just get text
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
if ($directfile == ''){
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
} else {
$text = implode('', file("{$CFG->dataroot}/{$directfile}"));
}
// extract keywords and other interesting meta information and put it back as real content for indexing
if (preg_match('/(.*)<meta ([^>]*)>(.*)/is', $text, $matches)){
@ -40,7 +44,7 @@ function get_text_for_indexing_htm(&$resource){
$text = preg_replace("/<[^>]*>/", '', $text);
$text = preg_replace("/<!--[^>]*-->/", '', $text);
$text = html_entity_decode($text, ENT_COMPAT, 'UTF-8');
$text = mb_convert_encoding($text, 'UTF-8', 'AUTO');
$text = mb_convert_encoding($text, 'UTF-8', 'auto');
/*
* debug code for tracing input

View file

@ -17,11 +17,11 @@
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_pdf(&$resource){
function get_text_for_indexing_pdf(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return;
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// adds moodle root switch if none was defined
if (!isset($CFG->block_search_usemoodleroot)){
@ -34,23 +34,24 @@ function get_text_for_indexing_pdf(&$resource){
if (!empty($CFG->block_search_pdf_to_text_cmd)){
preg_match("/^\S+/", $CFG->block_search_pdf_to_text_cmd, $matches);
if (!file_exists("{$moodleroot}{$matches[0]}")){
mtrace('Error with pdf to text converter command : exectuable not found at '.$moodleroot.$matches[0]);
}
else{
$file = escapeshellarg($CFG->dataroot.'/'.$resource->course.'/'.$resource->reference);
mtrace('Error with pdf to text converter command : executable not found at '.$moodleroot.$matches[0]);
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
}
$command = trim($CFG->block_search_pdf_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} $file -";
$result = shell_exec($text_converter_cmd);
if ($result){
return $result;
}
else{
} else {
mtrace('Error with pdf to text converter command : execution failed for '.$text_converter_cmd.'. Check for execution permission on pdf converter executable.');
return '';
}
}
}
else {
} else {
mtrace('Error with pdf to text converter command : command not set up. Execute once search block configuration.');
return '';
}

View file

@ -31,15 +31,19 @@
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_ppt(&$resource){
function get_text_for_indexing_ppt(&$resource, $directfile = ''){
global $CFG, $USER;
$indextext = null;
// SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return;
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
if ($directfile == ''){
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
} else {
$text = implode('', file("{$CFG->dataroot}/{$directfile}"));
}
$remains = $text;
$fragments = array();

View file

@ -0,0 +1,69 @@
<?php
/**
* Global Search Engine for Moodle
*
* @package search
* @category core
* @subpackage document_wrappers
* @author Valery Fremaux [valery.fremaux@club-internet.fr] > 1.8
* @date 2008/03/31
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*
* @note : The Adobe SWF Converters library is not GPL, although it can be of free use in some
* situations. This file is provided for convenience, but should use having a glance at
* {@link http://www.adobe.com/licensing/developer/}
*
* this is a format handler for getting text out of a proprietary binary format
* so it can be indexed by Lucene search engine
*/
/**
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_swf(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// adds moodle root switch if none was defined
if (!isset($CFG->block_search_usemoodleroot)){
set_config('block_search_usemoodleroot', 1);
}
$moodleroot = ($CFG->block_search_usemoodleroot) ? "{$CFG->dirroot}/" : '' ;
// just call pdftotext over stdout and capture the output
if (!empty($CFG->block_search_pdf_to_text_cmd)){
if (!file_exists("{$moodleroot}{$command}")){
mtrace('Error with swf to text converter command : executable not found as '.$moodleroot.$command);
} else {
if ($directfile == ''){
$file = escapeshellarg("{$CFG->dataroot}/{$resource->course}/{$resource->reference}");
} else {
$file = escapeshellarg("{$CFG->dataroot}/{$directfile}");
}
$command = trim($CFG->block_search_swf_to_text_cmd);
$text_converter_cmd = "{$moodleroot}{$command} -t $file";
$result = shell_exec($text_converter_cmd);
// result is in html. We must strip it off
$result = preg_replace("/<[^>]*>/", '', $result);
$result = preg_replace("/<!--[^>]*-->/", '', $result);
$result = html_entity_decode($result, ENT_COMPAT, 'UTF-8');
$result = mb_convert_encoding($result, 'UTF-8', 'auto');
if ($result){
return $result;
} else {
mtrace('Error with swf to text converter command : execution failed for '.$text_converter_cmd.'. Check for execution permission on swf converter executable.');
return '';
}
}
} else {
mtrace('Error with swf to text converter command : command not set up. Execute once search block configuration.');
return '';
}
}
?>

View file

@ -17,14 +17,19 @@
* @param object $resource
* @uses CFG, USER
*/
function get_text_for_indexing_txt(&$resource){
function get_text_for_indexing_txt(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return;
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// just try to get text empirically from ppt binary flow
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
if ($directfile == ''){
$text = implode('', file("{$CFG->dataroot}/{$resource->course}/{$resource->reference}"));
} else {
$text = implode('', file("{$CFG->dataroot}/{$directfile}"));
}
if (!empty($CFG->block_search_limit_index_body)){
$text = shorten($text, $CFG->block_search_limit_index_body);
}

View file

@ -21,7 +21,7 @@ function get_text_for_indexing_xml(&$resource, $directfile = ''){
global $CFG, $USER;
// SECURITY : do not allow non admin execute anything on system !!
if (!isadmin($USER->id)) return;
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) return;
// just get text
if ($directfile == ''){

View file

@ -334,7 +334,7 @@ function user_check_text_access($path, $itemtype, $this_id, $user, $group_id, $c
$userrecord = get_record('user', 'id', $this_id);
// we cannot see nothing from unconfirmed users
if (!$userrecord->confirmed and !isadmin()){
if (!$userrecord->confirmed and !has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
if (!empty($CFG->search_access_debug)) echo "search reject : unconfirmed user ";
return false;
}

View file

@ -35,6 +35,10 @@
*/
require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
//require_once("debugging.php");
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'\search'.$separator.ini_get('include_path'));
/// only administrators can index the moodle installation, because access to all pages is required
@ -44,7 +48,7 @@ require_once("$CFG->dirroot/search/lib.php");
error(get_string('globalsearchdisabled', 'search'));
}
if (!isadmin()) {
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
}
@ -59,6 +63,13 @@ require_once("$CFG->dirroot/search/lib.php");
exit(0);
}
/// check for php5 (lib.php)
if (!search_check_php5()) {
mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
exit(0);
}
//php5 found, continue including php5-only files
//require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
require_once("$CFG->dirroot/search/indexlib.php");
@ -86,13 +97,16 @@ require_once("$CFG->dirroot/search/lib.php");
mtrace("Data directory ($index_path) does not exist, attempting to create.");
if (!mkdir($index_path)) {
search_pexit("Error creating data directory at: $index_path. Please correct.");
} else {
}
else {
mtrace("Directory successfully created.");
}
} else {
}
else {
mtrace("Using $index_path as data directory.");
}
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
$index = new Zend_Search_Lucene($index_path, true);
/*
@ -116,31 +130,37 @@ require_once("$CFG->dirroot/search/lib.php");
// * mod_iterator
// * mod_get_content_for_index
//are the sole basis for including a module in the index at the moment.
$searchables = array();
$searchables = search_collect_searchables();
/// collects modules
/// start indexation
if ($mods = get_records('modules', '', '', '', 'id,name')) {
$searchables = array_merge($searchables, $mods);
}
mtrace(count($searchables).' modules found.');
// collects blocks as indexable information may be found in blocks either
if ($blocks = get_records('block', '', '', '', 'id,name')) {
// prepend the "block_" prefix to discriminate document type plugins
foreach(array_keys($blocks) as $aBlockId){
$blocks[$aBlockId]->name = 'block_'.$blocks[$aBlockId]->name;
}
$searchables = array_merge($searchables, $blocks);
mtrace(count($blocks).' blocks found.');
}
/// add virtual modules onto the back of the array
$searchables = array_merge($searchables, search_get_additional_modules());
if ($searchables){
foreach ($searchables as $mod) {
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
$key = 'search_in_'.$mod->name;
if (isset($CFG->$key) && !$CFG->$key) {
mtrace("module $key has been administratively disabled. Skipping...\n");
continue;
}
if ($mod->location == 'internal'){
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
} else {
$class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
}
/*
if (!file_exists($class_file)){
if (defined("PATH_FOR_SEARCH_TYPE_{$mod->name}")){
eval("\$pluginpath = PATH_FOR_SEARCH_TYPE_{$mod->name}");
$class_file = "{$CFG->dirroot}/{$pluginpath}/searchlib.php";
} else {
mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
continue;
}
}
*/
if (file_exists($class_file)) {
include_once($class_file);
@ -187,6 +207,8 @@ require_once("$CFG->dirroot/search/lib.php");
mtrace("-- $counter documents indexed");
mtrace("done.\n");
}
} else {
mtrace ("No search document found for plugin {$mod->name}. Ignoring.");
}
}
}

View file

@ -18,7 +18,11 @@
* includes and requires
*/
require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
require_once("{$CFG->dirroot}/search/lib.php");
/// makes inclusions of the Zend Engine more reliable
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'\search'.$separator.ini_get('include_path'));
/// check global search is enabled
@ -28,10 +32,18 @@ require_once("$CFG->dirroot/search/lib.php");
error(get_string('globalsearchdisabled', 'search'));
}
if (!isadmin()) {
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
}
/// check for php5 (lib.php)
if (!search_check_php5()) {
$phpversion = phpversion();
mtrace("Sorry, global search requires PHP 5.0.0 or later (currently using version ".phpversion().")");
exit(0);
}
require_once("$CFG->dirroot/search/indexlib.php");
$indexinfo = new IndexInfo();

View file

@ -50,90 +50,102 @@
/// check for php5, but don't die yet (see line 52)
require_once("{$CFG->dirroot}/search/querylib.php");
$page_number = optional_param('page', -1, PARAM_INT);
$pages = ($page_number == -1) ? false : true;
$advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false;
$query_string = optional_param('query_string', '', PARAM_CLEAN);
if ($pages && isset($_SESSION['search_advanced_query'])) {
// if both are set, then we are busy browsing through the result pages of an advanced query
$adv = unserialize($_SESSION['search_advanced_query']);
} else if ($advanced) {
// otherwise we are dealing with a new advanced query
unset($_SESSION['search_advanced_query']);
session_unregister('search_advanced_query');
if ($check = search_check_php5()) {
require_once("{$CFG->dirroot}/search/querylib.php");
$page_number = optional_param('page', -1, PARAM_INT);
$pages = ($page_number == -1) ? false : true;
$advanced = (optional_param('a', '0', PARAM_INT) == '1') ? true : false;
$query_string = optional_param('query_string', '', PARAM_CLEAN);
// chars to strip from strings (whitespace)
$chars = " \t\n\r\0\x0B,-+";
/**
* discard harmfull searches
*/
if (preg_match("/^[\*\?]+$/", $query_string)){
$query_string = '';
$error = get_string('fullwildcardquery','search');
}
// retrieve advanced query variables
$adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
$adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
$adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
$adv->module = optional_param('module', '', PARAM_CLEAN);
$adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
$adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
if ($pages && isset($_SESSION['search_advanced_query'])) {
// if both are set, then we are busy browsing through the result pages of an advanced query
$adv = unserialize($_SESSION['search_advanced_query']);
} elseif ($advanced) {
// otherwise we are dealing with a new advanced query
unset($_SESSION['search_advanced_query']);
session_unregister('search_advanced_query');
// chars to strip from strings (whitespace)
$chars = " \t\n\r\0\x0B,-+";
// retrieve advanced query variables
$adv->mustappear = trim(optional_param('mustappear', '', PARAM_CLEAN), $chars);
$adv->notappear = trim(optional_param('notappear', '', PARAM_CLEAN), $chars);
$adv->canappear = trim(optional_param('canappear', '', PARAM_CLEAN), $chars);
$adv->module = optional_param('module', '', PARAM_CLEAN);
$adv->title = trim(optional_param('title', '', PARAM_CLEAN), $chars);
$adv->author = trim(optional_param('author', '', PARAM_CLEAN), $chars);
}
if ($advanced) {
//parse the advanced variables into a query string
//TODO: move out to external query class (QueryParse?)
$query_string = '';
// get all available module types
$module_types = array_merge(array('all'), array_values(search_get_document_types()));
$adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all';
// convert '1 2' into '+1 +2' for required words field
if (strlen(trim($adv->mustappear)) > 0) {
$query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
}
// convert '1 2' into '-1 -2' for not wanted words field
if (strlen(trim($adv->notappear)) > 0) {
$query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
}
// this field is left untouched, apart from whitespace being stripped
if (strlen(trim($adv->canappear)) > 0) {
$query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
}
// add module restriction
$doctypestr = get_string('doctype', 'search');
$titlestr = get_string('title', 'search');
$authorstr = get_string('author', 'search');
if ($adv->module != 'all') {
$query_string .= " +{$doctypestr}:".$adv->module;
}
// create title search string
if (strlen(trim($adv->title)) > 0) {
$query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
}
// create author search string
if (strlen(trim($adv->author)) > 0) {
$query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
}
// save our options if the query is valid
if (!empty($query_string)) {
$_SESSION['search_advanced_query'] = serialize($adv);
}
}
// normalise page number
if ($page_number < 1) {
$page_number = 1;
}
//run the query against the index
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
$sq = new SearchQuery($query_string, $page_number, 10, false);
}
if ($advanced) {
//parse the advanced variables into a query string
//TODO: move out to external query class (QueryParse?)
$query_string = '';
// get all available module types
$module_types = array_merge(array('all'), array_values(search_get_document_types()));
$adv->module = in_array($adv->module, $module_types) ? $adv->module : 'all';
// convert '1 2' into '+1 +2' for required words field
if (strlen(trim($adv->mustappear)) > 0) {
$query_string = ' +'.implode(' +', preg_split("/[\s,;]+/", $adv->mustappear));
}
// convert '1 2' into '-1 -2' for not wanted words field
if (strlen(trim($adv->notappear)) > 0) {
$query_string .= ' -'.implode(' -', preg_split("/[\s,;]+/", $adv->notappear));
}
// this field is left untouched, apart from whitespace being stripped
if (strlen(trim($adv->canappear)) > 0) {
$query_string .= ' '.implode(' ', preg_split("/[\s,;]+/", $adv->canappear));
}
// add module restriction
$doctypestr = get_string('doctype', 'search');
$titlestr = get_string('title', 'search');
$authorstr = get_string('author', 'search');
if ($adv->module != 'all') {
$query_string .= " +{$doctypestr}:".$adv->module;
}
// create title search string
if (strlen(trim($adv->title)) > 0) {
$query_string .= " +{$titlestr}:".implode(" +{$titlestr}:", preg_split("/[\s,;]+/", $adv->title));
}
// create author search string
if (strlen(trim($adv->author)) > 0) {
$query_string .= " +{$authorstr}:".implode(" +{$authorstr}:", preg_split("/[\s,;]+/", $adv->author));
}
// save our options if the query is valid
if (!empty($query_string)) {
$_SESSION['search_advanced_query'] = serialize($adv);
}
}
// normalise page number
if ($page_number < 1) {
$page_number = 1;
}
//run the query against the index
$sq = new SearchQuery($query_string, $page_number, 10, false);
if (!$site = get_site()) {
redirect("index.php");
}
@ -153,6 +165,17 @@
print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
}
//keep things pretty, even if php5 isn't available
if (!$check) {
print_heading(search_check_php5(true));
print_footer();
exit(0);
}
if (!empty($error)){
notice ($error);
}
print_box_start();
print_heading($strquery);
@ -168,7 +191,6 @@
}
}
?>
<form id="query" method="get" action="query.php">
<?php
if (!$advanced) {
@ -178,7 +200,8 @@
<a href="query.php?a=1"><?php print_string('advancedsearch', 'search') ?></a> |
<a href="stats.php"><?php print_string('statistics', 'search') ?></a>
<?php
} else {
}
else {
print_box_start();
?>
<input type="hidden" name="a" value="<?php print $advanced; ?>"/>
@ -267,7 +290,8 @@
if ($sq->is_valid_index()) {
//use cached variable to show up-to-date index size (takes deletions into account)
print $CFG->search_index_size;
} else {
}
else {
print "0";
}
@ -275,7 +299,7 @@
print_string('documents', 'search');
print '.';
if (!$sq->is_valid_index() and isadmin()) {
if (!$sq->is_valid_index() and has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
print '<p>' . get_string('noindexmessage', 'search') . '<a href="indexersplash.php">' . get_string('createanindex', 'search')."</a></p>\n";
}

View file

@ -377,7 +377,7 @@ class SearchQuery {
* course related checks
*/
// admins can see everything, anyway.
if (isadmin()){
if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))){
return true;
}

View file

@ -30,9 +30,13 @@ require_once("{$CFG->dirroot}/search/lib.php");
error(get_string('globalsearchdisabled', 'search'));
}
require_once("{$CFG->dirroot}/search/indexlib.php");
$indexinfo = new IndexInfo();
/// check for php5, but don't die yet
if ($check = search_check_php5()) {
require_once("{$CFG->dirroot}/search/indexlib.php");
$indexinfo = new IndexInfo();
}
if (!$site = get_site()) {
redirect("index.php");
@ -54,6 +58,12 @@ require_once("{$CFG->dirroot}/search/lib.php");
/// keep things pretty, even if php5 isn't available
if (!$check) {
print_heading(search_check_php5(true));
print_footer();
exit(0);
}
print_box_start();
print_heading($strquery);
@ -67,7 +77,7 @@ require_once("{$CFG->dirroot}/search/lib.php");
/// this table is only for admins, shows index directory size and location
if (isadmin()) {
if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
$datadirectorystr = get_string('datadirectory', 'search');
$inindexdirectorystr = get_string('filesinindexdirectory', 'search');
$totalsizestr = get_string('totalsize', 'search');
@ -137,7 +147,7 @@ require_once("{$CFG->dirroot}/search/lib.php");
/// add extra fields if we're admin
if (isadmin()) {
if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
//don't want to confuse users if the two totals don't match (hint: they should)
$table->data[] = array($documentsinindexstr, $indexinfo->indexcount);

View file

@ -1,99 +1,147 @@
<?php
/* Used to test if modules/blocks are ready to included in the search index.
* Carries out some basic function/file existence tests - the search module
* is expected to exist, along with the db schema files and the search data
* directory.
* */
/* Used to test if modules/blocks are ready to included in the search index.
* Carries out some basic function/file existence tests - the search module
* is expected to exist, along with the db schema files and the search data
* directory.
**/
@set_time_limit(0);
@ob_implicit_flush(true);
@ob_end_flush();
@set_time_limit(0);
@ob_implicit_flush(true);
@ob_end_flush();
require_once('../../config.php');
require_once("$CFG->dirroot/search/lib.php");
require_once('../../config.php');
require_once("$CFG->dirroot/search/lib.php");
require_login();
$strsearch = get_string('search', 'search');
$strquery = get_string('stats');
/// makes inclusions of the Zend Engine more reliable
$separator = (array_key_exists('WINDIR', $_SERVER)) ? ';' : ':' ;
ini_set('include_path', $CFG->dirroot.'\search'.$separator.ini_get('include_path'));
require_login();
$navlinks[] = array('name' => $strsearch, 'link' => "../index.php", 'type' => 'misc');
$navlinks[] = array('name' => $strquery, 'link' => "../stats.php", 'type' => 'misc');
$navlinks[] = array('name' => get_string('runindexertest','search'), 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
$site = get_site();
print_header("$strsearch", "$site->fullname" , $navigation, "", "", true, "&nbsp;", navmenu($site));
if (empty($CFG->enableglobalsearch)) {
print_error('Global searching is not enabled.');
}
if (empty($CFG->enableglobalsearch)) {
error('Global searching is not enabled.');
}
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
} //if
mtrace('<pre>Server Time: '.date('r',time()));
mtrace("Testing global search capabilities:\n");
$phpversion = phpversion();
if (!search_check_php5()) {
mtrace("ERROR: PHP 5.0.0 or later required (currently using version $phpversion).");
exit(0);
} else {
mtrace("Success: PHP 5.0.0 or later is installed ($phpversion).\n");
} //else
//fix paths for testing
set_include_path(get_include_path().":../");
require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
mtrace("Checking activity modules:\n");
//the presence of the required search functions -
// * mod_iterator
// * mod_get_content_for_index
//are the sole basis for including a module in the index at the moment.
/// get all installed modules
if ($mods = get_records('modules', '', '', 'name', 'id,name')){
if (!isadmin()) {
print_error("You need to be an admin user to use this page.", '', "$CFG->wwwroot/login/index.php");
} //if
$searchabletypes = array_values(search_get_document_types());
mtrace('<pre>Server Time: '.date('r',time()));
mtrace("Testing global search capabilities:\n");
$phpversion = phpversion();
//fix paths for testing
set_include_path(get_include_path().":../");
require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
mtrace("Checking activity modules:\n");
//the presence of the required search functions -
// * mod_iterator
// * mod_get_content_for_index
//are the sole basis for including a module in the index at the moment.
if ($mods = get_records_select('modules')) {
$mods = array_merge($mods, search_get_additional_modules());
foreach ($mods as $mod) {
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
foreach($mods as $mod){
if (in_array($mod->name, $searchabletypes)){
$mod->location = 'internal';
$searchables[] = $mod;
} else {
$documentfile = $CFG->dirroot."/mod/{$mod->name}/search_document.php";
$mod->location = 'mod';
if (file_exists($documentfile)){
$searchables[] = $mod;
}
}
}
mtrace(count($searchables).' modules to search in / '.count($mods).' modules found.');
}
/// collects blocks as indexable information may be found in blocks either
if ($blocks = get_records('block', '', '', 'name', 'id,name')) {
$blocks_searchables = array();
// prepend the "block_" prefix to discriminate document type plugins
foreach($blocks as $block){
$block->dirname = $block->name;
$block->name = 'block_'.$block->name;
if (in_array('SEARCH_TYPE_'.strtoupper($block->name), $searchabletypes)){
$mod->location = 'internal';
$blocks_searchables[] = $block;
} else {
$documentfile = $CFG->dirroot."/blocks/{$block->dirname}/search_document.php";
if (file_exists($documentfile)){
$mod->location = 'blocks';
$blocks_searchables[] = $block;
}
}
}
mtrace(count($blocks_searchables).' blocks to search in / '.count($blocks).' blocks found.');
$searchables = array_merge($searchables, $blocks_searchables);
}
/// add virtual modules onto the back of the array
$additional = search_get_additional_modules();
mtrace(count($additional).' additional to search in.');
$searchables = array_merge($searchables, $additional);
foreach ($searchables as $mod) {
if ($mod->location == 'internal'){
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
} else {
$class_file = $CFG->dirroot.'/'.$mod->location.'/'.$mod->name.'/search_document.php';
}
if (file_exists($class_file)) {
include_once($class_file);
if (!defined('SEARCH_TYPE_'.strtoupper($mod->name))) {
mtrace("ERROR: Constant 'SEARCH_TYPE_".strtoupper($mod->name)."' is not defined in /search/lib.php");
mtrace("ERROR: Constant 'SEARCH_TYPE_".strtoupper($mod->name)."' is not defined in search/searchtypes.php or in module");
continue;
} //if
}
$iter_function = $mod->name.'_iterator';
$index_function = $mod->name.'_get_content_for_index';
if (function_exists($index_function) && function_exists($iter_function)) {
if (is_array($iter_function())) {
$documents = $index_function(array_pop($iter_function()));
if (is_array($documents)) {
mtrace("Success: '$mod->name' module seems to be ready for indexing.");
} else {
mtrace("ERROR: $index_function() doesn't seem to be returning an array.");
} //else
}
} else {
mtrace("ERROR: $iter_function() doesn't seem to be returning an object array.");
} //else
}
} else {
mtrace("ERROR: $iter_function() and/or $index_function() does not exist in $class_file");
} //else
}
} else {
mtrace("Notice: $class_file does not exist, this module will not be indexed.");
} //else
} //foreach
} //if
//finished modules
mtrace("\nFinished checking activity modules.");
//now blocks...
//
mtrace("<br/><a href='../index.php'>Back to query page</a> or <a href='../indexersplash.php'>Start indexing</a>.");
mtrace('</pre>');
print_footer();
?>
}
}
//finished modules
mtrace("\nFinished checking activity modules.");
//now blocks...
//
mtrace("<br/><a href='../index.php'>Back to query page</a> or <a href='../indexersplash.php'>Start indexing</a>.");
mtrace('</pre>');
?>

View file

@ -34,7 +34,7 @@
error(get_string('globalsearchdisabled', 'search'));
}
if (!isadmin()) {
if (!has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) {
error(get_string('beadmin', 'search'), "$CFG->wwwroot/login/index.php");
}