2016-03-09 20:13:48 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 21:15:29 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Special;
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\NotifUser;
|
2024-06-12 18:31:47 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLForm;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\SpecialPage\FormSpecialPage;
|
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2022-11-02 21:15:29 +00:00
|
|
|
|
2016-05-28 00:02:55 +00:00
|
|
|
/**
|
|
|
|
* Form for marking notifications as read by ID.
|
|
|
|
*
|
|
|
|
* This uses the normal HTMLForm handling when receiving POSTs.
|
|
|
|
* However, for a better user no-JS user experience, we integrate
|
|
|
|
* a version of the form into Special:Notifications. Thus, this
|
|
|
|
* page should normally not need to be visited directly.
|
|
|
|
*/
|
2016-03-09 20:13:48 +00:00
|
|
|
class SpecialNotificationsMarkRead extends FormSpecialPage {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct( 'NotificationsMarkRead' );
|
|
|
|
}
|
|
|
|
|
2016-05-25 18:17:07 +00:00
|
|
|
public function doesWrites() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:13:48 +00:00
|
|
|
public function execute( $par ) {
|
|
|
|
parent::execute( $par );
|
|
|
|
|
|
|
|
$out = $this->getOutput();
|
2023-08-10 14:02:39 +00:00
|
|
|
$out->setPageTitleMsg( $this->msg( 'echo-specialpage-markasread' ) );
|
2016-03-09 20:13:48 +00:00
|
|
|
|
|
|
|
// Redirect to login page and inform user of the need to login
|
|
|
|
$this->requireLogin( 'echo-notification-loginrequired' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isListed() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-28 00:02:55 +00:00
|
|
|
public function getDisplayFormat() {
|
|
|
|
return 'ooui';
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:13:48 +00:00
|
|
|
/**
|
|
|
|
* Get an HTMLForm descriptor array
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return array[]
|
2016-03-09 20:13:48 +00:00
|
|
|
*/
|
|
|
|
protected function getFormFields() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
'id' => [
|
2016-03-09 20:13:48 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'required' => true,
|
|
|
|
'default' => $this->par,
|
2021-05-04 16:06:42 +00:00
|
|
|
'filter-callback' => static function ( $value, $alldata ) {
|
2016-03-09 20:13:48 +00:00
|
|
|
// Allow for a single value or a set of values
|
2024-10-23 15:15:39 +00:00
|
|
|
return explode( ',', $value ?? '' );
|
2016-03-09 20:13:48 +00:00
|
|
|
},
|
|
|
|
'validation-callback' => function ( $value, $alldata ) {
|
2017-08-12 14:00:35 +00:00
|
|
|
if ( $value === [ 'ALL' ] ) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 20:13:48 +00:00
|
|
|
if ( (int)$value <= 0 ) {
|
|
|
|
return $this->msg( 'echo-specialpage-markasread-invalid-id' );
|
|
|
|
}
|
|
|
|
foreach ( $value as $val ) {
|
|
|
|
if ( (int)( $val ) <= 0 ) {
|
|
|
|
return $this->msg( 'echo-specialpage-markasread-invalid-id' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
];
|
2016-03-09 20:13:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 00:02:55 +00:00
|
|
|
/**
|
|
|
|
* Gets a pre-filled version of the form; this should not have a legend or anything
|
|
|
|
* visible, except the button.
|
|
|
|
*
|
|
|
|
* @param int|array $idValue ID or array of IDs
|
|
|
|
* @param string $submitButtonValue Value attribute for button
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param bool $framed Whether the button should be framed
|
|
|
|
* @param string $submitLabelHtml Raw HTML to use for button label
|
2016-05-28 00:02:55 +00:00
|
|
|
*
|
|
|
|
* @return HTMLForm
|
|
|
|
*/
|
|
|
|
public function getMinimalForm( $idValue, $submitButtonValue, $framed, $submitLabelHtml ) {
|
|
|
|
if ( !is_array( $idValue ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$idValue = [ $idValue ];
|
2016-05-28 00:02:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 09:51:55 +00:00
|
|
|
$idString = implode( ',', $idValue );
|
2016-05-28 00:02:55 +00:00
|
|
|
|
|
|
|
$this->setParameter( $idString );
|
|
|
|
|
|
|
|
$form = HTMLForm::factory(
|
|
|
|
$this->getDisplayFormat(),
|
|
|
|
$this->getFormFields(),
|
|
|
|
$this->getContext(),
|
|
|
|
$this->getMessagePrefix()
|
|
|
|
);
|
|
|
|
|
|
|
|
// HTMLForm assumes that the main submit button is always 'primary',
|
|
|
|
// which means it is colored. Since this form is being embedded multiple
|
|
|
|
// places on the page, it has to be neutral, so we make the button
|
|
|
|
// manually.
|
|
|
|
$form->suppressDefaultSubmit();
|
2022-03-27 13:02:30 +00:00
|
|
|
$form->setTitle( $this->getPageTitle() );
|
2016-05-28 00:02:55 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$form->addButton( [
|
2016-05-28 00:02:55 +00:00
|
|
|
'name' => 'submit',
|
|
|
|
'value' => $submitButtonValue,
|
|
|
|
'label-raw' => $submitLabelHtml,
|
|
|
|
'framed' => $framed,
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
2016-05-28 00:02:55 +00:00
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a custom label
|
|
|
|
*
|
|
|
|
* This is only called when the form is actually visited directly, which is not the
|
|
|
|
* main intended use.
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param HTMLForm $form
|
2016-05-28 00:02:55 +00:00
|
|
|
*/
|
2016-03-09 20:13:48 +00:00
|
|
|
protected function alterForm( HTMLForm $form ) {
|
2016-05-28 00:02:55 +00:00
|
|
|
$form->setSubmitText( $this->msg( 'echo-notification-markasread' )->text() );
|
2016-03-09 20:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the form on POST submission.
|
|
|
|
* @param array $data
|
2018-08-13 07:33:57 +00:00
|
|
|
* @return bool
|
2016-03-09 20:13:48 +00:00
|
|
|
*/
|
2018-04-04 15:11:39 +00:00
|
|
|
public function onSubmit( array $data ) {
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( $this->getUser() );
|
2017-08-12 14:00:35 +00:00
|
|
|
|
|
|
|
// Allow for all IDs
|
|
|
|
if ( $data['id'] === [ 'ALL' ] ) {
|
|
|
|
return $notifUser->markAllRead();
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:13:48 +00:00
|
|
|
// Allow for multiple IDs or a single ID
|
|
|
|
$ids = $data['id'];
|
|
|
|
return $notifUser->markRead( $ids );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSuccess() {
|
|
|
|
$page = SpecialPage::getTitleFor( 'Notifications' );
|
2019-06-11 21:43:05 +00:00
|
|
|
$this->getOutput()->redirect( $page->getFullURL() );
|
2016-03-09 20:13:48 +00:00
|
|
|
}
|
|
|
|
}
|