File: InterwikiLinksTable.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (97 lines) | stat: -rw-r--r-- 2,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

namespace MediaWiki\Deferred\LinksUpdate;

use MediaWiki\Parser\ParserOutput;

/**
 * iwlinks
 *
 * Link ID format: string[]
 *    - 0: Interwiki prefix
 *    - 1: Foreign title
 *
 * @since 1.38
 */
class InterwikiLinksTable extends LinksTable {
	/** @var array */
	private $newLinks = [];

	/** @var array|null */
	private $existingLinks;

	public function setParserOutput( ParserOutput $parserOutput ) {
		$this->newLinks = $parserOutput->getInterwikiLinks();
	}

	protected function getTableName() {
		return 'iwlinks';
	}

	protected function getFromField() {
		return 'iwl_from';
	}

	protected function getExistingFields() {
		return [ 'iwl_prefix', 'iwl_title' ];
	}

	protected function getNewLinkIDs() {
		foreach ( $this->newLinks as $prefix => $links ) {
			foreach ( $links as $title => $unused ) {
				yield [ (string)$prefix, (string)$title ];
			}
		}
	}

	/**
	 * Get the existing links as a 2-d array, with the prefix in the first key,
	 * the title in the second key, and the value arbitrary.
	 *
	 * @return array|null
	 */
	private function getExistingLinks() {
		if ( $this->existingLinks === null ) {
			$this->existingLinks = [];
			foreach ( $this->fetchExistingRows() as $row ) {
				$this->existingLinks[$row->iwl_prefix][$row->iwl_title] = true;
			}
		}
		return $this->existingLinks;
	}

	protected function getExistingLinkIDs() {
		foreach ( $this->getExistingLinks() as $prefix => $links ) {
			foreach ( $links as $title => $unused ) {
				yield [ (string)$prefix, (string)$title ];
			}
		}
	}

	protected function isExisting( $linkId ) {
		$links = $this->getExistingLinks();
		[ $prefix, $title ] = $linkId;
		return isset( $links[$prefix][$title] );
	}

	protected function isInNewSet( $linkId ) {
		[ $prefix, $title ] = $linkId;
		return isset( $this->newLinks[$prefix][$title] );
	}

	protected function insertLink( $linkId ) {
		[ $prefix, $title ] = $linkId;
		$this->insertRow( [
			'iwl_prefix' => $prefix,
			'iwl_title' => $title
		] );
	}

	protected function deleteLink( $linkId ) {
		[ $prefix, $title ] = $linkId;
		$this->deleteRow( [
			'iwl_prefix' => $prefix,
			'iwl_title' => $title
		] );
	}
}