Passed
Push — main ( ec4ebd...49d96d )
by MusikAnimal
04:21
created

LargestPagesRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace App\Repository;
6
7
use App\Model\Page;
8
use App\Model\Project;
9
use GuzzleHttp\Client;
10
use Psr\Cache\CacheItemPoolInterface;
11
use Psr\Container\ContainerInterface;
12
use Psr\Log\LoggerInterface;
13
14
/**
15
 * A LargestPagesRepository is responsible for retrieving information from the database for the LargestPages tool.
16
 * @codeCoverageIgnore
17
 */
18
class LargestPagesRepository extends Repository
19
{
20
    protected PageRepository $pageRepo;
21
22
    /**
23
     * @param ContainerInterface $container
24
     * @param CacheItemPoolInterface $cache
25
     * @param Client $guzzle
26
     * @param LoggerInterface $logger
27
     * @param bool $isWMF
28
     * @param int $queryTimeout
29
     * @param PageRepository $pageRepo
30
     */
31
    public function __construct(
32
        ContainerInterface $container,
33
        CacheItemPoolInterface $cache,
34
        Client $guzzle,
35
        LoggerInterface $logger,
36
        bool $isWMF,
37
        int $queryTimeout,
38
        PageRepository $pageRepo
39
    ) {
40
        $this->pageRepo = $pageRepo;
41
        parent::__construct($container, $cache, $guzzle, $logger, $isWMF, $queryTimeout);
42
    }
43
44
    /** @var int Max rows to display. */
45
    public const MAX_ROWS = 500;
46
47
    private function getLikeSql(string &$includePattern, string &$excludePattern): string
48
    {
49
        $sql = '';
50
51
        if ($includePattern) {
52
            $sql .= "page_title LIKE :include_pattern ";
53
            $includePattern = str_replace(' ', '_', $includePattern);
54
        }
55
        if ($excludePattern) {
56
            if ($includePattern) {
57
                $sql .= ' AND ';
58
            }
59
            $sql .= "page_title NOT LIKE :exclude_pattern ";
60
            $excludePattern = str_replace(' ', '_', $excludePattern);
61
        }
62
63
        return $sql;
64
    }
65
66
    /**
67
     * Fetches the largest pages for the given project.
68
     * @param Project $project
69
     * @param int|string $namespace Namespace ID or 'all' for all namespaces.
70
     * @param string $includePattern Either regular expression (starts/ends with forward slash),
71
     *   or a wildcard pattern with % as the wildcard symbol.
72
     * @param string $excludePattern Either regular expression (starts/ends with forward slash),
73
     *   or a wildcard pattern with % as the wildcard symbol.
74
     * @return array Results with keys 'page_namespace', 'page_title', 'page_len' and 'pa_class'
75
     */
76
    public function getData(Project $project, $namespace, string $includePattern, string $excludePattern): array
77
    {
78
        $pageTable = $project->getTableName('page');
79
80
        $where = '';
81
        $likeCond = $this->getLikeSql($includePattern, $excludePattern);
82
        $namespaceCond = '';
83
        if ('all' !== $namespace) {
84
            $namespaceCond = 'page_namespace = :namespace';
85
            if ($likeCond) {
86
                $namespaceCond .= ' AND ';
87
            }
88
        }
89
        if ($likeCond || $namespaceCond) {
90
            $where = 'WHERE ';
91
        }
92
93
        $sql = "SELECT page_namespace, page_title, page_len
94
                FROM $pageTable
95
                $where $namespaceCond
96
                $likeCond
97
                ORDER BY page_len DESC
98
                LIMIT ".self::MAX_ROWS;
99
100
        $rows = $this->executeProjectsQuery($project, $sql, [
101
            'namespace' => $namespace,
102
            'include_pattern' => $includePattern,
103
            'exclude_pattern' => $excludePattern,
104
        ])->fetchAllAssociative();
105
106
        $pages = [];
107
108
        foreach ($rows as $row) {
109
            $pages[] = Page::newFromRow($this->pageRepo, $project, $row);
110
        }
111
112
        return $pages;
113
    }
114
}
115