Author: rick
Date: 2010-03-18 17:06:30 +0100 (Thu, 18 Mar 2010)
New Revision: 28611

Modified:
   plugins/sfCouchPlugin/trunk/lib/sfCouchConnection.class.php
   plugins/sfCouchPlugin/trunk/lib/sfCouchResponse.class.php
   plugins/sfCouchPlugin/trunk/lib/sfCouchView.class.php
Log:


Modified: plugins/sfCouchPlugin/trunk/lib/sfCouchConnection.class.php
===================================================================
--- plugins/sfCouchPlugin/trunk/lib/sfCouchConnection.class.php 2010-03-18 
16:00:06 UTC (rev 28610)
+++ plugins/sfCouchPlugin/trunk/lib/sfCouchConnection.class.php 2010-03-18 
16:06:30 UTC (rev 28611)
@@ -349,7 +349,7 @@
             fwrite( $fp, "\n" . $rawHeaders . "\n" . $body . "\n" );
             fclose( $fp );
         }
-        
+
         // Handle some response state as special cases
         switch ( $headers['status'] )
         {
@@ -359,14 +359,14 @@
             case 307:
                 $path = parse_url( $headers['location'], PHP_URL_PATH );
                 return $this->request( $method, $path, $data );
-               break;
+              break;
             case 404:
-               return null;
-                       break;
+              return null;
+               break;
         }
-        
-        if ($headers['content-type'] == 'application/octet-stream') {
-               return ($body);
+
+        if (substr($headers['content-type'], 0, 5) != 'text/') {
+          return ($body);
         }
 
         // Create repsonse object from couch db response

Modified: plugins/sfCouchPlugin/trunk/lib/sfCouchResponse.class.php
===================================================================
--- plugins/sfCouchPlugin/trunk/lib/sfCouchResponse.class.php   2010-03-18 
16:00:06 UTC (rev 28610)
+++ plugins/sfCouchPlugin/trunk/lib/sfCouchResponse.class.php   2010-03-18 
16:06:30 UTC (rev 28611)
@@ -111,7 +111,7 @@
     public static function parse( array $headers, $body)
     {
         $response = json_decode( $body, true );
-        
+
         // To detect the type of the response from the couch DB server we use
         // the response status which indicates the return type.
         switch ( $headers['status'] )
@@ -146,7 +146,11 @@
                 // All other unhandled HTTP codes are for now handled as an 
error.
                 // This may not be true, as lots of other status code may be 
used
                 // for valid repsonses.
-                throw new exception('sfCouch: ' . $response['error']  . ': ' . 
$response['reason']);
+                if ($response) {
+                  throw new exception('sfCouch: ' . $response['error']  . ': ' 
. $response['reason']);
+                } else {
+                  throw new exception('sfCouch: ' . $body);
+                }
         }
     }
 }
\ No newline at end of file

