Skip to content

Commit

Permalink
Add more type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonXLF committed Nov 25, 2023
1 parent 4fd0790 commit 7297291
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion includes/APIHelpExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class APIHelpExamples implements HtmlProducer {
private $examples;
private $prefix;

public function __construct(...$examples) {
public function __construct(string ...$examples) {
$this->examples = $examples;
$this->prefix = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
}
Expand Down
2 changes: 1 addition & 1 deletion includes/APIHelpObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class APIHelpObject implements HtmlProducer {
private $keys;

public function __construct(...$keys) {
public function __construct(string ...$keys) {
$this->keys = $keys;
}

Expand Down
4 changes: 2 additions & 2 deletions includes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ private static function &load() {
return self::$config;
}

static function get($key) {
public static function get(string $key) {
return self::load()[$key];
}

static function set($key, $value) {
public static function set(string $key, string $value) {
return self::load()[$key] = $value;
}
}
32 changes: 19 additions & 13 deletions includes/CountQuery.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

enum CountQueryMode {
case Redirect;
case Link;
case Transclusion;
}

class CountQuery {
public const SINGLE_NS = 1;
public const NO_FROM_NS = 2;
Expand All @@ -9,17 +15,17 @@ class CountQuery {
private $db;
private $title;

public function __construct($fromNamespaces, $db, $title) {
public function __construct(string $fromNamespaces, PDO $db, Title $title) {
$this->fromNamespaces = $fromNamespaces;
$this->db = $db;
$this->title = $title;
}

private function createCond(
$prefix,
$titleSQL,
$namespaceSQL,
$flags,
string $prefix,
string $titleSQL,
string $namespaceSQL,
int $flags,
$joins = [],
$wheres = []
) {
Expand Down Expand Up @@ -58,7 +64,7 @@ private function createCond(
return implode(' ', $joins) . " WHERE " . implode(' AND ', $wheres);
}

private function createDirectCond($prefix, $flags) {
private function createDirectCond(string $prefix, int $flags) {
return $this->createCond(
$prefix,
$this->db->quote($this->title->getDBKey()),
Expand All @@ -67,7 +73,7 @@ private function createDirectCond($prefix, $flags) {
);
}

private function createIndirectCond($table, $prefix, $flags) {
private function createIndirectCond(string $table, string $prefix, int $flags) {
$joins = [
'JOIN page AS target ON target.page_id = rd_from',
"JOIN $table"
Expand All @@ -89,9 +95,9 @@ private function createIndirectCond($table, $prefix, $flags) {
);
}

private function createQuery($table, $prefix, $mode, $flags) {
private function createQuery(string $table, string $prefix, CountQueryMode $mode, int $flags) {
return match ($mode) {
self::MODE_REDIRECT => <<<SQL
CountQueryMode::Redirect => <<<SQL
SELECT COUNT(rd_from) FROM $table
{$this->createDirectCond($prefix, $flags)}
AND ({$prefix}_interwiki is NULL or {$prefix}_interwiki = {$this->db->quote('')})
Expand All @@ -100,7 +106,7 @@ private function createQuery($table, $prefix, $mode, $flags) {
// There is no way to differentiate from a page with a indirect link and a page with a indirect and a direct link
// in this case, only the indirect link is recorded. Pages can also transclude a page with a redirect without
// following the redirect, so a valid indirect link must have an associated direct link.
self::MODE_TRANSCLUSION => <<<SQL
CountQueryMode::Transclusion => <<<SQL
SELECT
COUNT({$prefix}_from),
COUNT({$prefix}_from) - COUNT(indirect_link),
Expand All @@ -113,7 +119,7 @@ private function createQuery($table, $prefix, $mode, $flags) {
) AS temp ON {$prefix}_from = indirect_link
{$this->createDirectCond($prefix, $flags)}
SQL,
self::MODE_LINK => <<<SQL
CountQueryMode::Link => <<<SQL
SELECT
COUNT(DISTINCT COALESCE(direct_link, indirect_link)),
COUNT(direct_link),
Expand All @@ -131,11 +137,11 @@ private function createQuery($table, $prefix, $mode, $flags) {
};
}

public function runQuery($table, $prefix, $mode, $flags = 0) {
public function runQuery(string $table, string $prefix, CountQueryMode $mode, $flags = 0) {
$query = $this->createQuery($table, $prefix, $mode, $flags);
$res = $this->db->query($query)->fetch();

return $mode == self::MODE_REDIRECT ? (int) $res[0] : [
return $mode == CountQueryMode::Redirect ? (int) $res[0] : [
'all' => (int) $res[0],
'direct' => (int) $res[1],
'indirect' => (int) $res[2]
Expand Down
2 changes: 1 addition & 1 deletion includes/JsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class JsLoader {
public $type;
public $files;

public function __construct(...$files) {
public function __construct(string ...$files) {
$this->files = $files;
}

Expand Down
24 changes: 10 additions & 14 deletions includes/LinkCount.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

class LinkCount implements HtmlProducer, JsonProducer {
public const COUNT_MODE_REDIRECT = 'redirect';
public const COUNT_MODE_LINK = 'link';
public const COUNT_MODE_TRANSCLUSION = 'transclusion';

public $counts;
public $error;
public string $error;

private $projectURL;
private $title;
private $countQuery;
private string $projectURL;
private Title $title;
private CountQuery $countQuery;

private $typeInfo = [
'filelinks' => [
Expand All @@ -35,7 +31,7 @@ class LinkCount implements HtmlProducer, JsonProducer {
]
];

public function __construct($page, $project, $namespaces = '') {
public function __construct(string $page, string $project, $namespaces = '') {
if (!$page && !$project && $namespaces === '') {
return;
}
Expand Down Expand Up @@ -77,33 +73,33 @@ public function __construct($page, $project, $namespaces = '') {
? $this->countQuery->runQuery(
'imagelinks',
'il',
CountQuery::MODE_TRANSCLUSION,
CountQueryMode::Transclusion,
CountQuery::SINGLE_NS | CountQuery::NO_LINK_TARGET
)
: null,
'categorylinks' => $this->title->getNamespaceId() === 14
? $this->countQuery->runQuery(
'categorylinks',
'cl',
CountQuery::MODE_LINK,
CountQueryMode::Link,
CountQuery::SINGLE_NS | CountQuery::NO_FROM_NS | CountQuery::NO_LINK_TARGET
)
: null,
'wikilinks' => $this->countQuery->runQuery(
'pagelinks',
'pl',
CountQuery::MODE_LINK,
CountQueryMode::Link,
CountQuery::NO_LINK_TARGET
),
'redirects' => $this->countQuery->runQuery(
'redirect',
'rd',
CountQuery::MODE_REDIRECT,
CountQueryMode::Redirect,
CountQuery::NO_FROM_NS | CountQuery::NO_LINK_TARGET),
'transclusions' => $this->countQuery->runQuery(
'templatelinks',
'tl',
CountQuery::MODE_TRANSCLUSION
CountQueryMode::Transclusion
)
];

Expand Down
2 changes: 1 addition & 1 deletion includes/ProjectPrefixSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ProjectPrefixSearch implements JsonProducer {
public $projects = [];
public $exact;

public function __construct($prefix) {
public function __construct(string $prefix) {
if ($prefix == '') {
$this->projects = [];
return;
Expand Down
20 changes: 10 additions & 10 deletions includes/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
class Title {
public const REDIS_DB_VER = 3;

private $databaseName;
private $projectURL;
private $title;
private $namespaceInfo;
private string $databaseName;
private string $projectURL;
private string $title;
private array $namespaceInfo;

public function __construct($text, $databaseName, $projectURL) {
public function __construct(string $text, string $databaseName, string $projectURL) {
$this->databaseName = $databaseName;
$this->projectURL = $projectURL;

Expand All @@ -32,31 +32,31 @@ public function __construct($text, $databaseName, $projectURL) {
$this->namespaceInfo = $namespaceInfo;
}

public function getNamespaceId() {
public function getNamespaceId(): int {
return $this->namespaceInfo[0];
}

public function getDBKey() {
public function getDBKey(): string {
return strtr($this->title, ' ', '_');
}

public function getFullText() {
public function getFullText(): string {
if ($this->namespaceInfo[1] == '') {
return $this->title;
}

return "{$this->namespaceInfo[1]}:{$this->title}";
}

private function breakupText($text) {
private function breakupText(string $text) {
if (strpos($text, ':') === false) {
return ['', $text];
} else {
return explode(':', $text, 2);
}
}

private function getNamespaceInfo($namespace) {
private function getNamespaceInfo(string $namespace): array|null {
$redis = new Redis;

$redis->connect(Config::get('redis-server'), Config::get('redis-port'));
Expand Down

1 comment on commit 7297291

@chikitai
Copy link

Choose a reason for hiding this comment

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

10
Original file line number Diff line number Diff line change
@@ -3,12 +3,12 @@
class Title {
public const REDIS_DB_VER = 3;

private $databaseName;
private $projectURL;
private $title;
private $namespaceInfo;
private string $databaseName;
private string $projectURL;
private string $title;
private array $namespaceInfo;

public function __construct($text, $databaseName, $projectURL) {
public function __construct(string $text, string $databaseName, string $projectURL) {
	$this->databaseName = $databaseName;
	$this->projectURL = $projectURL;

@@ -32,31 +32,31 @@ public function __construct($text, $databaseName, $projectURL) {
$this->namespaceInfo = $namespaceInfo;
}

public function getNamespaceId() {
public function getNamespaceId(): int {
	return $this->namespaceInfo[0];
}

public function getDBKey() {
public function getDBKey(): string {
	return strtr($this->title, ' ', '_');
}

public function getFullText() {
public function getFullText(): string {
	if ($this->namespaceInfo[1] == '') {
		return $this->title;
	}

	return "{$this->namespaceInfo[1]}:{$this->title}";
}

private function breakupText($text) {
private function breakupText(string $text) {
	if (strpos($text, ':') === false) {
		return ['', $text];
	} else {
		return explode(':', $text, 2);
	}
}

private function getNamespaceInfo($namespace) {
private function getNamespaceInfo(string $namespace): array|null {
	$redis = new Redis;

	$redis->connect(Config::get('redis-server'), Config::get('redis-port'));

Please sign in to comment.