Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: introduce namespace enum #806

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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\MediawikiNamespace;
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, MediawikiNamespace::item);
$properties = $this->fetchPagesInNamespace($wiki->domain, MediawikiNamespace::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, MediawikiNamespace::lexeme)
: [];

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

namespace App\Constants;

enum MediawikiNamespace: int {
case item = 120;
case property = 122;
case 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\MediawikiNamespace;
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', [MediawikiNamespace::item, MediawikiNamespace::property, MediawikiNamespace::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\MediawikiNamespace;
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, MediawikiNamespace::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, MediawikiNamespace::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
5 changes: 3 additions & 2 deletions app/Traits/PageFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace App\Traits;

use App\Constants\MediawikiNamespace;
use Illuminate\Support\Facades\Http;

trait PageFetcher
{
private string $apiUrl;

//this function is used to fetch pages on namespace
function fetchPagesInNamespace(string $wikiDomain, int $namespace): array
function fetchPagesInNamespace(string $wikiDomain, MediawikiNamespace $namespace): array
{
if (empty($this->apiUrl)) {
throw new \RuntimeException('API URL has not been set.');
Expand All @@ -25,7 +26,7 @@ function fetchPagesInNamespace(string $wikiDomain, int $namespace): array
[
'action' => 'query',
'list' => 'allpages',
'apnamespace' => $namespace,
'apnamespace' => $namespace->value,
'apcontinue' => $cursor,
'aplimit' => 'max',
'format' => 'json',
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\MediawikiNamespace;
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' => MediawikiNamespace::property,
],
[
'title' => 'Property:P9',
'namespace' => 120,
'namespace' => MediawikiNamespace::property,
],
[
'title' => 'Property:P11',
'namespace' => 120,
'namespace' => MediawikiNamespace::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' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q2',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q3',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q4',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q5',
'namespace' => 122,
'namespace' => MediawikiNamespace::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' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q7',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q8',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q9',
'namespace' => 122,
'namespace' => MediawikiNamespace::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' => MediawikiNamespace::property,
],
[
'title' => 'Property:P9',
'namespace' => 120,
'namespace' => MediawikiNamespace::property,
],
[
'title' => 'Property:P11',
'namespace' => 120,
'namespace' => MediawikiNamespace::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' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q2',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q3',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q4',
'namespace' => 122,
'namespace' => MediawikiNamespace::item,
],
[
'title' => 'Item:Q5',
'namespace' => 122,
'namespace' => MediawikiNamespace::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' => MediawikiNamespace::property,
],
[
'title' => 'Property:P9',
'namespace' => 120,
'namespace' => MediawikiNamespace::property,
],
[
'title' => 'Property:P11',
'namespace' => 120,
'namespace' => MediawikiNamespace::property,
],
],
],
Expand Down
Loading
Loading