private function areIndexesDefined($file_id)
 {
     $index = new indexes($this->getDb());
     $indexes = $index->select('*', 'WHERE file="' . $file_id . '"');
     if (!isset($indexes[0]['id'])) {
         return false;
     } else {
         return true;
     }
 }
 public function base()
 {
     $results = array();
     $query = isset($_GET['q']) ? $_GET['q'] : false;
     $search = $query;
     if ($query !== false) {
         $stemmer = new PorterStemmer();
         $index = new indexes($this->getDb());
         $file = new files($this->getDb());
         $scores = array();
         $terms = explode(' ', $query);
         $db_searches = new searches($this->getDb());
         foreach ($terms as $term) {
             if ($term != '') {
                 $old = $db_searches->select('*', 'WHERE term="' . $term . '"');
                 if (isset($old[0]['id'])) {
                     $db_searches->update(array('count' => ++$old[0]['count'], 'date' => time()), 'WHERE term="' . $term . '"');
                 } else {
                     $db_searches->insert(array('term' => $term, 'count' => 1, 'date' => time()));
                 }
             }
         }
         $term_weight = 0.05;
         $wpm_weight = 5;
         //7
         $count_weight = 150;
         //135
         $count = 0;
         foreach ($terms as $term) {
             $term = 'indx-' . $stemmer->Stem($term);
             $data = $index->select('*', 'WHERE stem="' . $term . '" ORDER BY wpm DESC, count DESC LIMIT 0, 100');
             foreach ($data as $file_data) {
                 $file_id = $file_data['file'];
                 $wpm = $file_data['wpm'];
                 $index_count = $file_data['count'];
                 $weight = $wpm * $wpm_weight * (1 - $term_weight * $count) + $index_count * $count_weight * (1 - $term_weight * $count);
                 if (isset($scores[$file_id])) {
                     $scores[$file_id] += $weight;
                 } else {
                     $scores[$file_id] = $weight;
                 }
             }
             $count++;
         }
         arsort($scores);
         foreach ($scores as $key => $score) {
             $results[$key] = array();
             $results[$key]['score'] = $score;
         }
         $ids = array_keys($scores);
         $files = array();
         if (count($ids) > 0) {
             $files = $file->select('*', 'WHERE id IN (' . implode(', ', $ids) . ')');
         }
         foreach ($files as $selected) {
             $results[$selected['id']]['id'] = $selected['id'];
             $results[$selected['id']]['name'] = $selected['name'];
             $results[$selected['id']]['link'] = $selected['link'];
         }
         $this->set('results', $results);
     } else {
         $this->set('results', $results);
     }
     $this->set('search', $search);
     $searches = new searches($this->getDb());
     $this->set('popular', $searches->select('*', 'ORDER BY count DESC LIMIT 0, 10'));
 }