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