- Moderated by:
- Support Team
-
- rank:
-
Professional
- registered:
- December 1969
- Status:
- offline
- last visit:
- 30.12.05
- Posts:
- 1310
Important: The code in this posting has bugs in it. Look further down the thread for better versions that work.
I haven't been around the forums in awhile but someone tracked me down to ask about adding a search of a new module. I started to answer via email but figured that others would appreciate the info as well... BTW, the quickest way to reach me is via email to crs@mtrad.com...
Quote
Hello ColdRolledSteel,
I read some old messages on the board, and at one time or another, you stated that it would be easy to program the Search in PostNuke to include the search results from a directory of text files.
I can't do it, can't find it, and wonder what am I to do about it?
Hmm, did I really say that? Okay, I was probably showing off. But it is pretty easy to do, assuming you can do some PHP coding. If that's beyond you, then say so because the rest of this email may not be very helpful...
First off, let me say that I'm working on PN 0714. I don't think the Search functionality has changed that much up to PN 0726, but you still need to do a sanity check that what you see in your version of PN matches what I'm talking about...
One of the fundamental concepts that the Search module depends on is the use of "plug-ins" that other modules provide to perform a search customized to that module. So, the Search module doesn't really know how to search all the other modules, it just calls the plug-ins that it knows about and returns the output that the plug-ins generate.
The plug-ins are kept in the html/includes/search directory. Each file in that directory handles the plug-in for a different module. Within each file are 2 functions and a variable declaration that the Search module needs to do its thing. The first function generates the form output for the selection form part of the Search module. The second function generates the search output based on what the user selected to be searched. The variable declaration tells the search module the function names and title of the module.
Now - to add a search of text files to the Search module, you just need to add a plug-in to the includes/search directory. I would suggest that you start by copying an existing file, such as "faq.php", to your new plug-in, say "text_files.php". Now you need to make some changes to your new plug-in "text_files.php"...
First, find the declaration of $search_modules[] at the top. Change 'title' from 'FAQS' to something more appropriate like 'Interesting text files'. then change 'func_search' from 'search_faqs' to 'search_text_files' and 'func_opt' from 'search_faqs_opt' to 'search_text_files_opt'.
Now change the function names from 'function search_faqs_opt()' to 'function search_text_files_opt()' and 'function search_faqs()' to 'function search_text_files'.
At this point, the plug-in will be recognizable by the Search module. You can actually try it out by clicking on "Search" in your main menu. You should see two copies of the option "Search FAQs" in your selection form.
Now you need to go and modify the search_text_files_opt() function to provide the fragment of HTML form code that you need. These changes are all in this line of code:
Code
$output->Text("<table border=\"0\" width=\"100%\"><tr bgcolor=\"$bgcolor2\"><td><font class=\"pn-normal\" style=\"text-color:$textcolor1\"><input type=\"checkbox\" name=\"active_faqs\" id=\"active_faqs\" value=\"1\" checked> <label for=\"active_faqs\">"._SEARCH_FAQS."</label></font></td></tr></table>");
Change "active_faqs" to "active_textfiles" in all places in the HTML. That identifies the form variable for this option. Then change "_SEARCH_FAQS" to "_SEARCH_TEXTFILES". That identifies the text to be added. And, of course, you need to define _SEARCH_TEXTFILES somewhere. Probably in modules/Search/lang/eng/global.php.
At this point, you can test your changes by clicking on "Search" in the main menu. You should see your option displayed as SEARCH_TEXTFILES (or whatever you defined in the lang file.) If you look at the HTML source of the page, you should find "active_textfiles" used in the HTML form...
Now comes the interesting part. You need to change the function search_text_files() to do what you want... Almost all of the search plug-ins assume that you are searching a table for the entries that you want. So the plug-in creates an SQL query that finds what we're looking for. What we need to do is replace this logic with some logic that will search the directory of text files... The code that we are going to replace in the search_text_files() function starts after:Code
$output = new pnHTML();
$output->SetInputMode(_PNH_VERBATIMINPUT);
Here is a quick hack at the rest of the function:
Code
exec("grep $word html/text_files/*",$text_files_results);
if (!empty($text_files_results) {
reset($text_files_results);
$output->Text('<font class="pn-normal">'. _TEXTFILES . '</font>');
$url = "modules.php?op=modload&name=Search&file=index&action=search&active_textfiles=1&bool=$bool&q=$q";
$output->Text("<ul>");
foreach ($text_files_results as $text_file_name) {
$output->Text("<li><a class=\"pn-normal\" href=\"execute_whatever_command_you_want_with_$text_file_name">$text_file_name</a></li>");
}
$output->Text('</ul>');
$output->Linebreak(4);
// Munge URL for template
$urltemplate = $url . "&startnum=%%&total=$total";
$output->Pager($startnum,
$total,
$urltemplate,
10);
} else {
$output->SetInputMode(_PNH_VERBATIMINPUT);
$output->Text('<font class="pn-normal">'._SEARCH_NO_TEXT_FILES.'</font>');
$output->SetInputMode(_PNH_PARSEINPUT);
}
$output->Linebreak();
return $output->GetOutput();
Now, I don't guarantee that this will work. But you should be able to get the idea of what I did and do your own thing...
I created a command in the exec() function that will do the actual searching of the text files for whatever the user put into $word. You should test this out at the command line on your system to find the exact format of the command that you want.
The output of the exec() function are put in the $text_files_results array. First we test to see if there is actually anything in the array to see if we need to output a list or just a message saying _SEARCH_NO_TEXT_FILES. (ie - no files found.)
Assuming there's some results, we loop through the array using the foreach and generate a line of output for each file found. You need to change "execute_whatever_command_you_want_with_$text_file_name" to whatever link you want to show the user for that file.
At this point, you are basically done and should test it out. This code probably needs to be debugged. I am pretty sure that I broke the pager logic. (It looks like it depends on some stuff in the SQL query.) But it should be close enough to get you started. If you need more help, please send me an email to crs@mtrad.com. (I don't read the forums much any more.) -
- rank:
-
Freshman
- registered:
- December 2002
- Status:
- offline
- last visit:
- 27.07.08
- Posts:
- 42
I'm passed the little grammatical issues, no more parse errors.
I get the checkbox in the first part of search, but no results are displayed.
I added the define statements to my global.php as follows:
define('_SEARCH_TEXTFILES','Search Text');
define('_SEARCH_NO_TEXTFILES', 'No documents matched your search.');
I'm trying to search the text in all the *.htm files in '/home/jcchoaco/public_html/documents/' and to display a list of hyperlinks to the files that contain the text searched so they can pop up in an HTML window.
Here's the file with no parse errors ... it makes it through, but nothing is displayed in the results screen.
Quote
JammerPro -
- rank:
-
Professional
- registered:
- December 1969
- Status:
- offline
- last visit:
- 30.12.05
- Posts:
- 1310
JammerPro - I responded to your email with a solution. For everyone else, here is what I've done:
JammerPro provided a includes/search/text.php file that included the changes outlined above. There were a few syntax errors in the code that I provided and/or a couple of typos. But after that, there was still some parse problem. So I started over with faq.php and cut/paste our changes. The one thing I did differently is to use "active_textfiles" instead of "active_text_files". I don't know exactly where the problem was but the resulting file worked. So I'm not going to get anal about finding the root cause. :*)
A copy of this file is available from http://www.mtrad.com…blic_files/text.php.
This text.php will search the public_files directory on my server and list the files that include the search text. The output includes links so that when the user clicks on one of the file names, it opens it in the browser.
You can see it working on http://www.mtrad.com/Search+main.html (The only changes were to the selection form output so that users would see something that made sense...)
There are two problems with this implementation of text.php:
1. It does not handle multiple search criteria.
2. It displays all results on the first page. The pager logic was removed because it was linked into how
SQL queries were done.
These two problems are left as exercises for the advanced student.
For those coders who are reading this to learn how to add a search plug-in for their module, they should note that they should make most of the changes in the original posting in this thread (up to where we "hack at the rest of the function") and then go look at what's there. They should be able to just change the SQL query to make it do what they want. This works better and doesn't have the 2 implementation problems we have.
Cheers,
Craig -
- rank:
-
Freshman
- registered:
- December 2002
- Status:
- offline
- last visit:
- 27.07.08
- Posts:
- 42
Thank you Craig. I have dubbed the script ...
PnSimpleSearch! Version 1. Written by Craig R. Saunders with a little help from Elliot Zimmerman, 11.5.3.
For PostNuke o要ly! Tested o要 PostNuke 714 - 726.
Directions:
Place text.php in your PostNuke includes/search directory.
Add the following to your /language/eng/global.php file at the very end:
Code
Code
That's it!
Known shortcomings:
1) The search is case sensitive.
2) The search does not handle multiple search criteria.
3) The search displays all results o要 the first page.
The file is available at http://cyberlaw.info under PostNuke downloads. It is called pnSimpleSearch.zip. Registration is free but required. You can try it without registration by selecting "Search" at http://cyberlaw.info.
This version's search results "pop-up" in a new window when selected.
If any of you would like to contribute a boolean search option, case sensitive option, or work on the display, please let us all know your changes.
Again, thank you Craig!!!
JammerPro -
- rank:
-
Professional
- registered:
- December 1969
- Status:
- offline
- last visit:
- 30.12.05
- Posts:
- 1310
Another shortcoming is that it depends on the operation of the grep command on the server. I suspect that if you're running on Windows and some versions of Unix, you will have problems with grep.... -
- rank:
-
Freshman
- registered:
- December 2002
- Status:
- offline
- last visit:
- 27.07.08
- Posts:
- 42
PnSimpleSearch for Static Docs! Version 1. Written by Craig R. Saunders and Elliot Zimmerman, 11.5.3. PostNuke TEXT SEARCH MODULE FOR STATIC DOCS. Tested o要 PostNuke 714 - 726. Requires Static Docs module for PostNuke by Simon Wunderlin available at http://noc.postnuke.com/project/showfiles.php?group_id=9. There is also a download link for Static Docs at http://cyberlaw.info.
This module will display all Static Docs files that contain your single search term as a list of hyperlinks within the PostNuke interface. When a hyperlink is selected, the file is displayed in PostNuke. This is a great way to add those old HTML pages to your site with the ability to search and display their contents all within the PostNuke interface!
Add the following to your /language/eng/global.php file at the very end:
Code
define('_SEARCH_TEXTFILES','Search Static Docs: Search is case sensitive & for a single term o要ly! If no search term is entered, all documents are listed.');
define('_SEARCH_TEXTFILES_RESULTS','Static Docs: The following documents matched your search.');
define('_SEARCH_NO_TEXTFILES', 'Static Docs: No documents matched your search.');
Place this file (text.php) in your PostNuke includes/search directory after making the following changes.
Code
Change line (around 92) of this fille (text.php) to reflect the server path to your modules/Static_Docs/data/ directory and which files to search (i.e. *.htm).
Change line (around 106) of this file (text.php) to replace only the fictitious "yoursite.com" URL with your own (leave the rest of the tag alone).
That's it!
Known shortcomings:
1) The search is case sensitive.
The search does not handle multiple search criteria.
The file is available at http://cyberlaw.info under PostNuke Downloads. It is called pnSimpleSearchSD.zip. You can try it by selecting "Search" at http://cyberlaw.info. Leave the textbox for the search empty and the results will include all documents.
Special thanks to Craig for showing me the way.
--
EMZ
