Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ImportHeader | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getRevisions | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
createHeaderCleanupRevision | |
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 ArrayIterator; |
6 | use Flow\Import\IImportHeader; |
7 | use Flow\Import\IObjectRevision; |
8 | use MediaWiki\Title\Title; |
9 | use MediaWiki\Utils\MWTimestamp; |
10 | |
11 | class ImportHeader extends PageRevisionedObject implements IImportHeader { |
12 | /** @var ApiBackend */ |
13 | protected $api; |
14 | /** @var string */ |
15 | protected $title; |
16 | /** @var array|null */ |
17 | protected $pageData; |
18 | /** @var ImportSource */ |
19 | protected $source; |
20 | |
21 | public function __construct( ApiBackend $api, ImportSource $source, $title ) { |
22 | $this->api = $api; |
23 | $this->title = $title; |
24 | $this->source = $source; |
25 | $this->pageData = null; |
26 | } |
27 | |
28 | public function getRevisions() { |
29 | if ( $this->pageData === null ) { |
30 | // Previous revisions of the header are preserved in the underlying wikitext |
31 | // page history. Only the top revision is imported. |
32 | $response = $this->api->retrieveTopRevisionByTitle( [ $this->title ] ); |
33 | $this->pageData = reset( $response ); |
34 | } |
35 | |
36 | $revisions = []; |
37 | |
38 | if ( isset( $this->pageData['revisions'] ) && count( $this->pageData['revisions'] ) > 0 ) { |
39 | $lastLqtRevision = new ImportRevision( |
40 | end( $this->pageData['revisions'] ), $this, $this->source->getScriptUser() |
41 | ); |
42 | |
43 | $titleObject = Title::newFromText( $this->title ); |
44 | $cleanupRevision = $this->createHeaderCleanupRevision( $lastLqtRevision, $titleObject ); |
45 | |
46 | $revisions = [ |
47 | $lastLqtRevision, |
48 | $cleanupRevision |
49 | ]; |
50 | } |
51 | |
52 | return new ArrayIterator( $revisions ); |
53 | } |
54 | |
55 | /** |
56 | * @param IObjectRevision $lastRevision last imported header revision |
57 | * @param Title $archiveTitle archive page title associated with header |
58 | * @return IObjectRevision generated revision for cleanup edit |
59 | */ |
60 | protected function createHeaderCleanupRevision( IObjectRevision $lastRevision, Title $archiveTitle ) { |
61 | $wikitextForLastRevision = $lastRevision->getText(); |
62 | // This is will remove all instances, without attempting to check if it's in |
63 | // nowiki, etc. It also ignores case and spaces in places where it doesn't |
64 | // matter. |
65 | $newWikitext = ConversionStrategy::removeLqtMagicWord( $wikitextForLastRevision ); |
66 | $templateName = wfMessage( 'flow-importer-lqt-converted-template' )->inContentLanguage( |
67 | )->plain(); |
68 | $arguments = implode( |
69 | '|', |
70 | [ |
71 | 'archive=' . $archiveTitle->getPrefixedText(), |
72 | 'date=' . MWTimestamp::getInstance()->timestamp->format( 'Y-m-d' ), |
73 | ] |
74 | ); |
75 | |
76 | $newWikitext .= "\n\n{{{$templateName}|$arguments}}"; |
77 | |
78 | $cleanupRevision = new ScriptedImportRevision( |
79 | $this, $this->source->getScriptUser(), $newWikitext, $lastRevision |
80 | ); |
81 | |
82 | return $cleanupRevision; |
83 | } |
84 | |
85 | public function getObjectKey() { |
86 | return $this->source->getObjectKey( 'header_for', $this->title ); |
87 | } |
88 | } |