Skip to content

Commit

Permalink
Prevent title collision (#421)
Browse files Browse the repository at this point in the history
* prevent import on title collisions

* fix discard

---------

Co-authored-by: Daniel Riedmüller <[email protected]>
  • Loading branch information
danielriedmueller and Daniel Riedmüller authored Sep 4, 2024
1 parent 017afaf commit 663ebc3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"da-specialinbox-label-own-domain": "own domain",
"da-specialinbox-not-found": "Page not found in inbox",
"da-specialinbox-direct-merge": "Page \"'''$1'''\" does not exist on the wiki and can therefore be directly imported",
"da-specialinbox-direct-merge-title-collision": "Page \"'''$1'''\" already exists on the wiki and can therefore not be directly imported. Move existing page without leave redirect.",
"da-specialinbox-do-import-title": "Import a page from inbox",
"da-specialinbox-merge-submit": "Import page",
"da-specialinbox-direct-merge-success": "Page imported to [[$1]]",
Expand Down
31 changes: 22 additions & 9 deletions includes/SpecialInbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ private function tryCompare( $pageName ) {
$this->local = $this->getTargetEntity( $this->remote );

if ( !$this->local ) {
$this->outputDirectMerge( $this->remote );
// Abort if title collision with existing page, if genesis hashes are different
$title = $this->titleFactory->newFromDBkey( $pageName );
$hasTitleCollision = $title->exists();

$this->outputDirectMerge( $this->remote, $hasTitleCollision );
return;
}
$this->outputCompare( $this->remote, $this->local );
Expand All @@ -112,13 +116,18 @@ private function getTargetEntity( ?VerificationEntity $entity ): ?VerificationEn

/**
* @param VerificationEntity $draftEntity
* @param bool $disableSubmit
*
* @return void
*/
private function outputDirectMerge( VerificationEntity $draftEntity ) {
$this->getOutput()->addWikiMsg(
'da-specialinbox-direct-merge', $draftEntity->getTitle()->getText()
);
$this->outputForm( $draftEntity );
private function outputDirectMerge( VerificationEntity $draftEntity, bool $hasTitleCollision = false ) {
$msgKey = 'da-specialinbox-direct-merge';
if ( $hasTitleCollision ) {
$msgKey = 'da-specialinbox-direct-merge-title-collision';
}

$this->getOutput()->addWikiMsg( $msgKey, $draftEntity->getTitle()->getText() );
$this->outputForm( $draftEntity, 'new', $hasTitleCollision );
}

/**
Expand Down Expand Up @@ -224,11 +233,12 @@ private function makeSummaryHeader( array $treeData, VerificationEntity $draft,
/**
* @param VerificationEntity $remote
* @param string|null $changeType
* @param bool $hasTitleCollision
*
* @return void
*/
private function outputForm( VerificationEntity $remote, ?string $changeType = 'new' ) {
$somethingToImport = $changeType !== 'local' && $changeType !== 'none';
private function outputForm( VerificationEntity $remote, ?string $changeType = 'new', bool $hasTitleCollision = false ) {
$somethingToImport = $changeType !== 'local' && $changeType !== 'none' && !$hasTitleCollision;
$form = HTMLForm::factory(
'ooui',
[
Expand Down Expand Up @@ -308,7 +318,10 @@ private function outputDiscardForm( Title $remote ) {
*/
public function onAction( $formData ) {
$postValues = $this->getRequest()->getPostValues();
$shouldDiscard = $formData['action'] === 'discard' || $postValues['discard'] === 'Discard';
$shouldDiscard = $formData['action'] === 'discard'
|| $postValues['discard'] === 'Discard'
|| $postValues['discard'] === $this->msg( 'da-specialinbox-merge-discard' )->plain();

if ( $shouldDiscard ) {
return $this->doDiscard();
}
Expand Down

0 comments on commit 663ebc3

Please sign in to comment.