Passed
Pull Request — main (#398)
by MusikAnimal
08:04 queued 04:02
created

LargestPagesRepository::getLikeSql()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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