Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
27 / 33
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PaginationFactory
81.82% covered (warning)
81.82%
27 / 33
50.00% covered (danger)
50.00%
2 / 4
10.60
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isIndexTitleInCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPaginationForIndexTitle
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 buildPaginationForIndexContent
79.17% covered (warning)
79.17%
19 / 24
0.00% covered (danger)
0.00%
0 / 1
6.33
1<?php
2
3namespace ProofreadPage\Pagination;
4
5use MediaWiki\Title\Title;
6use ProofreadPage\FileNotFoundException;
7use ProofreadPage\FileProvider;
8use ProofreadPage\Index\IndexContent;
9use ProofreadPage\Index\IndexContentLookup;
10
11/**
12 * @license GPL-2.0-or-later
13 */
14class PaginationFactory {
15
16    /** @var FileProvider */
17    private $fileProvider;
18
19    /** @var IndexContentLookup */
20    private $indexContentLookup;
21
22    /** @var int */
23    private $pageNamespaceId;
24
25    /** @var Pagination[] */
26    private $paginations = [];
27
28    /**
29     * @param FileProvider $fileProvider
30     * @param IndexContentLookup $indexContentLookup
31     * @param int $pageNamespaceId
32     */
33    public function __construct(
34        FileProvider $fileProvider,
35        IndexContentLookup $indexContentLookup,
36        int $pageNamespaceId
37    ) {
38        $this->fileProvider = $fileProvider;
39        $this->indexContentLookup = $indexContentLookup;
40        $this->pageNamespaceId = $pageNamespaceId;
41    }
42
43    /**
44     * Check if the given index has a cached pagination
45     * @param Title $indexTitle
46     * @return bool
47     */
48    public function isIndexTitleInCache( Title $indexTitle ): bool {
49        return array_key_exists( $indexTitle->getDBkey(), $this->paginations );
50    }
51
52    /**
53     * @param Title $indexTitle
54     * @return Pagination
55     */
56    public function getPaginationForIndexTitle( Title $indexTitle ) {
57        $key = $indexTitle->getDBkey();
58
59        if ( !array_key_exists( $key, $this->paginations ) ) {
60            $indexContent = $this->indexContentLookup->getIndexContentForTitle( $indexTitle );
61            $this->paginations[$key] = $this->buildPaginationForIndexContent( $indexTitle, $indexContent );
62        }
63
64        return $this->paginations[$key];
65    }
66
67    /**
68     * @param Title $indexTitle
69     * @param IndexContent $indexContent
70     * @return Pagination
71     */
72    public function buildPaginationForIndexContent( Title $indexTitle, IndexContent $indexContent ): Pagination {
73        try {
74            $file = $this->fileProvider->getFileForIndexTitle( $indexTitle );
75        } catch ( FileNotFoundException $e ) {
76            $file = false;
77        }
78
79        // check if it is using pagelist
80        $pagelist = $indexContent->getPagelistTagContent();
81        if ( $pagelist !== null && $file ) {
82            if ( $file->isMultipage() ) {
83                return new FilePagination(
84                    $indexTitle,
85                    $pagelist,
86                    $file->pageCount(),
87                    $this->pageNamespaceId
88                );
89            } else {
90                return new SimpleFilePagination(
91                    $indexTitle,
92                    $pagelist,
93                    $this->pageNamespaceId
94                );
95            }
96        } else {
97            $links = $indexContent->getLinksToNamespace( $this->pageNamespaceId );
98            $pages = [];
99            $pageNumbers = [];
100            foreach ( $links as $link ) {
101                $pages[] = $link->getTarget();
102                $pageNumbers[] = new PageNumber( $link->getLabel() );
103            }
104            return new PagePagination( $pages, $pageNumbers );
105        }
106    }
107}