Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Composer dependencies
/vendor/
/composer.lock

# IDE files
.vscode/
.idea/

# Temporary files
*.tmp
*.log

# OS files
.DS_Store
Thumbs.db
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"phpunit/phpunit": "^9.0"
}
}
2 changes: 1 addition & 1 deletion i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"authors": [ "MyWikis LLC" ]
},
"crawlerprotection-accessdenied-title": "Zugriff verweigert",
"crawlerprotection-accessdenied-text": "Anmeldung erforderlich, um diese Aktion auzuführen oder Spezialseite anzusehen."
"crawlerprotection-accessdenied-text": "Anmeldung erforderlich, um diese Aktion auzuführen oder Spezialseite anzusehen. Bitte [[Special:UserLogin|melden Sie sich an]], um fortzufahren."
}
2 changes: 1 addition & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"authors": [ "MyWikis LLC" ]
},
"crawlerprotection-accessdenied-title": "Access denied",
"crawlerprotection-accessdenied-text": "You must be logged in to perform this action or view this special page."
"crawlerprotection-accessdenied-text": "You must be logged in to perform this action or view this special page. Please [[Special:UserLogin|log in]] to continue."
}
7 changes: 5 additions & 2 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ public function onSpecialPageBeforeExecute( $special, $subPage ) {
* @return void
*/
protected function denyAccess( OutputPage $output ): void {
$output->clearHTML();
$output->setStatusCode( 403 );
$output->addWikiTextAsInterface( wfMessage( 'crawlerprotection-accessdenied-text' )->plain() );


if ( version_compare( MW_VERSION, '1.41', '<' ) ) {
$output->setPageTitle( wfMessage( 'crawlerprotection-accessdenied-title' ) );
} else {
// @phan-suppress-next-line PhanUndeclaredMethod Exists in 1.41+
$output->setPageTitleMsg( wfMessage( 'crawlerprotection-accessdenied-title' ) );
}

$output->addWikiTextAsInterface( wfMessage( 'crawlerprotection-accessdenied-text' )->plain() );
$output->returnToMain();
}
}
17 changes: 17 additions & 0 deletions tests/phpunit/unit/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,21 @@ public function testNonRevisionTypeAlwaysAllowed() {
$result = $runner->onMediaWikiPerformAction( $output, $article, $title, $user, $request, $wiki );
$this->assertTrue( $result );
}

/**
* @covers ::denyAccess
*/
public function testDenyAccessSetsCorrectStatusAndContent() {
$output = $this->createMock( self::$outputPageClassName );
$output->expects( $this->once() )->method( 'clearHTML' );
$output->expects( $this->once() )->method( 'setStatusCode' )->with( 403 );
$output->expects( $this->once() )->method( 'addWikiTextAsInterface' );
$output->expects( $this->once() )->method( 'returnToMain' );

$hooks = new Hooks();
$reflection = new \ReflectionClass( $hooks );
$method = $reflection->getMethod( 'denyAccess' );
$method->setAccessible( true );
$method->invoke( $hooks, $output );
}
}