Location: PHPKode > projects > Aukyla Platform > doc/html/a00231.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Aukyla PHP Framework</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="StyleSheet" href="auton.css" type="text/css" media="screen">
  </head>

  <body>
    <div id="mainmenu">
      <a href="http://www.auton.nl/" class="home">Home</a>
    </div>
    <div id="submenu">
      <a class="logo" href="http://www.auton.nl/"></a>

      <div class="head">Ads</div>
      <a href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=116358&amp;type=1" alt="SourceForge.net Logo" style="padding: 2px 4px; width: 88px; height: 31px; border: 0px"></a>
      <a href="http://getfirefox.com/" title="Get Firefox - The Browser, Reloaded."><img src="http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png" alt="Get Firefox" style="width: 110px; height: 32px; border: 0px"></a>
    </div>
    <div id="main">
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">base</a></div>
<h1>Search.php</h1><div class="fragment"><pre class="fragment">00001 &lt;?php
00002 <span class="comment">/*</span>
00003 <span class="comment">     Search.php, main class for handling the search system</span>
00004 <span class="comment">     Copyright (C) 2005 Arend van Beelen, Auton Rijnsburg</span>
00005 <span class="comment"></span>
00006 <span class="comment">     This program is free software; you can redistribute it and/or modify it</span>
00007 <span class="comment">     under the terms of the GNU General Public License as published by the Free</span>
00008 <span class="comment">     Software Foundation; either version 2 of the License, or (at your option)</span>
00009 <span class="comment">     any later version.</span>
00010 <span class="comment"></span>
00011 <span class="comment">     This program is distributed in the hope that it will be useful, but WITHOUT</span>
00012 <span class="comment">     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span>
00013 <span class="comment">     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for</span>
00014 <span class="comment">     more details.</span>
00015 <span class="comment"></span>
00016 <span class="comment">     You should have received a copy of the GNU General Public License along</span>
00017 <span class="comment">     with this program; if not, write to the Free Software Foundation, Inc.,</span>
00018 <span class="comment">     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00019 <span class="comment"></span>
00020 <span class="comment">     For any questions, comments or whatever, you may mail me at: hide@address.com</span>
00021 <span class="comment">*/</span>
00022 
00023 require_once('<a class="code" href="a00092.html">Config</a>.php');
00024 require_once('<a class="code" href="a00097.html">Database</a>.php');
00025 require_once('<a class="code" href="a00100.html">Document</a>.php');
00026 require_once('MIME.php');
00027 require_once('<a class="code" href="a00163.html">URI</a>.php');
00028 
<a name="l00038"></a><a class="code" href="a00148.html">00038</a> <span class="keyword">class </span><a class="code" href="a00148.html">SearchResult</a>
00039 {
<a name="l00043"></a><a class="code" href="a00148.html#o0">00043</a>         <span class="keyword">public</span> $application;
00044 
<a name="l00051"></a><a class="code" href="a00148.html#o1">00051</a>         <span class="keyword">public</span> $uri;
00052 
<a name="l00057"></a><a class="code" href="a00148.html#o2">00057</a>         <span class="keyword">public</span> $fromGateway;
00058 
<a name="l00062"></a><a class="code" href="a00148.html#o3">00062</a>         <span class="keyword">public</span> $title;
00063 
<a name="l00067"></a><a class="code" href="a00148.html#o4">00067</a>         <span class="keyword">public</span> $summary;
00068 
<a name="l00072"></a><a class="code" href="a00148.html#o5">00072</a>         <span class="keyword">public</span> $mimeType;
00073 
<a name="l00077"></a><a class="code" href="a00148.html#o6">00077</a>         <span class="keyword">public</span> $metaData;
00078 
<a name="l00082"></a><a class="code" href="a00148.html#o7">00082</a>         <span class="keyword">public</span> $score;
00083 
<a name="l00088"></a><a class="code" href="a00148.html#o8">00088</a>         <span class="keyword">public</span> $flags;
00089 }
00090 
<a name="l00100"></a><a class="code" href="a00145.html">00100</a> <span class="keyword">class </span><a class="code" href="a00145.html">Search</a>
00101 {
<a name="l00107"></a><a class="code" href="a00145.html#e0">00107</a>         <span class="keyword">public</span> <span class="keyword">static</span> function instance()
00108         {
00109                 <span class="keywordflow">if</span>(self::$instance == null)
00110                 {
00111                         self::$instance = <span class="keyword">new</span> <a class="code" href="a00145.html">Search</a>();
00112                 }
00113 
00114                 <span class="keywordflow">return</span> self::$instance;
00115         }
00116 
00125         <span class="keyword">private</span> function __construct()
00126         {
00127                 $this-&gt;database = null;
00128                 $this-&gt;gateways = array();
00129                 $this-&gt;indexes = array();
00130                 $this-&gt;gatewaysOnly = <span class="keyword">false</span>;
00131                 $this-&gt;availableInterfaces = array();
00132                 $this-&gt;availableInterfaceNames = array();
00133                 $this-&gt;selectedInterfaces = array();
00134                 $this-&gt;selectedIndexes = array();
00135 
00136                 $this-&gt;connectToDatabase();
00137 
00138                 $this-&gt;loadInterfaces();
00139         }
00140 
00146         <span class="keyword">private</span> function connectToDatabase()
00147         {
00148                 $type = <a class="code" href="a00092.html#e0">Config::globals</a>('searchDatabaseType');
00149                 $server = <a class="code" href="a00092.html#e0">Config::globals</a>('searchDatabaseServer');
00150                 $database = <a class="code" href="a00092.html#e0">Config::globals</a>('searchDatabaseName');
00151                 $username = <a class="code" href="a00092.html#e0">Config::globals</a>('searchDatabaseUsername');
00152                 $password = <a class="code" href="a00092.html#e0">Config::globals</a>('searchDatabasePassword');
00153 
00154                 $this-&gt;database = <a class="code" href="a00097.html#e0">Database::connection</a>($type, $server, $database, $username, $password);
00155 
00156                 <span class="keywordflow">if</span>($this-&gt;database === <span class="keyword">false</span>)
00157                 {
00158                         $this-&gt;database = null;
00159                         $this-&gt;gatewaysOnly = <span class="keyword">true</span>;
00160                 }
00161         }
00162 
00168         <span class="keyword">private</span> function loadInterfaces()
00169         {
00170                 $interfaces = glob(AUKYLA_DIR.'/plugins/SearchInterfaces<span class="comment">/*.php');</span>
00171 <span class="comment">                if($interfaces !== false)</span>
00172 <span class="comment">                {</span>
00173 <span class="comment">                        foreach($interfaces as $interface)</span>
00174 <span class="comment">                        {</span>
00175 <span class="comment">                                $interfaceName = basename($interface, '.php');</span>
00176 <span class="comment">                                $className = "{$interfaceName}_SearchInterface";</span>
00177 <span class="comment"></span>
00178 <span class="comment">                                $include = include_once($interface);</span>
00179 <span class="comment">                                if($include == false ||</span>
00180 <span class="comment">                                   class_exists($className) == false)</span>
00181 <span class="comment">                                {</span>
00182 <span class="comment">                                        trigger_error("Invalid search interface \"$className\" in plugins/SearchInterfaces");</span>
00183 <span class="comment">                                        continue;</span>
00184 <span class="comment">                                }</span>
00185 <span class="comment"></span>
00186 <span class="comment">                                if($this-&gt;gatewaysOnly == true)</span>
00187 <span class="comment">                                {</span>
00188 <span class="comment">                                        trigger_error("Interface \"$className\" ignored because no database is available");</span>
00189 <span class="comment">                                        continue;</span>
00190 <span class="comment">                                }</span>
00191 <span class="comment"></span>
00192 <span class="comment">                                $interface = new $className();</span>
00193 <span class="comment">                                $this-&gt;indexes[$interfaceName] = $interface;</span>
00194 <span class="comment">                                $this-&gt;availableInterfaces[] = $interfaceName;</span>
00195 <span class="comment">                                $this-&gt;availableInterfaceNames[$interfaceName] = $interface-&gt;name();</span>
00196 <span class="comment">                                $this-&gt;selectedIndexes[] = $interfaceName;</span>
00197 <span class="comment">                                $this-&gt;selectedInterfaces[] = $interfaceName;</span>
00198 <span class="comment">                        }</span>
00199 <span class="comment">                }</span>
00200 <span class="comment"></span>
00201 <span class="comment">                $interfaces = glob(AUKYLA_DIR.'/plugins/SearchGatewayInterfaces/*.php');</span>
00202 <span class="comment">                if($interfaces !== false)</span>
00203 <span class="comment">                {</span>
00204 <span class="comment">                        foreach($interfaces as $interface)</span>
00205 <span class="comment">                        {</span>
00206 <span class="comment">                                $interfaceName = basename($interface, '.php');</span>
00207 <span class="comment">                                $className = "{$interfaceName}_SearchGatewayInterface";</span>
00208 <span class="comment"></span>
00209 <span class="comment">                                $include = include_once($interface);</span>
00210 <span class="comment">                                if($include == false ||</span>
00211 <span class="comment">                                   class_exists($className) == false)</span>
00212 <span class="comment">                                {</span>
00213 <span class="comment">                                        trigger_error("Invalid search interface \"$className\" in plugins/SearchGatewayInterfaces");</span>
00214 <span class="comment">                                        continue;</span>
00215 <span class="comment">                                }</span>
00216 <span class="comment"></span>
00217 <span class="comment">                                $gateway = new $className();</span>
00218 <span class="comment">                                $this-&gt;gateways[$interfaceName] = $gateway;</span>
00219 <span class="comment">                                $this-&gt;availableInterfaces[] = $interfaceName;</span>
00220 <span class="comment">                                $this-&gt;availableInterfaceNames[$interfaceName] = $gateway-&gt;name();</span>
00221 <span class="comment">                                $this-&gt;selectedInterfaces[] = $interfaceName;</span>
00222 <span class="comment">                        }</span>
00223 <span class="comment">                }</span>
00224 <span class="comment">        }</span>
00225 <span class="comment"></span>
00231 <span class="comment">        public function availableInterfaces()</span>
00232 <span class="comment">        {</span>
00233 <span class="comment">                return $this-&gt;availableInterfaces;</span>
00234 <span class="comment">        }</span>
00235 <span class="comment"></span>
00243 <span class="comment">        public function interfaceNames()</span>
00244 <span class="comment">        {</span>
00245 <span class="comment">                return $this-&gt;availableInterfaceNames;</span>
00246 <span class="comment">        }</span>
00247 <span class="comment"></span>
00255 <span class="comment">        public function setInterfaces($interfaces)</span>
00256 <span class="comment">        {</span>
00257 <span class="comment">                $this-&gt;selectedInterfaces = array();</span>
00258 <span class="comment">                foreach($interfaces as $interface)</span>
00259 <span class="comment">                {</span>
00260 <span class="comment">                        if(in_array($interface, $this-&gt;availableInterfaces))</span>
00261 <span class="comment">                        {</span>
00262 <span class="comment">                                $this-&gt;selectedInterfaces[] = $interface;</span>
00263 <span class="comment"></span>
00264 <span class="comment">                                if(isset($this-&gt;indexes[$interface]))</span>
00265 <span class="comment">                                {</span>
00266 <span class="comment">                                        $this-&gt;selectedIndexes[] = $interface;</span>
00267 <span class="comment">                                }</span>
00268 <span class="comment">                        }</span>
00269 <span class="comment">                }</span>
00270 <span class="comment">        }</span>
00271 <span class="comment"></span>
00285 <span class="comment">        public function doSearch($keywords, $maxResults = 10, $offset = 0)</span>
00286 <span class="comment">        {</span>
00287 <span class="comment">                $keywords = self::splitKeywords($keywords);</span>
00288 <span class="comment"></span>
00289 <span class="comment">                $results = array();</span>
00290 <span class="comment">                foreach($this-&gt;gateways as $gatewayName =&gt; $gateway)</span>
00291 <span class="comment">                {</span>
00292 <span class="comment">                        if(in_array($gatewayName, $this-&gt;selectedInterfaces))</span>
00293 <span class="comment">                        {</span>
00294 <span class="comment">                                $results[$gatewayName] = $gateway-&gt;search($keywords, $maxResults, $offset);</span>
00295 <span class="comment">                        }</span>
00296 <span class="comment">                }</span>
00297 <span class="comment"></span>
00298 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00299 <span class="comment">                {</span>
00300 <span class="comment">                        return $results;</span>
00301 <span class="comment">                }</span>
00302 <span class="comment"></span>
00303 <span class="comment">                foreach($this-&gt;selectedIndexes as $application)</span>
00304 <span class="comment">                {</span>
00305 <span class="comment">                        $results[$application] = array();</span>
00306 <span class="comment"></span>
00307 <span class="comment">                        $firstKeyword  = strtolower($keywords[0]);</span>
00308 <span class="comment">                        $otherKeywords = array_slice($keywords, 1);</span>
00309 <span class="comment"></span>
00310 <span class="comment">                        $application = addslashes($application);</span>
00311 <span class="comment"></span>
00312 <span class="comment">                        $query = "SELECT uri, SUM(weight) AS weight ".</span>
00313 <span class="comment">                                 "FROM (SELECT D.uri AS uri, C.weight AS weight ".</span>
00314 <span class="comment">                                 "      FROM Documents D, ContainsWord C, Words W ".</span>
00315 <span class="comment">                                 "      WHERE D.app = '$application' AND D.did = C.did AND C.wid = W.wid AND W.word LIKE '%{$firstKeyword}%'";</span>
00316 <span class="comment">                        foreach($otherKeywords as $keyword)</span>
00317 <span class="comment">                        {</span>
00318 <span class="comment">                                $query .=" INTERSECT ALL ".</span>
00319 <span class="comment">                                          "SELECT D.uri AS uri, C.weight AS weight ".</span>
00320 <span class="comment">                                          "FROM Documents D, ContainsWord C, Words W ".</span>
00321 <span class="comment">                                          "WHERE D.app = '$application' AND D.did = C.did AND C.wid = W.wid AND W.word LIKE '%".strtolower($keyword)."%'";</span>
00322 <span class="comment">                        }</span>
00323 <span class="comment">                        $query .= ") AS results ".</span>
00324 <span class="comment">                                  "GROUP BY uri ".</span>
00325 <span class="comment">                                  "ORDER BY weight DESC ".</span>
00326 <span class="comment">                                  "LIMIT $maxResults ".</span>
00327 <span class="comment">                                  "OFFSET $offset;";</span>
00328 <span class="comment"></span>
00329 <span class="comment">                        $this-&gt;database-&gt;query($query);</span>
00330 <span class="comment"></span>
00331 <span class="comment">                        for($i = 0; $i &lt; $this-&gt;database-&gt;numberOfRows(); $i++)</span>
00332 <span class="comment">                        {</span>
00333 <span class="comment">                                $row = $this-&gt;database-&gt;resultArray();</span>
00334 <span class="comment"></span>
00335 <span class="comment">                                $result = $results[$application][] = new SearchResult();</span>
00336 <span class="comment"></span>
00337 <span class="comment">                                $result-&gt;application = $application;</span>
00338 <span class="comment">                                $result-&gt;uri         = $row[0];</span>
00339 <span class="comment">                                $result-&gt;fromGateway = false;</span>
00340 <span class="comment">                                $result-&gt;score       = $row[1];</span>
00341 <span class="comment">                                $result-&gt;title       = URI::metaData($result-&gt;uri, 'name');</span>
00342 <span class="comment">                                $result-&gt;summary     = URI::metaData($result-&gt;uri, 'comments');</span>
00343 <span class="comment">                                $result-&gt;mimeType    = MIME::type($result-&gt;uri);</span>
00344 <span class="comment">                        }</span>
00345 <span class="comment">                }</span>
00346 <span class="comment"></span>
00347 <span class="comment">                return $results;</span>
00348 <span class="comment">        }</span>
00349 <span class="comment"></span>
00360 <span class="comment">        public function rebuildDatabase()</span>
00361 <span class="comment">        {</span>
00362 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00363 <span class="comment">                {</span>
00364 <span class="comment">                        return;</span>
00365 <span class="comment">                }</span>
00366 <span class="comment"></span>
00367 <span class="comment">                $this-&gt;createNewDatabase();</span>
00368 <span class="comment"></span>
00369 <span class="comment">                foreach($this-&gt;indexes as $index)</span>
00370 <span class="comment">                {</span>
00371 <span class="comment">                        $index-&gt;indexAll();</span>
00372 <span class="comment">                }</span>
00373 <span class="comment">        }</span>
00374 <span class="comment"></span>
00379 <span class="comment">        public function createNewDatabase()</span>
00380 <span class="comment">        {</span>
00381 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00382 <span class="comment">                {</span>
00383 <span class="comment">                        return;</span>
00384 <span class="comment">                }</span>
00385 <span class="comment"></span>
00386 <span class="comment">                $this-&gt;database-&gt;query('DROP TABLE MetaData;');</span>
00387 <span class="comment">                $this-&gt;database-&gt;query('DROP TABLE ContainsWord;');</span>
00388 <span class="comment">                $this-&gt;database-&gt;query('DROP TABLE Words;');</span>
00389 <span class="comment">                $this-&gt;database-&gt;query('DROP TABLE Documents;');</span>
00390 <span class="comment"></span>
00391 <span class="comment">                $this-&gt;database-&gt;query('CREATE TABLE Documents'.</span>
00392 <span class="comment">                                       '('.</span>
00393 <span class="comment">                                       ' did SERIAL NOT NULL,'.</span>
00394 <span class="comment">                                       ' app VARCHAR(255) NOT NULL,'.</span>
00395 <span class="comment">                                       ' uri VARCHAR(255) NOT NULL,'.</span>
00396 <span class="comment">                                       ' PRIMARY KEY(did),'.</span>
00397 <span class="comment">                                       ' UNIQUE(app, uri)'.</span>
00398 <span class="comment">                                       ');');</span>
00399 <span class="comment">                $this-&gt;database-&gt;query('CREATE TABLE Words'.</span>
00400 <span class="comment">                                       '('.</span>
00401 <span class="comment">                                       ' wid SERIAL NOT NULL,'.</span>
00402 <span class="comment">                                       ' word VARCHAR(50) NOT NULL,'.</span>
00403 <span class="comment">                                       ' PRIMARY KEY(wid),'.</span>
00404 <span class="comment">                                       ' UNIQUE(word)'.</span>
00405 <span class="comment">                                       ');');</span>
00406 <span class="comment">                $this-&gt;database-&gt;query('CREATE TABLE ContainsWord'.</span>
00407 <span class="comment">                                       '('.</span>
00408 <span class="comment">                                       ' did INTEGER NOT NULL,'.</span>
00409 <span class="comment">                                       ' wid INTEGER NOT NULL,'.</span>
00410 <span class="comment">                                       ' weight INTEGER NOT NULL DEFAULT 1,'.</span>
00411 <span class="comment">                                       ' PRIMARY KEY(did, wid),'.</span>
00412 <span class="comment">                                       ' FOREIGN KEY(did) REFERENCES Documents,'.</span>
00413 <span class="comment">                                       ' FOREIGN KEY(wid) REFERENCES Words'.</span>
00414 <span class="comment">                                       ');');</span>
00415 <span class="comment">                $this-&gt;database-&gt;query('CREATE TABLE MetaData'.</span>
00416 <span class="comment">                                       '('.</span>
00417 <span class="comment">                                       ' did SERIAL NOT NULL,'.</span>
00418 <span class="comment">                                       ' key VARCHAR(50) NOT NULL,'.</span>
00419 <span class="comment">                                       ' value VARCHAR(255) NOT NULL,'.</span>
00420 <span class="comment">                                       ' PRIMARY KEY(did, key),'.</span>
00421 <span class="comment">                                       ' FOREIGN KEY(did) REFERENCES Documents'.</span>
00422 <span class="comment">                                       ');');</span>
00423 <span class="comment">        }</span>
00424 <span class="comment"></span>
00440 <span class="comment">        public function addDocument($application, $uri)</span>
00441 <span class="comment">        {</span>
00442 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00443 <span class="comment">                {</span>
00444 <span class="comment">                        return false;</span>
00445 <span class="comment">                }</span>
00446 <span class="comment"></span>
00447 <span class="comment">                $application = addslashes($application);</span>
00448 <span class="comment">                $uri         = addslashes($uri);</span>
00449 <span class="comment"></span>
00450 <span class="comment">                $this-&gt;database-&gt;query("SELECT D.did ".</span>
00451 <span class="comment">                                       "FROM Documents D ".</span>
00452 <span class="comment">                                       "WHERE D.app = '$application' AND D.uri = '$uri';");</span>
00453 <span class="comment">                if($this-&gt;database-&gt;hasResults())</span>
00454 <span class="comment">                {</span>
00455 <span class="comment">                        $did = $this-&gt;database-&gt;result(0, 0);</span>
00456 <span class="comment">                }</span>
00457 <span class="comment">                else</span>
00458 <span class="comment">                {</span>
00459 <span class="comment">                        $this-&gt;database-&gt;query("SELECT nextval('Documents_did_seq') as key;");</span>
00460 <span class="comment">                        $did = $this-&gt;database-&gt;result(0, 0);</span>
00461 <span class="comment">                        $this-&gt;database-&gt;query("INSERT INTO Documents (did, app, uri) ".</span>
00462 <span class="comment">                                               "VALUES ($did, '$application', '$uri');");</span>
00463 <span class="comment">                }</span>
00464 <span class="comment"></span>
00465 <span class="comment">                Document::index($did, $uri);</span>
00466 <span class="comment"></span>
00467 <span class="comment">                return $did;</span>
00468 <span class="comment">        }</span>
00469 <span class="comment"></span>
00480 <span class="comment">        public function documentId($application, $uri)</span>
00481 <span class="comment">        {</span>
00482 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00483 <span class="comment">                {</span>
00484 <span class="comment">                        return false;</span>
00485 <span class="comment">                }</span>
00486 <span class="comment"></span>
00487 <span class="comment">                $application = addslashes($application);</span>
00488 <span class="comment">                $uri         = addslashes($uri);</span>
00489 <span class="comment"></span>
00490 <span class="comment">                $this-&gt;database-&gt;query("SELECT D.did ".</span>
00491 <span class="comment">                                       "FROM Documents D ".</span>
00492 <span class="comment">                                       "WHERE D.app = '$application' AND D.uri = '$uri';");</span>
00493 <span class="comment">                if($this-&gt;database-&gt;hasResults())</span>
00494 <span class="comment">                {</span>
00495 <span class="comment">                        return $this-&gt;database-&gt;result(0, 0);</span>
00496 <span class="comment">                }</span>
00497 <span class="comment">                else</span>
00498 <span class="comment">                {</span>
00499 <span class="comment">                        return false;</span>
00500 <span class="comment">                }</span>
00501 <span class="comment">        }</span>
00502 <span class="comment"></span>
00514 <span class="comment">        public function moveDocument($application, $sourceUri, $destUri)</span>
00515 <span class="comment">        {</span>
00516 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00517 <span class="comment">                {</span>
00518 <span class="comment">                        return;</span>
00519 <span class="comment">                }</span>
00520 <span class="comment"></span>
00521 <span class="comment">                $application = addslashes($application);</span>
00522 <span class="comment">                $sourceUri   = addslashes($sourceUri);</span>
00523 <span class="comment">                $destUri     = addslashes($destUri);</span>
00524 <span class="comment"></span>
00525 <span class="comment">                $this-&gt;database-&gt;query("UPDATE Documents SET uri = '$destUri' ".</span>
00526 <span class="comment">                                       "WHERE app = '$application' AND uri = '$sourceUri';");</span>
00527 <span class="comment">        }</span>
00528 <span class="comment"></span>
00539 <span class="comment">        public function removeDocument($application, $uri)</span>
00540 <span class="comment">        {</span>
00541 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00542 <span class="comment">                {</span>
00543 <span class="comment">                        return;</span>
00544 <span class="comment">                }</span>
00545 <span class="comment"></span>
00546 <span class="comment">                $application = addslashes($application);</span>
00547 <span class="comment">                $uri         = addslashes($uri);</span>
00548 <span class="comment"></span>
00549 <span class="comment">                $this-&gt;database-&gt;query("SELECT D.did ".</span>
00550 <span class="comment">                                       "FROM Documents D ".</span>
00551 <span class="comment">                                       "WHERE D.app = '$application' AND D.uri = '$uri';");</span>
00552 <span class="comment">                if($this-&gt;database-&gt;hasResults())</span>
00553 <span class="comment">                {</span>
00554 <span class="comment">                        $did = $this-&gt;database-&gt;result(0, 0);</span>
00555 <span class="comment">                        $this-&gt;database-&gt;query("DELETE FROM MetaData ".</span>
00556 <span class="comment">                                               "WHERE did = $did;");</span>
00557 <span class="comment">                        $this-&gt;database-&gt;query("DELETE FROM ContainsWord ".</span>
00558 <span class="comment">                                               "WHERE did = $did;");</span>
00559 <span class="comment">                        $this-&gt;database-&gt;query("DELETE FROM Documents ".</span>
00560 <span class="comment">                                               "WHERE did = $did;");</span>
00561 <span class="comment">                }</span>
00562 <span class="comment">        }</span>
00563 <span class="comment"></span>
00583 <span class="comment">        public function indexText($documentId, $text, $weight)</span>
00584 <span class="comment">        {</span>
00585 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00586 <span class="comment">                {</span>
00587 <span class="comment">                        return;</span>
00588 <span class="comment">                }</span>
00589 <span class="comment"></span>
00590 <span class="comment">                $keywords = self::splitKeywords($text);</span>
00591 <span class="comment">                foreach($keywords as $keyword)</span>
00592 <span class="comment">                {</span>
00593 <span class="comment">                        $this-&gt;indexWord($documentId, $keyword, $weight);</span>
00594 <span class="comment">                }</span>
00595 <span class="comment">        }</span>
00596 <span class="comment"></span>
00602 <span class="comment">        private function indexWord($documentId, $word, $weight)</span>
00603 <span class="comment">        {</span>
00604 <span class="comment">                $word = strtolower($word);</span>
00605 <span class="comment"></span>
00606 <span class="comment">                $this-&gt;database-&gt;query("SELECT W.wid ".</span>
00607 <span class="comment">                                       "FROM Words W ".</span>
00608 <span class="comment">                                       "WHERE W.word = '$word';");</span>
00609 <span class="comment">                if($this-&gt;database-&gt;hasResults())</span>
00610 <span class="comment">                {</span>
00611 <span class="comment">                        $wid = $this-&gt;database-&gt;result(0, 0);</span>
00612 <span class="comment">                }</span>
00613 <span class="comment">                else</span>
00614 <span class="comment">                {</span>
00615 <span class="comment">                        $this-&gt;database-&gt;query("SELECT nextval('Words_wid_seq') as key");</span>
00616 <span class="comment">                        $wid = $this-&gt;database-&gt;result(0, 0);</span>
00617 <span class="comment">                        $this-&gt;database-&gt;query("INSERT INTO Words (wid, word) ".</span>
00618 <span class="comment">                                               "VALUES ($wid, '$word');");</span>
00619 <span class="comment">                }</span>
00620 <span class="comment"></span>
00621 <span class="comment">                $this-&gt;database-&gt;query("SELECT C.weight ".</span>
00622 <span class="comment">                                       "FROM ContainsWord C ".</span>
00623 <span class="comment">                                       "WHERE C.did = $documentId AND C.wid = $wid;");</span>
00624 <span class="comment">                if($this-&gt;database-&gt;hasResults())</span>
00625 <span class="comment">                {</span>
00626 <span class="comment">                        $newWeight = $weight + $this-&gt;database-&gt;result(0, 0);</span>
00627 <span class="comment">                        $this-&gt;database-&gt;query("UPDATE ContainsWord SET weight = $newWeight ".</span>
00628 <span class="comment">                                               "WHERE did = $documentId AND wid = $wid;");</span>
00629 <span class="comment">                }</span>
00630 <span class="comment">                else</span>
00631 <span class="comment">                {</span>
00632 <span class="comment">                        $this-&gt;database-&gt;query("INSERT INTO ContainsWord (did, wid, weight) ".</span>
00633 <span class="comment">                                               "VALUES ($documentId, $wid, $weight);");</span>
00634 <span class="comment">                }</span>
00635 <span class="comment">        }</span>
00636 <span class="comment"></span>
00647 <span class="comment">        public function setMetaData($documentId, $key, $value)</span>
00648 <span class="comment">        {</span>
00649 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00650 <span class="comment">                {</span>
00651 <span class="comment">                        return;</span>
00652 <span class="comment">                }</span>
00653 <span class="comment"></span>
00654 <span class="comment">                $key   = addslashes($key);</span>
00655 <span class="comment">                $value = addslashes($value);</span>
00656 <span class="comment"></span>
00657 <span class="comment">                $this-&gt;database-&gt;query("SELECT M.did ".</span>
00658 <span class="comment">                                       "FROM MetaData M ".</span>
00659 <span class="comment">                                       "WHERE M.did = $documentId AND M.key = '$key';");</span>
00660 <span class="comment">                if($this-&gt;database-&gt;hasResults())</span>
00661 <span class="comment">                {</span>
00662 <span class="comment">                        $this-&gt;database-&gt;query("UPDATE MetaData SET value = '$value' ".</span>
00663 <span class="comment">                                               "WHERE did = $documentId AND M.key = '$key';");</span>
00664 <span class="comment">                }</span>
00665 <span class="comment">                else</span>
00666 <span class="comment">                {</span>
00667 <span class="comment">                        $this-&gt;database-&gt;query("INSERT INTO MetaData (did, key, value) ".</span>
00668 <span class="comment">                                               "VALUES ($documentId, '$key', '$value');");</span>
00669 <span class="comment">                }</span>
00670 <span class="comment">        }</span>
00671 <span class="comment"></span>
00681 <span class="comment">        public function unsetMetaData($documentId, $key)</span>
00682 <span class="comment">        {</span>
00683 <span class="comment">                if($this-&gt;gatewaysOnly == true)</span>
00684 <span class="comment">                {</span>
00685 <span class="comment">                        return;</span>
00686 <span class="comment">                }</span>
00687 <span class="comment"></span>
00688 <span class="comment">                $key = addslashes($key);</span>
00689 <span class="comment"></span>
00690 <span class="comment">                $this-&gt;database-&gt;query("DELETE FROM MetaData ".</span>
00691 <span class="comment">                                       "WHERE did = $documentId AND key = '$key';");</span>
00692 <span class="comment">        }</span>
00693 <span class="comment"></span>
00699 <span class="comment">        private static function splitKeywords($keywords)</span>
00700 <span class="comment">        {</span>
00701 <span class="comment">                $keywordsArray = array();</span>
00702 <span class="comment">                $word = '';</span>
00703 <span class="comment">                for($i = 0; $i &lt; strlen($keywords); $i++)</span>
00704 <span class="comment">                {</span>
00705 <span class="comment">                        if(ctype_alnum($keywords{$i}) == false)</span>
00706 <span class="comment">                        {</span>
00707 <span class="comment">                                if($keywords{$i} == '\'' || $keywords{$i} == '-')</span>
00708 <span class="comment">                                {</span>
00709 <span class="comment">                                        continue;</span>
00710 <span class="comment">                                }</span>
00711 <span class="comment">                                if($word != '')</span>
00712 <span class="comment">                                {</span>
00713 <span class="comment">                                        $keywordsArray[] = $word;</span>
00714 <span class="comment">                                        $word = '';</span>
00715 <span class="comment">                                }</span>
00716 <span class="comment">                        }</span>
00717 <span class="comment">                        else</span>
00718 <span class="comment">                        {</span>
00719 <span class="comment">                                $word .= $keywords{$i};</span>
00720 <span class="comment">                        }</span>
00721 <span class="comment">                }</span>
00722 <span class="comment">                if($word != '')</span>
00723 <span class="comment">                {</span>
00724 <span class="comment">                        $keywordsArray[] = $word;</span>
00725 <span class="comment">                        $word = '';</span>
00726 <span class="comment">                }</span>
00727 <span class="comment"></span>
00728 <span class="comment">                return $keywordsArray;</span>
00729 <span class="comment">        }</span>
00730 <span class="comment"></span>
00731 <span class="comment">        private static $instance = null;</span>
00732 <span class="comment">        private $database;</span>
00733 <span class="comment">        private $gateways;</span>
00734 <span class="comment">        private $indexes;</span>
00735 <span class="comment">        private $gatewaysOnly;</span>
00736 <span class="comment">        private $availableInterfaces;</span>
00737 <span class="comment">        private $availableInterfaceNames;</span>
00738 <span class="comment">        private $selectedInterfaces;</span>
00739 <span class="comment">        private $selectedIndexes;</span>
00740 <span class="comment">}</span>
00741 <span class="comment"></span>
00742 <span class="comment">?&gt;</span>
</pre></div>    </div>
  </body>
</html>
Return current item: Aukyla Platform