Skip to content
This repository has been archived by the owner on Dec 7, 2024. It is now read-only.

Commit

Permalink
Add initial support for non-wikitext content
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielWTQ committed Aug 13, 2024
1 parent 8bcacb9 commit 332bfb9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
14 changes: 11 additions & 3 deletions includes/PagePort.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function import( string $root, string $user = null ): array {
foreach ( $pages as $page ) {
$title = Title::newFromText( $page['fulltitle'] );
$wp = WikiPage::factory( $title );
$wp->doUserEditContent( new WikitextContent( $page['content'] ), $user, 'Imported by PagePort' );
$wp->doUserEditContent(
ContentHandler::makeContent( $page['content'], $title ),
$user,
'Imported by PagePort'
);
}
return $pages;
}
Expand Down Expand Up @@ -218,14 +222,18 @@ public function export( array $pages, string $root, bool $save = true ) {
if ( strpos( $namespaceName, '/' ) !== false ) {
$namespaceName = str_replace( '/', '|', $namespaceName );
}
$content = WikiPage::factory( $title )->getContent()->getWikitextForTransclusion();
$contentObj = WikiPage::factory( $title )->getContent();
$content = $contentObj->getWikitextForTransclusion();
if ( $save && !file_exists( $root . '/' . $namespaceName ) ) {
mkdir( $root . '/' . $namespaceName );
}
if ( strpos( $filename, '/' ) !== false ) {
$filename = str_replace( '/', '|', $filename );
}
$targetFileName = $root . '/' . $namespaceName . '/' . $filename . '.mediawiki';
$targetFileName = $root . '/' . $namespaceName . '/' . $filename;
if ( $contentObj->getModel() === CONTENT_MODEL_WIKITEXT ) {
$targetFileName .= '.mediawiki';
}
if ( $save ) {
file_put_contents( $targetFileName, $content );
} else {
Expand Down
36 changes: 34 additions & 2 deletions tests/phpunit/PagePortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public function setup(): void {
$this->insertPage( 'Page8Test', 'Page8TestContents', NS_CUSTOM_SLASH );
$this->insertPage( 'Page9Test', 'Page9TestContents [[Category:TestRootCategory]]', NS_CATEGORY );
$this->insertPage( 'Page10Test', 'Page10TestContents', NS_PROJECT );
$this->insertPage(
'Common.css',
'body { overflow-y: scroll; }',
NS_MEDIAWIKI
);
$this->insertPage(
'Example.js',
'$(document).ready(() => console.log("Loaded"))',
NS_MEDIAWIKI
);
$this->pp = PagePort::getInstance();
}

Expand Down Expand Up @@ -83,6 +93,8 @@ public function testExportPages(): void {
'CustomNamespace/With/Slashes:Page8Test',
'Project:Page10Test',
'RandomMetaName:Page10Test',
'MediaWiki:Common.css',
'MediaWiki:Example.js',
];
$result = $this->pp->export( $pages, "testRoot", false );
$this->assertCount( count( $pages ) - 1, $result, 'with $save=false an array is returned' );
Expand Down Expand Up @@ -141,6 +153,16 @@ public function testExportPages(): void {
array_keys( $result )[9],
'file root for project/meta pages exported correctly'
);
$this->assertEquals(
'testRoot/MediaWiki/Common.css',
array_keys( $result )[10],
'no .mediawiki for CSS files'
);
$this->assertEquals(
'testRoot/MediaWiki/Example.js',
array_keys( $result )[11],
'no .mediawiki for JS files'
);
}

/**
Expand All @@ -161,6 +183,8 @@ public function testExportPagesJSON(): void {
'CustomNamespace/With/Slashes:Page8Test',
'Project:Page10Test',
'RandomMetaName:Page10Test',
'MediaWiki:Common.css',
'MediaWiki:Example.js',
];
$result = $this->pp->exportJSON(
$pages,
Expand Down Expand Up @@ -277,7 +301,9 @@ public function testImport() {
'Page4Test/SubPage1Test',
'Template:Page5Test',
'File:Page6Test',
'Page with spaces'
'Page with spaces',
'MediaWiki:Common.css',
'MediaWiki:Example.js',
];
$this->pp->export( $pages, $tempDir );
foreach ( $pages as $page ) {
Expand All @@ -290,7 +316,13 @@ public function testImport() {
$title = Title::newFromText( $page );
$this->assertTrue( $title->exists() );
$wp = WikiPage::factory( $title );
$this->assertTrue( strlen( $wp->getContent()->getWikitextForTransclusion() ) > 0 );
$content = $wp->getContent();
$this->assertTrue( strlen( $content->getWikitextForTransclusion() ) > 0 );
if ( $page === 'MediaWiki:Common.css' ) {
$this->assertSame( CONTENT_MODEL_CSS, $content->getModel() );
} elseif ( $page === 'MediaWiki:Common.js' ) {
$this->assertSame( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
}
}
$allPages = $this->pp->getAllPages();
foreach ( $pages as $p ) {
Expand Down

0 comments on commit 332bfb9

Please sign in to comment.