Skip to content

Commit

Permalink
[bugfix & refactoring]
Browse files Browse the repository at this point in the history
  • Loading branch information
nibsirahsieu committed Jan 3, 2011
1 parent 2427433 commit 8cdb8a4
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 48 deletions.
8 changes: 0 additions & 8 deletions config/sfLuceneable15BehaviorPlugin.class.php

This file was deleted.

11 changes: 11 additions & 0 deletions config/sfLuceneable15BehaviorPluginConfiguration.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
class sfLuceneable15BehaviorPluginConfiguration extends sfPluginConfiguration
{
public function initialize()
{
if ($this->configuration instanceof sfApplicationConfiguration)
{
sfLuceneableToolkit::registerZend();
}
}
}
42 changes: 41 additions & 1 deletion lib/behavior/LuceneableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class LuceneableBehavior extends Behavior
{
protected function getLuceneFieldMethod($method)
{
$availableMethods = array('keyword' => 'Keyword', 'unidexed' => 'UnIndexed', 'binary' => 'Binary', 'text' => 'Text', 'unstored' => 'UnStored');
$availableMethods = array('keyword' => 'Keyword', 'unindexed' => 'UnIndexed', 'binary' => 'Binary', 'text' => 'Text', 'unstored' => 'UnStored');
$method = strtolower($method);
if (isset($availableMethods[$method])) return $availableMethods[$method];
throw new Exception(sprintf('Unknown Lucene Field method %s', $method));
Expand All @@ -29,6 +29,46 @@ public function postDelete($builder)
return "\$this->deleteLuceneIndex();";
}

public function addStaticStoredIndex()
{
$data = array();
$columns = $this->getParameters();
if (empty($columns))
{
$table = $this->getTable();
foreach ($table->getColumns() as $col) {
$clo = strtolower($col->getName());
$data[$clo] = $col->isPrimaryKey() ? 'Keyword' : 'Unstored';
}
}
else
{
foreach ($columns as $col => $type)
{
$clo = strtolower($col);
$data[$clo] = $this->getLuceneFieldMethod($type);
}
}
return $this->renderTemplate('staticStoredIndex', array(
'data' => $data,
));
}

public function addGetIndexedColumns()
{
return $this->renderTemplate('getIndexedColumns');
}

public function staticAttributes($builder)
{
return $this->addStaticStoredIndex();
}

public function staticMethods($builder)
{
return $this->addGetIndexedColumns();
}

public function objectMethods($builder)
{
$script = '';
Expand Down
11 changes: 11 additions & 0 deletions lib/behavior/templates/getIndexedColumns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

/**
* Returns an array of indexed columns.
* @return array
*/
static public function getIndexedColumns()
{
return self::$_indexedColumns;
}


10 changes: 10 additions & 0 deletions lib/behavior/templates/staticStoredIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/**
* holds an array of indexed columns
*
*/
private static $_indexedColumns = array(
<?php foreach ($data as $column => $method): ?>
'<?php echo $column ?>' => '<?php echo $method ?>',
<?php endforeach; ?>
);
89 changes: 89 additions & 0 deletions lib/sfLuceneModelResults.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
class sfLuceneModelResults implements Iterator, Countable, ArrayAccess
{
protected $model;
protected $results;
protected $pointer = 0;
protected $indexedColums = array();
protected $query = null;

public function __construct($model, $results, $query)
{
$this->model = $model;
$this->results = $results;
$this->query = $query;
$this->indexedColumns = call_user_func(array($model.'Peer', 'getIndexedColumns'));
}

public function current()
{
return $this->hydrate($this->results[$this->pointer]);
}

public function key()
{
return $this->pointer;
}

public function next()
{
$this->pointer++;
}

public function rewind()
{
$this->pointer = 0;
}

public function valid()
{
return isset($this->results[$this->pointer]);
}

public function count()
{
return count($this->results);
}

public function offsetExists($offset)
{
return isset($this->results[$offset]);
}

public function offsetGet($offset)
{
return $this->results[$offset];
}

public function offsetSet($offset, $set)
{
$this->results[$offset] = $set;
}

public function offsetUnset($offset)
{
unset($this->results[$offset]);
}

protected function hydrate($result)
{
$tmp = array();
foreach ($this->indexedColumns as $col => $type)
{
if ($col == 'id') $tmp[$col] = $result->pk;
elseif (strtolower($type) == 'text')
{
$tmp[$col] = $this->query->htmlFragmentHighlightMatches($result->$col);
}
else
{
$tmp[$col] = $result->$col;
}
}
$object = new $this->model;
$object->fromArray($tmp, BasePeer::TYPE_FIELDNAME);
$object->setNew(false);
$object->resetModified();
return $object;
}
}
176 changes: 176 additions & 0 deletions lib/sfLucenePager.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

class sfLucenePager
{
protected $model = null;
protected $results = array();
protected $search = null;
protected $page = 1, $perPage = 5, $nbResults = 0, $lastPage = 0;
protected $maxRecordLimit = false;

public function __construct($model, $search, $perPage = 5)
{
$this->model = $model;
$this->search = $search;
$this->perPage = $perPage;
}

public function init()
{
$hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false);
$maxRecordLimit = $this->getMaxRecordLimit();

$hits = sfLuceneableToolkit::getHits($this->model, $this->search);
$count = count($hits);
$this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count);
if (($this->getPage() == 0 || $this->getMaxPerPage() == 0)) {
$this->setLastPage(0);
} else {
$this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
}
$this->results = $hits;
}

public function getLinks($nb_links = 5)
{
$links = array();
$tmp = $this->getPage() - floor($nb_links / 2);
$check = $this->getLastPage() - $nb_links + 1;
$limit = ($check > 0) ? $check : 1;
$begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1;

$i = $begin;
while (($i < $begin + $nb_links) && ($i <= $this->getLastPage()))
{
$links[] = $i++;
}

return $links;
}

public function haveToPaginate()
{
return (($this->getPage() != 0) && ($this->getNbResults() > $this->getMaxPerPage()));
}

public function getMaxPerPage()
{
return $this->perPage;
}

public function setMaxPerPage($per)
{
$this->perPage = $per;
}

public function setPage($page)
{
$this->page = $page;
}

public function getNbResults()
{
return $this->nbResults;
}

protected function setNbResults($nb)
{
$this->nbResults = $nb;
}

public function getMaxRecordLimit()
{
return $this->maxRecordLimit;
}

public function setMaxRecordLimit($limit)
{
$this->maxRecordLimit = $limit;
}

public function getPage()
{
return $this->page;
}

public function getResults()
{
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();
$limit = $this->getMaxPerPage();
if ($limit == 0)
{
$results = $this->results;
}
$results = array_slice($this->results, $offset, $limit);
return new sfLuceneModelResults($this->model, $results, $this->search);
}

public function getFirstPage()
{
return 1;
}

public function getLastPage()
{
return $this->lastPage;
}

protected function setLastPage($page)
{
$this->lastPage = $page;
if ($this->getPage() > $page) {
$this->setPage($page);
}
}

public function getNextPage()
{
return min($this->getPage() + 1, $this->getLastPage());
}

public function getPreviousPage()
{
return max($this->getPage() - 1, $this->getFirstPage());
}

public function getFirstIndice()
{
if ($this->getPage() == 0)
{
return 1;
}
else
{
return ($this->getPage() - 1) * $this->getMaxPerPage() + 1;
}
}

public function getLastIndice()
{
if ($this->getPage() == 0)
{
return $this->getNbResults();
}
else
{
if (($this->getPage() * $this->getMaxPerPage()) >= $this->getNbResults())
{
return $this->getNbResults();
}
else
{
return ($this->getPage() * $this->getMaxPerPage());
}
}
}

public function isFirstPage()
{
return $this->getPage() == $this->getFirstPage();
}

public function isLastPage()
{
return $this->getPage() == $this->getLastPage();
}
}
19 changes: 19 additions & 0 deletions lib/sfLuceneableToolkit.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,23 @@ public static function createIndex($class)
$object->updateLuceneIndex($index);
}
}

static public function getHits($model, $query)
{
$results = array();
$index = self::getLuceneIndex($model);
try
{
$hits = $index->find($query);
foreach ($hits as $hit)
{
$results[] = $hit;
}
}
catch (Exception $e)
{
$results = array();
}
return $results;
}
}
Loading

0 comments on commit 8cdb8a4

Please sign in to comment.