MediaWiki  master
ExternalLinksTable.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use ParserOutput;
7 
16  private $newLinks = [];
17  private $existingLinks;
18 
19  public function setParserOutput( ParserOutput $parserOutput ) {
20  $links = LinkFilter::getIndexedUrlsNonReversed( array_keys( $parserOutput->getExternalLinks() ) );
21  foreach ( $links as $link ) {
22  $this->newLinks[$link] = true;
23  }
24  }
25 
26  protected function getTableName() {
27  return 'externallinks';
28  }
29 
30  protected function getFromField() {
31  return 'el_from';
32  }
33 
34  protected function getExistingFields() {
35  return [ 'el_to_domain_index', 'el_to_path' ];
36  }
37 
44  private function getExistingLinks() {
45  if ( $this->existingLinks === null ) {
46  $this->existingLinks = [];
47  foreach ( $this->fetchExistingRows() as $row ) {
48  $link = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path;
49  $this->existingLinks[$link] = true;
50  }
51  }
52  return $this->existingLinks;
53  }
54 
55  protected function getNewLinkIDs() {
56  foreach ( $this->newLinks as $link => $unused ) {
57  yield (string)$link;
58  }
59  }
60 
61  protected function getExistingLinkIDs() {
62  foreach ( $this->getExistingLinks() as $link => $unused ) {
63  yield (string)$link;
64  }
65  }
66 
67  protected function isExisting( $linkId ) {
68  return \array_key_exists( $linkId, $this->getExistingLinks() );
69  }
70 
71  protected function isInNewSet( $linkId ) {
72  return \array_key_exists( $linkId, $this->newLinks );
73  }
74 
75  protected function insertLink( $linkId ) {
76  foreach ( LinkFilter::makeIndexes( $linkId ) as $index ) {
77  $params = [
78  'el_to_domain_index' => substr( $index[0], 0, 255 ),
79  'el_to_path' => $index[1],
80  ];
81  $this->insertRow( $params );
82  }
83  }
84 
85  protected function deleteLink( $linkId ) {
86  foreach ( LinkFilter::makeIndexes( $linkId ) as $index ) {
87  $this->deleteRow( [
88  'el_to_domain_index' => substr( $index[0], 0, 255 ),
89  'el_to_path' => $index[1]
90  ] );
91  }
92  }
93 
100  public function getStringArray( $setType ) {
101  $ids = $this->getLinkIDs( $setType );
102  if ( is_array( $ids ) ) {
103  return $ids;
104  } else {
105  return iterator_to_array( $ids );
106  }
107  }
108 }
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:401
fetchExistingRows()
Do a select query to fetch the existing rows.
Definition: LinksTable.php:363
getLinkIDs( $setType)
Get an array or iterator of link IDs of a given type.
Definition: LinksTable.php:515
deleteRow( $conds)
Queue a deletion operation.
Definition: LinksTable.php:416