Skip to content

Commit

Permalink
Normalised first and lasted edited time to use revisions (#854)
Browse files Browse the repository at this point in the history
This commit calculates the last edited time using a similar
methodology to the existing one to calculate first edited
time. It uses the `allrevisions` list.

This method will exclude all revisions created by the
PlatformReservedUser. This means that it will erroneously
ignore revisions from importing entities.

This commit also introduces a migration to refresh all WikiLifecycleEvents

Bug: T364991
  • Loading branch information
tarrow authored Aug 8, 2024
1 parent 75d9064 commit cc2f6ff
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 241 deletions.
35 changes: 29 additions & 6 deletions app/Jobs/UpdateWikiSiteStatsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Wiki;
use App\WikiSiteStats;
use Carbon\CarbonInterface;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
Expand All @@ -13,6 +15,7 @@

class UpdateWikiSiteStatsJob extends Job implements ShouldBeUnique
{
use Dispatchable;
public $timeout = 3600;
public function handle (): void
{
Expand Down Expand Up @@ -73,7 +76,7 @@ private function updateSiteStats (Wiki $wiki): void
});
}

private function getFirstEditedDate (Wiki $wiki): ?\Carbon\CarbonInterface
private function getFirstEditedDate (Wiki $wiki): ?CarbonInterface
{
$allRevisions = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php',
Expand Down Expand Up @@ -111,18 +114,38 @@ private function getFirstEditedDate (Wiki $wiki): ?\Carbon\CarbonInterface
return Carbon::parse($result);
}

private function getLastEditedDate (Wiki $wiki): ?\Carbon\CarbonInterface
private function getLastEditedDate (Wiki $wiki): ?CarbonInterface
{
$recentChangesInfo = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
$allRevisions = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php',
[
'action' => 'query',
'format' => 'json',
'list' => 'allrevisions',
'formatversion' => 2,
'arvlimit' => 1,
'arvprop' => 'ids',
'arvexcludeuser' => 'PlatformReservedUser',
'arvdir' => 'older',
],
);
$lastRevision = data_get($allRevisions->json(), 'query.allrevisions.0.revisions.0.revid');
if (!$lastRevision) {
return null;
}

$revisionInfo = Http::withHeaders(['host' => $wiki->getAttribute('domain')])->get(
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php',
[
'action' => 'query',
'list' => 'recentchanges',
'format' => 'json',
'rcexcludeuser' => 'PlatformReservedUser',
'prop' => 'revisions',
'rvprop' => 'timestamp',
'formatversion' => 2,
'revids' => $lastRevision,
],
);
$result = data_get($recentChangesInfo->json(), 'query.recentchanges.0.timestamp');
$result = data_get($revisionInfo->json(), 'query.pages.0.revisions.0.timestamp');
if (!$result) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\Jobs\UpdateWikiSiteStatsJob;
use App\WikiLifecycleEvents;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
WikiLifecycleEvents::query()->delete();
UpdateWikiSiteStatsJob::dispatch();
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Loading

0 comments on commit cc2f6ff

Please sign in to comment.