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