MediaWiki master
ExternalLinksTable.php
Go to the documentation of this file.
1<?php
2
4
7
17 private $newLinks = [];
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
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
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}
array $params
The job parameters.
getFromField()
Get the name of the field which links to page_id.
isInNewSet( $linkId)
Determine whether a link (from the existing set) is in the new set.
getExistingLinkIDs()
Get an array (or iterator) of link IDs for the existing state.
getNewLinkIDs()
Get an array (or iterator) of link IDs for the new state.
getExistingFields()
Get the fields to be used in fetchExistingRows().
deleteLink( $linkId)
Delete a link identified by ID.
getStringArray( $setType)
Get an array of URLs of the given type.
setParserOutput(ParserOutput $parserOutput)
Subclasses should implement this to extract the data they need from the ParserOutput.
insertLink( $linkId)
Insert a link identified by ID.
isExisting( $linkId)
Determine whether a link (from the new set) is in the existing set.
The base class for classes which update a single link table.
insertRow( $row)
Queue a row for insertion.
fetchExistingRows()
Do a select query to fetch the existing rows.
getLinkIDs( $setType)
Get an array or iterator of link IDs of a given type.
deleteRow( $conds)
Queue a deletion operation.
ParserOutput is a rendering of a Content object or a message.