Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImportPost
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 13
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthor
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSignatureUser
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 extractUserFromSignature
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getCreatedTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getModifiedTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getReplies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getApiResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSource
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRevisions
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
 createSignatureClarificationRevision
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 getObjectKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Import\LiquidThreadsApi;
4
5use AppendIterator;
6use ArrayIterator;
7use Flow\Import\IImportPost;
8use Flow\Import\IObjectRevision;
9use Iterator;
10use MediaWiki\Extension\Notifications\DiscussionParser;
11use MediaWiki\Title\Title;
12
13class ImportPost extends PageRevisionedObject implements IImportPost {
14
15    /**
16     * @var array
17     */
18    protected $apiResponse;
19
20    /**
21     * @param ImportSource $source
22     * @param array $apiResponse
23     */
24    public function __construct( ImportSource $source, array $apiResponse ) {
25        parent::__construct( $source, $apiResponse['rootid'] );
26        $this->apiResponse = $apiResponse;
27    }
28
29    /**
30     * @return string
31     */
32    public function getAuthor() {
33        return $this->apiResponse['author']['name'];
34    }
35
36    /**
37     * Gets the username (or IP) from the signature.
38     *
39     * @return string|null Returns username, IP, or null if none could be detected
40     */
41    public function getSignatureUser() {
42        $signatureText = $this->apiResponse['signature'];
43
44        return self::extractUserFromSignature( $signatureText );
45    }
46
47    /**
48     * Gets the username (or IP) from the provided signature.
49     *
50     * @param string $signatureText
51     * @return string|null Returns username, IP, or null if none could be detected
52     */
53    public static function extractUserFromSignature( $signatureText ) {
54        $users = DiscussionParser::extractUsersFromLine( $signatureText );
55
56        if ( count( $users ) > 0 ) {
57            return $users[0];
58        } else {
59            return null;
60        }
61    }
62
63    /**
64     * @return string|false
65     */
66    public function getCreatedTimestamp() {
67        return wfTimestamp( TS_MW, $this->apiResponse['created'] );
68    }
69
70    /**
71     * @return string|false
72     */
73    public function getModifiedTimestamp() {
74        return wfTimestamp( TS_MW, $this->apiResponse['modified'] );
75    }
76
77    public function getTitle() {
78        $pageData = $this->importSource->getPageData( $this->apiResponse['rootid'] );
79
80        return Title::newFromText( $pageData['title'] );
81    }
82
83    /**
84     * @return Iterator<IImportPost>
85     */
86    public function getReplies() {
87        return new ReplyIterator( $this );
88    }
89
90    /**
91     * @return array
92     */
93    public function getApiResponse() {
94        return $this->apiResponse;
95    }
96
97    /**
98     * @return ImportSource
99     */
100    public function getSource() {
101        return $this->importSource;
102    }
103
104    public function getRevisions() {
105        $authorUsername = $this->getAuthor();
106        $signatureUsername = $this->getSignatureUser();
107        if ( $signatureUsername !== null && $signatureUsername !== $authorUsername ) {
108            $originalRevisionData = $this->getRevisionData();
109
110            // This is not the same object as the last one in the original iterator,
111            // but it should be fungible.
112            $lastLqtRevision = new ImportRevision(
113                end( $originalRevisionData['revisions'] ),
114                $this,
115                $this->importSource->getScriptUser()
116            );
117            $signatureRevision = $this->createSignatureClarificationRevision(
118                $lastLqtRevision,
119                $authorUsername,
120                $signatureUsername
121            );
122
123            $originalRevisions = parent::getRevisions();
124            $iterator = new AppendIterator();
125            $iterator->append( $originalRevisions );
126            $iterator->append( new ArrayIterator( [ $signatureRevision ] ) );
127
128            return $iterator;
129        } else {
130            return parent::getRevisions();
131        }
132    }
133
134    /**
135     * Creates revision clarifying signature difference
136     *
137     * @param IObjectRevision $lastRevision Last revision prior to the clarification revision
138     * @param string $authorUsername
139     * @param string $signatureUsername Username extracted from signature
140     * @return ScriptedImportRevision Generated top import revision
141     */
142    protected function createSignatureClarificationRevision( IObjectRevision $lastRevision, $authorUsername, $signatureUsername ) {
143        $wikitextForLastRevision = $lastRevision->getText();
144        $newWikitext = $wikitextForLastRevision;
145
146        $templateName = wfMessage(
147            'flow-importer-lqt-different-author-signature-template'
148        )->inContentLanguage()->plain();
149        $arguments = implode(
150            '|',
151            [
152                "authorUser=$authorUsername",
153                "signatureUser=$signatureUsername",
154            ]
155        );
156
157        $newWikitext .= "\n\n{{{$templateName}|$arguments}}";
158        $clarificationRevision = new ScriptedImportRevision(
159            $this, $this->importSource->getScriptUser(), $newWikitext, $lastRevision
160        );
161
162        return $clarificationRevision;
163    }
164
165    public function getObjectKey() {
166        return $this->importSource->getObjectKey( 'thread_id', $this->apiResponse['id'] );
167    }
168}