MediaWiki  master
ExternalLinksTable.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Config;
6 use LinkFilter;
8 use ParserOutput;
9 
18  private $newLinks = [];
19  private $existingLinks;
21  private $migrationStage;
22 
23  public function __construct( Config $config ) {
24  $this->migrationStage = $config->get( MainConfigNames::ExternalLinksSchemaMigrationStage );
25  }
26 
27  public function setParserOutput( ParserOutput $parserOutput ) {
28  $this->newLinks = $parserOutput->getExternalLinks();
29  }
30 
31  protected function getTableName() {
32  return 'externallinks';
33  }
34 
35  protected function getFromField() {
36  return 'el_from';
37  }
38 
39  protected function getExistingFields() {
40  return [ 'el_to' ];
41  }
42 
49  private function getExistingLinks() {
50  if ( $this->existingLinks === null ) {
51  $this->existingLinks = [];
52  foreach ( $this->fetchExistingRows() as $row ) {
53  $this->existingLinks[$row->el_to] = true;
54  }
55  }
56  return $this->existingLinks;
57  }
58 
59  protected function getNewLinkIDs() {
60  foreach ( $this->newLinks as $link => $unused ) {
61  yield (string)$link;
62  }
63  }
64 
65  protected function getExistingLinkIDs() {
66  foreach ( $this->getExistingLinks() as $link => $unused ) {
67  yield (string)$link;
68  }
69  }
70 
71  protected function isExisting( $linkId ) {
72  return \array_key_exists( $linkId, $this->getExistingLinks() );
73  }
74 
75  protected function isInNewSet( $linkId ) {
76  return \array_key_exists( $linkId, $this->newLinks );
77  }
78 
79  protected function insertLink( $linkId ) {
80  foreach ( LinkFilter::makeIndexes( $linkId ) as $index ) {
81  $params = [ 'el_to' => $linkId ];
82  if ( $this->migrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
83  $params['el_index'] = implode( '', $index );
84  $params['el_index_60'] = substr( implode( '', $index ), 0, 60 );
85  }
86  if ( $this->migrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
87  $params['el_to_domain_index'] = substr( $index[0], 0, 255 );
88  $params['el_to_path'] = $index[1];
89  }
90  $this->insertRow( $params );
91  }
92  }
93 
94  protected function deleteLink( $linkId ) {
95  $this->deleteRow( [ 'el_to' => $linkId ] );
96  }
97 
104  public function getStringArray( $setType ) {
105  $ids = $this->getLinkIDs( $setType );
106  if ( is_array( $ids ) ) {
107  return $ids;
108  } else {
109  return iterator_to_array( $ids );
110  }
111  }
112 }
const SCHEMA_COMPAT_WRITE_OLD
Definition: Defines.php:265
const SCHEMA_COMPAT_WRITE_NEW
Definition: Defines.php:269
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:174
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
A class containing constants representing the names of configuration variables.
const ExternalLinksSchemaMigrationStage
Name constant for the ExternalLinksSchemaMigrationStage setting, for use with Config::get()
Interface for configuration instances.
Definition: Config.php:30
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".