public static function getInstance()
 {
     if (is_null(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
    /**
     * run a standard search via sphinxsearch index
     * NOTE: $this->criteria->getSQLParts(...) needs to have been called before this function to populate sphinx criteria
     * @access private
     */
    function ExecuteSphinxRecordSet($pg)
    {
        global $CONF;
        $db = $this->_getDB();
        extract($this->criteria->sql, EXTR_PREFIX_ALL ^ EXTR_REFS, 'sql');
        $sphinx = new sphinxwrapper($this->criteria->sphinx['query']);
        $this->fullText = 1;
        $sphinx->pageSize = $this->criteria->resultsperpage + 0;
        $page = ($pg - 1) * $sphinx->pageSize;
        if ($page > 1000) {
            //todo - hard-coded 1000 needs autodetecting!
            //lets jump to the last page of 'past results'
            $pg = intval(ceil(1000 / $sphinx->pageSize));
            $this->currentPage = $pg;
        }
        //look for suggestions - this needs to be done before the filters are added - the same filters wont work on the gaz index
        if (isset($GLOBALS['smarty'])) {
            $suggestions = array();
            if (empty($this->countOnly) && $sphinx->q && strlen($sphinx->q) < 64 && empty($this->criteria->sphinx['x'])) {
                $suggestions = $sphinx->didYouMean($sphinx->q);
            } elseif ($this->criteria->searchclass == 'Placename' && strpos($this->criteria->searchdesc, $this->criteria->searchq) == FALSE && (empty($this->criteria->searchtext) || $this->criteria->searchq == $this->criteria->searchtext) && isset($GLOBALS['smarty'])) {
                $suggestions = array(array('gr' => '(anywhere)', 'localities' => 'as text search', 'query' => $this->criteria->searchq));
            }
            if (!empty($this->criteria->searchtext)) {
                if (is_numeric($this->criteria->searchtext)) {
                    require_once 'geograph/gridsquare.class.php';
                    require_once 'geograph/gridimage.class.php';
                    $image = new GridImage();
                    $image->loadFromId($this->criteria->searchtext);
                    if ($image->isValid() && ($image->moderation_status != 'rejected' && $image->moderation_status != 'pending' || $image->user_id == $GLOBALS['USER']->user_id)) {
                        $suggestions += array(array('link' => "/photo/{$image->gridimage_id}", 'gr' => '(anywhere)', 'localities' => "Image by " . htmlentities($image->realname) . ", ID: {$image->gridimage_id}", 'query' => htmlentities2($image->title)));
                    }
                } else {
                    require_once "3rdparty/spellchecker.class.php";
                    $correction = SpellChecker::Correct($this->criteria->searchtext);
                    if ($correction != $this->criteria->searchtext && levenshtein($correction, $this->criteria->searchtext) < 0.25 * strlen($correction)) {
                        $suggestions += array(array('gr' => '(anywhere)', 'localities' => '', 'query' => $correction));
                    }
                }
            }
            if (!empty($suggestions) && count($suggestions)) {
                $GLOBALS['smarty']->assign("suggestions", $suggestions);
            }
        }
        //setup the sphinx wrapper
        if (!empty($this->criteria->sphinx['sort'])) {
            $sphinx->setSort($this->criteria->sphinx['sort']);
        }
        if (empty($this->criteria->sphinx['sort']) || $this->criteria->sphinx['sort'] == '@relevance DESC, @id DESC') {
            if (preg_match('/\\w+/', preg_replace('/(@\\w+ |\\w+:)\\w+/', '', $this->criteria->sphinx['query']))) {
                $this->criteria->searchdesc = str_replace('undefined', 'relevance', $this->criteria->searchdesc);
            } elseif (strlen($this->criteria->sphinx['query'])) {
                #$this->criteria->searchdesc = str_replace(', in undefined order','',$this->criteria->searchdesc);
            }
        }
        if (!empty($this->criteria->sphinx['d'])) {
            $sphinx->setSpatial($this->criteria->sphinx);
        }
        //this step is handled internally by search and setSpatial
        //$sphinx->processQuery();
        if (!empty($CONF['fetch_on_demand'])) {
            $sphinx->upper_limit = $db->getOne("SELECT MAX(gridimage_id) FROM gridimage_search");
        }
        if (is_array($this->criteria->sphinx['filters']) && count($this->criteria->sphinx['filters'])) {
            $sphinx->addFilters($this->criteria->sphinx['filters']);
        }
        //run the sphinx search
        $ids = $sphinx->returnIds($pg, empty($this->criteria->sphinx['exact']) ? '_images' : '_images_exact');
        $this->resultCount = $sphinx->resultCount;
        $this->numberOfPages = $sphinx->numberOfPages;
        $this->maxResults = $sphinx->maxResults;
        $this->islimited = true;
        if (isset($GLOBALS['smarty']) && !empty($sphinx->res['words']) && (count($sphinx->res['words']) > 1 || !$this->resultCount)) {
            $GLOBALS['smarty']->assign("statistics", $sphinx->res['words']);
        }
        if ($this->countOnly || !$this->resultCount) {
            return 0;
        }
        if ($sql_order == ' dist_sqd ') {
            $this->sphinx_matches = $sphinx->res['matches'];
            $sql_fields = ',-1 as dist_sqd';
        }
        // fetch from database
        $id_list = implode(',', $ids);
        if ($this->noCache) {
            $sql = <<<END
/* i{$this->query_id} */ SELECT gi.*,x,y,gs.grid_reference,gi.realname as credit_realname,if(gi.realname!='',gi.realname,user.realname) as realname {$sql_fields}
FROM gridimage AS gi INNER JOIN gridsquare AS gs USING(gridsquare_id)
\tINNER JOIN user ON(gi.user_id=user.user_id)
WHERE gi.gridimage_id IN ({$id_list})
ORDER BY FIELD(gi.gridimage_id,{$id_list})
END;
        } else {
            $sql = "/* i{$this->query_id} */ SELECT gi.* {$sql_fields} FROM gridimage_search as gi WHERE gridimage_id IN ({$id_list}) ORDER BY FIELD(gi.gridimage_id,{$id_list})";
        }
        if (!empty($_GET['debug'])) {
            print "<BR><BR>{$sphinx->q}<BR><BR>{$sql}";
        }
        list($usec, $sec) = explode(' ', microtime());
        $querytime_before = (double) $usec + (double) $sec;
        $recordSet =& $db->Execute($sql);
        list($usec, $sec) = explode(' ', microtime());
        $querytime_after = (double) $usec + (double) $sec;
        if ($this->display == 'excerpt') {
            $docs = array();
            foreach ($ids as $c => $id) {
                $row = $rows[$id];
                $docs[$c] = strip_tags(preg_replace('/<i>.*?<\\/i>/', ' ', $row['post_text']));
            }
            $reply = $sphinx->BuildExcerpts($docs, empty($this->criteria->sphinx['exact']) ? 'gridimage' : 'gi_stemmmed', $sphinx->q);
        }
        $this->querytime = $querytime_after - $querytime_before + $sphinx->query_time;
        //finish off
        if (!empty($recordSet) && empty($_GET['BBOX']) && $this->display != 'reveal') {
            $db->Execute("replace into queries_count set id = {$this->query_id},`count` = {$this->resultCount}");
        }
        return $recordSet;
    }
Beispiel #3
0
				if (ele[q].type.toLowerCase() == 'select-one' 
						&& ele[q] == that
						&& ele[q].selectedIndex == ele[q].options.length-1
						) {
					ele[q].options[ele[q].selectedIndex].value = prompt("Enter the correct spelling of "+ele[q].value,ele[q].value);
				} 
				str = str + ele[q].value;
			}
			name = that.name.replace(/_/,'');
			that.form.elements[name].value = str;
		}
	</script>
	<?php 
        $query = "{$image->title} {$image->comment} {$image->imageclass}";
        // FIXME title2, comment2
        $xml = new SimpleXMLElement(SpellChecker::GetSuggestions($query));
        if (!$xml) {
            die("unable to contact spelling service");
        }
        $replacements = array();
        foreach ($xml->c as $correction) {
            $suggestions = explode("\t", (string) $correction);
            $offset = intval($correction['o']);
            $length = intval($correction['l']);
            $replacements[mb_substr($query, $offset, $length)] = $suggestions;
        }
        print "<form>";
        foreach (array('title' => 'Title', 'comment' => 'Description/Comment', 'imageclass' => 'Category') as $key => $name) {
            // FIXME title2, comment2
            print "<h3>{$name}</h3><blockquote>";
            $result = $select = $original = htmlentities2($image->{$key});
Beispiel #4
0
            $str = "";
        }
    }
    return $words;
}
$text = stripslashes($_REQUEST['text']);
// This is to deal with the fact that IE receives two characters (\r, \n) for every line break.
// This was throwing off the positions of words to be corrected.  The positions were off by
// one character for each line break entered in the textarea field.
$text = preg_replace("/\r/", "", $text);
if ($text) {
    $xml = '<?xml version="1.0" ?>';
    /*    $xml = '<?xml-stylesheet type="text/xsl" href="spellchecker.xsl" ?>'; */
    $xml .= '<spellcheck>';
    $xml .= "<originaltext>{$text}</originaltext>";
    $spell_checker = new SpellChecker();
    $textwords = strip_punct($text);
    $textwordsArray = word_list($textwords);
    $list = "";
    foreach ($textwordsArray as $n => $w) {
        $xml .= "<word>";
        $xml .= "<original pos=\"{$w->pos}\" len=\"{$w->len}\">{$w->word}</original>";
        if (!$spell_checker->check($w->word)) {
            $xml .= '<status>-1</status>';
            $suggestions = $spell_checker->suggest($w->word);
            if (sizeof($suggestions) > 0) {
                $xml .= '<suggestions>';
                foreach ($suggestions as $s) {
                    $xml .= "<sugword>{$s}</sugword>";
                }
                $xml .= '</suggestions>';