summaryrefslogtreecommitdiff
blob: e050e3bd7407792d706c86ba6ca6efa8e8842095 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

use MediaWiki\MediaWikiServices;

class ApiEchoMute extends ApiBase {

	private $centralIdLookup = null;

	private static $muteLists = [
		'user' => [
			'pref' => 'echo-notifications-blacklist',
			'type' => 'user',
		],
		'page-linked-title' => [
			'pref' => 'echo-notifications-page-linked-title-muted-list',
			'type' => 'title'
		],
	];

	public function execute() {
		$user = $this->getUser()->getInstanceForUpdate();
		if ( !$user || $user->isAnon() ) {
			$this->dieWithError(
				[ 'apierror-mustbeloggedin', $this->msg( 'action-editmyoptions' ) ],
				'notloggedin'
			);
		}

		$this->checkUserRightsAny( 'editmyoptions' );

		$params = $this->extractRequestParams();
		$mutelistInfo = self::$muteLists[ $params['type'] ];
		$prefValue = $user->getOption( $mutelistInfo['pref'] );
		$ids = $this->parsePref( $prefValue, $mutelistInfo['type'] );
		$targetsToMute = $params['mute'] ?? [];
		$targetsToUnmute = $params['unmute'] ?? [];

		$changed = false;
		$addIds = $this->lookupIds( $targetsToMute, $mutelistInfo['type'] );
		foreach ( $addIds as $id ) {
			if ( !in_array( $id, $ids ) ) {
				$ids[] = $id;
				$changed = true;
			}
		}
		$removeIds = $this->lookupIds( $targetsToUnmute, $mutelistInfo['type'] );
		foreach ( $removeIds as $id ) {
			$index = array_search( $id, $ids );
			if ( $index !== false ) {
				array_splice( $ids, $index, 1 );
				$changed = true;
			}
		}

		if ( $changed ) {
			$user->setOption( $mutelistInfo['pref'], $this->serializePref( $ids, $mutelistInfo['type'] ) );
			$user->saveSettings();
		}

		$this->getResult()->addValue( null, $this->getModuleName(), 'success' );
	}

	private function getCentralIdLookup() {
		if ( $this->centralIdLookup === null ) {
			$this->centralIdLookup = CentralIdLookup::factory();
		}
		return $this->centralIdLookup;
	}

	private function lookupIds( $names, $type ) {
		if ( $type === 'title' ) {
			$linkBatch = MediaWikiServices::getInstance()->getLinkBatchFactory()->newLinkBatch();
			foreach ( $names as $name ) {
				$linkBatch->addObj( Title::newFromText( $name ) );
			}
			$linkBatch->execute();

			$ids = [];
			foreach ( $names as $name ) {
				$title = Title::newFromText( $name );
				if ( $title instanceof Title && $title->getArticleID() > 0 ) {
					$ids[] = $title->getArticleID();
				}
			}
			return $ids;
		} elseif ( $type === 'user' ) {
			return $this->getCentralIdLookup()->centralIdsFromNames( $names, CentralIdLookup::AUDIENCE_PUBLIC );
		}
	}

	private function parsePref( $prefValue, $type ) {
		return preg_split( '/\n/', $prefValue, -1, PREG_SPLIT_NO_EMPTY );
	}

	private function serializePref( $ids, $type ) {
		return implode( "\n", $ids );
	}

	public function getAllowedParams( $flags = 0 ) {
		return [
			'type' => [
				ApiBase::PARAM_REQUIRED => true,
				ApiBase::PARAM_TYPE => array_keys( self::$muteLists ),
			],
			'mute' => [
				ApiBase::PARAM_ISMULTI => true,
			],
			'unmute' => [
				ApiBase::PARAM_ISMULTI => true,
			]
		];
	}

	public function needsToken() {
		return 'csrf';
	}

	public function getTokenSalt() {
		return '';
	}

	public function mustBePosted() {
		return true;
	}

	public function isWriteMode() {
		return true;
	}

}