Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
43 / 43 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
ExternalLinksTable | |
100.00% |
43 / 43 |
|
100.00% |
12 / 12 |
22 | |
100.00% |
1 / 1 |
setParserOutput | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
getTableName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFromField | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExistingFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExistingLinks | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getNewLinkIDs | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
getExistingLinkIDs | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
isExisting | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isInNewSet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
insertLink | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
deleteLink | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
getStringArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Deferred\LinksUpdate; |
4 | |
5 | use MediaWiki\ExternalLinks\LinkFilter; |
6 | use MediaWiki\Parser\ParserOutput; |
7 | |
8 | /** |
9 | * externallinks |
10 | * |
11 | * Link ID format: string URL |
12 | * |
13 | * @since 1.38 |
14 | */ |
15 | class ExternalLinksTable extends LinksTable { |
16 | private $newLinks = []; |
17 | private $existingLinks; |
18 | |
19 | public function setParserOutput( ParserOutput $parserOutput ) { |
20 | foreach ( $parserOutput->getExternalLinks() as $url => $unused ) { |
21 | foreach ( LinkFilter::makeIndexes( $url ) as [ $domainIndex, $path ] ) { |
22 | $this->newLinks[$domainIndex][$path] = true; |
23 | } |
24 | } |
25 | } |
26 | |
27 | protected function getTableName() { |
28 | return 'externallinks'; |
29 | } |
30 | |
31 | protected function getFromField() { |
32 | return 'el_from'; |
33 | } |
34 | |
35 | protected function getExistingFields() { |
36 | return [ 'el_to_domain_index', 'el_to_path' ]; |
37 | } |
38 | |
39 | /** |
40 | * Get the existing links as an array |
41 | * |
42 | * @return array |
43 | */ |
44 | private function getExistingLinks() { |
45 | if ( $this->existingLinks === null ) { |
46 | $this->existingLinks = []; |
47 | foreach ( $this->fetchExistingRows() as $row ) { |
48 | $this->existingLinks[$row->el_to_domain_index][$row->el_to_path] = true; |
49 | } |
50 | } |
51 | return $this->existingLinks; |
52 | } |
53 | |
54 | protected function getNewLinkIDs() { |
55 | foreach ( $this->newLinks as $domainIndex => $paths ) { |
56 | foreach ( $paths as $path => $unused ) { |
57 | yield [ (string)$domainIndex, (string)$path ]; |
58 | } |
59 | } |
60 | } |
61 | |
62 | protected function getExistingLinkIDs() { |
63 | foreach ( $this->getExistingLinks() as $domainIndex => $paths ) { |
64 | foreach ( $paths as $path => $unused ) { |
65 | yield [ (string)$domainIndex, (string)$path ]; |
66 | } |
67 | } |
68 | } |
69 | |
70 | protected function isExisting( $linkId ) { |
71 | [ $domainIndex, $path ] = $linkId; |
72 | return isset( $this->getExistingLinks()[$domainIndex][$path] ); |
73 | } |
74 | |
75 | protected function isInNewSet( $linkId ) { |
76 | [ $domainIndex, $path ] = $linkId; |
77 | return isset( $this->newLinks[$domainIndex][$path] ); |
78 | } |
79 | |
80 | protected function insertLink( $linkId ) { |
81 | [ $domainIndex, $path ] = $linkId; |
82 | $params = [ |
83 | 'el_to_domain_index' => substr( $domainIndex, 0, 255 ), |
84 | 'el_to_path' => $path, |
85 | ]; |
86 | $this->insertRow( $params ); |
87 | } |
88 | |
89 | protected function deleteLink( $linkId ) { |
90 | [ $domainIndex, $path ] = $linkId; |
91 | $this->deleteRow( [ |
92 | 'el_to_domain_index' => substr( $domainIndex, 0, 255 ), |
93 | 'el_to_path' => $path |
94 | ] ); |
95 | if ( $path === '' ) { |
96 | // el_to_path is nullable, but null is not valid in php arrays, |
97 | // so both values are handled as one key, delete both rows when exists |
98 | $this->deleteRow( [ |
99 | 'el_to_domain_index' => substr( $domainIndex, 0, 255 ), |
100 | 'el_to_path' => null |
101 | ] ); |
102 | } |
103 | } |
104 | |
105 | /** |
106 | * Get an array of URLs of the given type |
107 | * |
108 | * @param int $setType One of the link set constants as in LinksTable::getLinkIDs() |
109 | * @return string[] |
110 | */ |
111 | public function getStringArray( $setType ) { |
112 | $ids = $this->getLinkIDs( $setType ); |
113 | $stringArray = []; |
114 | foreach ( $ids as $linkId ) { |
115 | [ $domainIndex, $path ] = $linkId; |
116 | $stringArray[] = LinkFilter::reverseIndexes( $domainIndex ) . $path; |
117 | } |
118 | return $stringArray; |
119 | } |
120 | } |