Skip to content
Merged
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
45 changes: 45 additions & 0 deletions app/Http/Controllers/Backend/WikiReadOnlyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Wiki;
use Illuminate\Http\Request;

class WikiReadOnlyController extends Controller {
public function setWikiReadOnly(Request $request) {

$validated = $request->validate([
'domain' => 'required|string',
'readOnly' => 'required|boolean',
]);

$domain = $validated['domain'];
$readOnly = $validated['readOnly'];

$wiki = Wiki::where('domain', $domain)->first();
if (!$wiki) {
return response()->json([
'error' => "Wiki not found for domain '$domain'",
], 404);
}

if ($readOnly) {
$wiki->setSetting('wgReadOnly', 'This wiki is currently read-only.');

return response()->json([
'success' => true,
'domain' => $domain,
'message' => 'Wiki set to read-only successfully.',
]);
} else {
$wiki->deleteSetting('wgReadOnly');

return response()->json([
'success' => true,
'domain' => $domain,
'message' => 'Read-only setting successfully removed for wiki.',
]);
}
}
}
11 changes: 11 additions & 0 deletions app/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,15 @@ public function getDomainDecodedAttribute(): string {
public function wikiLatestProfile() {
return $this->hasOne(WikiProfile::class)->latestOfMany();
}

public function setSetting(string $name, string $value): void {
$this->settings()->updateOrCreate(
['name' => $name],
['value' => $value]
);
}

public function deleteSetting(string $name): ?string {
return $this->settings()->where('name', $name)->delete();
}
}
2 changes: 2 additions & 0 deletions routes/backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// GET
$router->get('healthz', fn () => "It's Alive");
$router->get('getWikiHostsForDomain', ['uses' => 'MediaWikiHostsController@getWikiHostsForDomain']);
// PUT
$router->put('setWikiReadOnly', ['uses' => 'WikiReadOnlyController@setWikiReadOnly']);

$router->group(['prefix' => 'ingress'], function () use ($router) {
// GET
Expand Down
70 changes: 70 additions & 0 deletions tests/Routes/Backend/WikiReadOnlyControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Tests\Routes\Backend;

use App\Wiki;
use App\WikiSetting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WikiReadOnlyControllerTest extends TestCase {
use RefreshDatabase;

protected string $route = '/backend/setWikiReadOnly';

public function testItReturns404WhenWikiNotFound() {
$response = $this->putJson($this->route, [
'domain' => 'nonexistent.wikibase.cloud',
'readOnly' => true,
]);

$response->assertStatus(404)
->assertJson([
'error' => "Wiki not found for domain 'nonexistent.wikibase.cloud'",
]);
}

public function testSetWikiToReadOnly() {
$wiki = Wiki::factory()->create([
'domain' => 'somewiki.wikibase.cloud',
]);

$response = $this->putJson($this->route, [
'domain' => 'somewiki.wikibase.cloud',
'readOnly' => true,
]);

$response->assertStatus(200)
->assertJson([
'success' => true,
'domain' => 'somewiki.wikibase.cloud',
'message' => 'Wiki set to read-only successfully.',
]);

$this->assertSame(
'This wiki is currently read-only.',
WikiSetting::whereWikiId($wiki->id)->whereName('wgReadOnly')->first()->value
);

}

public function testDeleteSettingForReadOnlyFalse() {
$wiki = Wiki::factory()->create([
'domain' => 'somewiki.wikibase.cloud',
]);
$wiki->setSetting('wgReadOnly', 'test');

$this->putJson($this->route, [
'domain' => $wiki->domain,
'readOnly' => false,
])
->assertStatus(200)
->assertJson(['message' => 'Read-only setting successfully removed for wiki.']);

$this->assertNull(
WikiSetting::whereWikiId($wiki->id)
->whereName('wgReadOnly')
->first(),
);
}
}
Loading