Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4135315
ApiEchoMute.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
ApiEchoMute.php
View Options
<?php
namespace
MediaWiki\Extension\Notifications\Api
;
use
MediaWiki\Api\ApiBase
;
use
MediaWiki\Api\ApiMain
;
use
MediaWiki\Cache\LinkBatchFactory
;
use
MediaWiki\Title\Title
;
use
MediaWiki\User\CentralId\CentralIdLookup
;
use
MediaWiki\User\Options\UserOptionsManager
;
use
Wikimedia\ParamValidator\ParamValidator
;
class
ApiEchoMute
extends
ApiBase
{
/** @var CentralIdLookup */
private
$centralIdLookup
;
/** @var LinkBatchFactory */
private
$linkBatchFactory
;
/** @var UserOptionsManager */
private
$userOptionsManager
;
/** @var string[][] */
private
const
MUTE_LISTS
=
[
'user'
=>
[
'pref'
=>
'echo-notifications-blacklist'
,
'type'
=>
'user'
,
],
'page-linked-title'
=>
[
'pref'
=>
'echo-notifications-page-linked-title-muted-list'
,
'type'
=>
'title'
],
];
/**
* @param ApiMain $main
* @param string $action
* @param CentralIdLookup $centralIdLookup
* @param LinkBatchFactory $linkBatchFactory
* @param UserOptionsManager $userOptionsManager
*/
public
function
__construct
(
ApiMain
$main
,
$action
,
CentralIdLookup
$centralIdLookup
,
LinkBatchFactory
$linkBatchFactory
,
UserOptionsManager
$userOptionsManager
)
{
parent
::
__construct
(
$main
,
$action
);
$this
->
centralIdLookup
=
$centralIdLookup
;
$this
->
linkBatchFactory
=
$linkBatchFactory
;
$this
->
userOptionsManager
=
$userOptionsManager
;
}
public
function
execute
()
{
$user
=
$this
->
getUser
();
if
(
!
$user
||
!
$user
->
isRegistered
()
)
{
$this
->
dieWithError
(
[
'apierror-mustbeloggedin'
,
$this
->
msg
(
'action-editmyoptions'
)
],
'notloggedin'
);
}
$this
->
checkUserRightsAny
(
'editmyoptions'
);
$params
=
$this
->
extractRequestParams
();
$mutelistInfo
=
self
::
MUTE_LISTS
[
$params
[
'type'
]
];
$prefValue
=
$this
->
userOptionsManager
->
getOption
(
$user
,
$mutelistInfo
[
'pref'
]
);
$ids
=
$this
->
parsePref
(
$prefValue
);
$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
)
{
$this
->
userOptionsManager
->
setOption
(
$user
,
$mutelistInfo
[
'pref'
],
$this
->
serializePref
(
$ids
)
);
$this
->
userOptionsManager
->
saveOptions
(
$user
);
}
$this
->
getResult
()->
addValue
(
null
,
$this
->
getModuleName
(),
'success'
);
}
private
function
lookupIds
(
$names
,
$type
)
{
if
(
$type
===
'title'
)
{
$linkBatch
=
$this
->
linkBatchFactory
->
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
->
centralIdLookup
->
centralIdsFromNames
(
$names
,
CentralIdLookup
::
AUDIENCE_PUBLIC
);
}
}
private
function
parsePref
(
$prefValue
)
{
return
preg_split
(
'/
\n
/'
,
$prefValue
,
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
private
function
serializePref
(
$ids
)
{
return
implode
(
"
\n
"
,
$ids
);
}
public
function
getAllowedParams
(
$flags
=
0
)
{
return
[
'type'
=>
[
ParamValidator
::
PARAM_REQUIRED
=>
true
,
ParamValidator
::
PARAM_TYPE
=>
array_keys
(
self
::
MUTE_LISTS
),
],
'mute'
=>
[
ParamValidator
::
PARAM_ISMULTI
=>
true
,
],
'unmute'
=>
[
ParamValidator
::
PARAM_ISMULTI
=>
true
,
]
];
}
public
function
needsToken
()
{
return
'csrf'
;
}
public
function
mustBePosted
()
{
return
true
;
}
public
function
isWriteMode
()
{
return
true
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Aug 19, 13:05 (3 w, 3 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
f4/bb/42f21115695b060440e0bd24e2af
Default Alt Text
ApiEchoMute.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment