protected function execute($arguments = array(), $options = array())
 {
     $app = $arguments['application'];
     $index = $arguments['index'];
     $culture = $arguments['culture'];
     $model = $options['model'];
     $delete = $options['delete'];
     $this->checkAppExists($app);
     if (sfConfig::get('sf_orm') != 'doctrine') {
         throw new LogicException('This feature is only implemented for Doctrine ORM');
     }
     $start = microtime(true);
     $search = sfLucene::getInstance($index, $culture, $this->configuration);
     $search->optimize();
     $models = $model ? array($model) : array_keys($search->getParameter('models')->getAll());
     foreach ($models as $model) {
         if ($delete) {
             $this->deleteModel($search, $model);
         }
         $this->update($app, $index, $culture, $model, $options);
     }
     $time = microtime(true) - $start;
     $final = $this->formatter->format('Update index done !!', array('fg' => 'green', 'bold' => true));
     $final .= $this->formatter->format(number_format($time, 5), array('fg' => 'cyan')) . ' seconds.';
     $this->dispatcher->notify(new sfEvent($this, 'command.log', array('', $final)));
 }
 public function __construct()
 {
     $this->lucene = sfLucene::getInstance('index', 'en');
     if (!$this->lucene->getSearchService()->ping()) {
         throw new Exception('Solr index server not loaded - please use >symfony lucene:service client start');
     }
     $this->service = $this->lucene->getSearchService();
 }
 /**
  * Returns an instance of Lucene that is supposed to be used for this app.
  */
 public static function getApplicationInstance($culture = null)
 {
     $name = sfConfig::get('app_lucene_index', null);
     if (!$name) {
         $possible = sfLucene::getAllNames();
         $name = current($possible);
     }
     if (!$name) {
         throw new sfLuceneException('A index to use could not be resolved');
     }
     return sfLucene::getInstance($name, $culture);
 }
 protected function execute($arguments = array(), $options = array())
 {
     $app = $arguments['application'];
     $index = $arguments['index'];
     $culture = $arguments['culture'];
     $this->checkAppExists($app);
     $this->standardBootstrap($app, $options['env']);
     $start = microtime(true);
     $instance = sfLucene::getInstance($index, $culture, true);
     $this->setupEventDispatcher($instance);
     $this->rebuild($instance);
     $time = microtime(true) - $start;
     $final = $this->formatter->format('All done!', array('fg' => 'red', 'bold' => true)) . ' Rebuilt for ' . $this->formatter->format(count($instances), array('fg' => 'cyan'));
     $final .= count($instances) == 1 ? ' index in ' : ' indexes in ';
     $final .= $this->formatter->format(number_format($time, 5), array('fg' => 'cyan')) . ' seconds.';
     $this->dispatcher->notify(new sfEvent($this, 'command.log', array('', $final)));
 }
 protected function execute($arguments = array(), $options = array())
 {
     $app = $arguments['application'];
     $index = $arguments['index'];
     $culture = $arguments['culture'];
     $model = $arguments['model'];
     $offset = $options['offset'];
     $limit = $options['limit'];
     $this->checkAppExists($app);
     $this->standardBootstrap($app, $options['env']);
     if (sfConfig::get('sf_orm') != 'doctrine') {
         throw new LogicException('This feature is only implemented for Doctrine ORM');
     }
     sfLucene::initIndex($index, $culture);
     $instance = sfLucene::getInstance($index, $culture, false);
     $this->setupEventDispatcher($instance);
     $this->rebuild($instance, $model, $offset, $limit);
 }
 protected function getSearchInstances($node)
 {
     static $instances;
     $class = get_class($node);
     if (!isset($instances)) {
         $instances = array();
     }
     if (!isset($instances[$class])) {
         $config = sfLucene::getConfig();
         foreach ($config as $name => $item) {
             if (isset($item['models'][$class])) {
                 foreach ($item['index']['cultures'] as $culture) {
                     $instances[$class][] = sfLucene::getInstance($name, $culture);
                 }
             }
         }
     }
     return $instances[$class];
 }
 protected function execute($arguments = array(), $options = array())
 {
     $app = $arguments['application'];
     $index = $arguments['index'];
     $culture = $arguments['culture'];
     $limit = $options['limit'];
     $model = $options['model'];
     $this->checkAppExists($app);
     $this->standardBootstrap($app, $options['env']);
     if (sfConfig::get('sf_orm') != 'doctrine') {
         throw new LogicException('This feature is only implemented for Doctrine ORM');
     }
     $start = microtime(true);
     $search = sfLucene::getInstance($index, $culture, is_null($model));
     $search->optimize();
     $this->setupEventDispatcher($search);
     $models = $search->getParameter('models')->getAll();
     $factory = new sfLuceneIndexerFactory($search);
     $handler = null;
     foreach ($factory->getHandlers() as $handler) {
         if ($handler instanceof sfLuceneModelIndexerHandler) {
             break;
         }
     }
     if (!$handler instanceof sfLuceneModelIndexerHandler) {
         throw new LogicException('No sfLuceneModelIndexerHandler defined !');
     }
     if ($model) {
         $this->update($handler, $app, $index, $culture, $model, $limit);
     } else {
         foreach ($models as $model => $params) {
             $this->update($handler, $app, $index, $culture, $model, $limit);
         }
     }
     $time = microtime(true) - $start;
     $final = $this->formatter->format('Update index done !!', array('fg' => 'green', 'bold' => true));
     $final .= $this->formatter->format(number_format($time, 5), array('fg' => 'cyan')) . ' seconds.';
     $this->dispatcher->notify(new sfEvent($this, 'command.log', array('', $final)));
 }
 protected function execute($arguments = array(), $options = array())
 {
     $app = $arguments['application'];
     $index = $arguments['index'];
     $culture = $arguments['culture'];
     $model = $arguments['model'];
     $state = $options['state'];
     $limit = $options['limit'];
     $page = $options['page'];
     $delete = $options['delete'];
     $this->checkAppExists($app);
     $this->standardBootstrap($app, $options['env']);
     if (sfConfig::get('sf_orm') != 'doctrine') {
         throw new LogicException('This feature is only implemented for Doctrine ORM');
     }
     if ($state) {
         // use state file
         // the state file only contains the last page used and the limit
         $state = $this->getState($model);
         $page = $state['page'];
         $limit = $state['limit'];
         $this->logSection('lucene', sprintf('Loading state page:%s, limit:%s', $page, $limit));
     }
     $this->dispatcher->connect('lucene.indexing_loop', array($this, 'handleMemoryLimitEvent'));
     $instance = sfLucene::getInstance($index, $culture, $this->configuration);
     $this->setupEventDispatcher($instance);
     if ($delete) {
         $query = 'sfl_model:' . $model;
         $instance->getSearchService()->deleteByQuery($query);
         $instance->getSearchService()->commit();
     }
     $this->rebuild($instance, $model, $page, $limit);
     if ($state) {
         $file = $this->getFilestatePath($model);
         $this->getFilesystem()->remove($file);
     }
 }
 /**
  * @see sfTask
  */
 protected function execute($arguments = array(), $options = array())
 {
     $config = sfLucene::getConfig();
     $query = $arguments['query'];
     // check and reduce indexes
     if ($options['index'] !== null) {
         if (!in_array($options['index'], array_keys($config))) {
             throw new Exception('Index %s not exist', $options['index']);
         }
         $config = array($config[$options['index']]);
     }
     // build the lucene criteria
     $criteria = sfLuceneCriteria::newInstance()->addPhrase($query)->setOffset($options['start'])->setLimit($options['limit']);
     // walk over the indexes
     foreach ($config as $index => $indexConfig) {
         // check culture
         $cultures = $indexConfig['index']['cultures'];
         if ($options['culture'] !== null) {
             if (!in_array($options['culture'], $cultures)) {
                 //TODO: change to log error
                 throw new Exception(sprintf('Culture %s is not configurate for index %s', $options['culture'], $index));
             }
             $cultures = array($options['culture']);
         }
         $this->log(sprintf('search for `%s` from %u to %u', $query, $options['start'], $options['limit']));
         // walk over the cultures
         foreach ($cultures as $culture) {
             // get lucene instance and retrieve the results
             $results = sfLucene::getInstance($index, $culture)->friendlyFind($criteria);
             $this->log(sprintf('found %u results in index `%s` with culture `%s`', count($results), $index, $culture));
             foreach ($results as $result) {
                 $this->logSection('result ', sprintf('%s %s (%u%%)', $result->getInternalModel(), $result->getInternalTitle(), $result->getScore()));
             }
         }
     }
 }
function inst()
{
    return sfLuceneCriteria::newInstance(sfLucene::getInstance('testLucene'));
}
<?php

/*
 * This file is part of the sfLucenePlugin package
 * (c) 2007 - 2008 Carl Vondrick <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * @package sfLucenePlugin
 * @subpackage Test
 * @author Carl Vondrick
 * @version SVN: $Id$
 */
require dirname(__FILE__) . '/../../bootstrap/unit.php';
$t = new limeade_test(1, limeade_output::get());
$lucene = sfLucene::getInstance('index', 'en', $app_configuration);
class MockResult extends sfLuceneDocument
{
    public function __construct($a)
    {
    }
}
$doc = new MockResult('a');
try {
    new sfLuceneActionResult($doc, $lucene);
    $t->pass('__construct() can be called');
} catch (Exception $e) {
    $t->fail('__construct() can be called');
}
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
/**
 * @package sfLucenePlugin
 * @subpackage Test
 * @author Carl Vondrick
 * @version SVN: $Id: sfLucenePagerTest.php 7108 2008-01-20 07:44:42Z Carl.Vondrick $
 */
require dirname(__FILE__) . '/../../bootstrap/unit.php';
$t = new limeade_test(30, limeade_output::get());
$limeade = new limeade_sf($t);
$app = $limeade->bootstrap();
$luceneade = new limeade_lucene($limeade);
$luceneade->configure()->clear_sandbox();
$lucene = sfLucene::getInstance('testLucene', 'en');
$t->diag('testing constructor');
try {
    new sfLucenePager('a', $lucene);
    $t->fail('__construct() rejects a non-array');
} catch (Exception $e) {
    $t->pass('__construct() rejects a non-array');
}
try {
    new sfLucenePager(new sfLuceneResults(array(), $lucene));
    $t->pass('__construct() accepts sfLuceneResults');
} catch (Exception $e) {
    $t->fail('__construct() accepts sfLuceneResults');
}
try {
    new sfLucenePager(array(), null);
 protected function getSearchInstances($node)
 {
     $class = get_class($node);
     if (!isset(self::$instances[$class])) {
         $config = sfLucene::getConfig();
         $configuration = sfProjectConfiguration::getActive();
         foreach ($config as $name => $item) {
             $inheritance_class = $this->getInheritanceClass($node, $item);
             if (!$inheritance_class) {
                 throw new sfException('Cannot find the correct inheritance class for the object type : ' . get_class($node));
             }
             if (isset($item['models'][$inheritance_class])) {
                 foreach ($item['index']['cultures'] as $culture) {
                     self::$instances[$class][] = sfLucene::getInstance($name, $culture, $configuration);
                 }
             }
         }
     }
     return self::$instances[$class];
 }
 /**
  * Finds all instances of sfLucene that this model appears in.  This does
  * not return the instance if the model does not exist in it.
  */
 protected function getSearchInstances($node)
 {
     static $instances;
     $class = get_class($node);
     if (!isset($instances)) {
         $instances = array();
     }
     // continue only if we have not already cached the instances for this class
     if (!isset($instances[$class])) {
         $instances[$class] = array();
         $config = sfLucene::getConfig();
         // go through each instance
         foreach ($config as $name => $item) {
             if (isset($item['models'][$class])) {
                 foreach ($item['index']['cultures'] as $culture) {
                     // store instance
                     $instances[$class][] = sfLucene::getInstance($name, $culture, sfProjectConfiguration::getActive());
                 }
             }
         }
     }
     if (count($instances[$class]) == 0) {
         throw new sfLuceneException('No sfLucene instances could be found for "' . $class . '"');
     }
     return $instances[$class];
 }
$root = sfConfig::get('sf_data_dir') . '/index/';
// build valid indexes structure
sfLucene::getInstance('testLucene', 'en')->getLucene();
sfLucene::getInstance('testLucene', 'fr')->getLucene();
// build invalid indexes structures
file_put_contents($root . 'testLucene/en/random_file', 'r@nd()');
mkdir($root . 'testLucene/foo', 0777, true);
file_put_contents($root . 'testLucene/foo/bar', 'foo');
mkdir($root . 'badIndex/en', 0777, true);
file_put_contents($root . 'badIndex/bar', 'foo');
$dirty = sfLuceneToolkit::getDirtyIndexRemains();
$t->ok(in_array($root . 'testLucene/foo', $dirty), '::getDirtyIndexRemains() schedules valid indexes but invalid cultures for deletion');
$t->ok(in_array($root . 'badIndex', $dirty), '::getDirtyIndexRemains() schedules the entire of a bad index for deletion');
$t->ok(!in_array($root . 'testLucene', $dirty), '::getDirtyIndexRemains() did not schedule an entire valid index for deletion');
$t->ok(!in_array($root . 'testLucene/en', $dirty), '::getDirtyIndexRemains() did not schedule a valid index and valid culture for deletion');
$t->ok(!in_array($root . 'testLucene/fr', $dirty), '::getDirtyIndexRemains() did not schedule another valid index and valid culture for deletion');
$t->ok(!in_array($root . 'testLucene/en/random_file', $dirty), '::getDirtyIndexRemains() did not schedule an alien file in a valid index and valid culture for deletion');
$t->diag('testing ::getApplicationInstance');
$t->ok(sfLuceneToolkit::getApplicationInstance('en') === sfLucene::getInstance('testLucene', 'en'), '::getApplicationInstance() guesses the first index with no configuration parameter set');
sfConfig::set('app_lucene_index', 'fooLucene');
$t->ok(sfLuceneToolkit::getApplicationInstance('en') === sfLucene::getInstance('fooLucene', 'en'), '::getApplicationInstance() acknowledges manual override from app.yml');
$limeade->config()->remove('app_lucene_index');
$cswap = $app->cswap($luceneade->config_dir . '/search.yml')->write('<?php $config = array();');
try {
    $e = $t->exception('::getApplicationInstance() fails if search.yml is empty');
    sfLuceneToolkit::getApplicationInstance();
    $e->no();
} catch (Exception $ex) {
    $e->caught($ex);
}
$cswap->restore();
예제 #16
0
    $t->ok($new->getParameter('is_new'), 'property "is_new" is true on a new index');
    $t->is($new->numDocs(), 0, '->numDocs() indicates index is empty');
} else {
    $t->skip('index has new status new status on new index');
    $t->skip('->numDocs() indicates index is empty');
}
$t->diag('testing ::getAllInstances()');
try {
    $t->no_exception('::getAllInstance() executes without exception');
    $instances = sfLucene::getAllInstances();
    $e->no();
} catch (Exception $ex) {
    $instances = array();
    $e->caught($ex);
}
$t->is_deeply($instances, array(sfLucene::getInstance('testLucene', 'en'), sfLucene::getInstance('testLucene', 'fr'), sfLucene::getInstance('fooLucene', 'en')), '::getAllInstances() returns all instances');
$t->is_deeply(sfLucene::getAllNames(), array('testLucene', 'fooLucene'), '::getAllNames() returns all configured names');
$t->diag('testing ->loadConfig()');
$h = $lucene->getParameterHolder();
$t->isa_ok($h, 'sfParameterHolder', '->getParameterHolder() returns a parameter holder');
$t->is($h->get('name'), 'testLucene', 'property "name" is the name of the index');
$t->is($h->get('culture'), 'en', 'property "culture" is the culture of the index');
$t->is($h->get('enabled_cultures'), array('en', 'fr'), 'property "enabled_cultures" contains all enabled cultures');
$t->like($h->get('index_location'), '#/index/testLucene/en$#', 'property "index_location" is the correct path');
$t->is($h->get('encoding'), 'UTF-8', 'property "encoding" is the encoding');
$t->is($h->get('stop_words'), array('and', 'the'), 'property "stop_words" contains the stop words');
$t->is($h->get('short_words'), 2, 'property "short_words" is the short word limit');
$t->is($h->get('mb_string'), true, 'property "mb_string" indicates if to use mb_string functions');
$t->isa_ok($h->get('models'), 'sfParameterHolder', 'property "models" is a sfParameterHolder');
$t->isa_ok($h->get('models')->get('FakeForum'), 'sfParameterHolder', 'properties of "models" are sfParameterHolders');
$m = $h->get('models')->get('FakeForum');
    {
        return new MockDocument();
    }
}
class MockDocument
{
    public function getFieldValue($field)
    {
        if ($field == 'sfl_type') {
            return 'regular';
        }
        throw new Exception('d');
    }
}
$data = array(new MockResult('foo'), new MockResult('bar'), new MockResult('baz'));
$search = sfLucene::getInstance('testLucene');
$results = new sfLuceneResults($data, $search);
$t->diag('testing ->getSearch(), ->toArray()');
$t->is($results->getSearch(), $search, '->getSearch() returns the same search instance');
$t->is($results->toArray(), $data, '->toArray() returns the same search data');
$t->diag('testing Iterator interface');
$got = array();
$once = false;
foreach ($results as $key => $value) {
    if (!$once) {
        $t->ok($value instanceof sfLuceneResult, 'iterator interface returns instances of sfLuceneResult');
        $once = true;
    }
    $got[$key] = $value->getResult();
}
$t->is($got, $data, 'sfLuceneResults implements the Iterator interface');
$behavior->preDelete($m1);
$t->is(count($behavior->_getDeleteQueue()), 0, '::setLock() disables the delete queue');
$behavior->clear();
sfLucenePropelBehavior::setLock(false);
$behavior->preSave($m1);
$t->is(count($behavior->_getSaveQueue()), 1, '::setLock() enables the save queue');
$behavior->preDelete($m1);
$t->is(count($behavior->_getDeleteQueue()), 1, '::setLock() enables the delete queue');
$behavior->clear();
foreach (array($m1, $m2, $m3, $m3) as $m) {
    $indexer = new sfLucenePropelIndexer(sfLucene::getInstance('testLucene', 'en'), $m);
    $indexer->delete();
}
$t->diag('testing ->insertIndex()');
$en = sfLucene::getInstance('testLucene', 'en');
$fr = sfLucene::getInstance('testLucene', 'fr');
foreach (array('en', 'fr') as $cult) {
    $indexer = new sfLucenePropelIndexer(${$cult}, $m1);
    $indexer->delete();
}
$behavior->insertIndex($m1);
$en->commit();
$fr->commit();
$t->is($en->numDocs(), 1, '->insertIndex() added model to first index it appears in');
$t->is($fr->numDocs(), 1, '->insertIndex() added model to second index it appears in');
$t->diag('testing ->deleteIndex();');
$behavior->deleteIndex($m1);
$en->commit();
$fr->commit();
$t->is($en->numDocs(), 0, '->deleteIndex() deleted model from first index it appears in');
$t->is($fr->numDocs(), 0, '->deleteIndex() deleted model from second index it appears in');