Version 4.0.13.1
This commit is contained in:
1 parent
f9e857d255
commit
c6a86f0d9b
7517 files changed
+597400
-668041
No files matched your search
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Abstract Search Index
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Search Index
|
||||
*/
|
||||
abstract class _Index extends \IPS\Patterns\Singleton
|
||||
{
|
||||
/**
|
||||
* @brief Singleton Instances
|
||||
*/
|
||||
protected static $instance = NULL;
|
||||
|
||||
/**
|
||||
* Get instance
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function i()
|
||||
{
|
||||
if( static::$instance === NULL )
|
||||
{
|
||||
$classname = '\\IPS\\Content\\Search\\' . ucfirst( \IPS\Settings::i()->search_engine ) . '\\Index';
|
||||
static::$instance = new $classname;
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear and rebuild search index
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rebuild()
|
||||
{
|
||||
/* Delete everything currently in it */
|
||||
$this->prune();
|
||||
|
||||
/* If the queue is already running, clear it out */
|
||||
\IPS\Db::i()->delete( 'core_queue', array( "`key`=?", 'RebuildSearchIndex' ) );
|
||||
|
||||
/* And set the queue in motion to rebuild */
|
||||
foreach ( \IPS\Content::routedClasses( FALSE ) as $class )
|
||||
{
|
||||
try
|
||||
{
|
||||
if( is_subclass_of( $class, 'IPS\Content\Searchable' ) )
|
||||
{
|
||||
\IPS\Task::queue( 'core', 'RebuildSearchIndex', array( 'class' => $class ) );
|
||||
}
|
||||
}
|
||||
catch( \OutOfRangeException $ex ) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get index data
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to add
|
||||
* @return array|NULL
|
||||
*/
|
||||
public function indexData( \IPS\Content\Searchable $object )
|
||||
{
|
||||
/* Init */
|
||||
$class = get_class( $object );
|
||||
$idColumn = $class::$databaseColumnId;
|
||||
$tags = ( $object instanceof \IPS\Content\Tags ) ? implode( ',', array_filter( array_merge( array( $object->prefix() ), $object->tags() ) ) ) : NULL;
|
||||
|
||||
/* If this is an item where the first comment is required, don't index because the comment will serve as both */
|
||||
if ( $object instanceof \IPS\Content\Item and $class::$firstCommentRequired )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Don't index if this is an item to be published in the future */
|
||||
if ( $object->isFutureDate() )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Or if this *is* the first comment, add the title and replace the tags */
|
||||
$title = $object->mapped('title');
|
||||
$isForItem = FALSE;
|
||||
if ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$itemClass = $class::$itemClass;
|
||||
if ( $itemClass::$firstCommentRequired and $object->isFirst() )
|
||||
{
|
||||
$title = $object->item()->mapped('title');
|
||||
$tags = ( $object->item() instanceof \IPS\Content\Tags ) ? implode( ',', array_filter( array_merge( array( $object->item()->prefix() ), $object->item()->tags() ) ) ) : NULL;
|
||||
$isForItem = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the last updated date */
|
||||
$dateUpdated = ( $object instanceof \IPS\Content\Item ) ? $object->mapped('last_comment') : $object->mapped('edit_time');
|
||||
|
||||
/* Take the HTML out of the content */
|
||||
$content = trim( strip_tags( preg_replace( "#<blockquote(.*?)>(.*)</blockquote>#ui", "", preg_replace( "#<script(.*?)>(.*)</script>#ui", "", ' ' . $object->mapped('content') . ' ' ) ) ) );
|
||||
|
||||
/* Work out the hidden status */
|
||||
$hiddenStatus = $object->hidden();
|
||||
if ( $hiddenStatus === 0 and method_exists( $object, 'item' ) and $object->item()->hidden() )
|
||||
{
|
||||
$hiddenStatus = $isForItem ? 1 : 2;
|
||||
}
|
||||
|
||||
if ( $hiddenStatus !== 0 and method_exists( $object, 'item' ) and $object->item()->isFutureDate() )
|
||||
{
|
||||
$hiddenStatus = 0;
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return array(
|
||||
'index_class' => $class,
|
||||
'index_object_id' => $object->$idColumn,
|
||||
'index_item_id' => ( $object instanceof \IPS\Content\Item ) ? $object->$idColumn : $object->mapped('item'),
|
||||
'index_container_id' => ( $object instanceof \IPS\Content\Item ) ? (int) $object->mapped('container') : (int) $object->item()->mapped('container'),
|
||||
'index_title' => $title,
|
||||
'index_content' => $content,
|
||||
'index_permissions' => $object->searchIndexPermissions(),
|
||||
'index_date_created' => $object->mapped('date'),
|
||||
'index_date_updated' => $dateUpdated ?: $object->mapped('date'),
|
||||
'index_author' => (int) $object->mapped('author'),
|
||||
'index_tags' => $tags,
|
||||
'index_hidden' => $hiddenStatus
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Index an item
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to add
|
||||
* @return void
|
||||
*/
|
||||
abstract public function index( \IPS\Content\Searchable $object );
|
||||
|
||||
/**
|
||||
* Remove item
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to remove
|
||||
* @return void
|
||||
*/
|
||||
abstract public function removeFromSearchIndex( \IPS\Content\Searchable $object );
|
||||
|
||||
/**
|
||||
* Removes all content for a classs
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $containerId The container ID to update, or NULL
|
||||
* @return void
|
||||
*/
|
||||
abstract public function removeClassFromSearchIndex( $class, $containerId=NULL );
|
||||
|
||||
/**
|
||||
* Removes all content for a specific application from the index (for example, when uninstalling).
|
||||
*
|
||||
* @param \IPS\Application $application The application
|
||||
* @return void
|
||||
*/
|
||||
public function removeApplicationContent( \IPS\Application $application )
|
||||
{
|
||||
foreach ( $application->extensions( 'core', 'ContentRouter' ) as $router )
|
||||
{
|
||||
foreach( $router->classes AS $class )
|
||||
{
|
||||
if ( is_subclass_of( $class, 'IPS\Content\Searchable' ) )
|
||||
{
|
||||
$this->removeClassFromSearchIndex( $class );
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
if ( is_subclass_of( $commentClass, 'IPS\Content\Searchable' ) )
|
||||
{
|
||||
$this->removeClassFromSearchIndex( $commentClass );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
if ( is_subclass_of( $reviewClass, 'IPS\Content\Searchable' ) )
|
||||
{
|
||||
$this->removeClassFromSearchIndex( $reviewClass );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mass Update (when permissions change, for example)
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $containerId The container ID to update, or NULL
|
||||
* @param int|NULL $itemId The item ID to update, or NULL
|
||||
* @param string|NULL $newPermissions New permissions (if applicable)
|
||||
* @param int|NULL $newHiddenStatus New hidden status (if applicable) special value 2 can be used to indicate hidden only by parent
|
||||
* @param int|NULL $newContainer New container ID (if applicable)
|
||||
* @return void
|
||||
*/
|
||||
abstract public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL );
|
||||
|
||||
/**
|
||||
* Prune search index
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $cutoff The date to delete index records from, or NULL to delete all
|
||||
* @return void
|
||||
*/
|
||||
abstract public function prune( \IPS\DateTime $cutoff = NULL );
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief MySQL Search Index
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search\Mysql;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL Search Index
|
||||
*/
|
||||
class _Index extends \IPS\Content\Search\Index
|
||||
{
|
||||
/**
|
||||
* Index an item
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to add
|
||||
* @return void
|
||||
*/
|
||||
public function index( \IPS\Content\Searchable $object )
|
||||
{
|
||||
$indexData = $this->indexData( $object );
|
||||
if( $indexData )
|
||||
{
|
||||
if ( !$indexData['index_permissions'] )
|
||||
{
|
||||
$this->removeFromSearchIndex( $object );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Db::i()->replace( 'core_search_index', $indexData );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove item
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to remove
|
||||
* @return void
|
||||
*/
|
||||
public function removeFromSearchIndex( \IPS\Content\Searchable $object )
|
||||
{
|
||||
$class = get_class( $object );
|
||||
$idColumn = $class::$databaseColumnId;
|
||||
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? AND index_object_id=?', $class, $object->$idColumn ) );
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? AND index_item_id=?', $commentClass, $object->$idColumn ) );
|
||||
}
|
||||
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? AND index_item_id=?', $reviewClass, $object->$idColumn ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all content for a classs
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $containerId The container ID to update, or NULL
|
||||
* @return void
|
||||
*/
|
||||
public function removeClassFromSearchIndex( $class, $containerId=NULL )
|
||||
{
|
||||
$where = array( array( 'index_class=?', $class ) );
|
||||
if ( $containerId !== NULL )
|
||||
{
|
||||
$where[] = array( 'index_container_id=?', $containerId );
|
||||
}
|
||||
|
||||
\IPS\Db::i()->delete( 'core_search_index', $where );
|
||||
}
|
||||
|
||||
/**
|
||||
* Mass Update (when permissions change, for example)
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $containerId The container ID to update, or NULL
|
||||
* @param int|NULL $itemId The item ID to update, or NULL
|
||||
* @param string|NULL $newPermissions New permissions (if applicable)
|
||||
* @param int|NULL $newHiddenStatus New hidden status (if applicable) special value 2 can be used to indicate hidden only by parent
|
||||
* @param int|NULL $newContainer New container ID (if applicable)
|
||||
* @return void
|
||||
*/
|
||||
public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL )
|
||||
{
|
||||
$where = array( array( 'index_class=?', $class ) );
|
||||
if ( $containerId !== NULL )
|
||||
{
|
||||
$where[] = array( 'index_container_id=?', $containerId );
|
||||
}
|
||||
if ( $itemId !== NULL )
|
||||
{
|
||||
$where[] = array( 'index_item_id=?', $itemId );
|
||||
}
|
||||
|
||||
$update = array();
|
||||
if ( $newPermissions !== NULL )
|
||||
{
|
||||
$update['index_permissions'] = $newPermissions;
|
||||
}
|
||||
if ( $newHiddenStatus !== NULL )
|
||||
{
|
||||
if ( $newHiddenStatus === 2 )
|
||||
{
|
||||
$where[] = array( 'index_hidden=0' );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( 'index_hidden=2' );
|
||||
}
|
||||
|
||||
$update['index_hidden'] = $newHiddenStatus;
|
||||
}
|
||||
if ( $newContainer )
|
||||
{
|
||||
$update['index_container_id'] = $newContainer;
|
||||
}
|
||||
|
||||
\IPS\Db::i()->update( 'core_search_index', $update, $where );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prune search index
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $cutoff The date to delete index records from, or NULL to delete all
|
||||
* @return void
|
||||
*/
|
||||
public function prune( \IPS\DateTime $cutoff = NULL )
|
||||
{
|
||||
if ( $cutoff )
|
||||
{
|
||||
\IPS\DB::i()->delete( 'core_search_index', array( 'index_date_updated < ?', $cutoff->getTimestamp() ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\DB::i()->delete( 'core_search_index' );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,412 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief MySQL Search Query
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search\Mysql;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL Search Query
|
||||
*/
|
||||
class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
/**
|
||||
* @brief The WHERE clause
|
||||
*/
|
||||
protected $where = array();
|
||||
|
||||
/**
|
||||
* @brief The WHERE clause for hidden/unhidden
|
||||
*/
|
||||
protected $hiddenClause = NULL;
|
||||
|
||||
/**
|
||||
* @brief The offset
|
||||
*/
|
||||
protected $offset = 0;
|
||||
|
||||
/**
|
||||
* @brief The ORDER BY clause
|
||||
*/
|
||||
protected $order = NULL;
|
||||
|
||||
/**
|
||||
* @brief Joins
|
||||
*/
|
||||
protected $joins = array();
|
||||
|
||||
/**
|
||||
* Filter by content type
|
||||
*
|
||||
* @param string $class The type of content to search (including all comment/review classes)
|
||||
* @param array|NULL $containers An array of container IDs to filter by, or NULL to not filter by containers
|
||||
* @param int|NULL $minimumComments The minimum number of comments
|
||||
* @param int|NULL $minimumReviews The minimum number of reviews
|
||||
* @param int|NULL $minimumViews The minimum number of views
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByContent( $class, $containers = NULL, $minimumComments = NULL, $minimumReviews = NULL, $minimumViews = NULL )
|
||||
{
|
||||
$classes = array( $class );
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$classes[] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$classes[] = $class::$reviewClass;
|
||||
}
|
||||
$this->where[] = array( \IPS\Db::i()->in( 'index_class', $classes ) );
|
||||
|
||||
if ( $containers !== NULL )
|
||||
{
|
||||
$this->where[] = array( \IPS\Db::i()->in( 'index_container_id', $containers ) );
|
||||
}
|
||||
|
||||
if ( $minimumComments !== NULL or $minimumReviews !== NULL or $minimumViews !== NULL )
|
||||
{
|
||||
$this->joins[ $class::$databaseTable ] = array( $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnId . '=core_search_index.index_item_id' );
|
||||
|
||||
if ( $minimumComments !== NULL )
|
||||
{
|
||||
$this->where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['num_comments'] . '>=?', $minimumComments );
|
||||
}
|
||||
if ( $minimumReviews !== NULL )
|
||||
{
|
||||
$this->where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['num_reviews'] . '>=?', $minimumReviews );
|
||||
}
|
||||
if ( $minimumViews !== NULL )
|
||||
{
|
||||
$this->where[] = array( $class::$databasePrefix . $class::$databaseColumnMap['views'] . '>=?', $minimumViews );
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content type to exclude
|
||||
*
|
||||
* @param array $classes The types of content to exclude (not including all comment/review classes)
|
||||
* @param bool $allowedContiners If TRUE, $class should be an array of allowed containers for that class - e.g. array( 'IPS\forums\Topic' => array( 1, 2 ) - otherwise, a simple array - e.g. array( 'IPS\forums\Topic' )
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByExcludeContent( array $classes, $allowedContiners=FALSE )
|
||||
{
|
||||
if ( $allowedContiners )
|
||||
{
|
||||
$where = array();
|
||||
$params = array();
|
||||
$excludeAll = array();
|
||||
foreach ( $classes as $class => $allowed )
|
||||
{
|
||||
if ( count( $allowed ) )
|
||||
{
|
||||
$where[] = '( index_class<>? OR ' . \IPS\Db::i()->in( 'index_container_id', $allowed ) . ' )';
|
||||
$params[] = $class;
|
||||
}
|
||||
else
|
||||
{
|
||||
$excludeAll[] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
$finalWhere = array();
|
||||
if ( count( $excludeAll ) )
|
||||
{
|
||||
$finalWhere[] = \IPS\Db::i()->in( 'index_class', $excludeAll, TRUE );
|
||||
}
|
||||
if ( count( $where ) )
|
||||
{
|
||||
$finalWhere[] = implode( ' OR ', $where );
|
||||
}
|
||||
|
||||
$this->where[] = array_merge( array( implode( ' AND ', $finalWhere ) ), $params );
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->where[] = array( \IPS\Db::i()->in( 'index_class', $classes, TRUE ) );
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content item
|
||||
*
|
||||
* @param \IPS\Content\Item $item The item
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByItem( \IPS\Content\Item $item )
|
||||
{
|
||||
$classes = array();
|
||||
if ( isset( $item::$commentClass ) )
|
||||
{
|
||||
$classes[] = $item::$commentClass;
|
||||
}
|
||||
if ( isset( $item::$reviewClass ) )
|
||||
{
|
||||
$classes[] = $item::$reviewClass;
|
||||
}
|
||||
|
||||
$this->where[] = array( \IPS\Db::i()->in( 'index_class', $classes ) );
|
||||
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
$this->where[] = array( 'index_item_id=?', $item->$idColumn );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by author
|
||||
*
|
||||
* @param \IPS\Member $author The author
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByAuthor( \IPS\Member $author )
|
||||
{
|
||||
$this->where[] = array( 'index_author=?', $author->member_id );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by start date
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $start The start date (only results AFTER this date will be returned)
|
||||
* @param \IPS\DateTime|NULL $end The end date (only results BEFORE this date will be returned)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByCreateDate( \IPS\DateTime $start = NULL, \IPS\DateTime $end = NULL )
|
||||
{
|
||||
if ( $start )
|
||||
{
|
||||
$this->where[] = array( 'index_date_created>?', $start->getTimestamp() );
|
||||
}
|
||||
if ( $end )
|
||||
{
|
||||
$this->where[] = array( 'index_date_created<?', $end->getTimestamp() );
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by last updated date
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $start The start date (only results AFTER this date will be returned)
|
||||
* @param \IPS\DateTime|NULL $end The end date (only results BEFORE this date will be returned)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByLastUpdatedDate( \IPS\DateTime $start = NULL, \IPS\DateTime $end = NULL )
|
||||
{
|
||||
if ( $start )
|
||||
{
|
||||
$this->where[] = array( 'index_date_updated>?', $start->getTimestamp() );
|
||||
}
|
||||
if ( $end )
|
||||
{
|
||||
$this->where[] = array( 'index_date_updated<?', $end->getTimestamp() );
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set hidden status
|
||||
*
|
||||
* @param int|array $statuses The statuses (array of HIDDEN_ constants)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setHiddenFilter( $statuses )
|
||||
{
|
||||
if ( is_null( $statuses ) )
|
||||
{
|
||||
$this->hiddenClause = NULL;
|
||||
}
|
||||
if ( is_array( $statuses ) )
|
||||
{
|
||||
$this->hiddenClause = array( \IPS\Db::i()->in( 'index_hidden', $statuses ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->hiddenClause = array( 'index_hidden=?', $statuses );
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set page
|
||||
*
|
||||
* @param int $page The page number
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setPage( $page )
|
||||
{
|
||||
$this->offset = ( $page - 1 ) * $this->resultsToGet;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set order
|
||||
*
|
||||
* @param int $order Order (see ORDER_ constants)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setOrder( $order )
|
||||
{
|
||||
switch ( $order )
|
||||
{
|
||||
case static::ORDER_NEWEST_UPDATED:
|
||||
$this->order = 'index_date_updated DESC';
|
||||
break;
|
||||
|
||||
case static::ORDER_NEWEST_CREATED:
|
||||
$this->order = 'index_date_created DESC';
|
||||
break;
|
||||
|
||||
case static::ORDER_RELEVANCY:
|
||||
$this->order = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build where
|
||||
*
|
||||
* @param string|null $term The term to search for
|
||||
* @param array|null $tags The tags to search for
|
||||
* @param int $method \IPS\Content\Search\Index::i()->TERM_OR_TAGS or \IPS\Content\Search\Index::i()->TERM_AND_TAGS
|
||||
* @return array
|
||||
*/
|
||||
protected function _searchWhereClause( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
/* Do we have a term? */
|
||||
$where = array();
|
||||
if ( $term !== NULL )
|
||||
{
|
||||
if( mb_strpos( $term, ' ' ) !== FALSE AND mb_strpos( $term, '+' ) === FALSE AND mb_strpos( $term, '-' ) === FALSE AND mb_strpos( $term, '"' ) === FALSE )
|
||||
{
|
||||
$terms = explode( ' ', $term );
|
||||
$newTerms = array();
|
||||
|
||||
foreach( $terms as $_term )
|
||||
{
|
||||
if( mb_strlen( $_term ) > 2 )
|
||||
{
|
||||
$newTerms[] = "+" . $_term;
|
||||
}
|
||||
else
|
||||
{
|
||||
$newTerms[] = $_term;
|
||||
}
|
||||
}
|
||||
|
||||
$term = implode( ' ', $newTerms );
|
||||
}
|
||||
|
||||
/* Try to guess what mode will be best for this query. If it looks like a MySQL boolean mode search, use that */
|
||||
if ( mb_substr( $term, 0, 1 ) === '+' or mb_substr( $term, -1, 1 ) === '*' or preg_match( '/^".*"$/', $term ) )
|
||||
{
|
||||
$mode = 'IN BOOLEAN MODE';
|
||||
}
|
||||
/* Otherwise, we're just using the default mode - we tried using query expansion for short queries, but it didn't work well (lots of irrelevant results) */
|
||||
else
|
||||
{
|
||||
$mode = '';
|
||||
}
|
||||
|
||||
/* If we also have tags, create a combined where */
|
||||
if ( $tags !== NULL )
|
||||
{
|
||||
if ( $method === static::TERM_OR_TAGS )
|
||||
{
|
||||
$where[] = array( "( MATCH(index_content,index_title) AGAINST (? {$mode}) OR " . \IPS\Db::i()->findInSet( 'index_tags', $tags ) . ' )', $term );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( "MATCH(index_content,index_title) AGAINST (? {$mode})", $term );
|
||||
$where[] = array( \IPS\Db::i()->findInSet( 'index_tags', $tags ) );
|
||||
}
|
||||
}
|
||||
/* Or just use the term */
|
||||
else
|
||||
{
|
||||
$where[] = array( "MATCH(index_content,index_title) AGAINST (? {$mode})", $term );
|
||||
}
|
||||
}
|
||||
/* Or do we have tags? */
|
||||
elseif ( $tags !== NULL )
|
||||
{
|
||||
//$this->where[] = array( \IPS\Db::i()->findInSet( 'index_tags', $tags ) );
|
||||
|
||||
$tagWhere = array();
|
||||
$params = array();
|
||||
|
||||
foreach( $tags as $tag )
|
||||
{
|
||||
array_push( $params, $tag, $tag, $tag, $tag );
|
||||
$tagWhere[] = "( index_tags=? OR index_tags LIKE CONCAT( ?, ',%' ) OR index_tags LIKE CONCAT( '%,', ? ) OR index_tags LIKE CONCAT( '%,', ?, ',%' ) )";
|
||||
}
|
||||
|
||||
$where[] = array_merge( array( '(' . implode( ' OR ', $tagWhere ) . ')' ), $params );
|
||||
}
|
||||
|
||||
/* Only get stuff we have permission for */
|
||||
$where[] = array( "( index_permissions = '*' OR " . \IPS\Db::i()->findInSet( 'index_permissions', $this->permissionArray() ) . ' )' );
|
||||
if ( $this->hiddenClause )
|
||||
{
|
||||
$where[] = $this->hiddenClause;
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* @param string|null $term The term to search for
|
||||
* @param array|null $tags The tags to search for
|
||||
* @param int $method \IPS\Content\Search\Index::i()->TERM_OR_TAGS or \IPS\Content\Search\Index::i()->TERM_AND_TAGS
|
||||
* @return \IPS\Content\Search\Results
|
||||
*/
|
||||
public function search( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
$query = \IPS\Db::i()->select( '*', 'core_search_index', array_merge( $this->where, $this->_searchWhereClause( $term, $tags, $method ) ), $this->order, array( $this->offset, $this->resultsToGet ), NULL, NULL, \IPS\Db::SELECT_SQL_CALC_FOUND_ROWS );
|
||||
foreach ( $this->joins as $table => $where )
|
||||
{
|
||||
$query->join( $table, $where );
|
||||
}
|
||||
|
||||
$count = $query->count( TRUE );
|
||||
return new \IPS\Content\Search\Results( iterator_to_array( $query ), $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count
|
||||
*
|
||||
* @param string|null $term The term to search for
|
||||
* @param array|null $tags The tags to search for
|
||||
* @param int $method \IPS\Content\Search\Index::i()->TERM_OR_TAGS or \IPS\Content\Search\Index::i()->TERM_AND_TAGS
|
||||
* @return int
|
||||
*/
|
||||
public function count( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
return \IPS\Db::i()->select( 'COUNT(*)', 'core_search_index', array_merge( $this->where, $this->_searchWhereClause( $term, $tags, $method ) ) )->first();
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
@@ -0,0 +1,282 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Abstract Search Query
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract Search Query
|
||||
*/
|
||||
abstract class _Query
|
||||
{
|
||||
const TERM_OR_TAGS = 1;
|
||||
const TERM_AND_TAGS = 2;
|
||||
|
||||
const HIDDEN_VISIBLE = 0;
|
||||
const HIDDEN_UNAPPROVED = 1;
|
||||
const HIDDEN_HIDDEN = -1;
|
||||
const HIDDEN_PARENT_HIDDEN = 2;
|
||||
|
||||
const ORDER_NEWEST_UPDATED = 1;
|
||||
const ORDER_NEWEST_CREATED = 2;
|
||||
const ORDER_RELEVANCY = 3;
|
||||
|
||||
const SUPPORTS_JOIN_FILTERS = TRUE;
|
||||
|
||||
/**
|
||||
* Create new query
|
||||
*
|
||||
* @param \IPS\Member $member The member performing the search (NULL for currently logged in member)
|
||||
* @return \IPS\Content\Search
|
||||
*/
|
||||
public static function init( \IPS\Member $member = NULL )
|
||||
{
|
||||
$class = '\\IPS\\Content\\Search\\' . ucfirst( \IPS\Settings::i()->search_engine ) . '\\Query';
|
||||
return new $class( $member ?: \IPS\Member::loggedIn() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Number of results to get
|
||||
*/
|
||||
public $resultsToGet = 25;
|
||||
|
||||
/**
|
||||
* @brief The member performing the search
|
||||
*/
|
||||
protected $member;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \IPS\Member $member The member performing the search
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( \IPS\Member $member )
|
||||
{
|
||||
$this->member = $member;
|
||||
|
||||
if ( !$member->modPermission('can_view_hidden_content') )
|
||||
{
|
||||
$this->setHiddenFilter( static::HIDDEN_VISIBLE );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude "first post" content items
|
||||
* Useful for cases such as activity streams where you do not want both the topic and the first post to show
|
||||
*
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function excludeFirstPostContentItems()
|
||||
{
|
||||
$classes = array();
|
||||
foreach ( \IPS\Content::routedClasses( TRUE, FALSE, TRUE ) as $class )
|
||||
{
|
||||
if ( in_array( 'IPS\Content\Searchable', class_implements( $class ) ) and $class::$firstCommentRequired )
|
||||
{
|
||||
$classes[] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !empty( $classes ) )
|
||||
{
|
||||
return $this->filterByExcludeContent( $classes );
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exclude disabled applications
|
||||
*
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function excludeDisabledApps()
|
||||
{
|
||||
$includeClasses = array();
|
||||
foreach ( \IPS\Content::routedClasses( TRUE, FALSE, TRUE ) as $class )
|
||||
{
|
||||
if ( in_array( 'IPS\Content\Searchable', class_implements( $class ) ) )
|
||||
{
|
||||
$includeClasses[] = $class;
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$includeClasses[] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$includeClasses[] = $class::$reviewClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
$allClasses = array();
|
||||
foreach ( \IPS\Content::routedClasses( FALSE, FALSE, TRUE ) as $class )
|
||||
{
|
||||
if ( in_array( 'IPS\Content\Searchable', class_implements( $class ) ) )
|
||||
{
|
||||
$allClasses[] = $class;
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$allClasses[] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$allClasses[] = $class::$reviewClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$classes = array_diff( $allClasses, $includeClasses );
|
||||
|
||||
if ( !empty( $classes ) )
|
||||
{
|
||||
return $this->filterByExcludeContent( $classes );
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content type
|
||||
*
|
||||
* @param string $class The type of content to search (including all comment/review classes)
|
||||
* @param array|NULL $containers An array of container IDs to filter by, or NULL to not filter by containers
|
||||
* @param int|NULL $minimumComments The minimum number of comments
|
||||
* @param int|NULL $minimumReviews The minimum number of reviews
|
||||
* @param int|NULL $minimumViews The minimum number of views
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function filterByContent( $class, $containers = NULL, $minimumComments = NULL, $minimumReviews = NULL, $minimumViews = NULL );
|
||||
|
||||
/**
|
||||
* Filter by content type to exclude
|
||||
*
|
||||
* @param string $classes The types of content to search (not including all comment/review classes)
|
||||
* @param bool $allowedContiners If TRUE, $class should be an array of allowed containers for that class - e.g. array( 'IPS\forums\Topic' => array( 1, 2 ) - otherwise, a simple array - e.g. array( 'IPS\forums\Topic' )
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function filterByExcludeContent( array $classes, $allowedContiners=FALSE );
|
||||
|
||||
/**
|
||||
* Filter by content item
|
||||
*
|
||||
* @param \IPS\Content\Item $item The item
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function filterByItem( \IPS\Content\Item $item );
|
||||
|
||||
/**
|
||||
* Filter by author
|
||||
*
|
||||
* @param \IPS\Member $author The author
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function filterByAuthor( \IPS\Member $author );
|
||||
|
||||
/**
|
||||
* Filter by start date
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $start The start date (only results AFTER this date will be returned)
|
||||
* @param \IPS\DateTime|NULL $end The end date (only results BEFORE this date will be returned)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function filterByCreateDate( \IPS\DateTime $start = NULL, \IPS\DateTime $end = NULL );
|
||||
|
||||
/**
|
||||
* Filter by last updated date
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $start The start date (only results AFTER this date will be returned)
|
||||
* @param \IPS\DateTime|NULL $end The end date (only results BEFORE this date will be returned)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function filterByLastUpdatedDate( \IPS\DateTime $start = NULL, \IPS\DateTime $end = NULL );
|
||||
|
||||
/**
|
||||
* Set hidden status
|
||||
*
|
||||
* @param int|array|NULL $statuses The statuses (see HIDDEN_ constants) or NULL for any
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function setHiddenFilter( $statuses );
|
||||
|
||||
/**
|
||||
* Set limit
|
||||
*
|
||||
* @param int $limit Number per page
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setLimit( $limit )
|
||||
{
|
||||
$this->resultsToGet = $limit;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set page
|
||||
*
|
||||
* @param int $page The page number
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function setPage( $page );
|
||||
|
||||
/**
|
||||
* Set order
|
||||
*
|
||||
* @param int $order Order (see ORDER_ constants)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
abstract public function setOrder( $order );
|
||||
|
||||
/**
|
||||
* Permission Array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function permissionArray()
|
||||
{
|
||||
return $this->member->permissionArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* @param string|null $term The term to search for
|
||||
* @param array|null $tags The tags to search for
|
||||
* @param int $method \IPS\Content\Search\Index::i()->TERM_OR_TAGS or \IPS\Content\Search\Index::i()->TERM_AND_TAGS
|
||||
* @return \Traversable
|
||||
*/
|
||||
abstract public function search( $term = NULL, $tags = NULL, $method = 1 );
|
||||
|
||||
/**
|
||||
* Get count
|
||||
*
|
||||
* @param string|null $term The term to search for
|
||||
* @param array|null $tags The tags to search for
|
||||
* @param int $method \IPS\Content\Search\Index::i()->TERM_OR_TAGS or \IPS\Content\Search\Index::i()->TERM_AND_TAGS
|
||||
* @return int
|
||||
*/
|
||||
public function count( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
return $this->search( $term, $tags, $method )->count( TRUE );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Search Results
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search Results
|
||||
*/
|
||||
class _Results extends \ArrayIterator
|
||||
{
|
||||
/**
|
||||
* @brief Count
|
||||
*/
|
||||
protected $countAllRows;
|
||||
|
||||
/**
|
||||
* @brief Has this been initated?
|
||||
*/
|
||||
protected $initated = FALSE;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $results The results
|
||||
* @param int $countAllRows Count for all rows
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $results, $countAllRows )
|
||||
{
|
||||
$this->countAllRows = $countAllRows;
|
||||
parent::__construct( $results );
|
||||
}
|
||||
|
||||
/**
|
||||
* Init
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init( $loadReputation=FALSE )
|
||||
{
|
||||
$classesToLoad = array( 'IPS\Member' => array() );
|
||||
$reputationIds = array();
|
||||
|
||||
foreach ( $this->getArrayCopy() as $row )
|
||||
{
|
||||
$classesToLoad[ 'IPS\Member' ][ $row['index_author'] ] = $row['index_author'];
|
||||
|
||||
$classesToLoad[ $row['index_class'] ][ $row['index_object_id'] ] = $row['index_object_id'];
|
||||
$itemClass = NULL;
|
||||
if ( $row['index_item_id'] and in_array( 'IPS\Content\Comment', class_parents( $row['index_class'] ) ) )
|
||||
{
|
||||
$itemClass = $row['index_class']::$itemClass;
|
||||
$classesToLoad[ $itemClass ][ $row['index_item_id'] ] = $row['index_item_id'];
|
||||
}
|
||||
if ( $row['index_container_id'] )
|
||||
{
|
||||
$containerClass = $itemClass ? $itemClass::$containerNodeClass : $row['index_class']::$containerNodeClass;
|
||||
$classesToLoad[ $containerClass ][ $row['index_container_id'] ] = $row['index_container_id'];
|
||||
}
|
||||
|
||||
if ( $loadReputation and in_array( 'IPS\Content\Reputation', class_implements( $row['index_class'] ) ) )
|
||||
{
|
||||
$reputationIds[ $row['index_class'] ][ $row['index_object_id'] ] = $row['index_object_id'];
|
||||
}
|
||||
}
|
||||
|
||||
$reputation = array();
|
||||
if ( count( $reputationIds ) )
|
||||
{
|
||||
$clause = array();
|
||||
$binds = array();
|
||||
foreach ( $reputationIds as $class => $ids )
|
||||
{
|
||||
$clause[] = "( app=? AND type=? AND " . \IPS\Db::i()->in( 'type_id', $ids ) . " )";
|
||||
$binds[] = $class::$application;
|
||||
$binds[] = $class::$reputationType;
|
||||
}
|
||||
|
||||
$where = array( array_merge( array( implode( ' OR ', $clause ) ), $binds ) );
|
||||
switch( \IPS\Settings::i()->reputation_point_types )
|
||||
{
|
||||
case 'positive':
|
||||
case 'like':
|
||||
$where[] = array( 'rep_rating=?', "1" );
|
||||
break;
|
||||
case 'negative':
|
||||
$where[] = array( 'rep_rating=?', "-1" );
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_reputation_index', $where ) as $rep )
|
||||
{
|
||||
$reputation[ $rep['app'] ][ $rep['type'] ][ $rep['type_id'] ][ $rep['member_id'] ] = $rep;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $classesToLoad as $class => $ids )
|
||||
{
|
||||
$ids = array_diff( $ids, $class::multitonIds() );
|
||||
$idColumn = $class::$databaseColumnId;
|
||||
|
||||
if ( !empty( $ids ) )
|
||||
{
|
||||
foreach ( \IPS\Db::i()->select( '*', $class::$databaseTable, \IPS\Db::i()->in( $class::$databasePrefix . $class::$databaseColumnId, $ids ) ) as $row )
|
||||
{
|
||||
$obj = $class::constructFromData( $row );
|
||||
if ( $loadReputation and $obj instanceof \IPS\Content\Reputation )
|
||||
{
|
||||
if ( isset( $reputation[ $class::$application ][ $class::$reputationType ][ $obj->$idColumn ] ) )
|
||||
{
|
||||
$obj->reputation = $reputation[ $class::$application ][ $class::$reputationType ][ $obj->$idColumn ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$obj->reputation = array();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->initated = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current
|
||||
*
|
||||
* @return \IPS\Patterns\ActiveRecord
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
$row = parent::current();
|
||||
return call_user_func( array( $row['index_class'], 'load' ), $row['index_object_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
if ( !$this->initated )
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
return parent::rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count
|
||||
*
|
||||
* @param bool $allRows If TRUE, will get the number of rows ignoring the limit
|
||||
* @return int
|
||||
*/
|
||||
public function count( $allRows = FALSE )
|
||||
{
|
||||
if ( $allRows )
|
||||
{
|
||||
return $this->countAllRows;
|
||||
}
|
||||
else
|
||||
{
|
||||
return parent::count();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Sphinx Search Index
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 26 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search\Sphinx;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sphinx Search Index
|
||||
*/
|
||||
class _Index extends \IPS\Content\Search\Index
|
||||
{
|
||||
/**
|
||||
* @brief The index table
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
\IPS\Db::i( 'sphinx', array(
|
||||
'sql_host' => \IPS\Settings::i()->search_sphinx_server,
|
||||
'sql_user' => NULL,
|
||||
'sql_pass' => NULL,
|
||||
'sql_database' => NULL,
|
||||
'sql_port' => (int) \IPS\Settings::i()->search_sphinx_port,
|
||||
'sql_socket' => NULL,
|
||||
'sql_utf8mb4' => \IPS\Settings::i()->sql_utf8mb4
|
||||
) );
|
||||
|
||||
$this->table = \IPS\Settings::i()->sphinx_prefix . 'ips';
|
||||
}
|
||||
|
||||
/**
|
||||
* Index an item
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to add
|
||||
* @return void
|
||||
*/
|
||||
public function index( \IPS\Content\Searchable $object )
|
||||
{
|
||||
$indexData = $this->indexData( $object );
|
||||
if ( $indexData )
|
||||
{
|
||||
$data = array_merge( array( 'id' => static::_getId( $object ), 'index_class_id' => static::_getClassId( get_class( $object ) ) ), $indexData );
|
||||
$data['index_permissions'] = json_encode( explode( ',', $data['index_permissions'] ) );
|
||||
|
||||
if( !$data['index_permissions'] )
|
||||
{
|
||||
$this->removeFromSearchIndex( $object );
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = \IPS\Db::i('sphinx')->query( "REPLACE INTO {$this->table} ( " . implode( ', ', array_keys( $data ) ) . ' ) VALUES ( ' . implode( ', ', array_map( array( $this, '_escapeValue' ), $data ) ) . ' )' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove item
|
||||
*
|
||||
* @param \IPS\Content\Searchable $object Item to remove
|
||||
* @return void
|
||||
*/
|
||||
public function removeFromSearchIndex( \IPS\Content\Searchable $object )
|
||||
{
|
||||
\IPS\Db::i('sphinx')->query( "DELETE FROM {$this->table} WHERE id=" . static::_getId( $object ) );
|
||||
|
||||
$class = get_class( $object );
|
||||
$idColumn = $class::$databaseColumnId;
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
|
||||
foreach ( \IPS\Db::i()->select( $commentClass::$databasePrefix . $commentClass::$databaseColumnId, $commentClass::$databaseTable, array( $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['item'] . '=?', $object->$idColumn ) ) as $commentid )
|
||||
{
|
||||
\IPS\Db::i('sphinx')->query( "DELETE FROM {$this->table} WHERE id=" . static::_getId( $commentid, $commentClass ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
|
||||
foreach ( \IPS\Db::i()->select( $reviewClass::$databasePrefix . $reviewClass::$databaseColumnId, $reviewClass::$databaseTable, array( $reviewClass::$databasePrefix . $reviewClass::$databaseColumnMap['item'] . '=?', $object->$idColumn ) ) as $reviewid )
|
||||
{
|
||||
\IPS\Db::i('sphinx')->query( "DELETE FROM {$this->table} WHERE id=" . static::_getId( $reviewid, $reviewClass ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all content for a classs
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $containerId The container ID to update, or NULL
|
||||
* @return void
|
||||
*/
|
||||
public function removeClassFromSearchIndex( $class, $containerId=NULL )
|
||||
{
|
||||
$where = array( 'index_class_id=' . static::_getClassId( $class ) );
|
||||
if ( $containerId !== NULL )
|
||||
{
|
||||
$where[] = "index_container_id={$containerId}";
|
||||
}
|
||||
|
||||
$where = implode( ' AND ', $where );
|
||||
|
||||
\IPS\Db::i('sphinx')->query( "DELETE FROM {$this->table} WHERE {$where}" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Mass Update (when permissions change, for example)
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $containerId The container ID to update, or NULL
|
||||
* @param int|NULL $itemId The item ID to update, or NULL
|
||||
* @param string|NULL $newPermissions New permissions (if applicable)
|
||||
* @param int|NULL $newHiddenStatus New hidden status (if applicable) special value 2 can be used to indicate hidden only by parent
|
||||
* @param int|NULL $newContainer New container ID (if applicable)
|
||||
* @return void
|
||||
*/
|
||||
public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL )
|
||||
{
|
||||
$where = array( 'index_class_id=' . static::_getClassId( $class ) );
|
||||
if ( $containerId !== NULL )
|
||||
{
|
||||
$where[] = "index_container_id={$containerId}";
|
||||
}
|
||||
if ( $itemId !== NULL )
|
||||
{
|
||||
$where[] = "index_item_id={$itemId}";
|
||||
}
|
||||
|
||||
$update = array();
|
||||
if ( $newPermissions !== NULL )
|
||||
{
|
||||
$update['index_permissions'] = explode( ',', str_replace( '*', 0, $newPermissions ) );
|
||||
}
|
||||
if ( $newHiddenStatus !== NULL )
|
||||
{
|
||||
if ( $newHiddenStatus === 2 )
|
||||
{
|
||||
$where[] = 'index_hidden=0';
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = 'index_hidden=2';
|
||||
}
|
||||
|
||||
$update['index_hidden'] = $newHiddenStatus;
|
||||
}
|
||||
if ( $newContainer )
|
||||
{
|
||||
$update['index_container_id'] = $newContainer;
|
||||
}
|
||||
|
||||
$set = array();
|
||||
foreach ( $update as $k => $v )
|
||||
{
|
||||
$set[] = "{$k}={$this->_escapeValue($v)}";
|
||||
}
|
||||
$set = implode( ', ', $set );
|
||||
|
||||
$where = implode( ' AND ', $where );
|
||||
|
||||
try
|
||||
{
|
||||
\IPS\Db::i('sphinx')->query( "UPDATE {$this->table} SET {$set} WHERE {$where}" );
|
||||
}
|
||||
catch ( \Exception $e )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prune search index
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $cutoff The date to delete index records from, or NULL to delete all
|
||||
* @return void
|
||||
*/
|
||||
public function prune( \IPS\DateTime $cutoff = NULL )
|
||||
{
|
||||
if ( $cutoff === NULL )
|
||||
{
|
||||
try
|
||||
{
|
||||
\IPS\Db::i('sphinx')->query( "TRUNCATE RTINDEX {$this->table}" );
|
||||
}
|
||||
catch( \IPS\Db\Exception $e )
|
||||
{
|
||||
/* If the index does not exist, we should not exit at this point in time */
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \UnexpectedValueException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an ID for an object
|
||||
*
|
||||
* @param \IPS\Content\Searchable|int $object Item to get ID for
|
||||
* @param string|NULL $class If $object is an ID rather than an object, provide the class
|
||||
* @return int
|
||||
*/
|
||||
public static function _getId( $object, $class = NULL )
|
||||
{
|
||||
if ( is_object( $object ) )
|
||||
{
|
||||
$class = get_class( $object );
|
||||
$idColumn = $class::$databaseColumnId;
|
||||
$id = $object->$idColumn;
|
||||
}
|
||||
else
|
||||
{
|
||||
$id = $object;
|
||||
}
|
||||
|
||||
return ( static::_getClassId( $class ) * 100000000 ) + $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an ID for an class
|
||||
*
|
||||
* @param string $class The class
|
||||
* @return int
|
||||
*/
|
||||
public static function _getClassId( $class )
|
||||
{
|
||||
$updateMap = FALSE;
|
||||
|
||||
/* Get the map (Older versions stored this in datastore, so we look there if we don't have it or use a blank array) */
|
||||
if ( !\IPS\Settings::i()->sphinx_id_map )
|
||||
{
|
||||
$map = isset( \IPS\Data\Store::i()->sphinxIdMap ) ? \IPS\Data\Store::i()->sphinxIdMap : array();
|
||||
$updateMap = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
$map = json_decode( \IPS\Settings::i()->sphinx_id_map, TRUE );
|
||||
}
|
||||
|
||||
/* Add it if we don't have it */
|
||||
if ( !in_array( $class, $map ) )
|
||||
{
|
||||
$map[] = $class;
|
||||
$updateMap = TRUE;
|
||||
}
|
||||
|
||||
/* Update the map if necessary */
|
||||
if ( $updateMap )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_sys_conf_settings', array( 'conf_value' => json_encode( $map ) ), array( 'conf_key=?', 'sphinx_id_map' ) );
|
||||
unset( \IPS\Data\Store::i()->settings );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return intval( array_search( $class, $map ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a value for the query (SphinxQL doesn't support prepared statements)
|
||||
*
|
||||
* @param mixed $val The value
|
||||
* @return mixed
|
||||
*/
|
||||
protected function _escapeValue( $val )
|
||||
{
|
||||
if ( is_numeric( $val ) )
|
||||
{
|
||||
return $val;
|
||||
}
|
||||
elseif ( is_array( $val ) )
|
||||
{
|
||||
return '(' . implode( ',', array_map( array( $this, '_escapeValue' ), $val ) ) . ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
return ( '\'' . \IPS\Db::i( 'sphinx' )->real_escape_string( $val ) . '\'' );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,375 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Sphinx Search Query
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @since 26 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\Content\Search\Sphinx;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sphinx Search Query
|
||||
*/
|
||||
class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
const SUPPORTS_JOIN_FILTERS = FALSE;
|
||||
|
||||
/**
|
||||
* @brief The index table
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* @brief The WHERE clause
|
||||
*/
|
||||
protected $where = array();
|
||||
|
||||
/**
|
||||
* @brief The WHERE clause for hidden/unhidden
|
||||
*/
|
||||
protected $hiddenClause = NULL;
|
||||
|
||||
/**
|
||||
* @brief The ORDER BY clause
|
||||
*/
|
||||
protected $order = NULL;
|
||||
|
||||
/**
|
||||
* @brief The offset
|
||||
*/
|
||||
protected $offset = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \IPS\Member $member The member performing the search
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( \IPS\Member $member )
|
||||
{
|
||||
\IPS\Db::i( 'sphinx', array(
|
||||
'sql_host' => \IPS\Settings::i()->search_sphinx_server,
|
||||
'sql_user' => NULL,
|
||||
'sql_pass' => NULL,
|
||||
'sql_database' => NULL,
|
||||
'sql_port' => (int) \IPS\Settings::i()->search_sphinx_port,
|
||||
'sql_socket' => NULL,
|
||||
'sql_utf8mb4' => \IPS\Settings::i()->sql_utf8mb4
|
||||
) );
|
||||
|
||||
$this->table = \IPS\Settings::i()->sphinx_prefix . 'ips';
|
||||
|
||||
parent::__construct( $member );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content type
|
||||
*
|
||||
* @param string $class The type of content to search (including all comment/review classes)
|
||||
* @param array|NULL $containers An array of container IDs to filter by, or NULL to not filter by containers
|
||||
* @param int|NULL $minimumComments The minimum number of comments
|
||||
* @param int|NULL $minimumReviews The minimum number of reviews
|
||||
* @param int|NULL $minimumViews The minimum number of views
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByContent( $class, $containers = NULL, $minimumComments = NULL, $minimumReviews = NULL, $minimumViews = NULL )
|
||||
{
|
||||
/* Classes */
|
||||
$classes = array( \IPS\Content\Search\Sphinx\Index::_getClassId( $class ) );
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$classes[] = \IPS\Content\Search\Sphinx\Index::_getClassId( $class::$commentClass );
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$classes[] = \IPS\Content\Search\Sphinx\Index::_getClassId( $class::$reviewClass );
|
||||
}
|
||||
$this->where[] = 'index_class_id IN(' . implode( ',', $classes ) . ')';
|
||||
|
||||
/* Containers */
|
||||
if ( $containers !== NULL )
|
||||
{
|
||||
$this->where[] = 'index_container_id IN(' . implode( ',', array_map( 'intval', $containers ) ) . ' )';
|
||||
}
|
||||
|
||||
/* We don't support anything else */
|
||||
if ( $minimumComments !== NULL or $minimumReviews !== NULL or $minimumViews !== NULL )
|
||||
{
|
||||
throw new \UnexpectedValueException;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content type to exclude
|
||||
*
|
||||
* @param array $classes The types of content to exclude (not including all comment/review classes)
|
||||
* @param bool $allowedContiners If TRUE, $class should be an array of allowed containers for that class - e.g. array( 'IPS\forums\Topic' => array( 1, 2 ) - otherwise, a simple array - e.g. array( 'IPS\forums\Topic' )
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByExcludeContent( array $classes, $allowedContiners=FALSE )
|
||||
{
|
||||
if ( $allowedContiners )
|
||||
{
|
||||
$where = array();
|
||||
foreach ( $classes as $class => $allowed )
|
||||
{
|
||||
if ( count( $allowed ) )
|
||||
{
|
||||
$where[] = '( index_class<>' . \IPS\Content\Search\Sphinx\Index::_getClassId( $class ) . ' OR index_container_id IN( ' . implode( ',', $allowed ) . ' ) )';
|
||||
}
|
||||
else
|
||||
{
|
||||
$excludeAll[] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
$finalWhere = array();
|
||||
if ( count( $excludeAll ) )
|
||||
{
|
||||
$finalWhere[] = 'index_class NOT IN('. implode( ',', array_map( array( 'IPS\Content\Search\Sphinx\Index', '_getClassId' ), $excludeAll ) ) . ')';
|
||||
}
|
||||
if ( count( $where ) )
|
||||
{
|
||||
$finalWhere[] = implode( ' OR ', $where );
|
||||
}
|
||||
|
||||
$this->where[] = array_merge( array( implode( ' AND ', $finalWhere ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->where[] = 'index_class NOT IN('. implode( ',', array_map( array( 'IPS\Content\Search\Sphinx\Index', '_getClassId' ), $classes ) ) . ')';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content item
|
||||
*
|
||||
* @param \IPS\Content\Item $item The item
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByItem( \IPS\Content\Item $item )
|
||||
{
|
||||
$class = get_class( $item );
|
||||
|
||||
$classes = array();
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$classes[] = \IPS\Content\Search\Sphinx\Index::_getClassId( $class::$commentClass );
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$classes[] = \IPS\Content\Search\Sphinx\Index::_getClassId( $class::$reviewClass );
|
||||
}
|
||||
$this->where[] = 'index_class_id IN(' . implode( ',', $classes ) . ')';
|
||||
|
||||
$idColumn = $item::$databaseColumnId;
|
||||
$this->where[] = "index_item_id={$item->$idColumn}";
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by author
|
||||
*
|
||||
* @param \IPS\Member $author The author
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByAuthor( \IPS\Member $author )
|
||||
{
|
||||
$this->where[] = "index_author={$author->member_id}";
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by start date
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $start The start date (only results AFTER this date will be returned)
|
||||
* @param \IPS\DateTime|NULL $end The end date (only results BEFORE this date will be returned)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByCreateDate( \IPS\DateTime $start = NULL, \IPS\DateTime $end = NULL )
|
||||
{
|
||||
if ( $start )
|
||||
{
|
||||
$this->where[] = 'index_date_created>' . $start->getTimestamp();
|
||||
}
|
||||
if ( $end )
|
||||
{
|
||||
$this->where[] = 'index_date_created<' . $end->getTimestamp();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by last updated date
|
||||
*
|
||||
* @param \IPS\DateTime|NULL $start The start date (only results AFTER this date will be returned)
|
||||
* @param \IPS\DateTime|NULL $end The end date (only results BEFORE this date will be returned)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByLastUpdatedDate( \IPS\DateTime $start = NULL, \IPS\DateTime $end = NULL )
|
||||
{
|
||||
if ( $start )
|
||||
{
|
||||
$this->where[] = 'index_date_updated>' . $start->getTimestamp();
|
||||
}
|
||||
if ( $end )
|
||||
{
|
||||
$this->where[] = 'index_date_updated<' . $end->getTimestamp();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set hidden status
|
||||
*
|
||||
* @param int|array $statuses The statuses (array of HIDDEN_ constants)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setHiddenFilter( $statuses )
|
||||
{
|
||||
if ( is_null( $statuses ) )
|
||||
{
|
||||
$this->hiddenClause = NULL;
|
||||
}
|
||||
if ( is_array( $statuses ) )
|
||||
{
|
||||
$this->hiddenClause = 'index_hidden IN(' . implode( ',', array_map( 'intval', $statuses ) ) . ' )';
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->hiddenClause = 'index_hidden=' . intval( $statuses );
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set page
|
||||
*
|
||||
* @param int $page The page number
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setPage( $page )
|
||||
{
|
||||
$page = $page ?: 1;
|
||||
$this->offset = ( $page - 1 ) * $this->resultsToGet;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set order
|
||||
*
|
||||
* @param int $order Order (see ORDER_ constants)
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function setOrder( $order )
|
||||
{
|
||||
switch ( $order )
|
||||
{
|
||||
case static::ORDER_NEWEST_UPDATED:
|
||||
$this->order = 'index_date_updated DESC';
|
||||
break;
|
||||
|
||||
case static::ORDER_NEWEST_CREATED:
|
||||
$this->order = 'index_date_created DESC';
|
||||
break;
|
||||
|
||||
case static::ORDER_RELEVANCY:
|
||||
$this->order = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* @param string|null $term The term to search for
|
||||
* @param array|null $tags The tags to search for
|
||||
* @param int $method \IPS\Content\Search\Index::i()->TERM_OR_TAGS or \IPS\Content\Search\Index::i()->TERM_AND_TAGS
|
||||
* @return \IPS\Content\Search\Results
|
||||
*/
|
||||
public function search( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
/* Build the match term */
|
||||
if ( $term !== NULL )
|
||||
{
|
||||
$match[] = "( @(index_title,index_content) {$this->escape( $term )} )";
|
||||
}
|
||||
if ( $tags !== NULL )
|
||||
{
|
||||
$match[] = "( @index_tags " . implode( ' | ', array_map( array( $this, 'escape' ), $tags ) ) . ' )';
|
||||
}
|
||||
if ( !empty( $match ) )
|
||||
{
|
||||
array_unshift( $this->where, 'MATCH( \'' . implode( ( $method === static::TERM_OR_TAGS ? ' | ' : ' ' ), $match ) . '\' )' );
|
||||
}
|
||||
|
||||
/* Only get stuff we have permission for */
|
||||
$permissions = array( '\'' . $this->escape( '*' ) . '\'' );
|
||||
foreach ( $this->permissionArray() as $id )
|
||||
{
|
||||
$permissions[] = '\'' . $this->escape( $id ) . '\'';
|
||||
}
|
||||
$permCheck = 'IN(index_permissions,' . implode( ',', $permissions ) . ') AS perm_check';
|
||||
$this->where[] = 'perm_check=1';
|
||||
|
||||
/* Add the hidden clause */
|
||||
if ( $this->hiddenClause )
|
||||
{
|
||||
$this->where[] = $this->hiddenClause;
|
||||
}
|
||||
|
||||
/* Query */
|
||||
$query = \IPS\Db::i('sphinx')->query( "SELECT *, {$permCheck} FROM {$this->table} WHERE " . implode( ' AND ', $this->where ) . ( $this->order ? " ORDER BY {$this->order}" : '' ) . " LIMIT {$this->offset}, {$this->resultsToGet}" );
|
||||
|
||||
/* Get count */
|
||||
$meta = \IPS\Db::i('sphinx')->query('SHOW META');
|
||||
while ( $row = $meta->fetch_assoc() )
|
||||
{
|
||||
if ( $row['Variable_name'] === 'total' )
|
||||
{
|
||||
$count = $row['Value'];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* Convert to array */
|
||||
$results = array();
|
||||
while ( $row = $query->fetch_assoc() )
|
||||
{
|
||||
$results[] = $row;
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return new \IPS\Content\Search\Results( $results, $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape string for SphinxQL query
|
||||
*
|
||||
* @param string $term Search term
|
||||
* @return string
|
||||
*/
|
||||
protected function escape( $term )
|
||||
{
|
||||
return str_replace( array( '\\', '(',')','|','-','!','@','~','"','&', '/', '^', '$', '=', "'", "\x00", "\n", "\r", "\x1a" ), array( '\\\\', '\\\(','\\\)','\\\|','\\\-','\\\!','\\\@','\\\~','\\\"', '\\\&', '\\\/', '\\\^', '\\\$', '\\\=', "\\'", "\\x00", "\\n", "\\r", "\\x1a" ), $term );
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
Whitespace-only changes.
Reference in new issue
Block a user