9use InvalidArgumentException;
19use MediaWiki\Watchlist\WatchlistSpecialPage;
21use Wikimedia\Codex\Utility\Codex;
31 private const SUBPAGE_EDIT =
'edit';
32 private const PARAM_ID =
'wll_id';
33 private const PARAM_NAME =
'wll_name';
35 use WatchlistSpecialPage;
42 $name =
'WatchlistLabels',
43 $restriction =
'viewmywatchlist',
45 parent::__construct( $name, $restriction,
false );
56 $right = $subPage === self::SUBPAGE_EDIT ?
'editmywatchlist' :
'viewmywatchlist';
57 if ( !$user->isRegistered()
58 || ( $user->isTemp() && !$user->isAllowed( $right ) )
68 $output->setPageTitleMsg( $this->
msg(
'watchlistlabels-title' ) );
71 $output->addHTML( Html::errorBox( $this->
msg(
'watchlistlabels-not-enabled' )->escaped() ) );
74 $output->addSubtitle( $this->getWatchlistOwnerHtml() );
75 if ( $subPage === self::SUBPAGE_EDIT ) {
85 private function showForm() {
86 $id = $this->
getRequest()->getInt( self::PARAM_ID );
90 'name' => self::PARAM_NAME,
91 'label-message' =>
'watchlistlabels-form-field-name',
92 'validation-callback' => [ $this,
'validateName' ],
93 'filter-callback' => [ $this,
'filterName' ],
99 $this->watchlistLabel = $this->labelStore->loadById( $this->
getUser(), $id );
100 if ( $this->watchlistLabel ) {
101 $descriptor[self::PARAM_NAME][
'default'] = $this->watchlistLabel->getName();
102 $descriptor[self::PARAM_ID] = [
104 'name' => self::PARAM_ID,
105 'default' => $this->watchlistLabel->getId(),
110 $form = HTMLForm::factory(
'codex', $descriptor, $this->
getContext() )
114 ->setHeaderHtml(
Html::element(
'h3', [], $this->
msg(
"watchlistlabels-form-header-$msgSuffix" )->text() ) )
120 ->setSubmitTextMsg(
"watchlistlabels-form-submit-$msgSuffix" )
121 ->setSubmitCallback( [ $this,
'onSubmit' ] );
136 return $label->getName();
147 $length = strlen( trim( $value ) );
148 if ( $length === 0 ) {
149 return Status::newFatal( $this->
msg(
'watchlistlabels-form-name-too-short', $length ) );
151 if ( $length > 255 ) {
152 return Status::newFatal( $this->
msg(
'watchlistlabels-form-name-too-long', $length ) );
154 $existingLabel = $this->labelStore->loadByName( $this->
getUser(), $value );
155 $thisId = $alldata[self::PARAM_ID] ??
null;
156 if ( $existingLabel && $thisId && $existingLabel->getId() !== (
int)$thisId ) {
157 return Status::newFatal( $this->
msg(
'watchlistlabels-form-name-exists', $existingLabel->getName() ) );
159 return Status::newGood();
169 if ( !isset( $data[self::PARAM_NAME] ) ) {
170 throw new InvalidArgumentException(
'No name data submitted.' );
172 if ( !$this->watchlistLabel ) {
175 $this->watchlistLabel->setName( $data[self::PARAM_NAME] );
177 $saved = $this->labelStore->save( $this->watchlistLabel );
178 if ( $saved->isOK() ) {
187 private function showTable() {
188 $codex =
new Codex();
189 $this->getOutput()->addModuleStyles(
'mediawiki.special.watchlistlabels' );
192 $this->getOutput()->addHTML(
193 Html::element(
'h3', [], $this->
msg(
'watchlistlabels-table-title' )->text() )
194 .
Html::element(
'p', [], $this->
msg(
'watchlistlabels-table-description' )->text() ),
199 'href' => $this->getPageTitle( self::SUBPAGE_EDIT )->getLinkURL(),
201 'class' =>
'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled'
202 .
' cdx-button--action-progressive cdx-button--weight-primary'
204 $addNew =
Html::element(
'a', $params, $this->
msg(
'watchlistlabels-table-new-link' )->text() );
208 $labels = $this->labelStore->loadAllForUser( $this->
getUser() );
209 $labelCounts = $this->labelStore->countItems( array_keys( $labels ) );
210 $editIcon =
Html::element(
'span', [
'class' =>
'cdx-button__icon mw-specialwatchlistlabels-icon--edit' ] );
211 foreach ( $labels as $label ) {
212 $id = $label->getId();
216 $url = $this->getPageTitle( self::SUBPAGE_EDIT )->getLocalURL( [ self::PARAM_ID => $id ] );
220 'class' =>
'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled'
221 .
' cdx-button--weight-quiet cdx-button--icon-only cdx-button--size-small',
222 'title' => $this->
msg(
'watchlistlabels-table-edit' )->text(),
225 'name' => htmlspecialchars( $label->getName() ),
226 'count' => $this->getLanguage()->formatNum( $labelCounts[ $id ] ),
227 'edit' => Html::rawElement(
'a', $params, $editIcon ),
232 $table = $codex->table()
233 ->setAttributes( [
'class' =>
'mw-specialwatchlistlabels-table' ] )
234 ->setCaption( $this->
msg(
'watchlistlabels-table-header' )->text() )
235 ->setHeaderContent( $addNew )
237 [
'id' =>
'name',
'label' => $this->
msg(
'watchlistlabels-table-col-name' )->escaped() ],
238 [
'id' =>
'count',
'label' => $this->
msg(
'watchlistlabels-table-col-count' )->escaped() ],
239 [
'id' =>
'edit',
'label' => $this->
msg(
'watchlistlabels-table-col-actions' )->escaped() ],
242 ->setPaginate(
false )
244 $this->getOutput()->addHTML( $table->getHtml() );
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const EnableWatchlistLabels
Name constant for the EnableWatchlistLabels setting, for use with Config::get()
Parent class for all special pages.
getUser()
Shortcut to get the User executing this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Generic operation result class Has warning/error list, boolean status and arbitrary value.