search.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00028 class search extends WebObject
00029 {
00030   function init()
00031   {
00032     $this->addSVar('expression', '');
00033     $this->addSVar('all_books', '');
00034     $this->addSVar('all_langs', '');
00035 
00036     $this->addSVar('recs_per_page', '20');
00037     $this->addSVar('current_page', '1');
00038     $this->addSVar('nr_of_recs', '0');
00039   }
00040 
00041   function on_new_search($event_args)
00042   {
00043     $this->setSVar('expression', $event_args['expression']);
00044     $this->setSVar('current_page', '1');
00045   }
00046 
00047   function on_help($event_args)
00048   {
00049     WebApp::addGlobalVar('help', 'true');
00050   }
00051 
00052   function on_next($event_args)
00053   {
00054     $page = $event_args['page'];
00055     $this->setSVar('current_page', $page);
00056   }
00057 
00058   function onParse()
00059   {
00060     $lng = WebApp::getSVar('docbook->lng');      
00061     $help_file = "help_$lng.html";
00062     $dir = dirname(__FILE__);
00063     if (!file_exists("$dir/help/$help_file")) $help_file = "help.html";
00064     WebApp::addVar('help_file', $help_file);
00065   }
00066 
00067   function onRender()
00068   {
00069     WebApp::addVars($this->get_page_vars());
00070     $search_results = $this->get_results_rs();
00071     global $webPage;
00072     $webPage->addRecordset($search_results);
00073 
00074     $expression = $this->getSVar('expression');
00075     $expression = str_replace('\"', '"', $expression);
00076     WebApp::addVar('expression', $expression);
00077   }
00078 
00080   function get_results_rs()
00081   {
00082     $rs = new EditableRS('search_results');
00083 
00084     $query = $this->get_query();
00085     if ($query==UNDEFINED)  return $rs;
00086 
00087     //execute the limited search query
00088     $output_format = "<book_id>::<lng>::<path>::<title>::<swishlastmodified>\n";
00089     $first = WebApp::getVar('FirstRec');
00090     $rp = $this->getSVar('recs_per_page');
00091     $cmd = SWISH_E." -H0 -x '$output_format' -b $first -m $rp $query";
00092     $results = shell($cmd);
00093     //print "<xmp>$cmd \n\n$results\n</xmp>";  //debug
00094 
00095     //get a list of books and their titles
00096     $arr_books = $this->get_book_list();
00097 
00098     //process the search results and fill the recordset
00099     $lines = explode("\n", $results);
00100     for ($i=0; $i < sizeof($lines); $i++)
00101       {
00102         list($book_id,$lang,$path,$title,$date) = explode('::', $lines[$i]);
00103         if ($date=='')  continue;
00104 
00105         //change the format of last modified date
00106         $arr = explode(' ', $date);
00107         list($year, $month, $day) = explode('-', $arr[0]);
00108         $date = date('M d, Y', mktime(0, 0, 0, $month, $day, $year));
00109 
00110         $rec = array(
00111                      'nr'         => $first+$i,
00112                      'book_id'    => $book_id,
00113                      'lang'       => $lang,
00114                      'book_title' => $arr_books[$book_id.':'.$lang],
00115                      'node_path'  => $path,
00116                      'node_title' => $title,
00117                      'date'       => $date,
00118                      );
00119         $rs->addRec($rec);
00120       }
00121     return $rs;
00122   }
00123 
00128   function get_book_list()
00129   {
00130     $arr_books = array();
00131     $lines = file(CONTENT.'books/book_list');
00132     for ($i=0; $i < sizeof($lines); $i++)
00133       {
00134         $line = $lines[$i];
00135         $line = trim($line);
00136         if ($line=='') continue;
00137         list($book_id, $lng, $book_title) = split(':', $line);
00138         $book_title = trim($book_title);
00139         $arr_books[$book_id.':'.$lng] = $book_title;
00140       }
00141 
00142     return $arr_books;
00143   }
00144 
00145   function get_query()
00146   {
00147     $expression = $this->getSVar('expression');
00148     if (trim($expression)=='')  return UNDEFINED;
00149 
00150     $all_books = $this->getSVar('all_books');
00151     $all_langs = $this->getSVar('all_langs');
00152     if ($all_books != 'checked')
00153       {
00154         $book_id = WebApp::getSVar('docbook->book_id');
00155         $expression = "book_id=$book_id and ($expression)";
00156       }
00157 
00158     if ($all_langs != 'checked')
00159       {
00160         $lng = WebApp::getSVar('docbook->lng');
00161         $expression = "lng=$lng and $expression";
00162       }
00163 
00164     $index_files = "search/global.index search/tag.index";
00165     $query = "-f $index_files -w '$expression'";
00166 
00167     return $query;
00168   }
00169 
00170   function get_nr_of_recs()
00171   {
00172     //If it is the first page, then run the unlimited
00173     //query, count the results, and save it in a state var.
00174     //For the other pages, get it from the state var.
00175     $current_page = $this->getSVar('current_page');
00176     if ($current_page==1)
00177       {
00178         $query = $this->get_query();
00179         if ($query==UNDEFINED)  return 0;
00180 
00181         $results = shell(SWISH_E." -H0 $query");
00182         $arr_lines = explode("\n", $results);
00183         $nr_of_recs = sizeof($arr_lines) - 1;
00184         $this->setSVar('nr_of_recs', $nr_of_recs);
00185       }
00186     else
00187       {
00188         $nr_of_recs = $this->getSVar('nr_of_recs');
00189       }
00190     return $nr_of_recs;
00191   }
00192 
00193   function get_page_vars()
00194   {           
00195     $current_page = $this->getSVar('current_page');
00196     $recs_per_page = $this->getSVar('recs_per_page');
00197     $nr_of_recs = $this->get_nr_of_recs();
00198 
00199     $nr_of_pages = ceil($nr_of_recs / $recs_per_page);
00200     if ($current_page>$nr_of_pages) $current_page=$nr_of_pages;
00201 
00202     if ($current_page==$nr_of_pages)
00203       $next_page = "1"; //next page of the last page is the first page
00204     else
00205       $next_page = $current_page + 1;
00206 
00207     if ($current_page==1)
00208       $prev_page = 1; //previous page of the first page is the first page itself
00209     else
00210       $prev_page = $current_page - 1;
00211 
00212     $first_rec = 1 + ($current_page - 1)*$recs_per_page;
00213     if ($first_rec < 1)  $first_rec = 1;
00214     $last_rec   = $current_page * $recs_per_page;
00215     if ($first_rec > $nr_of_recs)  $first_rec = $nr_of_recs; 
00216     if ($last_rec  > $nr_of_recs)  $last_rec  = $nr_of_recs; 
00217 
00218     $page_vars = 
00219       array(
00220             //the number of the first record of the page
00221             "FirstRec" => $first_rec,
00222             //the number of the last record of the page               
00223             "LastRec"  => $last_rec,
00224             //the number of all records that can be retrieved by the query
00225             "AllRecs"  => $nr_of_recs,
00226             //current page of records that is retrieved
00227             "CurrPage" => $current_page,
00228             //the number of the next page
00229             "NextPage" => $next_page,
00230             //the number of the previous page
00231             "PrevPage" => $prev_page,
00232             //the number of the last page
00233             "LastPage" => $nr_of_pages
00234             );
00235     return $page_vars;
00236   }
00237 }
00238 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2