Cleaned up files, fixed some discrepancies and made sure strings that needed escaping

used the correct function. Add description comments to files.
This commit is contained in:
mchampan 2006-07-05 14:37:16 +00:00
parent 71ffbac2da
commit abb4ea20d4
12 changed files with 137 additions and 112 deletions

View File

@ -363,22 +363,8 @@ function wiki_get_entries(&$wiki, $byindex=NULL) {
//rescued and converted from ewikimoodlelib.php
//retrieves latest version of a page
function wiki_get_latest_page(&$entry, $pagename, $version=0) {
global $CFG;
//need something like this in datalib.php?
switch ($CFG->dbtype) {
case 'mysql':
$f = 'mysql_real_escape_string';
break;
case 'postgres7':
$f = 'pg_escape_string';
break;
default:
$f = 'addslashes';
} //switch
$pagename = "'".$f($pagename)."'";
function wiki_get_latest_page(&$entry, $pagename, $version=0) {
$pagename = "'".addslashes($pagename)."'";
if ($version > 0 and is_int($version)) {
$version = "AND (version=$version)";
@ -456,7 +442,7 @@ function wiki_get_content_for_index(&$wiki) {
foreach($pages as $page) {
if (strlen($page->content) > 0) {
$i++;
$documents[] = new WikiSearchDocument($page, $entry->wikiid, $entry->course, $entry->userid, $entry->groupid);
$documents[] = new WikiSearchDocument($page, $entry->wikiid, $entry->course, $entry->groupid);
} //if
} //foreach

View File

@ -1,3 +1,9 @@
latest
------
Started cleaning and standardising things.
cvs v1.1
--------
This is the initial release (prototype) of Moodle's new search module -
so basically watch out for sharp edges.

View File

@ -5,10 +5,6 @@ CREATE TABLE IF NOT EXISTS `prefix_search_documents` (
`url` varchar(100) NOT NULL default '',
`updated` timestamp NOT NULL default CURRENT_TIMESTAMP,
`courseid` int(11) NOT NULL default '0',
`userid` int(11) NOT NULL default '0',
`groupid` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1;
--DELETE FROM `prefix_search_documents`;
--ALTER TABLE `prefix_search_documents` AUTO_INCREMENT =1;
) ENGINE=MyISAM AUTO_INCREMENT=1;

View File

@ -4,10 +4,6 @@ CREATE TABLE prefix_search_documents (
title varchar(100) NOT NULL default '',
url varchar(100) NOT NULL default '',
updated timestamp NOT NULL DEFAULT NOW(),
courseid int4,
userid int4,
courseid int4,
groupid int4
);
--DELETE FROM prefix_search_documents;
--SELECT setval('public.prefix_search_documents_id_seq', 1);
);

View File

@ -1,10 +1,12 @@
<?php
/* Base search document from which other module/block types can
* extend.
* */
class SearchDocument extends Zend_Search_Lucene_Document {
public function __construct($document_type, $cid, $uid, $gid) {
public function __construct($document_type, $cid, $gid) {
$this->addField(Zend_Search_Lucene_Field::Keyword('type', $document_type));
$this->addField(Zend_Search_Lucene_Field::Keyword('courseid', $cid));
$this->addField(Zend_Search_Lucene_Field::Keyword('userid', $uid));
$this->addField(Zend_Search_Lucene_Field::Keyword('courseid', $cid));
$this->addField(Zend_Search_Lucene_Field::Keyword('groupid', $gid));
} //constructor
} //SearchDocument

View File

@ -3,7 +3,7 @@
require_once("$CFG->dirroot/search/documents/document.php");
class WikiSearchDocument extends SearchDocument {
public function __construct(&$page, $wiki_id, $cid, $uid, $gid) {
public function __construct(&$page, $wiki_id, $cid, $gid) {
$this->addField(Zend_Search_Lucene_Field::Text('title', $page->pagename));
$this->addField(Zend_Search_Lucene_Field::Text('author', $page->author));
$this->addField(Zend_Search_Lucene_Field::UnStored('contents', $page->content));
@ -12,7 +12,7 @@
$this->addField(Zend_Search_Lucene_Field::Keyword('version', $page->version));
$this->addField(Zend_Search_Lucene_Field::Keyword('wiki', $wiki_id));
parent::__construct(SEARCH_WIKI_TYPE, $cid, $uid, $gid);
parent::__construct(SEARCH_WIKI_TYPE, $cid, $gid);
} //constructor
} //WikiSearchDocument

View File

@ -1,10 +1,8 @@
<?php
/*$id = required_param('id', PARAM_INT); // course
if (! $course = get_record("course", "id", $id)) {
error("Course ID is incorrect");
}
require_course_login($course);
add_to_log($course->id, "wiki", "view all", "index.php?id=$course->id", "");*/
/* Entry page for /search
* Redirects to query.php, because that is the most likely place a
* user intended to go to when typing moodle.site/search
* */
header("Location: query.php");
?>

View File

@ -1,4 +1,14 @@
<?php
/* The indexer logic -
* Look through each installed module's lib file for necessary search functions,
* and if they're present (and the module search document class file), add the
* content to the index. Repeat this for blocks.
*
* Along with the index data, each document's summary gets stored in the database
* and synchronised to the index (flat file) via the primary key ('id') which is mapped
* to the 'dbid' field in the index
* */
//this'll take some time, set up the environment
@set_time_limit(0);
@ob_implicit_flush(true);
@ -7,16 +17,20 @@
require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
//only administrators can index the moodle installation, because access to all pages is required
require_login();
if (!isadmin()) {
error("You need to be an admin user to use this page.", "$CFG->wwwroot/login/index.php");
} //if
//confirmation flag to prevent accidental reindexing (indexersplash.php is the correct entry point)
$sure = strtolower(optional_param('areyousure', '', PARAM_ALPHA));
if ($sure != 'yes') {
mtrace("Sorry, you weren't sure enough (<a href='index.php'>back to query page</a>).");
mtrace("<pre>Sorry, you need to confirm indexing via <a href='indexersplash.php'>indexersplash.php</a>"
.". (<a href='index.php'>Back to query page</a>).</pre>");
exit(0);
} //if
@ -27,6 +41,7 @@
exit(0);
} //if
//php5 found, continue including php5-only files
require_once("$CFG->dirroot/search/Zend/Search/Lucene.php");
//begin timer
@ -37,6 +52,7 @@
$index_path = $CFG->dataroot.'/search';
$index_db_file = "$CFG->dirroot/search/db/$CFG->dbtype.sql";
//setup directory in data root
if (!file_exists($index_path)) {
mtrace("Data directory ($index_path) does not exist, attempting to create.");
if (!mkdir($index_path)) {
@ -48,9 +64,6 @@
mtrace("Using $index_path as data directory.");
} //else
//stop accidental re-indexing (zzz)
//search_pexit("Not indexing at this time.");
$index = new Zend_Search_Lucene($index_path, true);
//create the database tables
@ -63,41 +76,32 @@
modify_database($index_db_file, '', false);
ob_end_clean(); //chuck the buffer and resume normal operation
} //else
//empty database table goes here
// delete * from search_documents;
// set auto_increment back to 1
//-------- debug stuff
/*
include_once("$CFG->dirroot/mod/wiki/lib.php");
$wikis = get_all_instances_in_courses("wiki", get_courses());
#search_pexit($wikis[1]);
$entries = wiki_get_entries($wikis[1]);
#search_pexit($entries);
#$r = wiki_get_pages($entries[134]);
$r = wiki_get_latest_pages($entries[95]);
search_pexit($r);
//ignore me --------*/
mtrace('Starting activity modules');
//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' /*'index this module?' where statement*/)) {
foreach ($mods as $mod) {
$libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
if (file_exists($libfile)) {
include_once($libfile);
$iter_function = $mod->name.'_iterator';
$index_function = $mod->name.'_get_content_for_index';
$include_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
$c = 0;
//specific module search document class
$class_file = $CFG->dirroot.'/search/documents/'.$mod->name.'_document.php';
$counter = 0;
$doc = new stdClass;
if (function_exists($index_function) && function_exists($iter_function)) {
include_once($include_file);
if (file_exists($class_file) && function_exists($index_function) && function_exists($iter_function)) {
include_once($class_file);
mtrace("Processing module function $index_function ...");
@ -107,19 +111,14 @@
//begin transaction
foreach($documents as $document) {
$c++;
//db sync increases indexing time from 55 sec to 73 (64 on Saturday?), so ~30%
//therefore, let us make a custom insert function for this search module
$counter++;
//data object for db
$doc->type = $document->type;
$doc->title = mysql_real_escape_string($document->title); //naughty
$doc->update = time();
$doc->permissions = 0;
$doc->title = search_escape_string($document->title);
$doc->update = time();
$doc->url = 'none';
$doc->courseid = $document->courseid;
$doc->userid = $document->userid;
$doc->courseid = $document->courseid;
$doc->groupid = $document->groupid;
//insert summary into db
@ -127,12 +126,14 @@
//synchronise db with index
$document->addField(Zend_Search_Lucene_Field::Keyword('dbid', $id));
//add document to index
$index->addDocument($document);
//commit every 100 new documents, and print a status message
if (($c%100) == 0) {
//commit every x new documents, and print a status message
if (($counter%200) == 0) {
$index->commit();
mtrace(".. $c");
mtrace(".. $counter");
} //if
} //foreach
@ -142,16 +143,21 @@
//commit left over documents, and finish up
$index->commit();
mtrace("-- $c documents indexed");
mtrace("-- $counter documents indexed");
mtrace('done.');
} //if
} //if
} //foreach
} //if
//done modules
//finished modules
mtrace('Finished activity modules');
search_stopwatch();
//now blocks...
//
mtrace(".<br><a href='index.php'>Back to query page</a>.");
mtrace('</pre>');

View File

@ -1,4 +1,9 @@
<?php
/* This file serves as a splash-screen (entry page) to the indexer script -
* it is in place to prevent accidental reindexing which can lead to a loss
* of time, amongst other things.
* */
require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
@ -28,16 +33,18 @@
$db_count = 0;
} //else
//elaborate on error messages, when db!=0 and index=0 -> corrupt, etc.
//TODO: elaborate on error messages, when db!=0 and index=0 -> corrupt, etc.
if ($index_filecount != 0 or $db_count != 0) {
mtrace("<pre>The data directory ($index_path) contains $index_filecount files, and "
."there are $db_count records in the <em>search_documents</em> table.");
mtrace('');
mtrace("This indicates that you have already indexed this site - click the following "
."link if you're sure you want to continue: <a href='indexer.php?areyousure=yes'>Go!</a>");
mtrace('');
mtrace("<a href='index.php'>Back to query page</a>.");
mtrace("</pre>");
mtrace("<pre>The data directory ($index_path) contains $index_filecount files, and\n"
."there are $db_count records in the <em>search_documents</em> table.\n"
."\n"
."This indicates that you have already succesfully indexed this site, or at least\n"
."started and cancelled an indexing session. Follow the link if you are sure that\n"
."you want to continue indexing - this will replace any existing index data (no\n"
."Moodle data is affected).\n"
."\n"
."<a href='indexer.php?areyousure=yes'>Continue indexing</a> or <a href='index.php'>Back to query page</a>."
."</pre>");
} else {
header('Location: indexer.php?areyousure=yes');
} //else

View File

@ -17,6 +17,23 @@
function search_shorten_url($url, $length=30) {
return substr($url, 0, $length)."...";
} //search_shorten_url
function search_escape_string($str) {
global $CFG;
switch ($CFG->dbtype) {
case 'mysql':
$s = mysql_real_escape_string($str);
break;
case 'postgres7':
$s = pg_escape_string($str);
break;
default:
$s = addslashes($str);
} //switch
return $s;
} //search_escape_string
//get a real php 5 version number, using 5.0.0 arbitrarily
function search_check_php5($feedback=false) {

View File

@ -41,6 +41,7 @@
print_heading($strquery);
print_simple_box_start('center', '', '', 20);
?>
<form name="query" method="get" action="query.php">

View File

@ -1,4 +1,8 @@
<?php
/* Prints some basic statistics about the current index.
* Allows the administrator to create an index if none exists.
* */
require_once('../config.php');
require_once("$CFG->dirroot/search/lib.php");
@ -10,10 +14,12 @@
$index_dir = get_directory_list($index_path, '', false, false);
$index_filecount = count($index_dir);
//indexed documents stats
$tables = $db->MetaTables();
//indexed documents stats (via db)
$db_exists = false;
$admin_tables = $db->MetaTables();
if (in_array($CFG->prefix.'search_documents', $tables)) {
if (in_array($CFG->prefix.'search_documents', $admin_tables)) {
$db_exists = true;
$types = search_get_document_types();
sort($types);
@ -51,39 +57,43 @@
print_simple_box_start('center', '', '', 20);
//this table is only for admins, shows index directory size and location
if (isadmin()) {
$admin_table->tablealign = "center";
$admin_table->align = array ("right", "left");
$admin_table->wrap = array ("nowrap", "nowrap");
$admin_table->cellpadding = 5;
$admin_table->cellspacing = 0;
$admin_table->width = '500';
$admin_table->data[] = array('<strong>Data directory</strong>', '<em><strong>'.$index_path.'</strong></em>');
$admin_table->data[] = array('Files in index directory', $index_filecount);
$admin_table->data[] = array('Total size', $index_size);
if ($index_filecount == 0 or !$db_exists) {
$admin_table->data[] = array('Click to create index', "<a href='indexersplash.php'>Indexer</a>");
} //if
} //if
//this is the standard summary table for normal users, shows document counts
$table->tablealign = "center";
$table->align = array ("right", "left");
$table->wrap = array ("nowrap", "nowrap");
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->width = '500';
$table->data[] = array('<strong>Data directory</strong>', '<em><strong>'.$index_path.'</strong></em>');
$table->data[] = array('Files in index directory', $index_filecount);
$table->data[] = array('Total size', $index_size);
if ($index_filecount == 0) {
$table->data[] = array('Click to create index', "<a href='indexersplash.php'>Indexer</a>");
} //if
$return_of_table->tablealign = "center";
$return_of_table->align = array ("right", "left");
$return_of_table->wrap = array ("nowrap", "nowrap");
$return_of_table->cellpadding = 5;
$return_of_table->cellspacing = 0;
$return_of_table->width = '500';
$return_of_table->data[] = array('<strong>Database</strong>', '<em><strong>search_documents<strong></em>');
$table->data[] = array('<strong>Database</strong>', '<em><strong>search_documents<strong></em>');
foreach($type_counts as $key => $value) {
$return_of_table->data[] = array($key, $value);
$table->data[] = array($key, $value);
} //foreach
if (isadmin()) {
print_table($table);
print_table($admin_table);
print_spacer(20);
} //if
print_table($return_of_table);
print_table($table);
print_simple_box_end();
print_simple_box_end();