MediaWiki master
ExternalLinksTable.php
Go to the documentation of this file.
1<?php
2
4
7
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
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
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}
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.