Issues (48)

src/Tasks/PublishAllPages.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Sunnysideup\MigrateData\Tasks;
4
5
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
The type SilverStripe\CMS\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Environment;
10
use SilverStripe\Dev\BuildTask;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\Security\SecurityToken;
14
use SilverStripe\Versioned\Versioned;
0 ignored issues
show
The type SilverStripe\Versioned\Versioned was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Sunnysideup\Flush\FlushNow;
16
use Sunnysideup\Flush\FlushNowImplementor;
17
18
/**
19
 * This code is stolen from somewhere, but unfortunately I am not sure exactly where.
20
 * my guess is that it used to be in the CMS.
21
 */
22
class PublishAllPages extends BuildTask
23
{
24
    protected $title = 'Publish All Pages';
25
26
    protected $description = 'Publish All Pages on the Entire Site (copy content from draft to live)';
27
28
    protected $step = 10;
29
30
    protected $allowed = true;
31
32
    protected $onlyPublishedPages = false;
33
34
    public function setAllowed(?bool $allowed = true): self
35
    {
36
        $this->allowed = $allowed;
37
38
        return $this;
39
    }
40
41
    public function setOnlyPublishedPages(?bool $bool = true): self
42
    {
43
        $this->onlyPublishedPages = $bool;
44
45
        return $this;
46
    }
47
48
    public function run($request)
49
    {
50
        Environment::increaseTimeLimitTo();
51
        Environment::increaseMemoryLimitTo();
52
        Config::modify()->set(DataObject::class, 'validation_enabled', false);
53
        if ($request->requestVar('confirm') || Director::is_cli() || $this->allowed) {
54
            // Protect against CSRF on destructive action
55
            if (Director::is_cli() || SecurityToken::inst()->checkRequest($request)) {
56
                $start = 0;
57
                $pages = SiteTree::get()->limit($this->step, $start);
58
                FlushNowImplementor:
59
                FlushNowImplementor::do_flush('<ol>');
60
                $count = 0;
61
                while ($pages->exists()) {
62
                    foreach ($pages as $page) {
63
                        $isPublished = $page->IsPublished() && !$page->isModifiedOnDraft();
64
                        FlushNowImplementor::do_flush('publishing: ' . $page->Title, 'created');
65
                        $page->writeToStage(Versioned::DRAFT, true);
66
                        if ($isPublished) {
67
                            $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
68
                            $page->publishRecursive();
69
                        }
70
                        $page->destroy();
71
                        unset($page);
72
                        ++$count;
73
                    }
74
                    $start += $this->step;
75
                    $pages = SiteTree::get()->limit($this->step, $start);
76
                }
77
                FlushNowImplementor::do_flush('</ol>');
78
                FlushNowImplementor::do_flush('<h2>--- PUBLISHED ' . $count . ' Pages ---</h2>');
79
            } else {
80
                Controller::curr()->httpError(400);
81
82
                return;
83
            }
84
        } else {
85
            $response = '';
86
            $token = SecurityToken::inst();
87
            $fields = FieldList::create();
88
            $token->updateFieldSet($fields);
89
            $tokenField = $fields->first();
90
            $tokenHtml = ($tokenField) ? $tokenField->FieldHolder() : '';
91
            $publishAllDescription = _t(
92
                __CLASS__ . '.PUBALLFUN2',
93
                'Pressing this button will do the equivalent of going to every page and pressing "publish".  '
94
                    . "It's intended to be used after there have been massive edits of the content, such as when "
95
                    . 'the site was first built.'
96
            );
97
            $response .=
98
                '<h1>' . _t(__CLASS__ . '.PUBALLFUN', 'Publish All functionality') . '</h1>
99
                <p>' . $publishAllDescription . '</p>
100
                <form method="get" action="">
101
                    <input type="submit" name="confirm" value="'
102
                . _t(__CLASS__ . '.PUBALLCONFIRM', 'Please publish every page in the site', 'Confirmation button') . '" />'
103
                . $tokenHtml .
104
                '</form>';
105
            FlushNowImplementor::do_flush($response);
106
        }
107
    }
108
}
109