Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
Lookup | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isDisambiguationPage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
filterDisambiguationPageIds | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Disambiguator; |
4 | |
5 | use MediaWiki\Title\Title; |
6 | use Wikimedia\Rdbms\IConnectionProvider; |
7 | |
8 | class Lookup { |
9 | |
10 | private IConnectionProvider $dbProvider; |
11 | |
12 | /** |
13 | * @param IConnectionProvider $dbProvider |
14 | */ |
15 | public function __construct( IConnectionProvider $dbProvider ) { |
16 | $this->dbProvider = $dbProvider; |
17 | } |
18 | |
19 | /** |
20 | * Convenience function for testing whether or not a page is a disambiguation page |
21 | * |
22 | * @param Title $title object of a page |
23 | * @param bool $includeRedirects Whether to consider redirects to disambiguations as |
24 | * disambiguations. |
25 | * @return bool |
26 | */ |
27 | public function isDisambiguationPage( Title $title, $includeRedirects = true ) { |
28 | $res = $this->filterDisambiguationPageIds( |
29 | [ $title->getArticleID() ], $includeRedirects ); |
30 | return (bool)count( $res ); |
31 | } |
32 | |
33 | /** |
34 | * Convenience function for testing whether or not pages are disambiguation pages |
35 | * |
36 | * @param int[] $pageIds |
37 | * @param bool $includeRedirects Whether to consider redirects to disambiguations as |
38 | * disambiguations. |
39 | * @return int[] The page ids corresponding to pages that are disambiguations |
40 | */ |
41 | public function filterDisambiguationPageIds( |
42 | array $pageIds, $includeRedirects = true |
43 | ) { |
44 | // Don't needlessly check non-existent and special pages |
45 | $pageIds = array_filter( |
46 | $pageIds, |
47 | static function ( $id ) { |
48 | return $id > 0; |
49 | } |
50 | ); |
51 | |
52 | $output = []; |
53 | if ( $pageIds ) { |
54 | $dbr = $this->dbProvider->getReplicaDatabase(); |
55 | |
56 | $redirects = []; |
57 | $redirectsMap = []; |
58 | if ( $includeRedirects ) { |
59 | // resolve redirects |
60 | $res = $dbr->newSelectQueryBuilder() |
61 | ->select( [ 'page_id', 'rd_from' ] ) |
62 | ->from( 'page' ) |
63 | ->join( 'redirect', null, [ |
64 | 'rd_namespace=page_namespace', |
65 | 'rd_title=page_title', |
66 | 'rd_interwiki' => '', |
67 | ] ) |
68 | ->where( [ 'rd_from' => $pageIds ] ) |
69 | ->caller( __METHOD__ ) |
70 | ->fetchResultSet(); |
71 | |
72 | foreach ( $res as $row ) { |
73 | $redirects[] = $row->rd_from; |
74 | // Key is the destination page ID, values are the source page IDs |
75 | $redirectsMap[$row->page_id][] = $row->rd_from; |
76 | } |
77 | } |
78 | $pageIdsWithRedirects = array_merge( array_keys( $redirectsMap ), |
79 | array_diff( $pageIds, $redirects ) ); |
80 | $res = $dbr->newSelectQueryBuilder() |
81 | ->select( 'pp_page' ) |
82 | ->from( 'page_props' ) |
83 | ->where( [ 'pp_page' => $pageIdsWithRedirects, 'pp_propname' => 'disambiguation' ] ) |
84 | ->caller( __METHOD__ ) |
85 | ->fetchResultSet(); |
86 | |
87 | foreach ( $res as $row ) { |
88 | $disambiguationPageId = $row->pp_page; |
89 | if ( array_key_exists( $disambiguationPageId, $redirectsMap ) ) { |
90 | $output = array_merge( $output, $redirectsMap[$disambiguationPageId] ); |
91 | } |
92 | if ( in_array( $disambiguationPageId, $pageIds ) ) { |
93 | $output[] = $disambiguationPageId; |
94 | } |
95 | } |
96 | } |
97 | |
98 | return $output; |
99 | } |
100 | } |