MediaWiki  master
ExternalLinksTable.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use LinkFilter;
6 use ParserOutput;
7 
16  private $newLinks = [];
17  private $existingLinks;
18 
19  public function setParserOutput( ParserOutput $parserOutput ) {
20  $this->newLinks = $parserOutput->getExternalLinks();
21  }
22 
23  protected function getTableName() {
24  return 'externallinks';
25  }
26 
27  protected function getFromField() {
28  return 'el_from';
29  }
30 
31  protected function getExistingFields() {
32  return [ 'el_to' ];
33  }
34 
41  private function getExistingLinks() {
42  if ( $this->existingLinks === null ) {
43  $this->existingLinks = [];
44  foreach ( $this->fetchExistingRows() as $row ) {
45  $this->existingLinks[$row->el_to] = true;
46  }
47  }
48  return $this->existingLinks;
49  }
50 
51  protected function getNewLinkIDs() {
52  foreach ( $this->newLinks as $link => $unused ) {
53  yield (string)$link;
54  }
55  }
56 
57  protected function getExistingLinkIDs() {
58  foreach ( $this->getExistingLinks() as $link => $unused ) {
59  yield (string)$link;
60  }
61  }
62 
63  protected function isExisting( $linkId ) {
64  return \array_key_exists( $linkId, $this->getExistingLinks() );
65  }
66 
67  protected function isInNewSet( $linkId ) {
68  return \array_key_exists( $linkId, $this->newLinks );
69  }
70 
71  protected function insertLink( $linkId ) {
72  foreach ( LinkFilter::makeIndexes( $linkId ) as $index ) {
73  $this->insertRow( [
74  'el_to' => $linkId,
75  'el_index' => $index,
76  'el_index_60' => substr( $index, 0, 60 ),
77  ] );
78  }
79  }
80 
81  protected function deleteLink( $linkId ) {
82  $this->deleteRow( [ 'el_to' => $linkId ] );
83  }
84 
91  public function getStringArray( $setType ) {
92  $ids = $this->getLinkIDs( $setType );
93  if ( is_array( $ids ) ) {
94  return $ids;
95  } else {
96  return iterator_to_array( $ids );
97  }
98  }
99 }
Some functions to help implement an external link filter for spam control.
Definition: LinkFilter.php:36
static makeIndexes( $url)
Converts a URL into a format for el_index.
Definition: LinkFilter.php:173
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.
Definition: LinksTable.php:41
insertRow( $row)
Queue a row for insertion.
Definition: LinksTable.php:407
fetchExistingRows()
Do a select query to fetch the existing rows.
Definition: LinksTable.php:369
getLinkIDs( $setType)
Get an array or iterator of link IDs of a given type.
Definition: LinksTable.php:522
deleteRow( $conds)
Queue a deletion operation.
Definition: LinksTable.php:422