Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
Pagination | |
0.00% |
0 / 11 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
getPageNumber | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getDisplayedPageNumber | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getNumberOfPages | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getPageTitle | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
pageNumberExists | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
rewind | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
next | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
current | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
valid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
prefetchPageLinks | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Pagination; |
4 | |
5 | use Iterator; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\Title\Title; |
8 | use OutOfBoundsException; |
9 | |
10 | /** |
11 | * @license GPL-2.0-or-later |
12 | * |
13 | * Pagination of a book |
14 | */ |
15 | abstract class Pagination implements Iterator { |
16 | |
17 | /** |
18 | * Have we already prefetched the page list? If so, don't do it again. |
19 | * |
20 | * @var bool |
21 | */ |
22 | private $linksCached = false; |
23 | |
24 | /** |
25 | * @var int position of the iterator |
26 | */ |
27 | private $position = 1; |
28 | |
29 | /** |
30 | * Returns the internal page number |
31 | * |
32 | * @param Title $pageTitle |
33 | * @return int |
34 | * @throws PageNotInPaginationException |
35 | */ |
36 | abstract public function getPageNumber( Title $pageTitle ): int; |
37 | |
38 | /** |
39 | * Returns the page number as it should be displayed from an internal page number |
40 | * |
41 | * @param int $pageNumber |
42 | * @return PageNumber |
43 | * @throws OutOfBoundsException |
44 | */ |
45 | abstract public function getDisplayedPageNumber( int $pageNumber ): PageNumber; |
46 | |
47 | /** |
48 | * Returns the number of pages |
49 | * |
50 | * @return int |
51 | */ |
52 | abstract public function getNumberOfPages(): int; |
53 | |
54 | /** |
55 | * Returns the page number $pageNumber of the book |
56 | * |
57 | * @param int $pageNumber |
58 | * @return Title |
59 | * @throws OutOfBoundsException |
60 | */ |
61 | abstract public function getPageTitle( int $pageNumber ): Title; |
62 | |
63 | /** |
64 | * Returns if a page number $pageNumber exists |
65 | * |
66 | * @param int $pageNumber |
67 | * @return bool |
68 | */ |
69 | abstract protected function pageNumberExists( int $pageNumber ): bool; |
70 | |
71 | /** |
72 | * @inheritDoc |
73 | */ |
74 | public function rewind(): void { |
75 | // pages numbers starts with 1 |
76 | $this->position = 1; |
77 | } |
78 | |
79 | /** |
80 | * @inheritDoc |
81 | */ |
82 | public function key(): int { |
83 | return $this->position; |
84 | } |
85 | |
86 | /** |
87 | * @inheritDoc |
88 | */ |
89 | public function next(): void { |
90 | $this->position++; |
91 | } |
92 | |
93 | /** |
94 | * @inheritDoc |
95 | * |
96 | * @return Title |
97 | */ |
98 | public function current(): Title { |
99 | return $this->getPageTitle( $this->position ); |
100 | } |
101 | |
102 | /** |
103 | * @inheritDoc |
104 | */ |
105 | public function valid(): bool { |
106 | return $this->pageNumberExists( $this->position ); |
107 | } |
108 | |
109 | /** |
110 | * Prefetch links for the index, so the subsequent getArticleID lookups |
111 | * can be cached for faster access |
112 | */ |
113 | public function prefetchPageLinks(): void { |
114 | if ( !$this->linksCached ) { |
115 | $batch = MediaWikiServices::getInstance() |
116 | ->getLinkBatchFactory()->newLinkBatch( $this ); |
117 | $batch->setCaller( __METHOD__ ); |
118 | $batch->execute(); |
119 | $this->linksCached = true; |
120 | } |
121 | } |
122 | } |