Issues (106)

Infrastructure/Mediawiki/ExtendedRevisionSaver.php (1 issue)

Severity
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure\Mediawiki;
11
12
use Mediawiki\Api\Service\RevisionSaver;
13
use Mediawiki\Api\SimpleRequest;
14
use Mediawiki\DataModel\EditInfo;
15
use Mediawiki\DataModel\Revision;
16
use RuntimeException;
17
18
/**
19
 * @access private
20
 * @author Addshore
21
 * @author DFelten (EditInfo fix)
22
 */
23
class ExtendedRevisionSaver extends RevisionSaver
24
{
25
    /**
26
     * @var mixed
27
     */
28
    protected $errors;
29
30
    /**
31
     * @since 0.2
32
     */
33
    public function save(Revision $revision, EditInfo $editInfo = null): bool
34
    {
35
        $editInfo = $editInfo ?: $revision->getEditInfo();
36
37
        $result = $this->api->postRequest(
38
            new SimpleRequest('edit', $this->getEditParams($revision, $editInfo))
39
        );
40
        $success = ($result['edit']['result'] == 'Success');
41
        if (!$success) {
42
            $this->errors = $result;
43
        }
44
45
        return $success;
46
    }
47
48
    public function getErrors(): ?array
49
    {
50
        return $this->errors;
51
    }
52
53
54
    /**
55
     * @throws RuntimeException
56
     */
57
    private function getEditParams(Revision $revision, EditInfo $editInfo = null): array
58
    {
59
        if (!$revision->getPageIdentifier()->identifiesPage()) {
60
            throw new RuntimeException('$revision PageIdentifier does not identify a page');
61
        }
62
63
        $params = [];
64
65
        $content = $revision->getContent();
66
        $data = $content->getData();
67
        if (!is_string($data)) {
68
            throw new RuntimeException('Dont know how to save content of this model.');
69
        }
70
        $params['text'] = $content->getData();
71
        $params['md5'] = md5((string) $content->getData());
72
73
        $timestamp = $revision->getTimestamp();
74
        if (!is_null($timestamp)) {
75
            $params['basetimestamp'] = $timestamp;
76
        }
77
78
        if (!is_null($revision->getPageIdentifier()->getId())) {
79
            $params['pageid'] = $revision->getPageIdentifier()->getId();
80
        } else {
81
            $params['title'] = $revision->getPageIdentifier()->getTitle()->getTitle();
0 ignored issues
show
Deprecated Code introduced by
The function Mediawiki\DataModel\Title::getTitle() has been deprecated: in 0.6 use getText (makes things look cleaner) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

81
            $params['title'] = /** @scrutinizer ignore-deprecated */ $revision->getPageIdentifier()->getTitle()->getTitle();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
82
        }
83
84
        $params['token'] = $this->api->getToken();
85
86
        if ($this->api->isLoggedin()) {
87
            $params['assert'] = 'user';
88
        }
89
90
        $this->addEditInfoParams($editInfo, $params);
91
92
        return $params;
93
    }
94
95
    private function addEditInfoParams(?EditInfo $editInfo, array &$params): void
96
    {
97
        if (!is_null($editInfo)) {
98
            $params['summary'] = $editInfo->getSummary();
99
            if ($editInfo->getMinor()) {
100
                $params['minor'] = true;
101
            }
102
            if ($editInfo->getBot()) {
103
                $params['bot'] = true;
104
                $params['assert'] = 'bot';
105
            }
106
        }
107
    }
108
}
109