Skip to content

Commit

Permalink
refactor: introduce namespace enum
Browse files Browse the repository at this point in the history
In order to reudce the chance of future mixups lets use
an enum for mediawiki namespaces. Athough these namespace
ids are not actually globally fixed within the wikibase cloud
and wbstack context we force them to be set this way.
  • Loading branch information
tarrow committed May 17, 2024
1 parent 70071b6 commit 78fa0c9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 90 deletions.
11 changes: 4 additions & 7 deletions app/Console/Commands/RebuildQueryserviceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Constants\MediawikiNamespaces;
use App\Traits;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
Expand All @@ -16,10 +17,6 @@ class RebuildQueryserviceData extends Command
{
use Traits\PageFetcher;

private const NAMESPACE_ITEM = 120;
private const NAMESPACE_PROPERTY = 122;
private const NAMESPACE_LEXEME = 146;

protected $signature = 'wbs-qs:rebuild {--domain=*} {--chunkSize=50} {--queueName=manual-intervention} {--sparqlUrlFormat=http://queryservice.default.svc.cluster.local:9999/bigdata/namespace/%s/sparql}';

protected $description = 'Rebuild the queryservice data for a certain wiki or all wikis';
Expand Down Expand Up @@ -80,8 +77,8 @@ public function handle()

private function getEntitiesForWiki (Wiki $wiki): array
{
$items = $this->fetchPagesInNamespace($wiki->domain, self::NAMESPACE_ITEM);
$properties = $this->fetchPagesInNamespace($wiki->domain, self::NAMESPACE_PROPERTY);
$items = $this->fetchPagesInNamespace($wiki->domain, MediawikiNamespaces::item);
$properties = $this->fetchPagesInNamespace($wiki->domain, MediawikiNamespaces::property);

$lexemesSetting = WikiSetting::where(
[
Expand All @@ -91,7 +88,7 @@ private function getEntitiesForWiki (Wiki $wiki): array
)->first();
$hasLexemesEnabled = $lexemesSetting !== null && $lexemesSetting->value === '1';
$lexemes = $hasLexemesEnabled
? $this->fetchPagesInNamespace($wiki->domain, self::NAMESPACE_LEXEME)
? $this->fetchPagesInNamespace($wiki->domain, MediawikiNamespaces::lexeme)
: [];

$merged = array_merge($items, $properties, $lexemes);
Expand Down
9 changes: 9 additions & 0 deletions app/Constants/MediawikiNamespaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Constants;

enum MediawikiNamespaces {
public const item = 120;
public const property = 122;
public const lexeme = 146;
}
7 changes: 2 additions & 5 deletions app/Jobs/CreateQueryserviceBatchesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Constants\MediawikiNamespaces;
use App\EventPageUpdate;
use App\QsBatch;
use App\QsCheckpoint;
Expand All @@ -12,10 +13,6 @@

class CreateQueryserviceBatchesJob extends Job
{
private const NAMESPACE_ITEM = 120;
private const NAMESPACE_PROPERTY = 122;
private const NAMESPACE_LEXEME = 146;

private int $entityLimit;

public $timeout = 3600;
Expand Down Expand Up @@ -58,7 +55,7 @@ private function getNewEntities(int $latestCheckpoint): array
EventPageUpdate::where(
'id', '>', $latestCheckpoint,
)
->whereIn('namespace', [self::NAMESPACE_ITEM, self::NAMESPACE_PROPERTY, self::NAMESPACE_LEXEME])
->whereIn('namespace', [MediawikiNamespaces::item, MediawikiNamespaces::property, MediawikiNamespaces::lexeme])
->has('wiki')
->chunk(100, function (Collection $chunk) use (&$newEntitiesFromEvents, &$latestEventId) {
foreach ($chunk as $event) {
Expand Down
8 changes: 3 additions & 5 deletions app/Jobs/PlatformStatsSummaryJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Constants\MediawikiNamespaces;
use App\Traits;
use App\Helper\MWTimestampHelper;
use App\Wiki;
Expand Down Expand Up @@ -38,9 +39,6 @@ class PlatformStatsSummaryJob extends Job

private $platformSummaryStatsVersion = "v1";

private const NAMESPACE_ITEM = 120;
private const NAMESPACE_PROPERTY = 122;

public function __construct() {
$this->inactiveThreshold = Config::get('wbstack.platform_summary_inactive_threshold');
$this->creationRateRanges = Config::get('wbstack.platform_summary_creation_rate_ranges');
Expand Down Expand Up @@ -86,13 +84,13 @@ public function prepareStats( array $allStats, $wikis): array {

//add items and properties counts of the wiki to the corresponded arrays
try {
$nextItemCount = count($this->fetchPagesInNamespace($wiki->domain, self::NAMESPACE_ITEM));
$nextItemCount = count($this->fetchPagesInNamespace($wiki->domain, MediawikiNamespaces::item));
array_push($itemsCount, $nextItemCount);
} catch (\Exception $ex) {
Log::warning("Failed to fetch item count for wiki ".$wiki->domain.", will use 0 instead.");
}
try {
$nextPropertyCount = count($this->fetchPagesInNamespace($wiki->domain, self::NAMESPACE_PROPERTY));
$nextPropertyCount = count($this->fetchPagesInNamespace($wiki->domain, MediawikiNamespaces::property));
array_push($propertiesCount, $nextPropertyCount);
} catch (\Exception $ex) {
Log::warning("Failed to fetch property count for wiki ".$wiki->domain.", will use 0 instead.");
Expand Down
59 changes: 30 additions & 29 deletions tests/Commands/RebuildQueryserviceDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Commands;

use App\Constants\MediawikiNamespaces;
use App\Wiki;
use App\QueryserviceNamespace;
use App\WikiSetting;
Expand Down Expand Up @@ -59,71 +60,71 @@ public function testWikiWithLexemes()
]);

Http::fake([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=&aplimit=max&format=json' => Http::response([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=&aplimit=max&format=json' => Http::response([
'query' => [
'allpages' => [
[
'title' => 'Property:P1',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
[
'title' => 'Property:P9',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
[
'title' => 'Property:P11',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
],
],
], 200),
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=&aplimit=max&format=json' => Http::response([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=&aplimit=max&format=json' => Http::response([
'continue' => [
'apcontinue' => 'Q6',
],
'query' => [
'allpages' => [
[
'title' => 'Item:Q1',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q2',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q3',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q4',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q5',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
],
],
], 200),
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=Q6&aplimit=max&format=json' => Http::response([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=Q6&aplimit=max&format=json' => Http::response([
'query' => [
'allpages' => [
[
'title' => 'Item:Q6',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q7',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q8',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q9',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
],
]
Expand Down Expand Up @@ -188,46 +189,46 @@ public function testWikiNoLexemes()
]);

Http::fake([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=&aplimit=max&format=json' => Http::response([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=&aplimit=max&format=json' => Http::response([
'query' => [
'allpages' => [
[
'title' => 'Property:P1',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
[
'title' => 'Property:P9',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
[
'title' => 'Property:P11',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
],
],
], 200),
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=&aplimit=max&format=json' => Http::response([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=&aplimit=max&format=json' => Http::response([
'query' => [
'allpages' => [
[
'title' => 'Item:Q1',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q2',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q3',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q4',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
[
'title' => 'Item:Q5',
'namespace' => 122,
'namespace' => MediawikiNamespaces::item,
],
],
],
Expand Down Expand Up @@ -264,20 +265,20 @@ public function testFailure()
]);

Http::fake([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=&aplimit=max&format=json' => Http::response([
getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=&aplimit=max&format=json' => Http::response([
'query' => [
'allpages' => [
[
'title' => 'Property:P1',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
[
'title' => 'Property:P9',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
[
'title' => 'Property:P11',
'namespace' => 120,
'namespace' => MediawikiNamespaces::property,
],
],
],
Expand Down
Loading

0 comments on commit 78fa0c9

Please sign in to comment.