Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ImportHeader | |
0.00% |
0 / 35 |
|
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 / 15 |
|
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()->plain(); |
| 67 | $arguments = implode( |
| 68 | '|', |
| 69 | [ |
| 70 | 'archive=' . $archiveTitle->getPrefixedText(), |
| 71 | 'date=' . MWTimestamp::getInstance()->timestamp->format( 'Y-m-d' ), |
| 72 | ] |
| 73 | ); |
| 74 | |
| 75 | $newWikitext .= "\n\n{{{$templateName}|$arguments}}"; |
| 76 | |
| 77 | $cleanupRevision = new ScriptedImportRevision( |
| 78 | $this, $this->source->getScriptUser(), $newWikitext, $lastRevision |
| 79 | ); |
| 80 | |
| 81 | return $cleanupRevision; |
| 82 | } |
| 83 | |
| 84 | public function getObjectKey() { |
| 85 | return $this->source->getObjectKey( 'header_for', $this->title ); |
| 86 | } |
| 87 | } |