-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDefaultController.php
More file actions
57 lines (47 loc) · 1.61 KB
/
DefaultController.php
File metadata and controls
57 lines (47 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace carlcs\deleteentryversions\console\controllers;
use Craft;
use yii\console\Controller;
use yii\helpers\Console;
use craft\db\Query;
class DefaultController extends Controller
{
/**
* Deletes all but the most recent entry versions for each entry.
*
* @return Response
* @throws ForbiddenHttpException
*/
public function actionIndex()
{
// Get the most recent entry versions for each entry
$subQuery = (new Query())
->select('entryId, siteId, max(dateCreated) MaxDateCreated')
->from('{{%entryversions}}')
->groupBy('entryId, siteId');
$query = (new Query())
->select('e.id')
->from('{{%entryversions}} e')
->innerJoin(['g' => $subQuery], [
'and',
'e.entryId = g.entryId',
'e.siteId = g.siteId',
'e.dateCreated = g.MaxDateCreated'
]);
$ids = $query->column();
// Delete all other versions
$count = Craft::$app->getDb()->createCommand()
->delete('{{%entryversions}}', ['not in', 'id', $ids])
->execute();
if (!$count) {
echo Craft::t('delete-entry-versions', 'No entry versions exist yet.');
return false;
}
// Update the latest versions’ version number
Craft::$app->getDb()->createCommand()
->update('{{%entryversions}}', ['num' => 1])
->execute();
echo Craft::t('delete-entry-versions', '{count} entry versions deleted.', ['count' => $count]);
return true;
}
}