import v2.0.0.0_RC3 | 2012-07-01
https://github.com/lucanos/CommunityID -> http://www.itadmins.net/archives/357
This commit is contained in:
@ -1,84 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
|
||||
* @copyright Copyright (C) 2005-2010 Keyboard Monkeys Ltd. http://www.kb-m.com
|
||||
* @license http://creativecommons.org/licenses/BSD/ BSD Licensese
|
||||
* @author Keyboard Monkeys Ltd.
|
||||
* @since Textroller 0.9
|
||||
* @package TextRoller
|
||||
* @package Monkeys Framework
|
||||
* @packager Keyboard Monkeys
|
||||
*/
|
||||
|
||||
class Monkeys_Lucene
|
||||
abstract class Monkeys_Lucene
|
||||
{
|
||||
const LUCENE_DIR = '/lucene';
|
||||
|
||||
private static $_index;
|
||||
|
||||
/**
|
||||
* @throws Zend_Search_Lucene_exception
|
||||
*/
|
||||
public static function getIndex()
|
||||
{
|
||||
try {
|
||||
$index = Zend_Search_Lucene::open(APP_DIR . self::LUCENE_DIR);
|
||||
} catch (Zend_Search_Lucene_Exception $e) {
|
||||
$index = Zend_Search_Lucene::create(APP_DIR . self::LUCENE_DIR);
|
||||
Zend_Registry::get('logger')->log('Created Lucene index file', Zend_Log::INFO);
|
||||
}
|
||||
|
||||
return $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Zend_Search_Lucene_exception
|
||||
* @return void
|
||||
*/
|
||||
public static function indexArticle(Model_Blog $blog, Zend_Db_Table_Rowset $tagsSet, $isNew)
|
||||
{
|
||||
if ($blog->draft || !$blog->hasBeenPublished()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tags = array();
|
||||
foreach ($tagsSet as $tag) {
|
||||
$tags[] = $tag->tag;
|
||||
}
|
||||
$tags = implode(' ', $tags);
|
||||
|
||||
$index = self::getIndex();
|
||||
|
||||
if (!$isNew) {
|
||||
$existingDocIds = $index->termDocs(new Zend_Search_Lucene_Index_Term($blog->id, 'blog_id'));
|
||||
if ($existingDocIds) {
|
||||
$index->delete($existingDocIds[0]);
|
||||
if (!@self::$_index) {
|
||||
try {
|
||||
self::$_index = Zend_Search_Lucene::open(APP_DIR . self::LUCENE_DIR);
|
||||
} catch (Zend_Search_Lucene_Exception $e) {
|
||||
self::$_index = Zend_Search_Lucene::create(APP_DIR . self::LUCENE_DIR);
|
||||
Zend_Registry::get('logger')->log('Created Lucene index file', Zend_Log::INFO);
|
||||
}
|
||||
}
|
||||
|
||||
// I won't be using Zend_Search_Lucene_Document_HTML 'cause articles are not full HTML documents
|
||||
$doc = new Zend_Search_Lucene_Document();
|
||||
|
||||
$doc->addField(Zend_Search_Lucene_Field::Keyword('blog_id', $blog->id));
|
||||
|
||||
$doc->addField(Zend_Search_Lucene_Field::Text('title', $blog->title, 'utf-8'));
|
||||
$doc->addField(Zend_Search_Lucene_Field::Text('excerpt', $blog->excerpt, 'utf-8'));
|
||||
$doc->addField(Zend_Search_Lucene_Field::Unstored('tag', $tags, 'utf-8'));
|
||||
$doc->addField(Zend_Search_Lucene_Field::Unstored('contents', $blog->getContentWithoutTags(), 'utf-8'));
|
||||
$index->addDocument($doc);
|
||||
$index->commit();
|
||||
}
|
||||
|
||||
public static function unIndexArticle(Blog $blog)
|
||||
{
|
||||
try {
|
||||
$index = self::getIndex();
|
||||
} catch (Zend_Search_Lucene_Exception $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
$existingDocIds = $index->termDocs(new Zend_Search_Lucene_Index_Term($blog->id, 'blog_id'));
|
||||
|
||||
if ($existingDocIds) {
|
||||
$index->delete($existingDocIds[0]);
|
||||
}
|
||||
return self::$_index;
|
||||
}
|
||||
|
||||
public static function optimizeIndex()
|
||||
@ -86,4 +36,59 @@ class Monkeys_Lucene
|
||||
$index = self::getIndex();
|
||||
$index->optimize();
|
||||
}
|
||||
|
||||
public static function checkPcreUtf8Support()
|
||||
{
|
||||
if (@preg_match('/\pL/u', 'a') == 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function clearIndex()
|
||||
{
|
||||
// need to remove the locks, otherwise error under windows
|
||||
self::getIndex()->removeReference();
|
||||
self::$_index = null;
|
||||
|
||||
self::_rmdirr(APP_DIR . self::LUCENE_DIR);
|
||||
}
|
||||
|
||||
/** * Delete a file, or a folder and its contents
|
||||
*
|
||||
* @author Aidan Lister <aidan@php.net>
|
||||
* @version 1.0.1
|
||||
* @param string $dirname Directory to delete
|
||||
* @return bool Returns TRUE on success, FALSE on failure
|
||||
*/
|
||||
private static function _rmdirr($dirname)
|
||||
{
|
||||
// Sanity check
|
||||
if (!file_exists($dirname)) {
|
||||
return false;
|
||||
}
|
||||
// Simple delete for a file
|
||||
if (is_file($dirname)) {
|
||||
return unlink($dirname);
|
||||
}
|
||||
// Loop through the folder
|
||||
$dir = dir($dirname);
|
||||
while (false !== $entry = $dir->read()) {
|
||||
// Skip pointers and dot directories
|
||||
if (substr($entry, 0, 1) == '.') {
|
||||
continue;
|
||||
}
|
||||
// Deep delete directories
|
||||
if (is_dir("$dirname/$entry")) {
|
||||
self::_rmdirr("$dirname/$entry");
|
||||
} else {
|
||||
unlink("$dirname/$entry");
|
||||
}
|
||||
}
|
||||
// Clean up
|
||||
$dir->close();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user