Skip to content
Open
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
2 changes: 1 addition & 1 deletion app/Http/Controllers/PublicWikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function index(Request $request) {
]);

$params = array_merge(self::$defaultParams, $request->input());
$query = Wiki::query();
$query = Wiki::query()->with('wikiLatestProfile');

if (array_key_exists('is_featured', $params)) {
$query = $query->where([
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Resources/PublicWikiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public function toArray($request): array {
'sitename' => $this->sitename,
'wiki_site_stats' => $this->wikiSiteStats,
'logo_url' => $logoSetting ? $logoSetting->value : null,

// Checking relation load state before reading it to avoid N+1 query
// This relies on the controller to eager load `wikiLatestProfile` relationship
'reuse_prototype' => $this->wikiLatestProfile
? $this->wikiLatestProfile->purpose === 'data_hub'
&& $this->wikiLatestProfile->temporality === 'permanent'
&& $this->wikiLatestProfile->audience === 'wide'
: null,
];
}
}
12 changes: 12 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Curl\CurlRequest;
use App\Http\Curl\HttpRequest;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider;
Expand All @@ -25,5 +26,16 @@ public function boot(): void {
$wrappedException = new \Exception("Executing Job '$name' failed.", 1, $event->exception);
report($wrappedException);
});

// Local-only SQL query logging for debugging
if ($this->app->environment('local')) {
\Event::listen(QueryExecuted::class, function (QueryExecuted $query) {
\Log::debug('Query Executed: ', [
'sql' => $query->sql,
'bindings' => $query->bindings,
'connection' => $query->connectionName,
]);
});
}
Comment on lines +31 to +39
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stream or file "/var/www/html/storage/logs/laravel-2026-05-29.log" could not be opened in append mode: Failed to open stream: Permission denied

I got the above error again and tracked it down to:

  • some of the logs in /var/www/html/storage/logs/ were owned by root:root rather than www-data:www-data
  • this event listening doing a log

I don't think it's an issue in staging/production as I don't think logging to a file is enabled.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I just checked and had the same issue. But you've already specified "local"-only so I don't think it's going to be a problem on staging/prod

}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
});

$router->apiResource('wiki', 'PublicWikiController')->only(['index', 'show']);
$router->apiResource('reusePrototype', 'PublicWikiController')->only(['index', 'show']);
$router->apiResource('wikiConversionData', 'ConversionMetricController')->only(['index']);
});
66 changes: 66 additions & 0 deletions tests/Http/Controllers/PublicWikiControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Http\Controllers;

use App\Http\Controllers\PublicWikiController;
use App\Http\Resources\PublicWikiResource;
use App\Wiki;
use App\WikiProfile;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class PublicWikiControllerTest extends TestCase {
use DatabaseTransactions;

public function testShowLoadsWikiLatestProfileForResource(): void {
$wiki = Wiki::factory()->create([
'domain' => 'controller-test.wikibase.cloud',
'sitename' => 'controller-test',
]);

WikiProfile::create([
'wiki_id' => $wiki->id,
'purpose' => 'data_hub',
'temporality' => 'permanent',
'audience' => 'wide',
]);

$controller = new PublicWikiController;
$resource = $controller->show($wiki->id);

$this->assertInstanceOf(PublicWikiResource::class, $resource);
$this->assertSame(true, $resource->toArray(new Request)['reuse_prototype']);
}

public function testIndexEagerLoadsWikiLatestProfileOnceForCollection(): void {
for ($i = 1; $i <= 3; $i++) {
$wiki = Wiki::factory()->create([
'domain' => "index-eager-load-test-{$i}.wikibase.cloud",
'sitename' => "Index Eager Load Test Site {$i}",
]);

WikiProfile::create([
'wiki_id' => $wiki->id,
'purpose' => 'data_hub',
'temporality' => 'permanent',
'audience' => 'wide',
]);
}

$profileQueryCount = 0;
DB::listen(function (QueryExecuted $query) use (&$profileQueryCount): void {
if (str_contains($query->sql, 'wiki_profiles')) {
$profileQueryCount++;
}
});

$controller = new PublicWikiController;
$resourceCollection = $controller->index(new Request);

$this->assertSame(1, $profileQueryCount);
$this->assertTrue($resourceCollection->first()->relationLoaded('wikiLatestProfile'));
}
}
51 changes: 51 additions & 0 deletions tests/Routes/Wiki/PublicWikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Routes\Wiki;

use App\Wiki;
use App\WikiProfile;
use App\WikiSetting;
use App\WikiSiteStats;
use Illuminate\Foundation\Testing\DatabaseTransactions;
Expand All @@ -18,12 +19,14 @@ class PublicWikiTest extends TestCase {
protected function setUp(): void {
parent::setUp();
Wiki::query()->delete();
WikiProfile::query()->delete();
WikiSiteStats::query()->delete();
WikiSetting::query()->delete();
}

protected function tearDown(): void {
Wiki::query()->delete();
WikiProfile::query()->delete();
WikiSiteStats::query()->delete();
WikiSetting::query()->delete();
parent::tearDown();
Expand Down Expand Up @@ -279,4 +282,52 @@ public function testLogoUrl() {
)
->assertJsonPath('data.1.logo_url', null);
}

public function testReusePrototype() {
$reusableWiki = Wiki::factory()->create([
'domain' => 'reusable.wikibase.cloud', 'sitename' => 'asite',
]);
WikiSiteStats::factory()->create([
'wiki_id' => $reusableWiki->id,
]);
WikiProfile::create([
'wiki_id' => $reusableWiki->id,
'purpose' => 'data_hub',
'temporality' => 'permanent',
'audience' => 'wide',
]);

$nonReusableWiki = Wiki::factory()->create([
'domain' => 'non-reusable.wikibase.cloud', 'sitename' => 'bsite',
]);
WikiSiteStats::factory()->create([
'wiki_id' => $nonReusableWiki->id,
]);
WikiProfile::create([
'wiki_id' => $nonReusableWiki->id,
'purpose' => 'other',
'temporality' => 'other',
'audience' => 'other',
]);

$noProfileWiki = Wiki::factory()->create([
'domain' => 'no-profile.wikibase.cloud', 'sitename' => 'csite',
]);
WikiSiteStats::factory()->create([
'wiki_id' => $noProfileWiki->id,
]);

$this->json('GET', 'reusePrototype')
->assertStatus(200)
->assertJsonPath('data.0.domain', 'reusable.wikibase.cloud')
->assertJsonPath('data.0.reuse_prototype', true)
->assertJsonPath('data.1.domain', 'non-reusable.wikibase.cloud')
->assertJsonPath('data.1.reuse_prototype', false)
->assertJsonPath('data.2.domain', 'no-profile.wikibase.cloud')
->assertJsonPath('data.2.reuse_prototype', null);

$this->json('GET', 'reusePrototype/' . $reusableWiki->id)
->assertStatus(200)
->assertJsonPath('data.reuse_prototype', true);
}
}
Loading