Version 4.1.19
This commit is contained in:
1 parent
28bd025b35
commit
2bd5025018
2674 files changed
+233518
-77766
No files matched your search
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @brief Content Filter for Search Queries
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 6 Jul 2015
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -89,10 +89,10 @@ class _ContentFilter
|
||||
*/
|
||||
public static function init( $itemClass, $includeItems=TRUE, $includeComments=TRUE, $includeReviews=TRUE )
|
||||
{
|
||||
$obj = new self;
|
||||
$obj = new static;
|
||||
$obj->itemClass = $itemClass;
|
||||
|
||||
if ( $includeItems and !$itemClass::$firstCommentRequired )
|
||||
if ( $includeItems and ( !isset( $itemClass::$firstCommentRequired ) OR !$itemClass::$firstCommentRequired ) )
|
||||
{
|
||||
$obj->classes[] = $itemClass;
|
||||
}
|
||||
|
||||
+114
-24
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @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.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -81,8 +81,9 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
/* 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;
|
||||
|
||||
$tags = ( $object instanceof \IPS\Content\Tags and \IPS\Settings::i()->tags_enabled ) ? implode( ',', array_filter( $object->tags() ) ) : NULL;
|
||||
$prefix = ( $object instanceof \IPS\Content\Tags and \IPS\Settings::i()->tags_enabled ) ? $object->prefix() : 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 )
|
||||
{
|
||||
@@ -104,7 +105,8 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
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;
|
||||
$tags = ( $object->item() instanceof \IPS\Content\Tags and \IPS\Settings::i()->tags_enabled ) ? implode( ',', array_filter( $object->item()->tags() ) ) : NULL;
|
||||
$prefix = ( $object->item() instanceof \IPS\Content\Tags and \IPS\Settings::i()->tags_enabled ) ? $object->item()->prefix() : NULL;
|
||||
$isForItem = TRUE;
|
||||
}
|
||||
}
|
||||
@@ -116,14 +118,23 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
}
|
||||
else
|
||||
{
|
||||
$dateUpdated = ( $object instanceof \IPS\Content\Item ) ? $object->mapped('last_comment') : $object->mapped('edit_time');
|
||||
$dateUpdated = ( $object instanceof \IPS\Content\Item ) ? $object->mapped('last_comment') : $object->mapped('date');
|
||||
}
|
||||
|
||||
/* Is the the latest content? */
|
||||
$isLastComment = 0;
|
||||
if ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$item = $object->item();
|
||||
try
|
||||
{
|
||||
$item = $object->item();
|
||||
}
|
||||
catch( \OutOfRangeException $ex )
|
||||
{
|
||||
/* Comment has no parent item, return */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
$latestThing = 0;
|
||||
foreach ( array( 'updated', 'last_comment', 'last_review' ) as $k )
|
||||
{
|
||||
@@ -137,16 +148,55 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
{
|
||||
$isLastComment = 1;
|
||||
}
|
||||
|
||||
/* If we are re-indexing the first post of an item, which is triggered by a comment being added to a $itemClass::$firstCommentRequired,
|
||||
then ensure that the isLastComment flag is not added as we index the comment first, which means the index_is_last_comment is incorrectly reset to 0 for the comment itself */
|
||||
if ( $isForItem and $isLastComment )
|
||||
{
|
||||
if ( isset( $item::$databaseColumnMap['num_comments'] ) and $item->mapped('num_comments') > 1 )
|
||||
{
|
||||
$isLastComment = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( $object instanceof \IPS\Content\Item and ! $class::$firstCommentRequired )
|
||||
{
|
||||
/* If this is item itself and not a comment, then we will store it as the last comment so the activity stream fetches the data correctly */
|
||||
$isLastComment = 1;
|
||||
|
||||
if ( isset( $class::$databaseColumnMap['num_comments'] ) and $object->mapped('num_comments') )
|
||||
{
|
||||
$isLastComment = 0;
|
||||
}
|
||||
else if ( isset( $class::$databaseColumnMap['num_reviews'] ) and $object->mapped('num_reviews') )
|
||||
{
|
||||
$isLastComment = 0;
|
||||
}
|
||||
|
||||
/* Is the item itself searchable but the comment not? */
|
||||
$commentClass = $object::$commentClass;
|
||||
if ( ! ( is_subclass_of( $commentClass, 'IPS\Content\Searchable' ) ) )
|
||||
{
|
||||
/* Then make this the last comment so it remains searchable */
|
||||
$isLastComment = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Take the HTML out of the content */
|
||||
$content = trim( strip_tags( preg_replace( "/(<br(?: \/)?>|<\/p>)/i", ' ', preg_replace( "#<blockquote(.*?)>(.*)</blockquote>#uis", "", preg_replace( "#<script(.*?)>(.*)</script>#uis", "", ' ' . $object->mapped('content') . ' ' ) ) ) ) );
|
||||
|
||||
/* Strip spoilers */
|
||||
$content = $object->mapped('content');
|
||||
if ( preg_match( '#<div\s+?class=["\']ipsSpoiler["\']#', $content ) )
|
||||
{
|
||||
$content = \IPS\Text\Parser::removeElements( $content, array( 'div[class=ipsSpoiler]' ) );
|
||||
}
|
||||
|
||||
/* Take the HTML out of the content */
|
||||
$content = trim( str_replace( chr(0xC2) . chr(0xA0), ' ', strip_tags( preg_replace( "/(<br(?: \/)?>|<\/p>)/i", ' ', preg_replace( "#<blockquote(?:[^>]+?)>.+?(?<!<blockquote)</blockquote>#s", " ", preg_replace( "#<script(.*?)>(.*)</script>#uis", "", ' ' . $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;
|
||||
$hiddenStatus = $isForItem ? $object->item()->hidden() : 2;
|
||||
}
|
||||
if ( $hiddenStatus !== 0 and method_exists( $object, 'item' ) and $object->item()->isFutureDate() )
|
||||
{
|
||||
@@ -162,16 +212,45 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
{
|
||||
try
|
||||
{
|
||||
$itemIndexId = $this->getIndexId( $object::load( $object->item()->mapped('first_comment_id') ) );
|
||||
$itemIndexId = $this->getIndexId( $object->item()->firstComment() );
|
||||
}
|
||||
catch ( \Exception $e ) { }
|
||||
}
|
||||
else
|
||||
{
|
||||
$itemIndexId = $this->getIndexId( $object->item() );
|
||||
try
|
||||
{
|
||||
$itemIndexId = $this->getIndexId( $object->item() );
|
||||
}
|
||||
catch ( \UnderflowException $e )
|
||||
{
|
||||
try
|
||||
{
|
||||
/* Try and index parent */
|
||||
\IPS\Content\Search\Index::i()->index( $object->item() );
|
||||
$itemIndexId = $this->getIndexId( $object->item() );
|
||||
}
|
||||
catch( \Exception $ex )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if ( $object instanceof \IPS\Content\Item )
|
||||
{
|
||||
if ( ! $object::$firstCommentRequired )
|
||||
{
|
||||
/* See if this has already been indexed */
|
||||
try
|
||||
{
|
||||
/* Good, we need the index_item_index_id so this is not wiped on re-index */
|
||||
$itemIndexId = $this->getIndexId( $object );
|
||||
}
|
||||
catch ( \Exception $e ) { }
|
||||
}
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return array(
|
||||
'index_class' => $class,
|
||||
@@ -185,6 +264,7 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
'index_date_updated' => intval( $dateUpdated ?: $object->mapped('date') ),
|
||||
'index_author' => (int) $object->mapped('author'),
|
||||
'index_tags' => $tags,
|
||||
'index_prefix' => $prefix,
|
||||
'index_hidden' => $hiddenStatus,
|
||||
'index_item_index_id' => $itemIndexId,
|
||||
'index_item_author' => intval( ( $object instanceof \IPS\Content\Item ) ? $object->mapped('author') : $object->item()->mapped('author') ),
|
||||
@@ -266,18 +346,19 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
/**
|
||||
* 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)
|
||||
* @param int|NULL $authorId The author ID to update, or NULL
|
||||
* @param int|NULL $newItemId The new item ID (if applicable)
|
||||
* @param int|NULL $$newItemAuthorId The new item author ID (if applicable)
|
||||
* @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)
|
||||
* @param int|NULL $authorId The author ID to update, or NULL
|
||||
* @param int|NULL $newItemId The new item ID (if applicable)
|
||||
* @param int|NULL $newItemAuthorId The new item author ID (if applicable)
|
||||
* @param bool $addAuthorToPermissions If true, the index_author_id will be added to $newPermissions - used when changing the permissions for a node which allows access only to author's items
|
||||
* @return void
|
||||
*/
|
||||
abstract public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL, $authorId = NULL, $newItemId = NULL, $newItemAuthorId = NULL );
|
||||
abstract public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL, $authorId = NULL, $newItemId = NULL, $newItemAuthorId = NULL, $addAuthorToPermissions = FALSE );
|
||||
|
||||
/**
|
||||
* Update data for the first and last comment after a merge
|
||||
@@ -295,4 +376,13 @@ abstract class _Index extends \IPS\Patterns\Singleton
|
||||
* @return void
|
||||
*/
|
||||
abstract public function prune( \IPS\DateTime $cutoff = NULL );
|
||||
|
||||
/**
|
||||
* Reset the last comment flag in any given class/index_item_id
|
||||
*
|
||||
* @param string $class The class
|
||||
* @param int|NULL $indexItemId The index item ID
|
||||
* @return void
|
||||
*/
|
||||
abstract public function resetLastComment( $class, $indexItemId );
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @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.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -33,10 +33,14 @@ class _Index extends \IPS\Content\Search\Index
|
||||
{
|
||||
/* Get the index data */
|
||||
$indexData = $this->indexData( $object );
|
||||
|
||||
$class = get_class( $object );
|
||||
|
||||
/* If we got the data... */
|
||||
if( $indexData )
|
||||
{
|
||||
/* MySQL specific look up hash for easy referencing across tables */
|
||||
$indexData['index_class_type_id_hash'] = md5( $indexData['index_class'] . ':' . $indexData['index_object_id'] );
|
||||
|
||||
/* If nobody has permission to access it, just remove it */
|
||||
if ( !$indexData['index_permissions'] )
|
||||
{
|
||||
@@ -45,52 +49,159 @@ class _Index extends \IPS\Content\Search\Index
|
||||
/* Otherwise, go ahead... */
|
||||
else
|
||||
{
|
||||
/* Index it */
|
||||
$newIndexId = \IPS\Db::i()->replace( 'core_search_index', $indexData, TRUE );
|
||||
|
||||
/* If that was successful... */
|
||||
if ( $newIndexId )
|
||||
$existingData = NULL;
|
||||
$existingIndexId = NULL;
|
||||
$resetLastComment = FALSE;
|
||||
$newIndexId = NULL;
|
||||
|
||||
try
|
||||
{
|
||||
$class = get_class( $object );
|
||||
$databaseColumnId = $object::$databaseColumnId;
|
||||
$existingData = \IPS\Db::i()->select( 'index_id, index_class, index_object_id, index_item_id, index_hidden, index_is_last_comment, index_author', 'core_search_index', array( 'index_class=? AND index_object_id=?', $indexData['index_class'], $indexData['index_object_id'] ) )->first();
|
||||
$existingIndexId = $existingData['index_id'];
|
||||
}
|
||||
catch( \Exception $e ) { }
|
||||
|
||||
/* Adjust tags */
|
||||
$tags = NULL;
|
||||
if ( array_key_exists( 'index_tags', $indexData ) )
|
||||
{
|
||||
$tags = array_filter( array_merge ( array( $indexData['index_prefix'] ), explode( ',', $indexData['index_tags'] ) ) );
|
||||
$prefix = $indexData['index_prefix'];
|
||||
unset( $indexData['index_tags'] );
|
||||
unset( $indexData['index_prefix'] );
|
||||
}
|
||||
|
||||
if ( $object instanceof \IPS\Content\Comment and $existingIndexId and $existingData['index_is_last_comment'] and $indexData['index_is_last_comment'] and $indexData['index_item_id'] and $indexData['index_hidden'] !== 0 )
|
||||
{
|
||||
/* We do not allow hidden or needing approval comments to become flagged as the last comment as this means users without hidden view permission never see the item in an item only stream */
|
||||
$indexData['index_is_last_comment'] = 0;
|
||||
|
||||
/* Set index_item_index_id on other index items */
|
||||
if ( $object instanceof \IPS\Content\Item )
|
||||
$resetLastComment = TRUE;
|
||||
}
|
||||
else if ( $indexData['index_is_last_comment'] and $indexData['index_item_id'] )
|
||||
{
|
||||
$classes = array( $class );
|
||||
|
||||
/* If this is the latest comment, unflag what was set before on both item and comment */
|
||||
if ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$itemClass = $object::$itemClass;
|
||||
if ( ! $itemClass::$firstCommentRequired )
|
||||
{
|
||||
$classes[] = $itemClass;
|
||||
}
|
||||
|
||||
if ( isset( $itemClass::$reviewClass ) )
|
||||
{
|
||||
$classes[] = $itemClass::$reviewClass;
|
||||
}
|
||||
}
|
||||
else if ( $object instanceof \IPS\Content\Item )
|
||||
{
|
||||
$subClasses = array( $class );
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$subClasses[] = $class::$commentClass;
|
||||
$classes[] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$subClasses[] = $class::$reviewClass;
|
||||
}
|
||||
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_item_index_id' => $newIndexId ), array( array( \IPS\Db::i()->in( 'index_class', $subClasses ) ), array( 'index_item_id=?', $object->$databaseColumnId ) ) );
|
||||
}
|
||||
elseif ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$itemClass = $object::$itemClass;
|
||||
if ( $itemClass::$firstCommentRequired and $object->isFirst() )
|
||||
{
|
||||
$itemColumnId = $class::$databaseColumnMap['item'];
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_item_index_id' => $newIndexId ), array( \IPS\Db::i()->in( 'index_class', array( $class, $class::$itemClass ) ) . ' AND index_item_id=?', $object->$itemColumnId ) );
|
||||
$classes[] = $class::$reviewClass;
|
||||
}
|
||||
}
|
||||
|
||||
/* And also set core_follow.follow_index_id */
|
||||
if ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$class = $class::$itemClass;
|
||||
}
|
||||
\IPS\Db::i()->update( 'core_follow', array( 'follow_index_id' => $newIndexId ), array( 'follow_app=? AND follow_area=? AND follow_rel_id=?', $object::$application, mb_strtolower( mb_substr( $class, mb_strrpos( $class, '\\' ) + 1 ) ), $object->$databaseColumnId ) );
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_is_last_comment' => 0 ), array( \IPS\Db::i()->in( 'index_class', $classes ) . ' AND index_item_id=?', $indexData['index_item_id'] ) );
|
||||
}
|
||||
|
||||
/* If this is the latest comment, unflag what was set before */
|
||||
if ( $indexData['index_is_last_comment'] and $indexData['index_item_id'] )
|
||||
if ( $existingData !== NULL and ( $indexData['index_class'] == $existingData['index_class'] and $indexData['index_object_id'] == $existingData['index_object_id'] ) )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_search_index', $indexData, array( 'index_class=? and index_object_id=?', $indexData['index_class'], $indexData['index_object_id'] ) );
|
||||
$newIndexId = $existingIndexId;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( $existingData !== NULL )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_is_last_comment' => 0 ), array( 'index_class=? AND index_item_id=? AND index_id<>?', $indexData['index_class'], $indexData['index_item_id'], $newIndexId ) );
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? and index_object_id=?', $indexData['index_class'], $indexData['index_object_id'] ) );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$newIndexId = \IPS\Db::i()->insert( 'core_search_index', $indexData );
|
||||
}
|
||||
catch( \IPS\Db\Exception $e )
|
||||
{
|
||||
if ( $e->getCode() == 1062 )
|
||||
{
|
||||
/* Duplicate key which could be caused by a race condition on rebuild. Use replace in this case, as it is more expensive than an insert, so we only use it when we have to */
|
||||
$newIndexId = \IPS\Db::i()->replace( 'core_search_index', $indexData );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If that was successful... */
|
||||
if ( $newIndexId )
|
||||
{
|
||||
/* Remove existing tags */
|
||||
if ( $existingIndexId )
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_search_index_tags', array( 'index_id=?', $existingIndexId ) );
|
||||
}
|
||||
|
||||
/* Add add them back if we have any */
|
||||
if ( count( $tags ) )
|
||||
{
|
||||
foreach( $tags as $tag )
|
||||
{
|
||||
\IPS\Db::i()->replace( 'core_search_index_tags', array( 'index_id' => $newIndexId, 'index_tag' => $tag, 'index_is_prefix' => ( $tag == $prefix ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
/* Populate the map table, we always populate it under the item class regardless */
|
||||
if ( $existingData == NULL or ( $existingData !== NULL and ( $existingData['index_author'] != $indexData['index_author'] OR $existingData['index_item_id'] != $indexData['index_item_id'] ) ) )
|
||||
{
|
||||
\IPS\Db::i()->replace( 'core_search_index_item_map', array( 'index_author_id' => $indexData['index_author'], 'index_item_id' => $indexData['index_item_id'], 'index_class' => ( $object instanceof \IPS\Content\Comment ? $object::$itemClass : $class ) ) );
|
||||
}
|
||||
|
||||
$databaseColumnId = $object::$databaseColumnId;
|
||||
|
||||
/* Set index_item_index_id on other index items */
|
||||
if ( $existingIndexId != $newIndexId )
|
||||
{
|
||||
if ( $object instanceof \IPS\Content\Item )
|
||||
{
|
||||
$subClasses = array( $class );
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$subClasses[] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$subClasses[] = $class::$reviewClass;
|
||||
}
|
||||
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_item_index_id' => $newIndexId ), array( array( \IPS\Db::i()->in( 'index_class', $subClasses ) ), array( 'index_item_id=?', $object->$databaseColumnId ) ) );
|
||||
}
|
||||
elseif ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$itemClass = $object::$itemClass;
|
||||
if ( $itemClass::$firstCommentRequired and $object->isFirst() )
|
||||
{
|
||||
$itemColumnId = $class::$databaseColumnMap['item'];
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_item_index_id' => $newIndexId ), array( \IPS\Db::i()->in( 'index_class', array( $class, $class::$itemClass ) ) . ' AND index_item_id=?', $object->$itemColumnId ) );
|
||||
}
|
||||
}
|
||||
|
||||
/* And also set core_follow.follow_index_id */
|
||||
if ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$class = $class::$itemClass;
|
||||
}
|
||||
\IPS\Db::i()->update( 'core_follow', array( 'follow_index_id' => $newIndexId ), array( 'follow_app=? AND follow_area=? AND follow_rel_id=?', $object::$application, mb_strtolower( mb_substr( $class, mb_strrpos( $class, '\\' ) + 1 ) ), $object->$databaseColumnId ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $resetLastComment )
|
||||
{
|
||||
$this->resetLastComment( array( $indexData['index_class'] ), $indexData['index_item_id'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,22 +229,93 @@ class _Index extends \IPS\Content\Search\Index
|
||||
{
|
||||
$class = get_class( $object );
|
||||
$idColumn = $class::$databaseColumnId;
|
||||
|
||||
/* Tags */
|
||||
$this->_deleteTagsFromIndex( $class, $object->$idColumn );
|
||||
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? AND index_object_id=?', $class, $object->$idColumn ) );
|
||||
|
||||
/* If this was a comment, we really need to reset the index_is_last_comment flag if it was set */
|
||||
if ( $object instanceof \IPS\Content\Comment )
|
||||
{
|
||||
$itemClass = $object::$itemClass;
|
||||
$classes = array( $class );
|
||||
|
||||
if ( ! $itemClass::$firstCommentRequired )
|
||||
{
|
||||
$classes[] = $itemClass;
|
||||
}
|
||||
|
||||
if ( isset( $itemClass::$reviewClass ) )
|
||||
{
|
||||
$classes[] = $itemClass::$reviewClass;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$this->resetLastComment( $classes, $object->mapped('item') );
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
|
||||
/* We need to see if this is the only comment the author has in this item and if so, remove their map */
|
||||
if ( ! \IPS\Db::i()->select( 'COUNT(*)', 'core_search_index', array( \IPS\Db::i()->in('index_class', $classes ) . ' and index_item_id=? and index_author=?', $object->mapped('item'), (int) $object->mapped('author') ) )->first() )
|
||||
{
|
||||
try
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_search_index_item_map', array( 'index_class=? AND index_item_id=? and index_author_id=?', $itemClass, $object->mapped('item'), (int) $object->mapped('author') ) );
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
}
|
||||
}
|
||||
else if ( $object instanceof \IPS\Content\Item )
|
||||
{
|
||||
/* Just remove all rows matching the item and class. */
|
||||
\IPS\Db::i()->delete( 'core_search_index_item_map', array( 'index_class=? AND index_item_id=?', $class, $object->$idColumn ) );
|
||||
}
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
$this->_deleteTagsFromIndex( $commentClass, $object->$idColumn );
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? AND index_item_id=?', $commentClass, $object->$idColumn ) );
|
||||
}
|
||||
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
$this->_deleteTagsFromIndex( $reviewClass, $object->$idColumn );
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_class=? AND index_item_id=?', $reviewClass, $object->$idColumn ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the index IDs associated with this class and $id
|
||||
*
|
||||
* @param \IPS\Content object $class The class
|
||||
* @param int $id The index_item_id
|
||||
* @return array
|
||||
*/
|
||||
protected function _deleteTagsFromIndex( $class, $id )
|
||||
{
|
||||
try
|
||||
{
|
||||
$ids = iterator_to_array( \IPS\Db::i()->select( 'index_id', 'core_search_index', array( 'index_class=? AND index_item_id=?', $class, $id ) ) );
|
||||
}
|
||||
catch( \Exception $ex )
|
||||
{
|
||||
$ids = FALSE;
|
||||
}
|
||||
|
||||
if ( is_array( $ids ) and count( $ids ) < 1000 )
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_search_index_tags', array( \IPS\Db::i()->in( 'index_id', $ids ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\Db::i()->delete( 'core_search_index_tags', array( 'index_id IN( ? )', \IPS\Db::i()->select( 'index_id', 'core_search_index', array( 'index_class=? AND index_item_id=?', $class, $id ) ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all content for a classs
|
||||
*
|
||||
@@ -149,24 +331,27 @@ class _Index extends \IPS\Content\Search\Index
|
||||
$where[] = array( 'index_container_id=?', $containerId );
|
||||
}
|
||||
|
||||
\IPS\Db::i()->delete( 'core_search_index_item_map', array( 'index_item_id IN( ? )', \IPS\Db::i()->select( 'index_item_id', 'core_search_index', $where ) ) );
|
||||
\IPS\Db::i()->delete( 'core_search_index_tags', array( 'index_id IN( ? )', \IPS\Db::i()->select( 'index_id', 'core_search_index', $where ) ) );
|
||||
\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)
|
||||
* @param int|NULL $authorId The author ID to update, or NULL
|
||||
* @param int|NULL $newItemId The new item ID (if applicable)
|
||||
* @param int|NULL $newItemAuthorId The new item author ID (if applicable)
|
||||
* @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)
|
||||
* @param int|NULL $authorId The author ID to update, or NULL
|
||||
* @param int|NULL $newItemId The new item ID (if applicable)
|
||||
* @param int|NULL $newItemAuthorId The new item author ID (if applicable)
|
||||
* @param bool $addAuthorToPermissions If true, the index_author_id will be added to $newPermissions - used when changing the permissions for a node which allows access only to author's items
|
||||
* @return void
|
||||
*/
|
||||
public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL, $authorId = NULL, $newItemId = NULL, $newItemAuthorId = NULL )
|
||||
public function massUpdate( $class, $containerId = NULL, $itemId = NULL, $newPermissions = NULL, $newHiddenStatus = NULL, $newContainer = NULL, $authorId = NULL, $newItemId = NULL, $newItemAuthorId = NULL, $addAuthorToPermissions = FALSE )
|
||||
{
|
||||
$where = array( array( 'index_class=?', $class ) );
|
||||
if ( $containerId !== NULL )
|
||||
@@ -204,6 +389,12 @@ class _Index extends \IPS\Content\Search\Index
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_search_index', $update, $where );
|
||||
}
|
||||
if ( $addAuthorToPermissions )
|
||||
{
|
||||
$addAuthorToPermissionsWhere = $where;
|
||||
$addAuthorToPermissionsWhere[] = array( 'index_author<>0' );
|
||||
\IPS\Db::i()->update( 'core_search_index', "index_permissions = CONCAT( index_permissions, ',m.', index_author )", $addAuthorToPermissionsWhere );
|
||||
}
|
||||
|
||||
if ( $newHiddenStatus !== NULL )
|
||||
{
|
||||
@@ -239,7 +430,6 @@ class _Index extends \IPS\Content\Search\Index
|
||||
if ( $item::$firstCommentRequired )
|
||||
{
|
||||
$update['index_title'] = NULL;
|
||||
$update['index_tags'] = NULL;
|
||||
}
|
||||
\IPS\Db::i()->update( 'core_search_index', $update, array( 'index_class=? AND index_item_id=?', $item::$commentClass, $item->$idColumn ) );
|
||||
|
||||
@@ -264,11 +454,37 @@ class _Index extends \IPS\Content\Search\Index
|
||||
{
|
||||
if ( $cutoff )
|
||||
{
|
||||
\IPS\DB::i()->delete( 'core_search_index', array( 'index_date_updated < ?', $cutoff->getTimestamp() ) );
|
||||
\IPS\Db::i()->delete( 'core_search_index_item_map', array( 'index_item_id IN( ? )', \IPS\Db::i()->select( 'index_item_id', 'core_search_index', array( 'index_date_updated < ?', $cutoff->getTimestamp() ) ) ) );
|
||||
\IPS\Db::i()->delete( 'core_search_index_tags', array( 'index_id IN( ? )', \IPS\Db::i()->select( 'index_id', 'core_search_index', array( 'index_date_updated < ?', $cutoff->getTimestamp() ) ) ) );
|
||||
\IPS\Db::i()->delete( 'core_search_index', array( 'index_date_updated < ?', $cutoff->getTimestamp() ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
\IPS\DB::i()->delete( 'core_search_index' );
|
||||
\IPS\Db::i()->delete( 'core_search_index_item_map' );
|
||||
\IPS\Db::i()->delete( 'core_search_index_tags' );
|
||||
\IPS\Db::i()->delete( 'core_search_index' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the last comment flag in any given class/index_item_id
|
||||
*
|
||||
* @param array $classes The classes (when first post is required, this is typically just \IPS\forums\Topic\Post but for others, it will be both item and comment classes)
|
||||
* @param int|NULL $indexItemId The index item ID
|
||||
* @return void
|
||||
*/
|
||||
public function resetLastComment( $classes, $indexItemId )
|
||||
{
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_is_last_comment' => 0 ), array( \IPS\Db::i()->in( 'index_class', $classes ) . ' AND index_item_id=?', $indexItemId ) );
|
||||
|
||||
try
|
||||
{
|
||||
$latest = \IPS\Db::i()->select( 'index_object_id, index_date_updated, index_class', 'core_search_index', array( \IPS\Db::i()->in( 'index_class', $classes ) . ' AND index_item_id=? and index_hidden=0', $indexItemId ), 'index_date_created DESC', array( 0, 1 ) )->first();
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_is_last_comment' => 1 ), array( 'index_class=? AND index_object_id=?', $latest['index_class'], $latest['index_object_id'] ) );
|
||||
|
||||
/* Now reset the item index with the latest comment time */
|
||||
\IPS\Db::i()->update( 'core_search_index', array( 'index_date_updated' => $latest['index_date_updated'] ), array( \IPS\Db::i()->in( 'index_class', $classes ) . ' AND index_item_id=? AND index_object_id=?', $indexItemId, $indexItemId ) );
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @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.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -38,6 +38,11 @@ class _Query extends \IPS\Content\Search\Query
|
||||
*/
|
||||
protected $hiddenClause = NULL;
|
||||
|
||||
/**
|
||||
* @brief The WHERE clause for last updated date
|
||||
*/
|
||||
protected $lastUpdatedClause = NULL;
|
||||
|
||||
/**
|
||||
* @brief The offset
|
||||
*/
|
||||
@@ -57,6 +62,17 @@ class _Query extends \IPS\Content\Search\Query
|
||||
* @brief Item classes included
|
||||
*/
|
||||
protected $itemClasses = NULL;
|
||||
|
||||
/**
|
||||
* @brief Force specific table index
|
||||
*/
|
||||
protected $forceIndex = NULL;
|
||||
|
||||
/**
|
||||
* @brief Filter by items I posted in?
|
||||
* @see filterByItemsIPostedIn()
|
||||
*/
|
||||
protected $filterByItemsIPostedIn = FALSE;
|
||||
|
||||
/**
|
||||
* Filter by multiple content types
|
||||
@@ -74,7 +90,7 @@ class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
$this->itemClasses = array();
|
||||
}
|
||||
|
||||
|
||||
/* Loop the filters */
|
||||
foreach ( $contentFilters as $filter )
|
||||
{
|
||||
@@ -101,6 +117,18 @@ class _Query extends \IPS\Content\Search\Query
|
||||
$clause[] = \IPS\Db::i()->in( 'index_container_id', $filter->containerIds, $filter->containerIdFilter === FALSE );
|
||||
}
|
||||
|
||||
$itemClass = $filter->itemClass;
|
||||
if ( isset( $itemClass::$containerNodeClass ) )
|
||||
{
|
||||
$containerClass = $itemClass::$containerNodeClass;
|
||||
$unsearchableIds = $containerClass::unsearchableNodeIds();
|
||||
|
||||
if ( $unsearchableIds != NULL )
|
||||
{
|
||||
$clause[] = \IPS\Db::i()->in( 'index_container_id', $unsearchableIds, TRUE );
|
||||
}
|
||||
}
|
||||
|
||||
/* Set the item IDs */
|
||||
if ( $filter->itemIdFilter !== NULL )
|
||||
{
|
||||
@@ -181,7 +209,7 @@ class _Query extends \IPS\Content\Search\Query
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter for profile
|
||||
@@ -191,9 +219,26 @@ class _Query extends \IPS\Content\Search\Query
|
||||
*/
|
||||
public function filterForProfile( \IPS\Member $member )
|
||||
{
|
||||
$this->where[] = array( '( index_author=? OR ( index_class=? AND index_container_id=? ) )', $member->member_id, 'IPS\core\Statuses\Status', $member->member_id );
|
||||
|
||||
return $this;
|
||||
/* Check to see if this member has even had anyone write on their status */
|
||||
$count = \IPS\Db::i()->select( 'COUNT(*)', 'core_search_index', array( 'index_class=? AND index_container_id=?', 'IPS\core\Statuses\Status', $member->member_id ) )->first();
|
||||
|
||||
if ( $count )
|
||||
{
|
||||
if ( $count > 500 )
|
||||
{
|
||||
$this->where[] = array( '( index_author=? OR ( index_class=? AND index_container_id=? ) )', $member->member_id, 'IPS\core\Statuses\Status', $member->member_id );
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->where[] = array( '( index_author=? OR (' . \IPS\Db::i()->in( 'index_id', iterator_to_array( \IPS\Db::i()->select( 'index_id', 'core_search_index', array( 'index_class=? AND index_container_id=?', 'IPS\core\Statuses\Status', $member->member_id ) ) ) ) . ') )', $member->member_id );
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->filterByAuthor( $member );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -221,6 +266,9 @@ class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
$where = array();
|
||||
$params = array();
|
||||
$followApps = $followAreas = $case = $containerCase = array();
|
||||
$followedItems = array();
|
||||
$followedContainers = array();
|
||||
|
||||
/* Are we including items or containers? */
|
||||
if ( $includeContainers or $includeItems )
|
||||
@@ -240,60 +288,114 @@ class _Query extends \IPS\Content\Search\Query
|
||||
}
|
||||
|
||||
/* Loop them */
|
||||
|
||||
foreach ( $classes as $class )
|
||||
{
|
||||
if( is_subclass_of( $class, 'IPS\Content\Followable' ) )
|
||||
{
|
||||
/* Construct a subquery which will get the IDs of the items of this class that we follow */
|
||||
$subQueryWhere = array( array( 'follow_app=?', $class::$application ) );
|
||||
$followApps[ $class::$application ] = $class::$application;
|
||||
$followArea = mb_strtolower( mb_substr( $class, mb_strrpos( $class, '\\' ) + 1 ) );
|
||||
|
||||
if ( $includeContainers and $includeItems )
|
||||
{
|
||||
$subQueryWhere[] = array( '( follow_area=? OR follow_area=? )', mb_strtolower( mb_substr( $class::$containerNodeClass, mb_strrpos( $class::$containerNodeClass, '\\' ) + 1 ) ), mb_strtolower( mb_substr( $class, mb_strrpos( $class, '\\' ) + 1 ) ) );
|
||||
$followAreas[] = mb_strtolower( mb_substr( $class::$containerNodeClass, mb_strrpos( $class::$containerNodeClass, '\\' ) + 1 ) );
|
||||
$followAreas[] = $followArea;
|
||||
}
|
||||
elseif ( $includeItems )
|
||||
{
|
||||
$subQueryWhere[] = array( 'follow_area=?', mb_strtolower( mb_substr( $class, mb_strrpos( $class, '\\' ) + 1 ) ) );
|
||||
$followAreas[] = $followArea;
|
||||
}
|
||||
elseif ( $includeContainers )
|
||||
{
|
||||
$subQueryWhere[] = array( 'follow_area=?', mb_strtolower( mb_substr( $class::$containerNodeClass, mb_strrpos( $class::$containerNodeClass, '\\' ) + 1 ) ) );
|
||||
$followAreas[] = mb_strtolower( mb_substr( $class::$containerNodeClass, mb_strrpos( $class::$containerNodeClass, '\\' ) + 1 ) );
|
||||
}
|
||||
$subQueryWhere[] = array( 'follow_member_id=?', $this->member->member_id );
|
||||
$subQuery = \IPS\Db::i()->select( 'follow_rel_id', 'core_follow', $subQueryWhere );
|
||||
|
||||
/* If we're only doing one class, there's no point in doing "item_class=? AND ( item_class=? AND item_id IN(?) )" - we can just do "item_class=? AND item_id IN(?)" */
|
||||
if ( count( $classes ) == 1 )
|
||||
/* Work out what classes this applies to - need to specify comment and review classes */
|
||||
if ( ! $class::$firstCommentRequired )
|
||||
{
|
||||
$where[] = 'index_item_id IN(?)';
|
||||
$params[] = $subQuery;
|
||||
$case[ $followArea ][] = $class;
|
||||
}
|
||||
/* But otherwise we need to group by class, because item IDs are not unique */
|
||||
else
|
||||
|
||||
if( $includeContainers )
|
||||
{
|
||||
/* Work out what classes this applies to - need to specify comment and review classes */
|
||||
$_classes = array( $class );
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$_classes[] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$_classes[] = $class::$reviewClass;
|
||||
}
|
||||
|
||||
/* Constuct the where */
|
||||
$where[] = '( ' . \IPS\Db::i()->in( 'index_class', $_classes ) . ' AND index_item_id IN( ? ) )';
|
||||
$params[] = $subQuery;
|
||||
$containerCase[ $followArea ] = mb_strtolower( mb_substr( $class::$containerNodeClass, mb_strrpos( $class::$containerNodeClass, '\\' ) + 1 ) ) ;
|
||||
}
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$case[ $followArea ][] = $class::$commentClass;
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$case[ $followArea ][] = $class::$reviewClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the stuff we follow */
|
||||
foreach( \IPS\Db::i()->select( '*', 'core_follow', array( 'follow_member_id=? AND ' . \IPS\Db::i()->in( 'follow_app', $followApps ) . ' AND ' . \IPS\Db::i()->in( 'follow_area', $followAreas ), $this->member->member_id ) ) as $follow )
|
||||
{
|
||||
if( array_key_exists( $follow['follow_area'], $case ) )
|
||||
{
|
||||
$followedItems[ $follow['follow_area'] ][] = $follow['follow_rel_id'];
|
||||
}
|
||||
else if( in_array( $follow['follow_area'], $containerCase ) )
|
||||
{
|
||||
$followedContainers[ $follow['follow_area'] ][] = $follow['follow_rel_id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach( $followedItems as $area => $item )
|
||||
{
|
||||
$where[] = '( ' . \IPS\Db::i()->in( 'index_class', $case[ $area ] ) . " AND index_item_id IN(" . implode( ',', $item ) . ") )";
|
||||
}
|
||||
|
||||
foreach( $followedContainers as $area => $container )
|
||||
{
|
||||
$indexClasses = array();
|
||||
|
||||
foreach( $containerCase as $followArea => $containerArea )
|
||||
{
|
||||
if( $containerArea == $area )
|
||||
{
|
||||
$indexClasses = $case[ $followArea ];
|
||||
}
|
||||
}
|
||||
|
||||
$where[] = '( ' . \IPS\Db::i()->in( 'index_class', $indexClasses ) . " AND index_container_id IN(" . implode( ',', $container ) . ") )";
|
||||
}
|
||||
|
||||
/* Are we including content posted by followed members? */
|
||||
if ( $includeMembers )
|
||||
{
|
||||
$where[] = 'index_author IN(?)';
|
||||
$params[] = \IPS\Db::i()->select( 'follow_rel_id', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_member_id=?', 'core', 'member', $this->member->member_id ) );
|
||||
/* Another area where a small result set can drastically slow down the entire query */
|
||||
try
|
||||
{
|
||||
$followed = iterator_to_array( \IPS\Db::i()->select( 'follow_rel_id', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_member_id=?', 'core', 'member', $this->member->member_id ), 'follow_rel_id asc', array( 0, 501 ) ) );
|
||||
|
||||
if ( count( $followed ) == 501 )
|
||||
{
|
||||
/* Assume we have loads of matches, so do a full query */
|
||||
$where[] = 'index_author IN(?)';
|
||||
$params[] = \IPS\Db::i()->select( 'follow_rel_id', 'core_follow', array( 'follow_app=? AND follow_area=? AND follow_member_id=?', 'core', 'member', $this->member->member_id ) );
|
||||
}
|
||||
else if ( count( $followed ) )
|
||||
{
|
||||
/* IN is not a SIN. It's been a long day */
|
||||
$where[] = \IPS\Db::i()->in( 'index_author', $followed );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* There are no results */
|
||||
$this->where[] = "1=2 /*Filter by followed returned nothing*/";
|
||||
}
|
||||
}
|
||||
catch( \UnderflowException $ex )
|
||||
{
|
||||
/* There are no results */
|
||||
$this->where[] = "1=2 /*Filter by followed returned nothing*/";
|
||||
}
|
||||
}
|
||||
|
||||
/* Put it all together */
|
||||
@@ -301,33 +403,30 @@ class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
$this->where[] = array_merge( array( '( ' . implode( ' OR ', $where ) . ' )' ), $params );
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
/* If we want to filter by followed content, and we don't actually follow any content then we shouldn't return anything */
|
||||
$this->where[] = "1=2 /*Filter by followed returned nothing*/";
|
||||
}
|
||||
|
||||
/* And return */
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by content the user has posted in
|
||||
* Filter by content the user has posted in. This must be at the end of the chain.
|
||||
*
|
||||
* @return \IPS\Content\Search\Query (for daisy chaining)
|
||||
*/
|
||||
public function filterByItemsIPostedIn()
|
||||
{
|
||||
$this->joins[] = array(
|
||||
'from' => \IPS\Db::i()->select(
|
||||
'index_item_index_id',
|
||||
array('core_search_index', 'sub' ),
|
||||
array( array( 'index_author=' . intval( $this->member->member_id ) ) ),
|
||||
NULL, NULL, NULL, NULL, \IPS\Db::SELECT_DISTINCT
|
||||
),
|
||||
'where' => 'sub.index_item_index_id=main.index_item_index_id',
|
||||
'type' => 'STRAIGHT_JOIN'
|
||||
);
|
||||
{
|
||||
/* We have to set a property because we need the other data like other filters and ordering to figure this out */
|
||||
$this->filterByItemsIPostedIn = TRUE;
|
||||
|
||||
/* Return for daisy chaining */
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filter by content the user has not read
|
||||
*
|
||||
@@ -382,8 +481,11 @@ class _Query extends \IPS\Content\Search\Query
|
||||
}
|
||||
}
|
||||
|
||||
/* Put it all together */
|
||||
$this->where[] = array_merge( array( '( ' . implode( ' OR ', $where ) . ' )' ), $params );
|
||||
if ( count( $where ) )
|
||||
{
|
||||
/* Put it all together */
|
||||
$this->where[] = array_merge( array( '( ' . implode( ' OR ', $where ) . ' )' ), $params );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,6 +544,11 @@ class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
unset( $markers[ $_key ] );
|
||||
}
|
||||
/* If the existing timestamp is higher, retain that since we reset the $markers array below */
|
||||
else
|
||||
{
|
||||
$mtime = $_mtime;
|
||||
}
|
||||
}
|
||||
|
||||
$markers[ $mtime . '.' . $mid ] = $mid;
|
||||
@@ -546,11 +653,11 @@ class _Query extends \IPS\Content\Search\Query
|
||||
{
|
||||
if ( $start )
|
||||
{
|
||||
$this->where[] = array( 'index_date_updated>?', $start->getTimestamp() );
|
||||
$this->lastUpdatedClause[] = array( 'index_date_updated>?', $start->getTimestamp() );
|
||||
}
|
||||
if ( $end )
|
||||
{
|
||||
$this->where[] = array( 'index_date_updated<?', $end->getTimestamp() );
|
||||
$this->lastUpdatedClause[] = array( 'index_date_updated<?', $end->getTimestamp() );
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
@@ -615,7 +722,7 @@ class _Query extends \IPS\Content\Search\Query
|
||||
break;
|
||||
|
||||
case static::ORDER_OLDEST_CREATED:
|
||||
$this->order = 'index_date_updated DESC';
|
||||
$this->order = 'index_date_created ASC';
|
||||
break;
|
||||
|
||||
case static::ORDER_RELEVANCY:
|
||||
@@ -636,86 +743,77 @@ class _Query extends \IPS\Content\Search\Query
|
||||
*/
|
||||
protected function _searchWhereClause( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
/* Boolean tags */
|
||||
$tagWhere = NULL;
|
||||
$termWhere = NULL;
|
||||
|
||||
/* Do we have tags? */
|
||||
if ( $tags !== NULL )
|
||||
{
|
||||
$theTags = array();
|
||||
foreach( $tags as $tagEntry )
|
||||
$itemsOnlyTagSearch = '';
|
||||
|
||||
if ( $method & static::TAGS_MATCH_ITEMS_ONLY )
|
||||
{
|
||||
foreach( explode( ' ', $tagEntry ) as $_term )
|
||||
$itemsOnlyTagSearch = 'index_item_index_id=index_id AND ';
|
||||
}
|
||||
|
||||
/* Large index tables and small tag tables can cause a significant slow down in this query execution, so we attempt to pre-fetch some results */
|
||||
try
|
||||
{
|
||||
$tagIds = iterator_to_array( \IPS\Db::i()->select( 'index_id', 'core_search_index_tags', array( \IPS\Db::i()->in( 'index_tag', $tags ) ), 'index_id asc', array( 0, 501 ) ) );
|
||||
|
||||
/* Now, if we have 501 results, then we have to assume there are more, so the join is required */
|
||||
if ( count( $tagIds ) == 501 )
|
||||
{
|
||||
if( mb_strlen( $_term ) > 2 )
|
||||
{
|
||||
$theTags[] = "+" . $_term;
|
||||
}
|
||||
else
|
||||
{
|
||||
$theTags[] = $_term;
|
||||
}
|
||||
$tagWhere = array( $itemsOnlyTagSearch . 'index_item_index_id IN ( ? )', \IPS\Db::i()->select( 'index_id', 'core_search_index_tags', array( \IPS\Db::i()->in( 'index_tag', $tags ) ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$tagWhere = array( $itemsOnlyTagSearch . \IPS\Db::i()->in( 'index_item_index_id', $tagIds ) );
|
||||
}
|
||||
}
|
||||
catch( \UnderflowException $ex )
|
||||
{
|
||||
/* No matches at all */
|
||||
if ( $method & static::TERM_AND_TAGS )
|
||||
{
|
||||
/* We want to match term and tags, so return an impossible result set */
|
||||
$tagWhere = array( "1=2" );
|
||||
}
|
||||
else
|
||||
{
|
||||
$tagWhere = array( "1=1" );
|
||||
}
|
||||
|
||||
$searchTags[] = implode( ' ', $theTags );
|
||||
}
|
||||
}
|
||||
|
||||
/* Do we have a term? */
|
||||
$where = array();
|
||||
if ( $term !== NULL )
|
||||
{
|
||||
$indexes = 'index_content,index_title';
|
||||
if ( $method & static::TERM_TITLES_ONLY )
|
||||
$termWhere = static::matchClause( $method & static::TERM_TITLES_ONLY ? 'index_title' : 'index_content,index_title', $term, $method & static::TERM_OR_MODE ? '' : '+' );
|
||||
}
|
||||
|
||||
/* Put those two together */
|
||||
if ( $termWhere !== NULL and $tagWhere !== NULL )
|
||||
{
|
||||
if ( $method & static::TERM_OR_TAGS )
|
||||
{
|
||||
$indexes = 'index_title';
|
||||
$where[] = array_merge( array( '( ( ' . array_shift( $termWhere ) . ' ) OR ( ' . array_shift( $tagWhere ) . ' ) )' ), array_merge( $termWhere, $tagWhere ) );
|
||||
}
|
||||
|
||||
/* Default to 'AND' boolean mode search unless asked otherwise*/
|
||||
if ( ! ( $method & static::TERM_OR_MODE ) and ! static::termIsPhrase( $term ) )
|
||||
{
|
||||
$theWords = array();
|
||||
foreach( static::termAsWordsArray( $term ) as $_term )
|
||||
{
|
||||
$potentialOperator = mb_substr( $_term, 0, 1 );
|
||||
|
||||
if ( ! in_array( $potentialOperator, array( '-', '+', '~' ) ) )
|
||||
{
|
||||
if( mb_strlen( $_term ) > 2 )
|
||||
{
|
||||
$theWords[] = "+" . $_term;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$term = implode( ' ', $theWords );
|
||||
}
|
||||
|
||||
/* If we also have tags, create a combined where */
|
||||
if ( $tags !== NULL )
|
||||
{
|
||||
if ( $method & static::TERM_OR_TAGS )
|
||||
{
|
||||
$where[] = array( "( MATCH({$indexes}) AGAINST (? IN BOOLEAN MODE) ) OR " . \IPS\Db::i()->findInSet( 'index_tags', $tags ), $term );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where[] = array( "MATCH({$indexes}) AGAINST (? IN BOOLEAN MODE)", $term );
|
||||
$where[] = \IPS\Db::i()->findInSet( 'index_tags', $tags );
|
||||
}
|
||||
}
|
||||
/* Or just use the term */
|
||||
else
|
||||
{
|
||||
$where[] = array( "MATCH({$indexes}) AGAINST (? IN BOOLEAN MODE)", $term );
|
||||
$where[] = $termWhere;
|
||||
$where[] = $tagWhere;
|
||||
}
|
||||
}
|
||||
/* Or do we have tags? */
|
||||
elseif ( $tags !== NULL )
|
||||
{
|
||||
$where[] = \IPS\Db::i()->findInSet( 'index_tags', $tags );
|
||||
|
||||
if ( $this->order = 'calcscore DESC' )
|
||||
{
|
||||
$this->setOrder( static::ORDER_NEWEST_UPDATED );
|
||||
}
|
||||
/* Or just use the term if that's all we have */
|
||||
elseif ( $termWhere !== NULL )
|
||||
{
|
||||
$where[] = $termWhere;
|
||||
}
|
||||
/* Or just use tags if that's all we have */
|
||||
elseif ( $tagWhere !== NULL )
|
||||
{
|
||||
$where[] = $tagWhere;
|
||||
}
|
||||
|
||||
/* Only get stuff we have permission for */
|
||||
@@ -725,16 +823,271 @@ class _Query extends \IPS\Content\Search\Query
|
||||
$where[] = $this->hiddenClause;
|
||||
}
|
||||
|
||||
/* MOST PERTINENT search? */
|
||||
if ( mb_substr( $this->order, 0, 9 ) === 'calcscore' )
|
||||
/* Filer by items I posted in? */
|
||||
if ( $this->filterByItemsIPostedIn and $this->member->member_id )
|
||||
{
|
||||
$this->select['main'] .= ',' . "( ( MATCH (index_title) AGAINST ('" . \IPS\Db::i()->escape_string( $term ) ."' IN BOOLEAN MODE) * 5 ) + (MATCH ({$indexes}) AGAINST ('" . \IPS\Db::i()->escape_string( $term ) ."' IN BOOLEAN MODE) ) ) / POWER( ( ( UNIX_TIMESTAMP( NOW() ) - index_date_updated ) / 3600 ) + 2, 1.5 ) AS calcscore";
|
||||
/* Another area where a small result set can drastically slow down the entire query */
|
||||
try
|
||||
{
|
||||
/* Work out what classes we need to examine */
|
||||
if ( $this->itemClasses !== NULL )
|
||||
{
|
||||
$classes = $this->itemClasses;
|
||||
}
|
||||
else
|
||||
{
|
||||
$classes = array();
|
||||
foreach ( \IPS\Application::allExtensions( 'core', 'ContentRouter', FALSE ) as $object )
|
||||
{
|
||||
$classes = array_merge( $object->classes, $classes );
|
||||
}
|
||||
}
|
||||
|
||||
$results = iterator_to_array( \IPS\Db::i()->select( 'index_item_id, index_class', array( 'core_search_index_item_map', 'sub' ), array( array( 'index_author_id=' . intval( $this->member->member_id ) . ' AND ' . \IPS\Db::i()->in( 'index_class', $classes ) ) ), 'index_item_id desc', array( 0, 501 ) ) );
|
||||
|
||||
if ( count( $results ) == 501 )
|
||||
{
|
||||
/* Assume we have loads of matches, so do a full query */
|
||||
$subClause = array( array( 'sub.index_item_id=main.index_item_id AND index_author_id=' . intval( $this->member->member_id ) . ' AND ' . $this->makeEmbeddedIfClauseForSearchMapTable('sub.index_class') ) );
|
||||
$query = \IPS\Db::i()->select( 'index_item_id', array( 'core_search_index_item_map', 'sub' ), $subClause );
|
||||
$where[] = array( 'EXISTS(?)', $query );
|
||||
}
|
||||
else if ( count( $results ) )
|
||||
{
|
||||
$classIds = array();
|
||||
foreach( $results as $result )
|
||||
{
|
||||
$classIds[ $result['index_class'] ][] = $result['index_item_id'];
|
||||
}
|
||||
|
||||
$subWhere = array();
|
||||
foreach( $classIds as $class => $ids )
|
||||
{
|
||||
$stack = array( $class );
|
||||
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$stack[] = $class::$commentClass;
|
||||
}
|
||||
|
||||
$subWhere[] = '(' . \IPS\Db::i()->in( 'index_class', $stack ) . ' and ' . \IPS\Db::i()->in( 'index_item_id', $ids ) . ')';
|
||||
}
|
||||
|
||||
/* IN is not a SIN. It's been a long day */
|
||||
$where[] = '(' . implode( ' OR ', $subWhere ) . ')';
|
||||
}
|
||||
else
|
||||
{
|
||||
/* There are no results */
|
||||
$where[] = "1=2 /*Filter by items I posted in returned nothing*/";
|
||||
}
|
||||
}
|
||||
catch( \UnderflowException $ex )
|
||||
{
|
||||
/* There are no results */
|
||||
$where[] = "1=2 /*Filter by items I posted in returned nothing*/";
|
||||
}
|
||||
}
|
||||
|
||||
/* Filter by last updated? */
|
||||
if ( $this->lastUpdatedClause !== NULL )
|
||||
{
|
||||
foreach( $this->lastUpdatedClause as $clause )
|
||||
{
|
||||
$where[] = $clause;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return */
|
||||
return $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an embedded search clause to convert comment classes to item classes
|
||||
*
|
||||
* @param string $column Column to use in the clause
|
||||
* @return string
|
||||
*/
|
||||
protected function makeEmbeddedIfClauseForSearchMapTable( $column )
|
||||
{
|
||||
/* Work out what classes we need to examine */
|
||||
if ( $this->itemClasses !== NULL )
|
||||
{
|
||||
$classes = $this->itemClasses;
|
||||
}
|
||||
else
|
||||
{
|
||||
$classes = array();
|
||||
foreach ( \IPS\Application::allExtensions( 'core', 'ContentRouter', FALSE ) as $object )
|
||||
{
|
||||
$classes = array_merge( $object->classes, $classes );
|
||||
}
|
||||
}
|
||||
|
||||
$stack = array();
|
||||
foreach( $classes as $class )
|
||||
{
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$stack[ $class::$commentClass ] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
if ( count( $stack ) )
|
||||
{
|
||||
$if = '';
|
||||
foreach( $stack as $commentClass => $class )
|
||||
{
|
||||
$if .= " IF( main.index_class='" . \IPS\Db::i()->escape_string( $commentClass ) . "', '" . \IPS\Db::i()->escape_string( $class ) . "', ";
|
||||
}
|
||||
|
||||
$if .= 'main.index_class' . str_repeat( ' )', count( $stack ) );
|
||||
}
|
||||
|
||||
return $column . '=' . ( $if == '' ? $column : $if );
|
||||
}
|
||||
|
||||
/**
|
||||
* Match clause
|
||||
*
|
||||
* @param string $columns Columns to match against (e.g. "index_tags" or "index_title,index_content")
|
||||
* @param string $term The term
|
||||
* @param string $defaultOperator The default operator to add to each word if there isn't one - "+" is "and mode"; "" is "or mode"
|
||||
* @param bool $prepared If FALSE, does not use prepared statements (used for the sorting algorithm because you can't use ?s in the select clause)
|
||||
* @return array|string
|
||||
*/
|
||||
public static function matchClause( $columns, $term, $defaultOperator='+', $prepared=TRUE )
|
||||
{
|
||||
/* Loop the words */
|
||||
$words = array();
|
||||
|
||||
/* If we have a phrase, we'd normally use the boolean engine to search it, but this is incredibly slow on larger tables (5+ million rows) because MySQL searches all the words in the word index
|
||||
but if some words are below the minimum length, it performs a full table scan which is very expensive.
|
||||
The solution here is to break down the phrase (i love the chocolate cake) into usable keywords ('love +chocolate +cake') and back up the full text results with a LIKE '%i love the chocolate cake%'
|
||||
search which is actually very efficient as MySQL performs a search on the FT index first to narrow down the results */
|
||||
if ( static::termIsPhrase( $term ) )
|
||||
{
|
||||
$term = str_replace( array( "'", '"' ), '', $term );
|
||||
|
||||
foreach ( static::termAsWordsArray( $term ) as $word )
|
||||
{
|
||||
/* Operators are not allowed */
|
||||
$word = preg_replace( '/^([\+\-~<>]){1,}/', '', $word );
|
||||
$word = preg_replace( '/([\+\-~\*<>]){2,}$/', '$1', $word );
|
||||
|
||||
/* +* and +- are not allowed anywhere in the word */
|
||||
$word = str_replace( array( '+*', '+-', '+' ), '', $word );
|
||||
|
||||
/* Trailing + or -s are not allowed */
|
||||
$word = rtrim( $word, '+-' );
|
||||
|
||||
/* Nor is @ or parenthesis (while paranthesis can be used to group words and apply operators to the group,
|
||||
(e.g. "+apple +(>turnover <strudel)") - it's unlikely a user intends this behaviour) */
|
||||
$word = str_replace( array( '(', ')' ), '', str_replace( '@', ' ', $word ) );
|
||||
|
||||
if ( $word )
|
||||
{
|
||||
$words[] = $word;
|
||||
}
|
||||
}
|
||||
|
||||
$booleanTerm = implode( ' +', $words );
|
||||
|
||||
/* Return */
|
||||
if ( $prepared )
|
||||
{
|
||||
if ( mb_strstr( $columns, ',' ) )
|
||||
{
|
||||
$like = array();
|
||||
$extra = '';
|
||||
foreach( explode( ',', $columns ) as $col )
|
||||
{
|
||||
$like[] = $col . ' LIKE \'%' . \IPS\Db::i()->escape_string( $term ) . '%\'';
|
||||
}
|
||||
|
||||
$extra = implode( ' OR ', $like );
|
||||
}
|
||||
else
|
||||
{
|
||||
$extra = $columns . ' LIKE \'%' . \IPS\Db::i()->escape_string( $term ) . '%\'';
|
||||
}
|
||||
|
||||
return array( "MATCH({$columns}) AGAINST (? IN BOOLEAN MODE) AND (" . $extra . ")", $booleanTerm );
|
||||
}
|
||||
else
|
||||
{
|
||||
return "MATCH({$columns}) AGAINST ('" . \IPS\Db::i()->escape_string( $booleanTerm ) . "' IN BOOLEAN MODE)";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ( static::termAsWordsArray( $term ) as $word )
|
||||
{
|
||||
/* Add the default operator */
|
||||
if ( $defaultOperator and !in_array( mb_substr( $word, 0, 1 ), array( '+', '-', '~', '<', '>' ) ) )
|
||||
{
|
||||
/* Clear out leading * symbols so we don't end up with +*a** */
|
||||
$word = $defaultOperator . ltrim( $word, '*' );
|
||||
}
|
||||
|
||||
/* Double operators are not allowed */
|
||||
$word = preg_replace( '/^([\+\-~\*<>]){2,}/', '$1', $word );
|
||||
$word = preg_replace( '/([\+\-~\*<>]){2,}$/', '$1', $word );
|
||||
|
||||
/* Trailing + or -s are not allowed */
|
||||
$word = rtrim( $word, '+-' );
|
||||
|
||||
/* These rules only apply if we're not in a quoted phrase... */
|
||||
if ( !static::termIsPhrase( $word ) )
|
||||
{
|
||||
/* We can't have any other operators as MySQL will interpret them as a separate word. If they exist, wrap the word in quotes */
|
||||
if ( preg_match( '/^.+[\+\-~<>]/', $word ) )
|
||||
{
|
||||
$word = "\"" . str_replace( '"', '', ltrim( $word, $defaultOperator ) ) . "\"";
|
||||
}
|
||||
/* Otherwise carry on... */
|
||||
else
|
||||
{
|
||||
/* +* and +- are not allowed anywhere in the word */
|
||||
$word = str_replace( array( '+*', '+-' ), '+', $word );
|
||||
|
||||
/* Nor is @ or parenthesis (while paranthesis can be used to group words and apply operators to the group,
|
||||
(e.g. "+apple +(>turnover <strudel)") - it's unlikely a user intends this behaviour) */
|
||||
$word = str_replace( array( '(', ')' ), '', str_replace( '@', ' ', $word ) );
|
||||
}
|
||||
}
|
||||
|
||||
/* Add it */
|
||||
$words[] = $word;
|
||||
}
|
||||
$term = implode( ' ', $words );
|
||||
|
||||
/* Return */
|
||||
if ( $prepared )
|
||||
{
|
||||
if ( count( $words ) > 1 )
|
||||
{
|
||||
return array( "MATCH({$columns}) AGAINST (? IN BOOLEAN MODE)", $term );
|
||||
}
|
||||
else
|
||||
{
|
||||
return array( "MATCH({$columns}) AGAINST (?)", ltrim( $term, $defaultOperator ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( count( $words ) > 1 )
|
||||
{
|
||||
return "MATCH({$columns}) AGAINST ('" . \IPS\Db::i()->escape_string( $term ) . "' IN BOOLEAN MODE)";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "MATCH({$columns}) AGAINST ('" . \IPS\Db::i()->escape_string( ltrim( $term, $defaultOperator ) ) . "')";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
@@ -745,14 +1098,55 @@ class _Query extends \IPS\Content\Search\Query
|
||||
*/
|
||||
public function search( $term = NULL, $tags = NULL, $method = 1 )
|
||||
{
|
||||
/* Set the select clause */
|
||||
$select = implode( ', ', $this->select );
|
||||
|
||||
/* Get the where clause */
|
||||
$where = array_merge( $this->where, $this->_searchWhereClause( $term, $tags, $method ) );
|
||||
$query = \IPS\Db::i()->select( implode( ', ', $this->select ), array( 'core_search_index', 'main' ), $where, $this->order, array( $this->offset, $this->resultsToGet ), NULL, NULL );
|
||||
|
||||
/* Set order clause */
|
||||
$order = $this->order;
|
||||
|
||||
/* But we're sorting by relevancy, we need to actually select that value with our fancy algorithm */
|
||||
if ( mb_substr( $this->order, 0, 9 ) === 'calcscore' )
|
||||
{
|
||||
/* But we can only do that if there's a term (rather than tag-only) */
|
||||
if ( $term !== NULL )
|
||||
{
|
||||
$select .= ', '
|
||||
. '('
|
||||
. '(' . static::matchClause( 'index_title', $term, $method & static::TERM_OR_MODE ? '' : '+', FALSE ) . '*5)' // The title score times 5
|
||||
. '+'
|
||||
. '(' . static::matchClause( 'index_content,index_title', $term, $method & static::TERM_OR_MODE ? '' : '+', FALSE ) . ')' // Plus the content score times 1
|
||||
. ')'
|
||||
. '/' // Divided by
|
||||
. 'POWER('
|
||||
. '( ( UNIX_TIMESTAMP( NOW() ) - ( CASE WHEN index_date_updated <= UNIX_TIMESTAMP( NOW() ) THEN index_date_updated ELSE 0 END )) / 3600 ) + 2' // The number of days between now and the updated date, plus 2
|
||||
. ',1.5)' // To the power of 1.5
|
||||
. ' AS calcscore';
|
||||
}
|
||||
/* So if we don't have a term, fallback to last updated */
|
||||
else
|
||||
{
|
||||
$order = 'index_date_updated DESC';
|
||||
}
|
||||
}
|
||||
|
||||
/* Construct the query */
|
||||
$query = \IPS\Db::i()->select( $select, array( 'core_search_index', 'main' ), $where, $order, array( $this->offset, $this->resultsToGet ), NULL, NULL );
|
||||
foreach ( $this->joins as $data )
|
||||
{
|
||||
$query->join( $data['from'], $data['where'], isset( $data['type'] ) ? $data['type'] : 'LEFT' );
|
||||
}
|
||||
|
||||
$count = $this->count( $term, $tags, $method );
|
||||
|
||||
/* Force index? */
|
||||
if ( $this->forceIndex )
|
||||
{
|
||||
$query->forceIndex( $this->forceIndex );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
$count = $this->count( $term, $tags, $method, FALSE );
|
||||
return new \IPS\Content\Search\Results( iterator_to_array( $query ), $count );
|
||||
}
|
||||
|
||||
@@ -762,17 +1156,69 @@ class _Query extends \IPS\Content\Search\Query
|
||||
* @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
|
||||
* @param boolean $returnCountAsInt If TRUE, it will return the count as an integer, when FALSE it will return the \IPS\Db\Select object
|
||||
* @return \IPS\Db\Query|int
|
||||
*/
|
||||
public function count( $term = NULL, $tags = NULL, $method = 1 )
|
||||
public function count( $term = NULL, $tags = NULL, $method = 1, $returnCountAsInt=TRUE )
|
||||
{
|
||||
$query = \IPS\Db::i()->select( 'COUNT(*)', array( 'core_search_index', 'main' ), array_merge( $this->where, $this->_searchWhereClause( $term, $tags, $method ) ) );
|
||||
/* Get the where clause */
|
||||
$where = array_merge( $this->where, $this->_searchWhereClause( $term, $tags, $method ) );
|
||||
|
||||
/* Construct the query */
|
||||
$query = \IPS\Db::i()->select( 'COUNT(*)', array( 'core_search_index', 'main' ), $where );
|
||||
foreach ( $this->joins as $data )
|
||||
{
|
||||
$query->join( $data['from'], $data['where'], isset( $data['type'] ) ? $data['type'] : 'LEFT' );
|
||||
}
|
||||
|
||||
return $query->first();
|
||||
/* Return */
|
||||
return ( ! $returnCountAsInt ) ? $query : $query->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default date cut off
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultDateCutOff()
|
||||
{
|
||||
if ( static::getTableSize() >= \IPS\USE_MYSQL_SEARCH_BASIC_MODE_THRESHOLD )
|
||||
{
|
||||
return 'year';
|
||||
}
|
||||
|
||||
return 'any';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default sort method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultSortMethod()
|
||||
{
|
||||
if ( static::getTableSize() >= \IPS\USE_MYSQL_SEARCH_BASIC_MODE_THRESHOLD )
|
||||
{
|
||||
return 'newest';
|
||||
}
|
||||
|
||||
return 'relevancy';
|
||||
}
|
||||
|
||||
protected static $tableCount = NULL;
|
||||
|
||||
/**
|
||||
* Get the row count from the core_search_index table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getTableSize()
|
||||
{
|
||||
if ( static::$tableCount === NULL )
|
||||
{
|
||||
static::$tableCount = \IPS\Db::i()->select( 'COUNT(*)', 'core_search_index' )->first();
|
||||
}
|
||||
|
||||
return static::$tableCount;
|
||||
}
|
||||
}
|
||||
+155
-33
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @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.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -27,6 +27,7 @@ abstract class _Query
|
||||
const TERM_AND_TAGS = 2;
|
||||
const TERM_OR_MODE = 4;
|
||||
const TERM_TITLES_ONLY = 8;
|
||||
const TAGS_MATCH_ITEMS_ONLY = 16;
|
||||
|
||||
const HIDDEN_VISIBLE = 0;
|
||||
const HIDDEN_UNAPPROVED = 1;
|
||||
@@ -80,22 +81,13 @@ abstract class _Query
|
||||
|
||||
/* Exclude disabled applications */
|
||||
$filters = array();
|
||||
foreach ( \IPS\Application::applications() as $application )
|
||||
foreach ( \IPS\Content::routedClasses( $member, FALSE, TRUE ) as $class )
|
||||
{
|
||||
if ( !$application->enabled )
|
||||
{
|
||||
foreach ( $application->extensions( 'core', 'ContentRouter' ) as $extension )
|
||||
{
|
||||
foreach ( $extension->classes as $class )
|
||||
{
|
||||
$filters[] = \IPS\Content\Search\ContentFilter::init( $class );
|
||||
}
|
||||
}
|
||||
}
|
||||
$filters[] = \IPS\Content\Search\ContentFilter::init( $class );
|
||||
}
|
||||
if ( !empty( $filters ) )
|
||||
{
|
||||
$this->filterByContent( $filters, FALSE );
|
||||
$this->filterByContent( $filters );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,6 +223,26 @@ abstract class _Query
|
||||
*/
|
||||
abstract public function search( $term = NULL, $tags = NULL, $method = 1 );
|
||||
|
||||
/**
|
||||
* Get the default date cut off
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultDateCutOff()
|
||||
{
|
||||
return 'any';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default sort method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultSortMethod()
|
||||
{
|
||||
return 'relevancy';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count
|
||||
*
|
||||
@@ -256,38 +268,148 @@ abstract class _Query
|
||||
}
|
||||
|
||||
/**
|
||||
* Term as words array
|
||||
* Returns an array of words. A word is considered as either an entire phrase (eg "sticks stones") when $ignorePhrase is FALSE, or a sequence of characters delimited by a space.
|
||||
* Words smaller than 2 characters are ignored.
|
||||
* Convert the term into an array of words
|
||||
*
|
||||
* @param string $term The term to search for
|
||||
* @param boolean $ignorePhrase When true, phrases are stripped of quotes and treated as normal words
|
||||
* @param int $minLength The minimum length a sequence of characters has to be before it is considered a word
|
||||
* @param string $term The term to search for
|
||||
* @param boolean $ignorePhrase When true, phrases are stripped of quotes and treated as normal words
|
||||
* @param int|NULL $minLength The minimum length a sequence of characters has to be before it is considered a word. If null, ft_min_word_len/innodb_ft_min_token_size is used.
|
||||
* @param int|NULL $maxLength The maximum length a sequence of characters can be for it to be considered a word. If null, ft_max_word_len/innodb_ft_max_token_size is used.
|
||||
* @return array
|
||||
*/
|
||||
public static function termAsWordsArray( $term, $ignorePhrase=FALSE, $minLength=3 )
|
||||
{
|
||||
if ( static::termIsPhrase( $term ) )
|
||||
public static function termAsWordsArray( $term, $ignorePhrase=FALSE, $minLength=NULL, $maxLength=NULL )
|
||||
{
|
||||
/* If we haven't set a preferred min/max length, use the MySQL configuration */
|
||||
if ( $minLength === NULL or $maxLength === NULL )
|
||||
{
|
||||
if ( ! $ignorePhrase )
|
||||
/* If we don't already know what they are, get the values from the MySQL configuration */
|
||||
if ( ( $minLength === NULL and !isset( \IPS\Data\Store::i()->mysqlMinWord ) ) or ( $maxLength === NULL and !isset( \IPS\Data\Store::i()->mysqlMaxWord ) ) )
|
||||
{
|
||||
return array( $term );
|
||||
/* The variable we need depends on whether the table is MyISAM or InnoDB */
|
||||
$tableDefinition = \IPS\Db::i()->getTableDefinition('core_search_index');
|
||||
if ( $tableDefinition['engine'] == 'InnoDB' )
|
||||
{
|
||||
$minVariable = 'innodb_ft_min_token_size';
|
||||
$maxVariable = 'innodb_ft_max_token_size';
|
||||
}
|
||||
else
|
||||
{
|
||||
$minVariable = 'ft_min_word_len';
|
||||
$maxVariable = 'ft_max_word_len';
|
||||
}
|
||||
|
||||
/* Now fetch those */
|
||||
try
|
||||
{
|
||||
foreach ( new \IPS\Db\Select( 'SHOW VARIABLES WHERE Variable_Name=? OR Variable_Name=?', array( $minVariable, $maxVariable ), \IPS\Db::i() ) as $row )
|
||||
{
|
||||
if ( $row['Variable_name'] === $minVariable )
|
||||
{
|
||||
\IPS\Data\Store::i()->mysqlMinWord = intval( $row['Value'] );
|
||||
}
|
||||
elseif ( $row['Variable_name'] === $maxVariable )
|
||||
{
|
||||
\IPS\Data\Store::i()->mysqlMaxWord = intval( $row['Value'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( \IPS\Db\Exception $e ) { }
|
||||
|
||||
/* If we weren't able to get them, set sensible defaults */
|
||||
if ( !isset( \IPS\Data\Store::i()->mysqlMinWord ) )
|
||||
{
|
||||
\IPS\Data\Store::i()->mysqlMinWord = 3;
|
||||
}
|
||||
if ( !isset( \IPS\Data\Store::i()->mysqlMaxWord ) )
|
||||
{
|
||||
\IPS\Data\Store::i()->mysqlMaxWord = 84;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
/* Set */
|
||||
if ( $minLength === NULL )
|
||||
{
|
||||
$term = str_replace( '"', '', $term );
|
||||
$minLength = \IPS\Data\Store::i()->mysqlMinWord;
|
||||
}
|
||||
if ( $maxLength === NULL )
|
||||
{
|
||||
$maxLength = \IPS\Data\Store::i()->mysqlMaxWord;
|
||||
}
|
||||
}
|
||||
|
||||
$theWords = array();
|
||||
foreach( explode( ' ', $term ) as $_term )
|
||||
/* Sub quotes with smaller than min word strings can cause issues for larger tables, so we convert the sub quote into normal words for a boolean search */
|
||||
if ( ! static::termIsPhrase( $term ) )
|
||||
{
|
||||
if ( mb_strlen( $_term ) > $minLength )
|
||||
preg_match_all( '#"([^"]+?)"#', $term, $matches, PREG_SET_ORDER );
|
||||
|
||||
foreach( $matches as $match )
|
||||
{
|
||||
$theWords[] = $_term;
|
||||
$allWordsInPhraseAreGood = true;
|
||||
$subWords = array();
|
||||
foreach( explode( ' ', $match[1] ) as $subWord )
|
||||
{
|
||||
if ( mb_strlen( $subWord ) <= $minLength or mb_strlen( $subWord ) >= $maxLength )
|
||||
{
|
||||
$allWordsInPhraseAreGood = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$subWords[] = $subWord;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $allWordsInPhraseAreGood )
|
||||
{
|
||||
$term = str_replace( $match[0], implode( ' ', $subWords ), $term );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $theWords;
|
||||
|
||||
/* Parse */
|
||||
$words = array();
|
||||
$currentWord = '';
|
||||
$inQuote = false;
|
||||
$termLength = mb_strlen( $term );
|
||||
for ( $i = 0; $i < $termLength; $i++ )
|
||||
{
|
||||
$c = mb_substr( $term, $i, 1 );
|
||||
if ( $c == '"' )
|
||||
{
|
||||
if ( $ignorePhrase )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$inQuote = !$inQuote;
|
||||
}
|
||||
elseif ( $c == ' ' and !$inQuote )
|
||||
{
|
||||
$words[] = trim( $currentWord );
|
||||
$currentWord = '';
|
||||
}
|
||||
$currentWord .= $c;
|
||||
}
|
||||
$words[] = trim( $currentWord );
|
||||
|
||||
/* Now check each of the words is acceptable */
|
||||
$finalWords = array();
|
||||
foreach( $words as $word )
|
||||
{
|
||||
if ( mb_strlen( $word ) >= $minLength and mb_strlen( $word ) <= $maxLength )
|
||||
{
|
||||
$finalWords[] = $word;
|
||||
}
|
||||
}
|
||||
|
||||
/* And return */
|
||||
return $finalWords;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is a rebuild task running?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isRebuildRunning()
|
||||
{
|
||||
return \IPS\Db::i()->select( 'COUNT(*)', 'core_queue', array( '`key`=?', 'RebuildSearchIndex' ) )->first() ? TRUE : FALSE;
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @brief Search Result from Index
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 15 Sep 2015
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -59,7 +59,7 @@ class _Content extends \IPS\Content\Search\Result
|
||||
* @param array $indexData Data from index
|
||||
* @param array $authorData Basic data about the author. Only includes columns returned by \IPS\Member::columnsForPhoto()
|
||||
* @param array $itemData Basic data about the item. Only includes columns returned by item::basicDataColumns()
|
||||
* @param array|NULL $containerData Basic data about the author. Only includes columns returned by container::basicDataColumns()
|
||||
* @param array|NULL $containerData Basic data about the container. Only includes columns returned by container::basicDataColumns()
|
||||
* @param array $reputationData Array of people who have given reputation and the reputation they gave
|
||||
* @param int|NULL $reviewRating If this is a review, the rating
|
||||
* @param bool $iPostedIn If the user has posted in the item
|
||||
@@ -83,11 +83,31 @@ class _Content extends \IPS\Content\Search\Result
|
||||
*
|
||||
* @param string $view 'expanded' or 'condensed'
|
||||
* @param bool $asItem Displaying results as items?
|
||||
* @param bool $canIgnoreComments Can ignore comments in the result stream? Activity stream can, but search results cannot.
|
||||
* @param array|NULL $template Optional custom template
|
||||
* @return string
|
||||
*/
|
||||
public function html( $view = 'expanded', $asItem = FALSE )
|
||||
public function html( $view = 'expanded', $asItem = FALSE, $canIgnoreComments=FALSE, $template=NULL )
|
||||
{
|
||||
return call_user_func( array( $this->indexData['index_class'], 'searchResult' ), $this->indexData, $this->authorData, $this->itemData, $this->containerData, $this->reputationData, $this->reviewRating, $this->iPostedIn, $view, $asItem );
|
||||
return call_user_func( array( $this->indexData['index_class'], 'searchResult' ), $this->indexData, $this->authorData, $this->itemData, $this->containerData, $this->reputationData, $this->reviewRating, $this->iPostedIn, $view, $asItem, $canIgnoreComments, $template );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return search index data as an array
|
||||
*
|
||||
* @return array( 'indexData' => array() ... )
|
||||
*/
|
||||
public function asArray()
|
||||
{
|
||||
return array(
|
||||
'indexData' => $this->indexData,
|
||||
'authorData' => $this->authorData,
|
||||
'itemData' => $this->itemData,
|
||||
'containerData' => $this->containerData,
|
||||
'reputationData' => $this->reputationData,
|
||||
'reviewRating' => $this->reviewRating,
|
||||
'iPostedIn' => $this->iPostedIn
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,6 +120,6 @@ class _Content extends \IPS\Content\Search\Result
|
||||
{
|
||||
$class = $this->indexData['index_class'];
|
||||
$object = $class::load( $this->indexData['index_object_id'] );
|
||||
$document->addItem( $object->mapped('title'), $object->url(), $this->indexData['index_content'], \IPS\DateTime::ts( $this->indexData['index_date_created'] ) );
|
||||
$document->addItem( $object instanceof \IPS\Content\Comment ? $object->item()->mapped('title') : $object->mapped('title'), $object->url(), $this->indexData['index_content'], \IPS\DateTime::ts( $this->indexData['index_date_created'] ) );
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @brief Search Result not from Index
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 15 Sep 2015
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @brief Search Result
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 15 Sep 2015
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -47,7 +47,12 @@ abstract class _Result
|
||||
$yesterday = clone $now;
|
||||
$yesterday = $yesterday->sub( new \DateInterval('P1D') );
|
||||
$diff = $date->diff( $now );
|
||||
if ( $diff->h < 1 && !$diff->d && !$diff->m )
|
||||
|
||||
if ( $date->format('Y-m-d') == $yesterday->format('Y-m-d') )
|
||||
{
|
||||
return 'yesterday';
|
||||
}
|
||||
elseif ( $diff->h < 1 && !$diff->d && !$diff->m )
|
||||
{
|
||||
return 'past_hour';
|
||||
}
|
||||
@@ -55,10 +60,6 @@ abstract class _Result
|
||||
{
|
||||
return 'today';
|
||||
}
|
||||
elseif ( $date->format('Y-m-d') == $yesterday->format('Y-m-d') )
|
||||
{
|
||||
return 'yesterday';
|
||||
}
|
||||
elseif ( !$diff->y and !$diff->m and $diff->d < 7 )
|
||||
{
|
||||
return 'last_week';
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
/**
|
||||
* @brief Search Results
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @package IPS Community Suite
|
||||
* @since 21 Aug 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
@@ -62,12 +62,17 @@ class _Results extends \ArrayIterator
|
||||
* @brief Index IDs of items I have posted in
|
||||
*/
|
||||
protected $iPostedIn = array();
|
||||
|
||||
/**
|
||||
* @brief Tag values for index_ids returned from search
|
||||
*/
|
||||
protected $tags = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $results The results
|
||||
* @param int $countAllRows Count for all rows
|
||||
* @param array $results The results
|
||||
* @param \IPS\Db\Select|int $countAllRows Count for all rows query or int
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $results, $countAllRows )
|
||||
@@ -90,6 +95,7 @@ class _Results extends \ArrayIterator
|
||||
$reputationIds = array();
|
||||
$reviewRatings = array();
|
||||
$itemIndexIds = array();
|
||||
$indexIds = array();
|
||||
|
||||
/* Loop */
|
||||
foreach ( $this->getArrayCopy() as $result )
|
||||
@@ -173,7 +179,10 @@ class _Results extends \ArrayIterator
|
||||
}
|
||||
|
||||
/* Load author data */
|
||||
$this->authorData = iterator_to_array( \IPS\Db::i()->select( \IPS\Member::columnsForPhoto(), 'core_members', \IPS\Db::i()->in( 'member_id', $membersToLoad ) )->setKeyField( 'member_id' ) );
|
||||
if( count( $membersToLoad ) )
|
||||
{
|
||||
$this->authorData = iterator_to_array( \IPS\Db::i()->select( \IPS\Member::columnsForPhoto(), 'core_members', \IPS\Db::i()->in( 'member_id', $membersToLoad ) )->setKeyField( 'member_id' ) );
|
||||
}
|
||||
|
||||
/* Load container data */
|
||||
foreach ( $containersToLoad as $containerClass => $containerIds )
|
||||
@@ -221,9 +230,16 @@ class _Results extends \ArrayIterator
|
||||
/* Load data for the "I posted in this" stars */
|
||||
if ( count( $itemIndexIds ) and \IPS\Member::loggedIn()->member_id )
|
||||
{
|
||||
$this->iPostedIn = iterator_to_array( \IPS\Db::i()->select( 'index_item_index_id', 'core_search_index', array( array( \IPS\Db::i()->in( 'index_item_index_id', $itemIndexIds ) ), array( 'index_author=?', \IPS\Member::loggedIn()->member_id ) ), NULL, NULL, NULL, NULL, \IPS\Db::SELECT_DISTINCT ) );
|
||||
$this->iPostedIn = iterator_to_array( \IPS\Db::i()->select( 'index_item_index_id', 'core_search_index', array( array( \IPS\Db::i()->in( 'index_item_index_id', $itemIndexIds ) ), array( 'index_author=?', \IPS\Member::loggedIn()->member_id ) ) )->setKeyField('index_item_index_id') );
|
||||
}
|
||||
|
||||
|
||||
/* Load tags and prefixes */
|
||||
if ( count( $itemIndexIds ) )
|
||||
{
|
||||
$this->tags = iterator_to_array( \IPS\Db::i()->select( 'index_id, GROUP_CONCAT(index_tag) as index_tags', 'core_search_index_tags', array( array( \IPS\Db::i()->in( 'index_id', $itemIndexIds ) ), array( 'index_is_prefix = 0' ) ), NULL, NULL, 'index_id' )->setKeyField('index_id') );
|
||||
$this->prefixes = iterator_to_array( \IPS\Db::i()->select( 'index_id, index_tag as index_prefix', 'core_search_index_tags', array( array( \IPS\Db::i()->in( 'index_id', $itemIndexIds ) ), array( 'index_is_prefix = 1' ) ) )->setKeyField('index_id') );
|
||||
}
|
||||
|
||||
/* Set that we're initiated */
|
||||
$this->initated = TRUE;
|
||||
}
|
||||
@@ -236,7 +252,7 @@ class _Results extends \ArrayIterator
|
||||
public function current()
|
||||
{
|
||||
$data = parent::current();
|
||||
|
||||
|
||||
$class = $data['index_class'];
|
||||
$itemClass = in_array( 'IPS\Content\Comment', class_parents( $class ) ) ? $class::$itemClass : $class;
|
||||
$containerClass = isset( $itemClass::$containerNodeClass ) ? $itemClass::$containerNodeClass : NULL;
|
||||
@@ -247,6 +263,14 @@ class _Results extends \ArrayIterator
|
||||
$reputationData = $this->reputationData[ $class::$application ][ $class::$reputationType ][ $data['index_object_id'] ];
|
||||
}
|
||||
|
||||
/* Ensure we have an item if results are gathered from the map table */
|
||||
if ( ! isset( $this->itemData[ $itemClass ][ $data['index_item_id'] ] ) )
|
||||
{
|
||||
/* This should not happen in production, so debug log and move on to the next item */
|
||||
\IPS\Log::debug( "index_item_id ID " . $data['index_item_id'] . " for " . $itemClass . " has no itemData" , 'searchResults' );
|
||||
return static::next();
|
||||
}
|
||||
|
||||
$itemData = $this->itemData[ $itemClass ][ $data['index_item_id'] ];
|
||||
if ( $class == 'IPS\core\Statuses\Status' and $data['index_container_id'] != $data['index_author'] )
|
||||
{
|
||||
@@ -260,6 +284,16 @@ class _Results extends \ArrayIterator
|
||||
$itemData['author'] = $this->authorData[ $memberId ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $this->tags[ $data['index_item_index_id'] ] ) )
|
||||
{
|
||||
$data['index_tags'] = $this->tags[ $data['index_item_index_id'] ]['index_tags'];
|
||||
}
|
||||
|
||||
if ( isset( $this->prefixes[ $data['index_item_index_id'] ]) )
|
||||
{
|
||||
$data['index_prefix'] = $this->prefixes[ $data['index_item_index_id'] ]['index_prefix'];
|
||||
}
|
||||
|
||||
return new Result\Content(
|
||||
$data,
|
||||
@@ -296,7 +330,20 @@ class _Results extends \ArrayIterator
|
||||
{
|
||||
if ( $allRows )
|
||||
{
|
||||
return $this->countAllRows;
|
||||
if ( is_integer( $this->countAllRows ) )
|
||||
{
|
||||
return $this->countAllRows;
|
||||
}
|
||||
else if ( gettype( $this->countAllRows ) === 'object' and get_class( $this->countAllRows ) === 'IPS\Db\Select' )
|
||||
{
|
||||
$this->countAllRows = $this->countAllRows->first();
|
||||
|
||||
return $this->countAllRows;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \LogicException;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -431,6 +478,9 @@ class _Results extends \ArrayIterator
|
||||
}
|
||||
}
|
||||
|
||||
/* If an application was not updated or is not installed, do not try to fetch it or we can get a class does not exist fatal error */
|
||||
$where[] = array( "follow_app IN('" . implode( "','", array_keys( \IPS\Application::enabledApplications() ) ) . "')" );
|
||||
|
||||
foreach ( \IPS\Db::i()->select( array( 'follow_app', 'follow_area', 'follow_rel_id', 'follow_member_id', 'follow_added' ), 'core_follow', $where, 'follow_added DESC', 10 ) as $follow )
|
||||
{
|
||||
if ( $follow['follow_app'] == 'core' and $follow['follow_area'] == 'member' )
|
||||
@@ -445,12 +495,16 @@ class _Results extends \ArrayIterator
|
||||
$class = 'IPS\\' . $follow['follow_app'] . '\\' . ucfirst( $follow['follow_area'] );
|
||||
try
|
||||
{
|
||||
$thingBeingFollowed = $class::loadAndCheckPerms( $follow['follow_rel_id'] );
|
||||
|
||||
$extraItems[] = new Result\Custom(
|
||||
\IPS\DateTime::ts( $follow['follow_added'] ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( 'activity_member_followed', FALSE, array( 'htmlsprintf' => array( \IPS\Theme::i()->getTemplate( 'global', 'core', 'front' )->userLink( \IPS\Member::load( $follow['follow_member_id'] ) ), \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->basicUrl( $thingBeingFollowed->url(), FALSE, $thingBeingFollowed instanceof \IPS\Node\Model ? $thingBeingFollowed->_title : $thingBeingFollowed->mapped('title'), FALSE ) ) ) )
|
||||
);
|
||||
if ( class_exists( $class ) )
|
||||
{
|
||||
$thingBeingFollowed = $class::loadAndCheckPerms( $follow['follow_rel_id'] );
|
||||
|
||||
$extraItems[] = new Result\Custom(
|
||||
\IPS\DateTime::ts( $follow['follow_added'] ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( 'activity_member_followed', FALSE, array( 'htmlsprintf' => array( \IPS\Theme::i()->getTemplate( 'global', 'core', 'front' )->userLink( \IPS\Member::load( $follow['follow_member_id'] ) ), \IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->basicUrl( $thingBeingFollowed->url(), FALSE, $thingBeingFollowed instanceof \IPS\Node\Model ? $thingBeingFollowed->_title : $thingBeingFollowed->mapped('title'), FALSE ) ) ) )
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
}
|
||||
@@ -458,7 +512,7 @@ class _Results extends \ArrayIterator
|
||||
}
|
||||
|
||||
/* Likes */
|
||||
if ( in_array( 'like', $extra ) or in_array( 'rep_neg', $extra ) )
|
||||
if ( \IPS\Settings::i()->reputation_enabled and \IPS\Member::loggedIn()->group['gbw_view_reps'] and ( in_array( 'like', $extra ) or in_array( 'rep_neg', $extra ) ) )
|
||||
{
|
||||
$where = array( array( 'rep_date>?', $lastTime ) );
|
||||
if ( $firstTime )
|
||||
@@ -473,72 +527,28 @@ class _Results extends \ArrayIterator
|
||||
{
|
||||
$where[] = array( 'rep_rating=-1' );
|
||||
}
|
||||
|
||||
/* get only the reputation data for installed applications */
|
||||
$where[] = \IPS\Db::i()->in( 'app', array_keys( \IPS\Application::applications() ) ) ;
|
||||
|
||||
if ( $author )
|
||||
{
|
||||
$where[] = array( '( member_id=? OR member_received=? )', $author->member_id, $author->member_id );
|
||||
}
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_reputation_index', $where, 'rep_date DESC', 10 ) as $rep )
|
||||
{
|
||||
try
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_reputation_index', array_merge( $where, array( array( 'member_id=?', $author->member_id ) ) ), 'rep_date DESC', 10 ) as $rep )
|
||||
{
|
||||
$thingBeingRepped = NULL;
|
||||
foreach ( \IPS\Application::load( $rep['app'] )->extensions( 'core', 'ContentRouter', TRUE, TRUE ) as $ext )
|
||||
{
|
||||
foreach ( $ext->classes as $class )
|
||||
{
|
||||
if ( in_array( 'IPS\Content\Reputation', class_implements( $class ) ) and $class::$reputationType == $rep['type'] )
|
||||
{
|
||||
$thingBeingRepped = $class::loadAndCheckPerms( $rep['type_id'] );
|
||||
break;
|
||||
}
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
if ( in_array( 'IPS\Content\Reputation', class_implements( $commentClass ) ) and $commentClass::$reputationType == $rep['type'] )
|
||||
{
|
||||
$thingBeingRepped = $commentClass::loadAndCheckPerms( $rep['type_id'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
if ( in_array( 'IPS\Content\Reputation', class_implements( $reviewClass ) ) and $reviewClass::$reputationType == $rep['type'] )
|
||||
{
|
||||
$thingBeingRepped = $reviewClass::loadAndCheckPerms( $rep['type_id'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( !$thingBeingRepped )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
}
|
||||
|
||||
if ( \IPS\Settings::i()->reputation_point_types == 'like' )
|
||||
{
|
||||
$lang = 'activity_member_liked';
|
||||
}
|
||||
elseif ( $rep['rep_rating'] > 0 )
|
||||
{
|
||||
$lang = 'activity_rep_pos';
|
||||
}
|
||||
else
|
||||
{
|
||||
$lang = 'activity_rep_neg';
|
||||
}
|
||||
|
||||
$extraItems[] = new Result\Custom(
|
||||
\IPS\DateTime::ts( $rep['rep_date'] ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( $lang, FALSE, array( 'htmlsprintf' => array(
|
||||
\IPS\Theme::i()->getTemplate( 'global', 'core', 'front' )->userLink( \IPS\Member::load( $rep['member_id'] ) ),
|
||||
$thingBeingRepped->indefiniteArticle(),
|
||||
\IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->basicUrl( $thingBeingRepped->url(), FALSE, $thingBeingRepped instanceof \IPS\Content\Item ? $thingBeingRepped->mapped('title') : $thingBeingRepped->item()->mapped('title'), FALSE )
|
||||
) ) )
|
||||
);
|
||||
$this->addReputationExtraItem( $rep, $extraItems );
|
||||
}
|
||||
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_reputation_index', array_merge( $where, array( array( 'member_received=?', $author->member_id ) ) ), 'rep_date DESC', 10 ) as $rep )
|
||||
{
|
||||
$this->addReputationExtraItem( $rep, $extraItems );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ( \IPS\Db::i()->select( '*', 'core_reputation_index', $where, 'rep_date DESC', 10 ) as $rep )
|
||||
{
|
||||
$this->addReputationExtraItem( $rep, $extraItems );
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -567,4 +577,75 @@ class _Results extends \ArrayIterator
|
||||
/* And return */
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add reputation given/received to extra things array
|
||||
*
|
||||
* @param array $rep Reputation row from database
|
||||
* @param array $extraItems Array of extra items (passed by reference)
|
||||
* @return void
|
||||
*/
|
||||
protected function addReputationExtraItem( $rep, &$extraItems )
|
||||
{
|
||||
try
|
||||
{
|
||||
$thingBeingRepped = NULL;
|
||||
foreach ( \IPS\Application::load( $rep['app'] )->extensions( 'core', 'ContentRouter', TRUE, TRUE ) as $ext )
|
||||
{
|
||||
foreach ( $ext->classes as $class )
|
||||
{
|
||||
if ( in_array( 'IPS\Content\Reputation', class_implements( $class ) ) and $class::$reputationType == $rep['type'] )
|
||||
{
|
||||
$thingBeingRepped = $class::loadAndCheckPerms( $rep['type_id'] );
|
||||
break;
|
||||
}
|
||||
if ( isset( $class::$commentClass ) )
|
||||
{
|
||||
$commentClass = $class::$commentClass;
|
||||
if ( in_array( 'IPS\Content\Reputation', class_implements( $commentClass ) ) and $commentClass::$reputationType == $rep['type'] )
|
||||
{
|
||||
$thingBeingRepped = $commentClass::loadAndCheckPerms( $rep['type_id'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( isset( $class::$reviewClass ) )
|
||||
{
|
||||
$reviewClass = $class::$reviewClass;
|
||||
if ( in_array( 'IPS\Content\Reputation', class_implements( $reviewClass ) ) and $reviewClass::$reputationType == $rep['type'] )
|
||||
{
|
||||
$thingBeingRepped = $reviewClass::loadAndCheckPerms( $rep['type_id'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( !$thingBeingRepped )
|
||||
{
|
||||
throw new \OutOfRangeException;
|
||||
}
|
||||
|
||||
if ( \IPS\Settings::i()->reputation_point_types == 'like' )
|
||||
{
|
||||
$lang = 'activity_member_liked';
|
||||
}
|
||||
elseif ( $rep['rep_rating'] > 0 )
|
||||
{
|
||||
$lang = 'activity_rep_pos';
|
||||
}
|
||||
else
|
||||
{
|
||||
$lang = 'activity_rep_neg';
|
||||
}
|
||||
|
||||
$extraItems[] = new Result\Custom(
|
||||
\IPS\DateTime::ts( $rep['rep_date'] ),
|
||||
\IPS\Member::loggedIn()->language()->addToStack( $lang, FALSE, array( 'htmlsprintf' => array(
|
||||
\IPS\Theme::i()->getTemplate( 'global', 'core', 'front' )->userLink( \IPS\Member::load( $rep['member_id'] ) ),
|
||||
$thingBeingRepped->indefiniteArticle(),
|
||||
\IPS\Theme::i()->getTemplate( 'global', 'core', 'global' )->basicUrl( $thingBeingRepped->url(), FALSE, $thingBeingRepped instanceof \IPS\Content\Item ? $thingBeingRepped->mapped('title') : $thingBeingRepped->item()->mapped('title'), FALSE )
|
||||
) ) )
|
||||
);
|
||||
}
|
||||
catch ( \OutOfRangeException $e ) { }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user