Development

Changeset 6816

You must first sign up to be able to contribute.

Changeset 6816

Show
Ignore:
Timestamp:
12/29/07 22:29:23 (1 year ago)
Author:
Carl.Vondrick
Message:

sfLucene: improved php-docs and code formatting

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfLucenePlugin/branches/1.1/lib/filter/sfLuceneHighlightFilter.class.php

    r6735 r6816  
    5353        'css'                       => '../sfLucenePlugin/css/search.css', 
    5454        'possible_refers'           => array( 
    55                                         'google'  => array('qs' => 'q', 'name' => 'Google'), 
    56                                         'yahoo'   => array('qs' => 'p', 'name' => 'Yahoo'), 
    57                                         'msn'     => array('qs' => 'q', 'name' => 'MSN'), 
    58                                         'ask'     => array('qs' => 'q', 'name' => 'Ask') 
     55                                        'google'  => array('qs' => 'q',         'name' => 'Google'), 
     56                                        'yahoo'   => array('qs' => 'p',         'name' => 'Yahoo!'), 
     57                                        'msn'     => array('qs' => 'q',         'name' => 'MSN'), 
     58                                        'live'    => array('qs' => 'q',         'name' => 'Live'), 
     59                                        'ask'     => array('qs' => 'q',         'name' => 'Ask'), 
     60                                        'a9'      => array('qs' => 'query',     'name' => 'A9'), 
    5961                                      ) 
    6062      ) 
     
    6769  } 
    6870 
     71  /** 
     72   * Executes the filter 
     73   */ 
    6974  public function execute($filterChain) 
    7075  { 
     
    97102      if (!$this->highlight()) 
    98103      { 
     104        // highlighting did not occur, so remove notice 
    99105        $this->removeNotice(); 
    100106      } 
    101107    } 
    102     catch (sfLuceneHighlighterException $e) 
     108    catch (Exception $e) 
    103109    { 
    104110      $timer->addTime(); 
    105111      throw $e; 
    106112    } 
    107     catch (Exception $e) 
    108     { 
    109       $timer->addTime(); 
    110       throw $e; 
    111     } 
    112113 
    113114    $timer->addTime(); 
    114115  } 
    115116 
     117  /** 
     118   * Attempt to highlight the page 
     119   * @return bool True if highlighting occured, false otherwise 
     120   */ 
    116121  protected function highlight() 
    117122  { 
    118     $terms = $this->getContext()->getRequest()->getParameter( $this->getParameter('highlight_qs')); 
     123    $terms = $this->getContext()->getRequest()->getParameter($this->getParameter('highlight_qs')); 
    119124    $terms = $this->prepareTerms($terms); 
    120125 
     126    // attempt to highlight from sfLucene 
    121127    if (count($terms)) 
    122128    { 
     
    127133      return true; 
    128134    } 
     135    // attempt to highlight from referer (ie, google) 
    129136    elseif ($this->getParameter('check_referer')) 
    130137    { 
    131138      $referer = $this->getContext()->getRequest()->getReferer(); 
    132139 
     140      // continue only if we have a referer 
    133141      if ($referer) 
    134142      { 
     143        // go through each referer and stop once we have a match 
    135144        foreach ($this->getParameter('possible_refers') as $domain => $value) 
    136145        { 
    137           if (preg_match($this->getRefererRegex($domain, $value['qs']), $referer, $matches)) 
     146          $regex = '#^https?://(?:\w+\.)*' . preg_quote($domain, '#') . '(?:\.[a-z]+)+.*' . preg_quote($value['qs'], '#') . '=(.*?)(&|$)#'; 
     147 
     148          // valid referer? 
     149          if (preg_match($regex, $referer, $matches)) 
    138150          { 
     151            // referer match.  highlight! 
     152 
    139153            $terms = $this->prepareTerms($matches[1]); 
    140154 
     
    142156            $this->addCss(); 
    143157            $this->doHighlight($terms); 
     158 
     159            // stop looking for referers now. 
    144160 
    145161            return true; 
     
    149165    } 
    150166 
     167    // we failed to do anything, so return false 
    151168    return false; 
    152169  } 
    153170 
    154   protected function doHighlight($terms) 
     171  /** 
     172   * Highlights the content for $terms 
     173   */ 
     174  protected function doHighlight(array $terms) 
    155175  { 
    156176    $content = $this->getContext()->getResponse()->getContent(); 
    157177 
     178    // configure highlighter 
    158179    $lighter = new sfLuceneHighlighter($content); 
    159180    $lighter->addKeywords($terms); 
     
    164185  } 
    165186 
     187  /** 
     188   * Add the neccessary CSS for the response 
     189   */ 
    166190  protected function addCss() 
    167191  { 
     
    181205  } 
    182206 
     207  /** 
     208   * Prepares terms by exploding them out 
     209   */ 
    183210  protected function prepareTerms($terms) 
    184211  { 
     
    190217  } 
    191218 
     219  /** 
     220   * Removes the notice token from the content because highlighting didn't happen 
     221   */ 
    192222  protected function removeNotice() 
    193223  { 
     
    197227  } 
    198228 
     229  /** 
     230   * Replace the notice with a message that highlighting did occur 
     231   */ 
    199232  protected function addNotice($terms, $from = null) 
    200233  { 
     
    223256  } 
    224257 
     258  /** 
     259   * Helper function to do translations 
     260   */ 
    225261  protected function translate($text, $args) 
    226262  { 
     
    234270    } 
    235271  } 
    236  
    237   protected function getRefererRegex($domain, $qs) 
    238   { 
    239     $domain = preg_quote($domain, '#'); 
    240     $qs = preg_quote($qs, '#'); 
    241  
    242     return '#^https?://(?:\w+\.)*' . $domain . '(?:\.[a-z]+)+.*' . $qs . '=(.*?)(&|$)#'; 
    243   } 
    244272} 
  • plugins/sfLucenePlugin/branches/1.1/lib/form/sfLuceneAdvancedFormBase.class.php

    r6525 r6816  
    2323abstract class sfLuceneAdvancedFormBase extends sfLuceneForm 
    2424{ 
     25  /** 
     26   * This overriden constructor looks useless, but it is important: it specifies 
     27   * not to use a CRSF Secret by default! 
     28   */ 
    2529  public function __construct($defaults = array(), $options = array(), $CSRFSecret = false) 
    2630  { 
     
    2832  } 
    2933 
     34  /** 
     35   * Setup the form.  To overload, you should use ->configure() 
     36   */ 
    3037  public function setup() 
    3138  { 
     
    7178    { 
    7279      $widgetSchema['category'] = new sfWidgetFormSelect(array('choices' => $this->getCategories(), 'multiple' => false)); 
    73  
    7480      $widgetSchema->setLabel('category', 'Must be in category'); 
    7581 
  • plugins/sfLucenePlugin/branches/1.1/lib/form/sfLuceneForm.class.php

    r6525 r6816  
    1212 * sfLuceneSimpleForm instead. 
    1313 * 
    14  * This form represents the simple form that is displayed on the standard search 
    15  * interface. 
    16  * 
    1714 * @package    sfLucenePlugin 
    1815 * @subpackage Form 
     
    2320abstract class sfLuceneForm extends sfForm 
    2421{ 
    25   public function setCategories($categories = array()) 
     22  /** 
     23   * Gives this form these categories 
     24   * @param array $categories The array of categories to assign to this form 
     25   */ 
     26  public function setCategories($categories) 
    2627  { 
    2728    if (!is_array($categories)) 
     
    3031    } 
    3132 
     33    // set categories 
    3234    $this->setOption('categories', $categories); 
    3335 
     36    // we now must reconfigure the form 
    3437    $this->setup(); 
    3538    $this->configure(); 
    3639  } 
    3740 
     41  /** 
     42   * Returns all the categories configured for this form 
     43   */ 
    3844  public function getCategories() 
    3945  { 
     
    4147  } 
    4248 
     49  /** 
     50   * Returns true if the form has categories, false if not 
     51   */ 
    4352  public function hasCategories() 
    4453  { 
  • plugins/sfLucenePlugin/branches/1.1/lib/form/sfLuceneSimpleFormBase.class.php

    r6577 r6816  
    2323abstract class sfLuceneSimpleFormBase extends sfLuceneForm 
    2424{ 
     25  /** 
     26   * This overriden constructor looks useless, but it is important: it specifies 
     27   * not to use a CRSF Secret by default! 
     28   */ 
    2529  public function __construct($defaults = array(), $options = array(), $CSRFSecret = false) 
    2630  { 
     
    2832  } 
    2933 
     34  /** 
     35   * Setup the form.  To overload, you should use ->configure() 
     36   */ 
    3037  public function setup() 
    3138  { 
     
    7178  } 
    7279 
     80  /** 
     81   * Gets the query string for a certain page 
     82   */ 
    7383  public function getQueryString($page = null) 
    7484  { 
  • plugins/sfLucenePlugin/branches/1.1/lib/indexer/sfLuceneIndexer.class.php

    r6689 r6816  
    6767  } 
    6868 
     69  /** 
     70   * Return the context that the search is bound to 
     71   */ 
    6972  protected function getContext() 
    7073  { 
     
    7376 
    7477  /** 
    75   * Searches the index for anything with that guid and will delete it. 
     78  * Searches the index for anything with that guid and will delete it, while 
     79  * taking care to update categories cache. 
     80  * 
    7681  * @param string $guid The guid to search for 
     82  * @return int The number of documents deleted 
    7783  */ 
    7884  protected function deleteGuid($guid) 
     
    8490    } 
    8591 
    86     $term = $this->getLuceneField('index term', 'sfl_guid', $guid ); 
    87  
     92    $term = $this->getLuceneField('index term', 'sfl_guid', $guid); 
    8893    $query = new Zend_Search_Lucene_Search_Query_Term($term); 
    8994 
    9095    $hits = $this->getSearch()->find($query); 
    9196 
     97    // loop through each document that has this guid 
    9298    foreach ($hits as $hit) 
    9399    { 
     100      // build categories that this document has 
    94101      $categories = unserialize($hit->sfl_categories_cache); 
    95102 
     103      // delete each category that this document references 
    96104      foreach ($categories as $category) 
    97105      { 
     
    99107      } 
    100108 
     109      // delete item from index 
    101110      $this->getSearch()->getLucene()->delete($hit->id); 
    102111    } 
    103112 
     113    // commit changes 
    104114    $this->getSearch()->commit(); 
    105115 
     
    108118 
    109119  /** 
    110   * Adds a document 
     120  * Adds a document to the index while attaching a GUID 
    111121  */ 
    112   protected function addDocument($document, $guid) 
     122  protected function addDocument(Zend_Search_Lucene_Document $document, $guid) 
    113123  { 
    114124    $document->addField($this->getLuceneField('keyword', 'sfl_guid', $guid)); 
  • plugins/sfLucenePlugin/branches/1.1/lib/indexer/sfLuceneModelIndexer.class.php

    r6689 r6816  
    8585    return $categories; 
    8686  } 
     87 
     88  /** 
     89   * Configures meta data about the document 
     90   */ 
     91  protected function configureDocumentMetas(Zend_Search_Lucene_Document $doc) 
     92  { 
     93    $doc->addField($this->getLuceneField('unindexed', 'sfl_model', $this->getModelName())); 
     94    $doc->addField($this->getLuceneField('unindexed', 'sfl_type', 'model')); 
     95 
     96    return $doc; 
     97  } 
     98 
     99  /** 
     100   * Configures categories into the document 
     101   */ 
     102  protected function configureDocumentCategories(Zend_Search_Lucene_Document $doc) 
     103  { 
     104    $categories = $this->getModelCategories(); 
     105 
     106    if (count($categories) > 0) 
     107    { 
     108      foreach ($categories as $category) 
     109      { 
     110        $this->addCategory($category); 
     111      } 
     112 
     113      $doc->addField( $this->getLuceneField('text', 'sfl_category', implode(' ', $categories)) ); 
     114    } 
     115 
     116    $doc->addField( $this->getLuceneField('unindexed', 'sfl_categories_cache', serialize($categories)) ); 
     117 
     118    return $doc; 
     119  } 
    87120} 
  • plugins/sfLucenePlugin/branches/1.1/lib/indexer/sfLucenePropelIndexer.class.php

    r6792 r6816  
    1818class sfLucenePropelIndexer extends sfLuceneModelIndexer 
    1919{ 
     20  /** 
     21   * Constructs a new instance 
     22   * @param sfLucene $search The search instance to index to 
     23   * @param BaseObject $instance The model instance to index 
     24   */ 
    2025  public function __construct($search, $instance) 
    2126  { 
     
    3035  /** 
    3136  * Inserts the provided model into the index based off parameters in search.yml. 
    32   * @param BaseObject $this->getModel() The model to insert 
    3337  */ 
    3438  public function insert() 
     
    3741    if (!$this->shouldIndex()) 
    3842    { 
     43      // indexer said to skip indexing 
    3944      $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 'indexer.log', array('Ignoring model "%s" from index with primary key = %s', $this->getModelName(), $this->getModel()->getPrimaryKey()))); 
    4045 
     
    5156    } 
    5257 
     58    // build document 
    5359    $doc = $this->getBaseDocument(); 
    5460    $doc = $this->configureDocumentFields($doc); 
     
    5662    $doc = $this->configureDocumentMetas($doc); 
    5763 
    58     // add document 
     64    // add document to index 
    5965    $this->addDocument($doc, $this->getModelGuid()); 
    6066 
     
    6571    } 
    6672 
     73    // notify about new record 
    6774    $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 'indexer.log', array('Inserted model "%s" from index with primary key = %s', $this->getModelName(), $this->getModel()->getPrimaryKey()))); 
    6875 
     
    7178 
    7279  /** 
    73    * Returns the base document 
     80   * Returns the base document to work with.  Most of the time this will just 
     81   * return an empty Zend_Search_Lucene_Document, but if a callback is specified 
     82   * it will return that. 
    7483   */ 
    7584  protected function getBaseDocument() 
     
    8190    { 
    8291      $cb = $properties->get('callback'); 
    83  
    8492      $doc = $this->getModel()->$cb(); 
    8593 
     
    98106 
    99107  /** 
    100    * Builds the fields into the document as configured by the search.yml file
    101    */ 
    102   protected function configureDocumentFields($doc) 
     108   * Builds the fields into the document as configured by the parameters
     109   */ 
     110  protected function configureDocumentFields(Zend_Search_Lucene_Document $doc) 
    103111  { 
    104112    $properties = $this->getModelProperties(); 
    105113 
     114    // loop through each field 
    106115    foreach ($properties->get('fields')->getNames() as $field) 
    107116    { 
    108117      $field_properties = $properties->get('fields')->get($field); 
    109118 
     119      // build getter by converting from underscore case to camel case 
    110120      $getter = 'get' . sfInflector::camelize($field); 
     121      $value = $this->getModel()->$getter(); 
    111122 
    112123      $type = $field_properties->get('type'); 
    113124      $boost = $field_properties->get('boost'); 
    114125 
    115       $value = $this->getModel()->$getter(); 
    116  
    117       // validate value 
     126      // validate value to make sure we can really index this 
    118127      if (is_object($value) && method_exists($value, '__toString')) 
    119128      { 
     
    126135      elseif (!is_scalar($value)) 
    127136      { 
    128         throw new sfLuceneIndexerException('Field value returned is not a string (got a ' . gettype($value) . ' ) and it could be casted to a string.'); 
     137        throw new sfLuceneIndexerException('Field value returned is not a string (got a ' . gettype($value) . ' ) and it could be casted to a string for field ' . $field); 
    129138      } 
    130139 
    131140      // handle a possible transformation function 
    132       if ($field_properties->get('transform')) 
    133       { 
    134         if (!is_callable($field_properties->get('transform'))) 
    135         { 
    136           throw new sfLuceneIndexerException('Transformation function cannot be called in field "' . $field . '" on model "' . $this->getModelName() . '"'); 
    137         } 
    138  
    139         $value = call_user_func($field_properties->get('transform'), $value); 
     141      if ($transform = $field_properties->get('transform')) 
     142      { 
     143        if (!is_callable($transform)) 
     144        { 
     145          throw new sfLuceneIndexerException('Transformation function ' . $transform . ' does not exist'); 
     146        } 
     147 
     148        $value = call_user_func($transform, $value); 
    140149      } 
    141150 
     
    149158  } 
    150159 
    151   protected function configureDocumentMetas($doc) 
    152   { 
    153     $doc->addField($this->getLuceneField('unindexed', 'sfl_model', $this->getModelName())); 
    154     $doc->addField($this->getLuceneField('unindexed', 'sfl_type', 'model')); 
    155  
    156     return $doc; 
    157   } 
    158  
    159   protected function configureDocumentCategories($doc) 
    160   { 
    161     // category support 
    162     $categories = $this->getModelCategories(); 
    163  
    164     if (count($categories) > 0) 
    165     { 
    166       foreach ($categories as $category) 
    167       { 
    168         $this->addCategory($category); 
    169       } 
    170  
    171       $doc->addField( $this->getLuceneField('text', 'sfl_category', implode(' ', $categories)) ); 
    172     } 
    173  
    174     $doc->addField( $this->getLuceneField('unindexed', 'sfl_categories_cache', serialize($categories)) ); 
    175  
    176     return $doc; 
    177   } 
    178  
    179160  /** 
    180161  * Deletes the old model 
    181   * @param BaseObject $this->getModel() The model to delete 
    182162  */ 
    183163  public function delete() 
     
    199179    $method = $properties->get('validator'); 
    200180 
    201     if ($method && method_exists($this->getModel(), $method)
     181    if ($method
    202182    { 
    203183      return (bool) $this->getModel()->$method(); 
     
    207187  } 
    208188 
     189  /** 
     190   * Returns an array of all the categories that this model is configured for 
     191   */ 
    209192  protected function getModelCategories() 
    210193  { 
    211194    $retval = array(); 
    212195 
     196    // change i18n to this culture 
    213197    if (sfConfig::get('sf_i18n')) 
    214198    { 
     
    220204    foreach (parent::getModelCategories() as $category) 
    221205    { 
     206      // if category fits into syntax "%XXX%" then we must replace it with ->getXXX() on the model 
    222207      if (substr($category, 0, 1) == '%' && substr($category, -1, 1) == '%') 
    223208      { 
     
    225210 
    226211        $getter = 'get' . sfInflector::camelize($category); 
    227  
    228212        $getterValue = $this->getModel()->$getter(); 
    229213 
     214        // attempt to convert value to string 
    230215        if (is_object($getterValue) && method_exists($getterValue, '__toString')) 
    231216        { 
     
    234219        elseif (!is_scalar($getterValue)) 
    235220        { 
    236           throw new sfLuceneIndexerException('Category value returned is not a string (got a ' . gettype($getterValue) . ' ) and could not be transformed into a string.'); 
    237         } 
    238  
     221          throw new sfLuceneIndexerException('Category value returned is not a string (got a ' . gettype($getterValue) . ') and could not be transformed into a string.'); 
     222        } 
     223 
     224        // store value for returning.  as the value comes the model, it is already 
     225        // configured for i18n 
    239226        $retval[] = $getterValue; 
    240227      } 
    241228      else 
    242229      { 
     230        // value did not come from model, so store it using i18n if possible 
     231 
    243232        if (isset($i18n) && $i18n) 
    244233        { 
     
    255244  } 
    256245 
     246  /** 
     247   * Calculates the GUID for the model 
     248   */ 
    257249  public function getModelGuid() 
    258250  { 
  • plugins/sfLucenePlugin/branches/1.1/lib/storage/sfLuceneStorageBlackhole.class.php

    r6392 r6816  
    1010/** 
    1111 * This storage container does not write to the disc, but instead just stores 
    12  * in memory.  This is useful for memory teesting. 
     12 * in memory.  This is useful for unit testing. 
    1313 * @package    sfLucenePlugin 
    1414 * @subpackage Storage