Skip to content

Commit

Permalink
Merge pull request #64 from ticktackk/develop
Browse files Browse the repository at this point in the history
Rebuild attachments on upgrade #61
  • Loading branch information
ticktackk authored Apr 19, 2020
2 parents 2a6117b + 67d42e8 commit 254314b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
## 2.0.11 (`2001170`)

- **Fix:** When changing content owner attachment owner is not updated (#61)
- **On upgrade:** Will make an attempt to rebuild attachment owners

## 2.0.10 (`2001070`)

Expand Down
143 changes: 143 additions & 0 deletions Job/Upgrade/RebuildAttachmentOwner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace TickTackk\ChangeContentOwner\Job\Upgrade;

use TickTackk\ChangeContentOwner\ChangeOwner\AbstractHandler as AbstractChangeOwnerHandler;
use XF\Entity\Attachment as AttachmentEntity;
use XF\Job\AbstractRebuildJob;
use XF\App as BaseApp;
use XF\Mvc\Entity\Entity;
use XF\Phrase;
use XF\Db\AbstractAdapter as DbAdapter;

/**
* Class RebuildAttachmentOwner
*
* @package TickTackk\ChangeContentOwner\Job\Upgrade
*/
class RebuildAttachmentOwner extends AbstractRebuildJob
{
/**
* @var array[]
*/
protected $defaultData = [
'content_type' => null
];

/**
* @param int $start
* @param int $batch
*
* @return array
*/
protected function getNextIds($start, $batch) : array
{
$contentType = $this->getContentType();
if (!$contentType)
{
return [];
}

$app = $this->app();
$entityName = $app->getContentTypeEntity($contentType);
if (!$entityName)
{
return [];
}
$structure = $app->em()->getEntityStructure($entityName);
if (!$structure)
{
return [];
}

$db = $this->db();
return $db->fetchAllColumn($db->limit(
"
SELECT {$structure->primaryKey}
FROM {$structure->table}
WHERE {$structure->primaryKey} > ?
ORDER BY {$structure->primaryKey}
", $batch
), $start);
}

/**
* @param int $id
*
* @throws \XF\Db\Exception
*/
protected function rebuildById($id) : void
{
$contentType = $this->getContentType();
if (!$contentType)
{
return;
}

/** @var Entity $content */
$content = $this->app()->findByContentType($contentType, $id);
if (!$content)
{
return;
}

if (!\is_callable([\get_class($content), 'getChangeOwnerHandler']))
{
return;
}

/** @var AbstractChangeOwnerHandler $changeOwnerHandler */
$changeOwnerHandler = $content->getChangeOwnerHandler();
if (!$changeOwnerHandler)
{
return;
}

// current owner
$oldUser = $changeOwnerHandler->getOldOwner($content);
if (!$oldUser)
{
return;
}

$this->db()->query("
UPDATE xf_attachment_data AS attachment_data
INNER JOIN xf_attachment AS attachment
ON (attachment_data.data_id = attachment.data_id)
SET attachment_data.user_id = ?
WHERE attachment.content_type = ? AND content_id = ?
", [$oldUser->user_id, $content->getEntityContentType(), $content->getEntityId()]);
}

/**
* @return Phrase
*/
protected function getStatusType() : Phrase
{
return \XF::phrase('attachments');
}

/**
* @return string|null
*/
protected function getContentType() :? string
{
return $this->data['content_type'];
}

/**
* @return BaseApp
*/
protected function app() : BaseApp
{
return $this->app;
}

/**
* @return DbAdapter
*/
protected function db() : DbAdapter
{
return $this->app()->db();
}
}
13 changes: 13 additions & 0 deletions Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ public function upgrade2000770Step1() : void
);
}

public function upgrade2001170Step1() : void
{
foreach (['thread', 'post', 'xfmg_media', 'xfmg_album', 'xfmg_comment', 'graph'] AS $contentType)
{
$this->jobManager()->enqueueUnique(
'tckChangeContentOwner-' . __FUNCTION__ . '-' . $contentType,
'TickTackk\ChangeContentOwner:Upgrade\RebuildAttachmentOwner',
['content_type' => $contentType],
true
);
}
}

/**
* @param array $errors
* @param array $warnings
Expand Down

0 comments on commit 254314b

Please sign in to comment.