Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -12,29 +12,48 @@
|
||||
namespace IPS\cms\extensions\core\Queue;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\cms\Databases;
|
||||
use IPS\cms\Records;
|
||||
use IPS\cms\Records\Comment;
|
||||
use IPS\Content\Search\Index;
|
||||
use IPS\Db;
|
||||
use IPS\Extensions\QueueAbstract;
|
||||
use IPS\forums\Topic;
|
||||
use IPS\forums\Topic\Post;
|
||||
use IPS\Log;
|
||||
use IPS\Member;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use IPS\Task;
|
||||
use OutOfRangeException;
|
||||
use Throwable;
|
||||
use UnderflowException;
|
||||
use function defined;
|
||||
use const IPS\REBUILD_SLOW;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Background Task
|
||||
*/
|
||||
class _MoveComments
|
||||
class MoveComments extends QueueAbstract
|
||||
{
|
||||
/**
|
||||
* @brief Number of content items to rebuild per cycle
|
||||
*/
|
||||
public $rebuild = \IPS\REBUILD_SLOW;
|
||||
public int $rebuild = REBUILD_SLOW;
|
||||
|
||||
/**
|
||||
* Parse data before queuing
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
* @return array|null
|
||||
*/
|
||||
public function preQueueData( $data )
|
||||
public function preQueueData( array $data ): ?array
|
||||
{
|
||||
$data['done'] = 0;
|
||||
$data['lastId'] = 0;
|
||||
@@ -46,27 +65,27 @@ class _MoveComments
|
||||
case 'forums':
|
||||
if ( isset( $data['categoryId'] ) )
|
||||
{
|
||||
$where = array( \IPS\Db::i()->in( 'comment_record_id', iterator_to_array( \IPS\Db::i()->select( 'record_id', "cms_custom_database_{$data['databaseId']}", array( "category_id=?", $data['categoryId'] ) ) ) ) );
|
||||
$where = array( Db::i()->in( 'comment_record_id', iterator_to_array( Db::i()->select( 'record_id', "cms_custom_database_{$data['databaseId']}", array( "category_id=?", $data['categoryId'] ) ) ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where = array( "comment_database_id=?", $data['databaseId'] );
|
||||
}
|
||||
|
||||
$data['count'] = \IPS\Db::i()->select( 'COUNT(*)', 'cms_database_comments', $where )->first();
|
||||
$data['count'] = Db::i()->select( 'COUNT(*)', 'cms_database_comments', $where )->first();
|
||||
break;
|
||||
|
||||
case 'pages':
|
||||
if ( isset( $data['categoryId'] ) )
|
||||
{
|
||||
$where = array( "new_topic=? AND " . \IPS\Db::i()->in( 'topic_id', iterator_to_array( \IPS\Db::i()->select( 'record_topicid', "cms_custom_database_{$data['databaseId']}", array( "category_id=?", $data['categoryId'] ) ) ) ), 0 );
|
||||
$where = array( "new_topic=? AND " . Db::i()->in( 'topic_id', iterator_to_array( Db::i()->select( 'record_topicid', "cms_custom_database_{$data['databaseId']}", array( "category_id=?", $data['categoryId'] ) ) ) ), 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where = array( "new_topic=? AND " . \IPS\Db::i()->in( 'topic_id', iterator_to_array( \IPS\Db::i()->select( 'record_topicid', "cms_custom_database_{$data['databaseId']}" ) ) ), 0 );
|
||||
$where = array( "new_topic=? AND " . Db::i()->in( 'topic_id', iterator_to_array( Db::i()->select( 'record_topicid', "cms_custom_database_{$data['databaseId']}" ) ) ), 0 );
|
||||
}
|
||||
|
||||
$data['count'] = \IPS\Db::i()->select( 'COUNT(*)', 'forums_posts', $where )->first();
|
||||
$data['count'] = Db::i()->select( 'COUNT(*)', 'forums_posts', $where )->first();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -76,16 +95,16 @@ class _MoveComments
|
||||
/* But wait... if we're going from Pages to Forums, we still need to create the topics, so kick off that task instead. */
|
||||
if ( $data['to'] === 'forums' )
|
||||
{
|
||||
\IPS\Task::queue( 'cms', 'ResyncTopicContent', array( 'databaseId' => $data['databaseId'] ), 3, array( 'databaseId' ) );
|
||||
Task::queue( 'cms', 'ResyncTopicContent', array( 'databaseId' => $data['databaseId'] ), 3, array( 'databaseId' ) );
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
catch( \Throwable $e )
|
||||
catch( Throwable $e )
|
||||
{
|
||||
/* Something went wrong - log it and return */
|
||||
\IPS\Log::log( $e, 'cms_move_comments' );
|
||||
Log::log( $e, 'cms_move_comments' );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -98,9 +117,9 @@ class _MoveComments
|
||||
* @param mixed $data Data as it was passed to \IPS\Task::queue()
|
||||
* @param int $offset Offset
|
||||
* @return int New offset
|
||||
* @throws \IPS\Task\Queue\OutOfRangeException Indicates offset doesn't exist and thus task is complete
|
||||
* @throws Task\Queue\OutOfRangeException Indicates offset doesn't exist and thus task is complete
|
||||
*/
|
||||
public function run( &$data, $offset )
|
||||
public function run( mixed &$data, int $offset ): int
|
||||
{
|
||||
/* Okay let's figure out what we're doing. Separate these out to different methods since they can be a little complex. */
|
||||
switch( $data['to'] )
|
||||
@@ -120,7 +139,7 @@ class _MoveComments
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \IPS\Task\Queue\OutOfRangeException;
|
||||
throw new Task\Queue\OutOfRangeException;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,12 +149,12 @@ class _MoveComments
|
||||
* @param mixed $data Data as it was passed to \IPS\Task::queue()
|
||||
* @param int $offset Offset
|
||||
* @return array( 'text' => 'Doing something...', 'complete' => 50 ) Text explaining task and percentage complete
|
||||
* @throws \OutOfRangeException Indicates offset doesn't exist and thus task is complete
|
||||
* @throws OutOfRangeException Indicates offset doesn't exist and thus task is complete
|
||||
*/
|
||||
public function getProgress( $data, $offset )
|
||||
public function getProgress( mixed $data, int $offset ): array
|
||||
{
|
||||
$database = \IPS\cms\Databases::load( $data['databaseId'] );
|
||||
return array( 'text' => \IPS\Member::loggedIn()->language()->addToStack( 'moving_database_comments', FALSE, array( 'sprintf' => array( $database->_title ) ) ), 'complete' => $data['done'] ? ( round( 100 / $data['count'] * $data['done'], 2 ) ) : 0 );
|
||||
$database = Databases::load( $data['databaseId'] );
|
||||
return array( 'text' => Member::loggedIn()->language()->addToStack( 'moving_database_comments', FALSE, array( 'sprintf' => array( $database->_title ) ) ), 'complete' => $data['done'] ? ( round( 100 / $data['count'] * $data['done'], 2 ) ) : 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,33 +164,34 @@ class _MoveComments
|
||||
* @param int $offset Offset
|
||||
* @return int New offset
|
||||
*/
|
||||
protected function _toForums( &$data, int $offset ): int
|
||||
protected function _toForums( mixed &$data, int $offset ): int
|
||||
{
|
||||
$done = 0;
|
||||
/* This is easy... kind of */
|
||||
$database = \IPS\cms\Databases::load( $data['databaseId'] );
|
||||
$database = Databases::load( $data['databaseId'] );
|
||||
$commentClass = '\IPS\cms\Records\Comment' . $database->_id;
|
||||
if ( isset( $data['categoryId'] ) )
|
||||
{
|
||||
$where = array( "comment_database_id=? AND comment_id>? AND " . \IPS\Db::i()->in( 'comment_record_id', iterator_to_array( \IPS\Db::i()->select( 'primary_id_field', "cms_custom_database_{$database->_id}", array( "category_id=?", $data['categoryId'] ) ) ) ), $database->_id, $data['lastId'] );
|
||||
$where = array( "comment_database_id=? AND comment_id>? AND " . Db::i()->in( 'comment_record_id', iterator_to_array( Db::i()->select( 'primary_id_field', "cms_custom_database_{$database->_id}", array( "category_id=?", $data['categoryId'] ) ) ) ), $database->_id, $data['lastId'] );
|
||||
}
|
||||
else
|
||||
{
|
||||
$where = array( "comment_database_id=? AND comment_id>?", $database->_id, $data['lastId'] );
|
||||
}
|
||||
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'cms_database_comments', $where, "comment_id ASC", $this->rebuild ), $commentClass ) AS $row )
|
||||
foreach( new ActiveRecordIterator( Db::i()->select( '*', 'cms_database_comments', $where, "comment_id ASC", $this->rebuild ), $commentClass ) AS $row )
|
||||
{
|
||||
/* Check the record exists */
|
||||
try
|
||||
{
|
||||
/* @var Comment $row */
|
||||
$row->item();
|
||||
|
||||
/* Check the topic exists */
|
||||
try
|
||||
{
|
||||
\IPS\forums\Topic::load( $row->item()->record_topicid );
|
||||
Topic::load( $row->item()->record_topicid );
|
||||
}
|
||||
catch( \OutOfRangeException )
|
||||
catch( OutOfRangeException $e )
|
||||
{
|
||||
/* If the topic does not exist, reset the value so it can be re-created */
|
||||
$row->item()->record_topicid = 0;
|
||||
@@ -181,11 +201,11 @@ class _MoveComments
|
||||
/* First, create the topic if it doesn't already exist */
|
||||
if ( !$row->item()->record_topicid )
|
||||
{
|
||||
$row->item()->syncTopic( FALSE ); # Sync Topic will create it for us and link it, while also handling database and category level settings.
|
||||
$row->item()->syncTopic(); # Sync Topic will create it for us and link it, while also handling database and category level settings.
|
||||
}
|
||||
}
|
||||
// Exception may be that the item does not exist, or the category does not have syncing enabled
|
||||
catch( \UnderflowException | \OutOfRangeException $e )
|
||||
catch( UnderflowException | OutOfRangeException $e )
|
||||
{
|
||||
$data['lastId'] = $row->id;
|
||||
$data['done']++;
|
||||
@@ -208,7 +228,7 @@ class _MoveComments
|
||||
$queued = -1;
|
||||
}
|
||||
|
||||
$post = new \IPS\forums\Topic\Post;
|
||||
$post = new Post;
|
||||
$post->author_id = $row->user;
|
||||
$post->append_edit = $row->edit_show;
|
||||
$post->edit_time = $row->edit_date;
|
||||
@@ -223,9 +243,9 @@ class _MoveComments
|
||||
$post->save();
|
||||
|
||||
/* Update post before register */
|
||||
\IPS\Db::i()->update( 'core_post_before_registering', array( 'class' => "\IPS\forums\Topic\Post", 'id' => $post->id ), array( 'class=? and id=?', $commentClass, $row->id ) );
|
||||
Db::i()->update( 'core_post_before_registering', array( 'class' => "\IPS\forums\Topic\Post", 'id' => $post->id ), array( 'class=? and id=?', $commentClass, $row->id ) );
|
||||
|
||||
\IPS\Content\Search\Index::i()->index( $post );
|
||||
Index::i()->index( $post );
|
||||
|
||||
$data['lastId'] = $row->id;
|
||||
$data['done']++;
|
||||
@@ -234,9 +254,9 @@ class _MoveComments
|
||||
$row->delete();
|
||||
|
||||
/* Are we done? If so, sync the topic */
|
||||
if ( !(bool) \IPS\Db::i()->select( 'COUNT(*)', 'cms_database_comments', array( "comment_database_id=? AND comment_record_id=?", $database->_id, $row->item()->primary_id_field ) )->first() )
|
||||
if ( !(bool) Db::i()->select( 'COUNT(*)', 'cms_database_comments', array( "comment_database_id=? AND comment_record_id=?", $database->_id, $row->item()->primary_id_field ) )->first() )
|
||||
{
|
||||
$topic = \IPS\forums\Topic::load( $row->item()->record_topicid );
|
||||
$topic = Topic::load( $row->item()->record_topicid );
|
||||
$topic->resyncCommentCounts();
|
||||
$topic->save();
|
||||
|
||||
@@ -265,24 +285,25 @@ class _MoveComments
|
||||
* @param int $offset Offset
|
||||
* @return int New offset
|
||||
*/
|
||||
protected function _toPages( &$data, int $offset ): int
|
||||
protected function _toPages( mixed &$data, int $offset ): int
|
||||
{
|
||||
$done = 0;
|
||||
|
||||
/* This ones a little more complicated */
|
||||
$database = \IPS\cms\Databases::load( $data['databaseId'] );
|
||||
$database = Databases::load( $data['databaseId'] );
|
||||
$commentClass = '\IPS\cms\Records\Comment' . $database->_id;
|
||||
$recordClass = '\IPS\cms\Records' . $database->_id;
|
||||
if ( isset( $data['categoryId'] ) )
|
||||
{
|
||||
$subquery = iterator_to_array( \IPS\Db::i()->select( 'record_topicid', "cms_custom_database_{$database->_id}", array( "category_id=?", $data['categoryId'] ) ) );
|
||||
$subquery = iterator_to_array( Db::i()->select( 'record_topicid', "cms_custom_database_{$database->_id}", array( "category_id=?", $data['categoryId'] ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
$subquery = iterator_to_array( \IPS\Db::i()->select( 'record_topicid', "cms_custom_database_{$database->_id}" ) );
|
||||
$subquery = iterator_to_array( Db::i()->select( 'record_topicid', "cms_custom_database_{$database->_id}" ) );
|
||||
}
|
||||
foreach( new \IPS\Patterns\ActiveRecordIterator( \IPS\Db::i()->select( '*', 'forums_posts', array( "pid>? AND new_topic=? AND " . \IPS\Db::i()->in( 'topic_id', $subquery ), $data['lastId'], 0 ), "pid ASC", $this->rebuild ), 'IPS\forums\Topic\Post' ) AS $row )
|
||||
foreach( new ActiveRecordIterator( Db::i()->select( '*', 'forums_posts', array( "pid>? AND new_topic=? AND " . Db::i()->in( 'topic_id', $subquery ), $data['lastId'], 0 ), "pid ASC", $this->rebuild ), 'IPS\forums\Topic\Post' ) AS $row )
|
||||
{
|
||||
/* @var Records $recordClass */
|
||||
$record = $recordClass::load( $row->topic_id, 'record_topicid' );
|
||||
|
||||
$approved = $row->queued;
|
||||
@@ -310,9 +331,9 @@ class _MoveComments
|
||||
$comment->save();
|
||||
|
||||
/* Update post before register */
|
||||
\IPS\Db::i()->update( 'core_post_before_registering', array( 'class' => $commentClass, 'id' => $comment->id ), array( 'class=? and id=?', "\IPS\forums\Topic\Post", $row->id ) );
|
||||
Db::i()->update( 'core_post_before_registering', array( 'class' => $commentClass, 'id' => $comment->id ), array( 'class=? and id=?', "\IPS\forums\Topic\Post", $row->id ) );
|
||||
|
||||
\IPS\Content\Search\Index::i()->index( $comment );
|
||||
Index::i()->index( $comment );
|
||||
|
||||
$row->delete();
|
||||
|
||||
@@ -321,9 +342,9 @@ class _MoveComments
|
||||
$done++;
|
||||
|
||||
/* If we're no longer keeping the topic, then delete it, but only if we're really done with it (ex, only the first post still exists). Otherwise, we still want the topic linked, we're just not using it for comments anymore. */
|
||||
if ( $data['deleteTopics'] AND (int) \IPS\Db::i()->select( 'COUNT(*)', 'forums_posts', array( "topic_id=?", $row->topic_id ) )->first() === 1 )
|
||||
if ( $data['deleteTopics'] AND (int) Db::i()->select( 'COUNT(*)', 'forums_posts', array( "topic_id=?", $row->topic_id ) )->first() === 1 )
|
||||
{
|
||||
\IPS\forums\Topic::load( $row->topic_id )->delete();
|
||||
Topic::load( $row->topic_id )->delete();
|
||||
$record->record_topicid = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user