Ejemplo n.º 1
0
	function get_data() {
		global $url; global $error; global $search; global $sets;
		$return['display'] = array('search_info','search_content','navi');
		if (!in_array($url[2],$this->areas)) {
			$area = str_split($url[2]);
			foreach ($area as &$one) $one = $this->areas[$one];
			$area = array_filter($area);
			unset($one);
		} else {
			$area = array($url[2]);
		}

		$pp = $sets['pp']['search'];

		if ($url[3] != 'rel') {
			$main = 'and s.area="main"';
			$lim = (max(1,$url[6])-1)*$pp.', '.$pp;

			if ($url[3] == 'date') {
				$limit = ' order by s.sortdate desc limit '.$lim;
			} elseif ($url[3] == 'rdate') {
				$limit = ' order by s.sortdate limit '.$lim;
			}
		}

		if (empty($area)) $return['display'] = array('search_info','search_error');
		else {
			if (!$search) $search = new search();
			$request = urldecode($url[4]);
//			$request = preg_replace('/(\s|^)('.implode('|',$this->minus_words).')\s+(?=[^\s])/uis', ' -', $request);
			$request = preg_replace('/(\s|^)('.implode('|',$this->cyrillic_stoplist).')\s*/uis', ' ', $request);
			$terms = $search->parse_text($request);

			if ($search->error == true) {
				$error = true;
				return;
			}

			$pretty_query = $search->prepare_string(urldecode($url[4]),true);

			if (empty($terms)) $return['display'] = array('search_info','search_error');
			else {
				foreach ($terms as $term) {
					if (mb_strlen($term, 'UTF-8') > 3) $longterms[] = str_replace('-','_',$term);
					else $shortterms[] = $term;
				}

				if (!empty($longterms)) {
					$longquery = ' and match (s.`index`) against ("'.implode(' ',$longterms).'" in boolean mode)';
				}
				if (!empty($shortterms)) {
					$shortquery = '';
					foreach ($shortterms as $shortterm) {
						$not = $shortterm{0} == '-';
						$shortquery .= 'and s.`index` '.($not ? 'not ' : '').'like "%|'.substr($shortterm,1).'=%"';
					}
				}

				if (empty($query)) {
					$query = '(s.place="'.implode('" or s.place="',$area).'") '.$main.$shortquery.$longquery.$limit;
					$navi_query = '(s.place="'.implode('" or s.place="',$area).'") '.$main.$shortquery.$longquery;
				}
				$data = obj::db()->sql('select s.place, s.item_id, s.`index`, s.area, s.sortdate from search as s where ' . $query);

				if (empty($data)) {
					$data = $this->search_in_titles($area, $pretty_query);
				}

				if (empty($data)) {
					foreach ($area as $one) $zero[] = 0;
					obj::db()->update('search_queries',$area,$zero,$pretty_query,'query');
					$return['variants'] = $this->get_variants($pretty_query, $area);
					$return['display'] = array('search_info','search_error');
				} else {
					if (!$limit) {
						$return['navi']['last'] = ceil(count($data)/$pp);

						$this->weights = obj::db()->sql('select place, weight from search_weights','place');
						if (count($terms) > 1) $data = $this->relevance($data,$terms,$pp,max(1,$url[6]));
						else $data = $this->relevance_simple($data,$terms,$pp,max(1,$url[6]));
					} else {
						$return['navi']['last'] = ceil(obj::db()->sql('select count(*) from search as s where '. $navi_query,2)/$pp);
					}

					foreach ($data as $one) {
						$function = 'fetch_'.$one['place'];
						$return['data'][] = $this->$function($one['item_id']);
						$found[$one['place']] = true;
					}

					foreach ($area as $one) {
						$update .= ($found[$one] ? ", ".$one."=".$one."+1" : '');
						$insert .= ", ".($found[$one] ? 1 : 0);
					}
					if (!strpos($pretty_query, 'md5:'))
					{
						obj::db()->sql("insert into search_queries (`id` ,`query` ,`length` , `".implode("` ,`",$area)."`) values('','".$pretty_query."',".mb_strlen($pretty_query).$insert.") on duplicate key update ".substr($update,1).";",0);
					}

					$return['navi']['curr'] = max(1,$url[6]);
					$return['navi']['start'] = max($return['navi']['curr']-5,2);
					$return['navi']['base'] = '/search/'.$url[2].'/'.$url[3].'/'.$url[4].'/';
				}
			}
		}
		if ($url[2] == 'a' && strpos($return['display'][0],'info')) unset ($return['display'][0]);
		return $return;
	}
Ejemplo n.º 2
0
 function search($query, $page)
 {
     global $search;
     if (!$search) {
         $search = new search();
     }
     $return = array();
     $return['display'] = array('logs_search', 'logs_results', 'navi');
     $perpage = 10;
     $start = ($page - 1) * $perpage;
     $base = substr(get_called_class(), 8);
     $query = $search->prepare_string(urldecode($query), true);
     $mark = preg_split('/\\s+/s', $query);
     $fixed_query = '+' . preg_replace('/\\s+/s', ' +', $query);
     $select_query = ' MATCH (text) AGAINST ("' . $fixed_query . '" IN BOOLEAN MODE) and cache_key = ' . static::$cache_key;
     $sql = 'SELECT id, date FROM raw_logs WHERE' . $select_query;
     $days = obj::db()->sql($sql . ' group by date order by date desc limit ' . $start . ', ' . $perpage, 'id');
     if (!empty($days)) {
         $select_days = '"' . implode('","', $days) . '"';
         $data = obj::db()->sql('SELECT `date`, `time`, `text` FROM raw_logs WHERE `date` in (' . $select_days . ') and' . $select_query);
         foreach ($days as $day) {
             foreach ($data as $key => $log) {
                 if ($log['date'] == $day) {
                     $return['days'][$day][] = array('time' => $log['time'], 'text' => $this->format_logs($log['text'], false, $mark));
                     unset($data[$key]);
                     if (count($return['days'][$day]) > 4) {
                         break;
                     }
                 }
             }
         }
         $return['navi']['meta'] = '/' . $base . '/search/' . $query . '/';
         $return['navi']['curr'] = $page;
         $return['navi']['start'] = max($return['navi']['curr'] - 5, 2);
         $return['navi']['last'] = ceil(obj::db()->sql('select count(distinct(date)) from raw_logs WHERE MATCH (text) AGAINST ("' . $fixed_query . '" IN BOOLEAN MODE) and cache_key = ' . static::$cache_key, 2) / $perpage);
     } else {
         $return['display'] = array('logs_search', 'logs_results');
     }
     $return['log_base'] = $base;
     return $return;
 }