Debian Codegraph @

includes/deferred/LinksUpdate/ExternalLinksTable.php

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