MediaWiki  master
ExternalLinksTable.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Config;
9 use ParserOutput;
10 
19  private const CONSTRUCTOR_OPTIONS = [
21  ];
22 
23  private $newLinks = [];
24  private $existingLinks;
26  private $migrationStage;
27 
28  public function __construct( Config $config ) {
29  $options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
30  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
31 
32  $this->migrationStage = $options->get( MainConfigNames::ExternalLinksSchemaMigrationStage );
33  }
34 
35  public function setParserOutput( ParserOutput $parserOutput ) {
36  $this->newLinks = $parserOutput->getExternalLinks();
37  }
38 
39  protected function getTableName() {
40  return 'externallinks';
41  }
42 
43  protected function getFromField() {
44  return 'el_from';
45  }
46 
47  protected function getExistingFields() {
48  return [ 'el_to' ];
49  }
50 
57  private function getExistingLinks() {
58  if ( $this->existingLinks === null ) {
59  $this->existingLinks = [];
60  foreach ( $this->fetchExistingRows() as $row ) {
61  $this->existingLinks[$row->el_to] = true;
62  }
63  }
64  return $this->existingLinks;
65  }
66 
67  protected function getNewLinkIDs() {
68  foreach ( $this->newLinks as $link => $unused ) {
69  yield (string)$link;
70  }
71  }
72 
73  protected function getExistingLinkIDs() {
74  foreach ( $this->getExistingLinks() as $link => $unused ) {
75  yield (string)$link;
76  }
77  }
78 
79  protected function isExisting( $linkId ) {
80  return \array_key_exists( $linkId, $this->getExistingLinks() );
81  }
82 
83  protected function isInNewSet( $linkId ) {
84  return \array_key_exists( $linkId, $this->newLinks );
85  }
86 
87  protected function insertLink( $linkId ) {
88  foreach ( LinkFilter::makeIndexes( $linkId ) as $index ) {
89  $params = [ 'el_to' => $linkId ];
90  if ( $this->migrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
91  $params['el_index'] = implode( '', $index );
92  $params['el_index_60'] = substr( implode( '', $index ), 0, 60 );
93  }
94  if ( $this->migrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
95  $params['el_to_domain_index'] = substr( $index[0], 0, 255 );
96  $params['el_to_path'] = $index[1];
97  }
98  $this->insertRow( $params );
99  }
100  }
101 
102  protected function deleteLink( $linkId ) {
103  $this->deleteRow( [ 'el_to' => $linkId ] );
104  }
105 
112  public function getStringArray( $setType ) {
113  $ids = $this->getLinkIDs( $setType );
114  if ( is_array( $ids ) ) {
115  return $ids;
116  } else {
117  return iterator_to_array( $ids );
118  }
119  }
120 }
const SCHEMA_COMPAT_WRITE_OLD
Definition: Defines.php:265
const SCHEMA_COMPAT_WRITE_NEW
Definition: Defines.php:269
A class for passing options to services.
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:517
deleteRow( $conds)
Queue a deletion operation.
Definition: LinksTable.php:416
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