Modified: plugins/sfCouchPlugin/trunk/lib/sfCouchView.class.php
===================================================================
--- plugins/sfCouchPlugin/trunk/lib/sfCouchView.class.php       2010-03-18 
16:00:06 UTC (rev 28610)
+++ plugins/sfCouchPlugin/trunk/lib/sfCouchView.class.php       2010-03-18 
16:06:30 UTC (rev 28611)
@@ -8,10 +8,11 @@
  */
 class sfCouchView
 {
-       /*
-        * The ID of the design-view
-        */
-       const viewName = '_design/sfCouch';
+  /*
+   * The ID of the design-view
+   */
+  const viewName = '_design/sfCouch';
+  const searchName = '_fti/lucene';
 
     /**
      * Build view query string from options
@@ -39,6 +40,9 @@
                 case 'key':
                 case 'startkey':
                 case 'endkey':
+                case 'analyzer':
+                case 'callback':
+                case 'sort':
                     // These values has to be valid JSON encoded strings, so we
                     // just encode the passed data, whatever it is, as CouchDB
                     // may use complex datatypes as a key, like arrays or
@@ -48,6 +52,7 @@
 
                 case 'startkey_docid':
                 case 'endkey_docid':
+                case 'q':
                     // The startkey and endkey is handled different than the 
other
                     // keys and is just passed as a string, because it always
                     // is and can only be a string.
@@ -67,12 +72,12 @@
                     break;
 
                 case 'stale':
-                       // This can only be 'ok'
-                       if ($value) {
-                               $queryString .= $key . '=ok'; 
-                       }
-                       break;
-                    
+                  // This can only be 'ok'
+                  if ($value) {
+                    $queryString .= $key . '=ok';
+                  }
+                  break;
+
                 case 'skip':
                 case 'group_level':
                     // Theses options accept integers defining the limits of
@@ -98,6 +103,25 @@
     }
 
     /**
+     * Search with couchdb-lucene (http://github.com/rnewson/couchdb-lucene)
+     * the design doc for the searches shall be '_design/lucene'
+     * @param unknown_type $view
+     * @param array $options
+     */
+    public static function search($view, array $options = array()) {
+      $response = null;
+      $url = self::searchName . '/' . $view;
+      $url .= self::buildViewQuery( $options );
+
+      // Get database connection, because we directly execute a query here.
+      $db = sfCouchConnection::getInstance();
+
+      $response = $db->get( $url );
+
+      return $response;
+    }
+
+    /**
      * Query a view
      *
      * Query the specified view to get a set of results. You may optionally use
@@ -111,8 +135,8 @@
      */
     public static function query( $view, array $options = array() )
     {
-       $response = null;
-       
+      $response = null;
+
         // Build query string, just as a normal HTTP GET query string
         $url = self::viewName . '/_view/' . $view;
         $url .= self::buildViewQuery( $options );
@@ -122,9 +146,9 @@
 
         // Always refresh the configuration in debug mode
         if(sfConfig::get('sf_debug')) {
-               self::checkDesignDoc($view);
+          self::checkDesignDoc($view);
         }
-        
+
         try
         {
             // Try to execute query, a failure most probably means, the view
@@ -137,7 +161,7 @@
             // the query again. If it still fails, there is most probably a
             // real problem.
             if (!sfConfig::get('sf_debug') && self::checkDesignDoc($view)) {
-               $response = $db->get($url);
+              $response = $db->get($url);
             }
         }
 
@@ -156,44 +180,44 @@
      */
     public static function checkDesignDoc($checkView = null)
     {
-       $designDoc = new sfCouchDocument(self::viewName);
-       
-       $designDoc->language = 'javascript';
-       
-       // Build the maps/reduces from the files in the config dir
-       $mapDir = sfConfig::get('sf_config_dir').'/couchdb/';
-       $designDoc->views = self::getViewsFromConfig($mapDir);
+      $designDoc = new sfCouchDocument(self::viewName);
 
+      $designDoc->language = 'javascript';
+
+      // Build the maps/reduces from the files in the config dir
+      $mapDir = sfConfig::get('sf_config_dir').'/couchdb/';
+      $designDoc->views = self::getViewsFromConfig($mapDir);
+
         $designDoc->save();
-        
+
         if ($checkView) {
-               if (!array_key_exists($checkView, $designDoc->views)) {
-               throw new sfException("The view '$checkView' doesn't exist. 
-                       Create it in /config/couchdb/".$checkView."_map.js");
+          if (!array_key_exists($checkView, $designDoc->views)) {
+              throw new sfException("The view '$checkView' doesn't exist.
+                Create it in /config/couchdb/".$checkView."_map.js");
             }
         }
-        
+
         return true;
     }
-    
+
     private static function getViewsFromConfig($dir)
     {
-       $views = array();
-       foreach (glob($dir.'*.js') as $fileName) {
-               preg_match('/.*\/(.*)_map.js/', $fileName, $filematches);
-               if (isset($filematches[1])) {
-                       
-                       $viewName = $filematches[1];
-                       $views[$viewName] = array();
-                       $views[$viewName]['map'] = file_get_contents($fileName);
-                       
-                       $reduceFileName = $dir.$viewName.'_reduce.js';
-                       if (file_exists($reduceFileName)) {
-                         $views[$viewName]['reduce'] = 
file_get_contents($reduceFileName);
-                       }
-               }
-       }
-        
-       return $views;
-    } 
+      $views = array();
+      foreach (glob($dir.'*.js') as $fileName) {
+        preg_match('/.*\/(.*)_map.js/', $fileName, $filematches);
+        if (isset($filematches[1])) {
+
+          $viewName = $filematches[1];
+          $views[$viewName] = array();
+          $views[$viewName]['map'] = file_get_contents($fileName);
+
+          $reduceFileName = $dir.$viewName.'_reduce.js';
+            if (file_exists($reduceFileName)) {
+              $views[$viewName]['reduce'] = file_get_contents($reduceFileName);
+            }
+        }
+      }
+
+      return $views;
+    }
 }
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to