From abb4ea20d4077a921da4dfb15bb440b25bbd4862 Mon Sep 17 00:00:00 2001 From: mchampan Date: Wed, 5 Jul 2006 14:37:16 +0000 Subject: [PATCH] Cleaned up files, fixed some discrepancies and made sure strings that needed escaping used the correct function. Add description comments to files. --- mod/wiki/lib.php | 20 +------ search/README.txt | 6 ++ search/db/mysql.sql | 6 +- search/db/postgres7.sql | 8 +-- search/documents/document.php | 8 ++- search/documents/wiki_document.php | 4 +- search/index.php | 10 ++-- search/indexer.php | 90 ++++++++++++++++-------------- search/indexersplash.php | 25 ++++++--- search/lib.php | 17 ++++++ search/query.php | 1 + search/stats.php | 54 ++++++++++-------- 12 files changed, 137 insertions(+), 112 deletions(-) diff --git a/mod/wiki/lib.php b/mod/wiki/lib.php index 487847c9cb..6f99d2543e 100644 --- a/mod/wiki/lib.php +++ b/mod/wiki/lib.php @@ -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 diff --git a/search/README.txt b/search/README.txt index c3d4ab18e6..f473073714 100644 --- a/search/README.txt +++ b/search/README.txt @@ -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. diff --git a/search/db/mysql.sql b/search/db/mysql.sql index 8317ebbe40..1efb7e442d 100644 --- a/search/db/mysql.sql +++ b/search/db/mysql.sql @@ -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; \ No newline at end of file +) ENGINE=MyISAM AUTO_INCREMENT=1; \ No newline at end of file diff --git a/search/db/postgres7.sql b/search/db/postgres7.sql index 86b26849e5..52d1be4bf9 100644 --- a/search/db/postgres7.sql +++ b/search/db/postgres7.sql @@ -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); \ No newline at end of file +); \ No newline at end of file diff --git a/search/documents/document.php b/search/documents/document.php index f5d4697d76..c09ec37c90 100644 --- a/search/documents/document.php +++ b/search/documents/document.php @@ -1,10 +1,12 @@ 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 diff --git a/search/documents/wiki_document.php b/search/documents/wiki_document.php index a6d75aef8d..c70fa5ccb9 100644 --- a/search/documents/wiki_document.php +++ b/search/documents/wiki_document.php @@ -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 diff --git a/search/index.php b/search/index.php index 8c4db6584e..7ae73089ab 100644 --- a/search/index.php +++ b/search/index.php @@ -1,10 +1,8 @@ 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"); ?> \ No newline at end of file diff --git a/search/indexer.php b/search/indexer.php index 0b9db792bc..5fda789744 100644 --- a/search/indexer.php +++ b/search/indexer.php @@ -1,4 +1,14 @@ 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 (back to query page)."); + mtrace("
Sorry, you need to confirm indexing via indexersplash.php"
+          .". (Back to query page).
"); + 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(".
Back to query page."); mtrace(''); diff --git a/search/indexersplash.php b/search/indexersplash.php index 961c61c08a..35e798f097 100644 --- a/search/indexersplash.php +++ b/search/indexersplash.php @@ -1,4 +1,9 @@ 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("
The data directory ($index_path) contains $index_filecount files, and "
-          ."there are $db_count records in the search_documents table.");    
-    mtrace('');    
-    mtrace("This indicates that you have already indexed this site - click the following "
-          ."link if you're sure you want to continue: Go!");          
-    mtrace('');          
-    mtrace("Back to query page.");
-    mtrace("
"); + mtrace("
The data directory ($index_path) contains $index_filecount files, and\n"
+          ."there are $db_count records in the search_documents 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"          
+          ."Continue indexing or Back to query page."
+          ."
"); } else { header('Location: indexer.php?areyousure=yes'); } //else diff --git a/search/lib.php b/search/lib.php index 081d9ef0bf..f6c17a8cca 100644 --- a/search/lib.php +++ b/search/lib.php @@ -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) { diff --git a/search/query.php b/search/query.php index 59169b5d2e..5a65273fdb 100644 --- a/search/query.php +++ b/search/query.php @@ -41,6 +41,7 @@ print_heading($strquery); print_simple_box_start('center', '', '', 20); + ?>
diff --git a/search/stats.php b/search/stats.php index dbe59c7228..37ac6f5834 100644 --- a/search/stats.php +++ b/search/stats.php @@ -1,4 +1,8 @@ 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('Data directory', ''.$index_path.''); + $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', "Indexer"); + } //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('Data directory', ''.$index_path.''); - $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', "Indexer"); - } //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('Database', 'search_documents'); + $table->data[] = array('Database', 'search_documents'); 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(); -- 2.39.5