Version 4.6.0

This commit is contained in:
Neo committed 2025-12-19 05:55:31 -08:00
1 parent f79dcf067a
commit 517a5e1f70
2036 files changed
+110041 -26162

No files matched your search

+127 -40
View File
@@ -34,11 +34,7 @@ trait Statistics
*/
public function topAttachments( $count = 5 ): array
{
$attachments = $this->_getAllAttachments();
uasort( $attachments, function ( $a, $b ) {
return $a['attach_hits'] < $b['attach_hits'];
} );
$attachments = $this->_getAllAttachments( array(), $count, 'attach_hits DESC' );
$attachments = \array_slice( $attachments, 0, $count );
@@ -54,7 +50,7 @@ trait Statistics
*/
public function imageAttachments( $count = 10 ): array
{
$attachments = $this->_getAllAttachments( array( 'attach_is_image=1' ) );
$attachments = $this->_getAllAttachments( array( 'attach_is_image=1' ), $count, 'attach_date DESC' );
$attachments = \array_slice( $attachments, 0, $count );
return $attachments;
@@ -77,9 +73,7 @@ trait Statistics
throw new \BadMethodCallException();
}
$commentItemField = $commentClass::$databaseColumnMap['item'];
$authorColumn = $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['author'];
$idField = static::$databaseColumnId;
$cacheKey = 'topPosters_' . $count;
try
@@ -89,11 +83,10 @@ trait Statistics
catch( \OutOfRangeException $e )
{
$where = $this->_getVisibleWhere();
array_push( $where, array( $commentItemField . '=?', $this->$idField ) );
$members = \IPS\Db::i()->select( "count(*) as sum, {$authorColumn}", $commentClass::$databaseTable, $where, 'sum DESC', array(0, $count), array($commentClass::$databasePrefix . $commentClass::$databaseColumnMap['author']) );
$members = iterator_to_array( \IPS\Db::i()->select( "count(*) as sum, {$authorColumn}", $commentClass::$databaseTable, $where, 'sum DESC', array( 0, $count ), array( $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['author'] ) ) );
$this->_storeCached( $cacheKey, iterator_to_array( $members ) );
$this->_storeCached( $cacheKey, $members );
}
$contributors = array();
@@ -110,7 +103,7 @@ trait Statistics
}
$return = array();
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_members', array(\IPS\Db::i()->in( 'member_id', $contributors )), "FIND_IN_SET( member_id, '" . implode( ",", $contributors ) . "' )" ), 'IPS\Member' ) as $member )
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'core_members', array( \IPS\Db::i()->in( 'member_id', $contributors ) ) ), 'IPS\Member' ) as $member )
{
$return[] = array( 'member' => $member, 'count' => $counts[ $member->member_id ] );
}
@@ -121,7 +114,7 @@ trait Statistics
/**
* Members with most posts
*
* @param int $count The number of results to return
* @param int $count The number of results to return, max 100
* @return array
* @throw BadMethodCallException
* @throws \Exception
@@ -129,13 +122,18 @@ trait Statistics
public function topReactedPosts( $count = 5 ): array
{
$commentClass = static::$commentClass;
$commentIdField = $commentClass::$databaseColumnId;
$commentIdField = $commentClass::$databasePrefix . $commentClass::$databaseColumnId;
$idField = static::$databaseColumnId;
if ( !\IPS\IPS::classUsesTrait( $commentClass, 'IPS\Content\Reactable' ) )
{
throw new \BadMethodCallException();
}
if ( $count > 100 )
{
throw new \BadMethodCallException();
}
$cacheKey = 'topReactedPosts_' . $count;
@@ -145,16 +143,18 @@ trait Statistics
}
catch( \OutOfRangeException $e )
{
$where = array('app=? and type=? and item_id=?', $commentClass::$application, $commentClass::reactionType(), $this->$idField);
$posts = \IPS\Db::i()->select( "count(*) as sum, type_id", 'core_reputation_index', $where, NULL, NULL, array('type_id') );
$where = array( 'app=? and type=? and item_id=?', $commentClass::$application, $commentClass::reactionType(), $this->$idField );
$posts = \IPS\Db::i()->select( "count(*) as sum, type_id", 'core_reputation_index', $where, NULL, NULL, array( 'type_id' ) );
$posts = iterator_to_array( $posts );
usort($posts, function( $item1, $item2 )
usort( $posts, function( $item1, $item2 )
{
return $item2['sum'] <=> $item1['sum'];
} );
/* Just store the top 100 posts, they are already sorted by highest to lowest */
$posts = \array_slice( $posts, 0, 100 );
$this->_storeCached( $cacheKey, $posts );
}
@@ -172,9 +172,9 @@ trait Statistics
}
$return = array();
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', $commentClass::$databaseTable, array(\IPS\Db::i()->in( $commentClass::$databaseColumnId, $postIds )), "FIND_IN_SET( {$commentClass::$databaseColumnId}, '" . implode( ",", $postIds ) . "' )", array( 0, $count ) ), $commentClass ) as $comment )
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', $commentClass::$databaseTable, array( \IPS\Db::i()->in( $commentIdField, $postIds ) ), "FIND_IN_SET( {$commentIdField}, '" . implode( ",", $postIds ) . "' )", array( 0, $count ) ), $commentClass ) as $comment )
{
if ( $comment->canView() )
if ( $comment->canView() and $comment->mapped('item') == $this->$idField )
{
$return[] = array( 'comment' => $comment, 'count' => $counts[ $comment->$commentIdField ] );
}
@@ -194,8 +194,7 @@ trait Statistics
{
$return = array();
$commentClass = static::$commentClass;
$idField = static::$databaseColumnId;
$commentItemField = $commentClass::$databaseColumnMap['item'];
$commentIdField = $commentClass::$databasePrefix . $commentClass::$databaseColumnId;
$rows = array();
$commentIds = array();
@@ -209,9 +208,8 @@ trait Statistics
{
$dateColumn = $commentClass::$databasePrefix . ( isset( $commentClass::$databaseColumnMap['updated'] ) ? $commentClass::$databaseColumnMap['updated'] : $commentClass::$databaseColumnMap['date'] );
$where = $this->_getVisibleWhere();
array_push( $where, array( $commentItemField . '=?', $this->$idField ) );
$posts = iterator_to_array( \IPS\Db::i()->select( "COUNT(*) AS count, MIN({$commentClass::$databaseColumnId}) as commentId, (DATE_FORMAT( FROM_UNIXTIME( IFNULL( {$dateColumn}, 0 ) ), '%Y-%c-%e' ) ) as time", $commentClass::$databaseTable, $where, 'count desc', array( 0, $count ), array('time') ) );
$posts = iterator_to_array( \IPS\Db::i()->select( "COUNT(*) AS count, MIN({$commentIdField}) as commentId, (DATE_FORMAT( FROM_UNIXTIME( IFNULL( {$dateColumn}, 0 ) ), '%Y-%c-%e' ) ) as time", $commentClass::$databaseTable, $where, 'count desc', array( 0, $count ), array( 'time' ) ) );
$this->_storeCached( $cacheKey, $posts );
}
@@ -249,21 +247,27 @@ trait Statistics
public function clearCachedStatistics()
{
$idField = static::$databaseColumnId;
$cacheKey = $this->$idField . '_' . static::$module . '_activity';
\IPS\Data\Cache::i()->storeWithExpire( $cacheKey, array(), \IPS\DateTime::create()->add( new \DateInterval('P1M') ), TRUE );
\IPS\Db::i()->delete( 'core_item_statistics_cache', array( 'cache_class=? and cache_item_id=?', \get_class( $this ), $this->$idField ) );
}
/**
* @brief Loaded Extensions
*/
protected static $loadedExtensions = array();
/**
* Get all attachments for this item
*
* @param array $where Additional where for this
* @param array $extraWhere Additional where clause
* @param int $limit Number to return/limit to
* @param string|NULL $orderBy Order by clause (optional)
* @return array
* @throws \Exception
*/
protected function _getAllAttachments( $extraWhere=NULL )
protected function _getAllAttachments( $extraWhere=NULL, $limit=10, $orderBy=NULL )
{
$idField = static::$databaseColumnId;
$cacheKey = 'allAttachments';
$cacheKey = 'allAttachments' . md5( json_encode( $extraWhere ) . $limit . (string) $orderBy );
$return = array();
try
@@ -272,7 +276,7 @@ trait Statistics
}
catch( \OutOfRangeException $e )
{
$where = array( array('location_key=? and id1=? and id2 IN(?)', static::$application . '_' . mb_ucfirst( static::$module ), $this->$idField, $this->_subQueryVisibleComments() ) );
$where = array( array( 'location_key=? and id1=? and id2 IN(?)', static::$application . '_' . mb_ucfirst( static::$module ), $this->$idField, $this->_subQueryVisibleComments( $this->$idField ) ) );
if ( $extraWhere !== NULL )
{
@@ -280,7 +284,39 @@ trait Statistics
}
/* Get attachments from all comments */
$return = iterator_to_array( \IPS\Db::i()->select( '*', 'core_attachments_map', $where )->join( 'core_attachments', array('attach_id=attachment_id') ) );
$return = iterator_to_array( \IPS\Db::i()->select( '*', 'core_attachments_map', $where, $orderBy, $limit )->join( 'core_attachments', array( 'attach_id=attachment_id' ) ) );
/* Get link to comments */
foreach( $return as $k => $map )
{
/* Get the attachment extension if we don't already have it */
if ( !isset( static::$loadedExtensions[ $map['location_key'] ] ) )
{
$exploded = explode( '_', $map['location_key'] );
try
{
$extensions = \IPS\Application::load( $exploded[0] )->extensions( 'core', 'EditorLocations' );
if ( isset( $extensions[ $exploded[1] ] ) )
{
static::$loadedExtensions[ $map['location_key'] ] = $extensions[ $exploded[1] ];
}
}
catch ( \OutOfRangeException $e ) { }
catch ( \UnexpectedValueException $e ) { }
}
if ( isset( static::$loadedExtensions[ $map['location_key'] ] ) )
{
try
{
$url = static::$loadedExtensions[ $map['location_key'] ]->attachmentLookup( $map['id1'], $map['id2'], $map['id3'] );
$return[ $k ]['commentUrl'] = (string) $url->url();
}
catch ( \LogicException $e ) { }
catch ( \BadMethodCallException $e ){ }
}
}
$this->_storeCached( $cacheKey, $return );
}
@@ -291,12 +327,13 @@ trait Statistics
/**
* Return a sub query to fetch only visible posts
*
* @param int $id Content item ID
* @return \IPS\Db\Select
*/
protected function _subQueryVisibleComments()
protected function _subQueryVisibleComments( $id )
{
$commentClass = static::$commentClass;
return \IPS\Db::i()->select( $commentClass::$databaseColumnId, $commentClass::$databaseTable, $this->_getVisibleWhere() );
return \IPS\Db::i()->select( $commentClass::$databasePrefix . $commentClass::$databaseColumnId, $commentClass::$databaseTable, array_merge( $this->_getVisibleWhere(), array( array( $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['item'] . '=' . $id ) ) ) );
}
/**
@@ -307,18 +344,23 @@ trait Statistics
protected function _getVisibleWhere()
{
$commentClass = static::$commentClass;
$idField = static::$databaseColumnId;
$commentItemField = $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['item'];
$where = array();
if ( isset( $commentClass::$databaseColumnMap['approved'] ) )
{
$approvedColumn = $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['approved'];
$where[] = array("{$approvedColumn} = 1");
$where[] = array( "{$approvedColumn} = 1" );
}
if ( isset( $commentClass::$databaseColumnMap['hidden'] ) )
{
$hiddenColumn = $commentClass::$databasePrefix . $commentClass::$databaseColumnMap['hidden'];
$where[] = array("{$hiddenColumn} = 0");
$where[] = array( "{$hiddenColumn} = 0" );
}
$where[] = array( $commentItemField . '=?', $this->$idField );
return $where;
}
@@ -339,9 +381,21 @@ trait Statistics
if ( static::$cachedActivity === NULL )
{
$idField = static::$databaseColumnId;
$cacheKey = $this->$idField . '_' . static::$module . '_activity';
static::$cachedActivity = \IPS\Data\Cache::i()->getWithExpire( $cacheKey, TRUE );
try
{
$cache = \IPS\Db::i()->select( '*', 'core_item_statistics_cache', array( 'cache_class=? and cache_item_id=?', \get_class( $this ), $this->$idField ) )->first();
if ( $cache['cache_added'] > time() - 86400 )
{
static::$cachedActivity = json_decode( $cache['cache_contents'], TRUE );
}
else
{
\IPS\Db::i()->delete( 'core_item_statistics_cache', array( 'cache_class=? and cache_item_id=?', \get_class( $this ), $this->$idField ) );
}
}
catch( \UnderflowException $e ) { }
}
if ( static::$cachedActivity !== NULL and isset( static::$cachedActivity[ $key ] ) )
@@ -350,10 +404,21 @@ trait Statistics
}
else
{
if( static::$cachedActivity === NULL )
{
static::$cachedActivity = array();
}
static::$cachedActivity[ $key ] = array();
throw new \OutOfRangeException;
}
}
/**
* @brief Should we store the data in the cache?
*/
protected $storeCache = NULL;
/**
* Set cached data
*
@@ -370,10 +435,32 @@ trait Statistics
catch( \Exception $e ) { }
$idField = static::$databaseColumnId;
$cacheKey = $this->$idField . '_' . static::$module . '_activity';
static::$cachedActivity[ $key ] = $value;
\IPS\Data\Cache::i()->storeWithExpire( $cacheKey, static::$cachedActivity, \IPS\DateTime::create()->add( new \DateInterval('P1D') ), TRUE );
$this->storeCache = $this->$idField;
}
/**
* Store the cache during destruction
*
* @return void
*/
public function __destruct()
{
if( $this->storeCache )
{
\IPS\Db::i()->insert( 'core_item_statistics_cache', array(
'cache_class' => \get_class( $this ),
'cache_item_id' => $this->storeCache,
'cache_contents' => json_encode( static::$cachedActivity ),
'cache_added' => time()
), TRUE );
}
if( \is_callable( 'parent::__destruct' ) )
{
parent::__destruct();
}
}
}