‪TYPO3CMS  ‪main
AutoPublishCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Console\Attribute\AsCommand;
21 use Symfony\Component\Console\Command\Command;
22 use Symfony\Component\Console\Input\InputInterface;
23 use Symfony\Component\Console\Output\OutputInterface;
24 use Symfony\Component\Console\Style\SymfonyStyle;
32 
38 #[AsCommand('workspace:autopublish', 'Publish a workspace with a publication date.')]
39 class ‪AutoPublishCommand extends Command
40 {
41  public function ‪__construct(
42  private readonly ‪WorkspaceService $workspaceService,
43  private readonly ‪ConnectionPool $connectionPool
44  ) {
45  parent::__construct();
46  }
47 
51  public function ‪configure()
52  {
53  $this->setHelp('Some workspaces can have an auto-publish publication date to put all "ready to publish" content online on a certain date.');
54  }
55 
59  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
60  {
61  // Make sure the _cli_ user is loaded
63  $io = new SymfonyStyle($input, ‪$output);
64 
65  // Select all workspaces that needs to be published / unpublished
66  $statement = $this->‪getAffectedWorkspacesToPublish();
67 
68  $affectedWorkspaces = 0;
69  while ($workspaceRecord = $statement->fetchAssociative()) {
70  // First, clear start/end time so it doesn't get selected once again
71  $this->connectionPool
72  ->getConnectionForTable('sys_workspace')
73  ->update(
74  'sys_workspace',
75  ['publish_time' => 0],
76  ['uid' => (int)$workspaceRecord['uid']]
77  );
78 
79  // Get CMD array
80  $cmd = $this->workspaceService->getCmdArrayForPublishWS((int)$workspaceRecord['uid']);
81  $tce = GeneralUtility::makeInstance(DataHandler::class);
82  $tce->start([], $cmd);
83  $tce->process_cmdmap();
84  $affectedWorkspaces++;
85  }
86 
87  if ($affectedWorkspaces > 0) {
88  $io->success('Published ' . $affectedWorkspaces . ' workspaces.');
89  } else {
90  $io->note('Nothing to do.');
91  }
92  return Command::SUCCESS;
93  }
94 
99  protected function ‪getAffectedWorkspacesToPublish()
100  {
101  $queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_workspace');
102  $queryBuilder->getRestrictions()
103  ->removeAll()
104  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
105 
106  return $queryBuilder
107  ->select('uid', 'publish_time')
108  ->from('sys_workspace')
109  ->where(
110  $queryBuilder->expr()->eq(
111  'pid',
112  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
113  ),
114  $queryBuilder->expr()->neq(
115  'publish_time',
116  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
117  ),
118  $queryBuilder->expr()->lte(
119  'publish_time',
120  $queryBuilder->createNamedParameter(‪$GLOBALS['EXEC_TIME'], ‪Connection::PARAM_INT)
121  )
122  )
123  ->executeQuery();
124  }
125 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Workspaces\Command\AutoPublishCommand
Definition: AutoPublishCommand.php:40
‪TYPO3\CMS\Workspaces\Command\AutoPublishCommand\configure
‪configure()
Definition: AutoPublishCommand.php:51
‪TYPO3\CMS\Workspaces\Command\AutoPublishCommand\__construct
‪__construct(private readonly WorkspaceService $workspaceService, private readonly ConnectionPool $connectionPool)
Definition: AutoPublishCommand.php:41
‪TYPO3\CMS\Workspaces\Command\AutoPublishCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: AutoPublishCommand.php:59
‪TYPO3\CMS\Workspaces\Command
Definition: AutoPublishCommand.php:18
‪TYPO3\CMS\Workspaces\Service\WorkspaceService
Definition: WorkspaceService.php:38
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:62
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\Bootstrap\initializeBackendAuthentication
‪static initializeBackendAuthentication()
Definition: Bootstrap.php:527
‪TYPO3\CMS\Workspaces\Command\AutoPublishCommand\getAffectedWorkspacesToPublish
‪Doctrine DBAL Result getAffectedWorkspacesToPublish()
Definition: AutoPublishCommand.php:99