search
Multifolder search engine
This is a rather complex script that uses
Xtscript and
filelist to create a
javascript array of the search results
which are then sorted displayed and paged using
javascript
It uses "post" so no extra query string variables
There are all sorts of tricks and strange looking code used to output the js correctly so careless editing may break the script
The actual file lists are not in the xtscript so should not get timeouts
The search query must be 2 or more chars and is filtered against malicious input
Uncomment the 3 lines in
# Query health check to ban
- negation and
* ? wildcards in the search query
This version is set up to search 4 folders extending it is fairly simple
1/ Insert another line like this into the
# Filelist patterns
var $list5 = xt:filelist folder="/FOLDER_5" $params
2/ Insert another filelist into the
// Results array before the
{_$$dummy|} entry
<{_$$list5|}>
Try to use meaningful names for your files as the name of the files determines the search results
Remember This uses javascript so it uses memory on the users device to store the whole array of the results
Example code
Show in textarea<!--parser:xtscript-->
# Get the search query
get search
# Trim any spaces
var $search = call trim $val=$search
# No search query
if not $search
var $disable_js = //
goto @skip_1
endif
# Search query length check
var $len = call strlen $val=$search
if $len < 2
del $search
var $disable_js = //
print <b>Search query too short min 2 chars</b> <br />
goto @skip_1
endif
# Search query health check
# uncomment the next 3 lines to ban wildcards and negation in the search query
# var $neg = call strpos $haystack=#$search;$needle=-
# var $star = call strpos $haystack=#$search;$needle=*
# var $query = call strpos $haystack=#$search;$needle=?
var $dquot = call strpos $haystack=#$search;$needle="
var $squot = call strpos $haystack=#$search;$needle='
if $star or $query or $dquot or $squot or $neg==1
del $search
var $disable_js = //
print <b>Search query contains invalid chars</b> <br />
goto @skip_1
endif
# Set up the search
# Check for "anywhere"
get anywhere
if $anywhere
var $checked = checked="checked"
var $any = *
endif
# Filelist template that creates a javascript array
var $template = ['.file_name_parsed.','.file_url.','.file_size.','.file_datetime.','.file_mime.','.file_type.'],
# Filelist parameters
var $params = template="$template" filter="$any$search*" per_page="0"
# Filelist patterns
var $list1 = xt:filelist folder="/FOLDER_1" $params
var $list2 = xt:filelist folder="/FOLDER_2" $params
var $list3 = xt:filelist folder="/FOLDER_3" $params
var $list4 = xt:filelist folder="/FOLDER_4" $params
# Dummy array entry to use up the last , in the filelist template
var $dummy = ['dummy']
# skip_1 landing point
@skip_1
# The search form
print {{
<div class="search_form" id="srch">
<form action="<xt:url/>#srch" method="post">
<input type="search" name="search" value="$search" />
<input type="submit" value="Search" /><br />
Anywhere in filename <input type="checkbox" name="anywhere" value="1" $checked />
</form>
</div><br />}}
# The actual filelist functions are created outside the xtscript to avoid timeouts
# Create an array of the filelist arrays
<!--/parser:xtscript-->
<script type="text/javascript">
// Results array
{_$$disable_js|}var list=[<{_$$list1|}><{_$$list2|}><{_$$list3|}><{_$$list4|}>{_$$dummy|}];
<!--parser:xtscript-->
if $search
# Get page number
get_or_default results_page;0
# Results per page
var $per_page=5
# Create javascript
print {{list.length=list.length-1;
var length=list.length;
if (length){
var total_pages=Math.floor(}}
print {{list.length/$per_page);
var results_page=$results_page;
var search='$search';
var per_page=$per_page}}
print_raw {{;
var offset=(results_page*per_page);
var page_end=(offset + per_page);
if (page_end >= length)page_end=length;
// Output results
list.sort();
document.write('<div class="search_results">');
if (length >= 1)document.write('<div class="results_data">'+(offset+1)+' to '+page_end+' of ');
document.write( length+' Result');
if (length > 1) document.write('s');
document.write(' For "<span> '+search+' </span>"</div>');
for(i=offset;i < page_end;i++){
document.write('\
<div class="search_result">\
<div class="result_name">\
Name <span>'+list[i][0]+' <img src="http://xtgem.com/images/icons/mimes/'+list[i][5]+'.png" alt="*"/></span>\
</div>\
<div class="result_download_link">\
<a target="_BLANK" href="'+list[i][1]+'?__xt_download=1">Download</a> \
<a target="_BLANK" href="'+list[i][1]+'">Open</a>\
</div>\
<div class="result_url">\
Url <input type="text" value="'+list[i][1]+'" />\
</div>\
<div class="result_meta">\
Size <span>'+list[i][2]+'</span> \
Date <span>'+list[i][3]+'</span> \
Type <span>'+list[i][4]+'</span>\
</div>\
</div>');}
}}
# Pagination
var $common_part = document.write('<form style="display:inline" action="<xt:url />#srch" method="post"><input type="hidden" name="search" value="$search" /><input type="hidden" name="anywhere" value="$anywhere" /><input type="hidden" name="results_page" value="'+(results_page
print_raw {{
if (length > per_page){
document.write('<div class="results_pagination">');
if (offset > per_page){}}
print $common_part
print_raw {{-2)+'" /><span><input type="submit" value="'+(results_page-1)+'" /></span></form>');}
if (offset){}}
print $common_part
print_raw {{-1)+'" /><span><input type="submit" value="'+results_page+'" /></span></form>');}
document.write('<form style="display:inline"><span><input type="button" value="'+(results_page+1)+'" /></span></form>');
if (page_end < length){}}
print $common_part
print_raw {{+1)+'" /><span><input type="submit" value="'+(results_page+2)+'" /></span></form>');}
if (page_end < (length-per_page)){}}
print $common_part
print_raw {{+2)+'" /><span><input type="submit" value="'+(results_page+3)+'" /></span></form>');}
document.write('</div>');}
document.write('</div>');}}
# No search results
print } else {document.write('<div class="no_search_results"> No results for "<b>$search</b>"
print ;</div>');}
endif
<!--/parser:xtscript-->
</script>
Show in textarea Example output
Files in test folders
/FOLDER_1
4bvgrad.png | Cogs_in_motion.gif | android.png | bart_skate.gif | cartoon_clouds.gif | finger.gif | kiss_me_quick.png | mini-Dan_Dare_Mekon.JPG | rotating_skull.gif | us.png | weather_sun.png |
/FOLDER_2
Meaning_of_Liff_front_cover.jpg | border-4-big.jpg | brighton-pier-centre-lightning-storm.jpg | clematis.jpg | copper.jpg | dawn.jpg | dia.png | dmblbit.jpg | morning_sky.jpg | ngt.jpg | piswasser.jpg | rr_blowjob.jpg | saint.jpg |
/FOLDER_3
bbb.mp4 | beethovens_9th.wma | movie.mp4 | sample_iPod.m4v | sims.3gp | strange_man.3gp |
/FOLDER_4
Crazy_Frog-740.mp3 | Dj_BONEZ01.mp3 | Excuse_me_boss.mp3 | Its_A_Weirdo.mp3 | Joe_Walsh_Book_Ends.mp3 | Long Haired Country Boy.mp3 | Low_Rider.m4a | On_The_Run.mp3 | The_Italian_Job.mp3 | black_sabbath_paranoid.mp3 | black_sabbath_rat_salad.mp3 | blues_brothers_closing.mp3 | bonzo_Intro_Outro.mp3 | chicken_hard_bass.mp3 | eat.mp3 | police_siren_4000.mp3 |
Css used in demo