MediaWiki master
ChangeTags.php
Go to the documentation of this file.
1<?php
40
62 public const TAG_CONTENT_MODEL_CHANGE = 'mw-contentmodelchange';
67 public const TAG_NEW_REDIRECT = 'mw-new-redirect';
71 public const TAG_REMOVED_REDIRECT = 'mw-removed-redirect';
75 public const TAG_CHANGED_REDIRECT_TARGET = 'mw-changed-redirect-target';
79 public const TAG_BLANK = 'mw-blank';
83 public const TAG_REPLACE = 'mw-replace';
91 public const TAG_ROLLBACK = 'mw-rollback';
98 public const TAG_UNDO = 'mw-undo';
104 public const TAG_MANUAL_REVERT = 'mw-manual-revert';
112 public const TAG_REVERTED = 'mw-reverted';
116 public const TAG_SERVER_SIDE_UPLOAD = 'mw-server-side-upload';
117
122
126 public const BYPASS_MAX_USAGE_CHECK = 1;
127
133 private const MAX_DELETE_USES = 5000;
134
138 private const CHANGE_TAG = 'change_tag';
139
140 public const DISPLAY_TABLE_ALIAS = 'changetagdisplay';
141
150 public static function getSoftwareTags( $all = false ) {
151 return MediaWikiServices::getInstance()->getChangeTagsStore()->getSoftwareTags( $all );
152 }
153
167 public static function formatSummaryRow( $tags, $unused, MessageLocalizer $localizer = null ) {
168 if ( $tags === '' || $tags === null ) {
169 return [ '', [] ];
170 }
171 if ( !$localizer ) {
172 $localizer = RequestContext::getMain();
173 }
174
175 $classes = [];
176
177 $tags = explode( ',', $tags );
178 $order = array_flip( MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags() );
179 usort( $tags, static function ( $a, $b ) use ( $order ) {
180 return ( $order[ $a ] ?? INF ) <=> ( $order[ $b ] ?? INF );
181 } );
182
183 $displayTags = [];
184 foreach ( $tags as $tag ) {
185 if ( $tag === '' ) {
186 continue;
187 }
188 $classes[] = Sanitizer::escapeClass( "mw-tag-$tag" );
189 $description = self::tagDescription( $tag, $localizer );
190 if ( $description === false ) {
191 continue;
192 }
193 $displayTags[] = Html::rawElement(
194 'span',
195 [ 'class' => 'mw-tag-marker ' .
196 Sanitizer::escapeClass( "mw-tag-marker-$tag" ) ],
197 $description
198 );
199 }
200
201 if ( !$displayTags ) {
202 return [ '', $classes ];
203 }
204
205 $markers = $localizer->msg( 'tag-list-wrapper' )
206 ->numParams( count( $displayTags ) )
207 ->rawParams( implode( ' ', $displayTags ) )
208 ->parse();
209 $markers = Html::rawElement( 'span', [ 'class' => 'mw-tag-markers' ], $markers );
210
211 return [ $markers, $classes ];
212 }
213
227 public static function tagShortDescriptionMessage( $tag, MessageLocalizer $context ) {
228 $msg = $context->msg( "tag-$tag" );
229 if ( !$msg->exists() ) {
230 // No such message
231 // Pass through ->msg(), even though it seems redundant, to avoid requesting
232 // the user's language from session-less entry points (T227233)
233 return $context->msg( new RawMessage( '$1', [ Message::plaintextParam( $tag ) ] ) );
234 }
235 if ( $msg->isDisabled() ) {
236 // The message exists but is disabled, hide the tag.
237 return false;
238 }
239
240 // Message exists and isn't disabled, use it.
241 return $msg;
242 }
243
256 public static function tagHelpLink( $tag, MessageLocalizer $context ) {
257 $msg = $context->msg( "tag-$tag-helppage" )->inContentLanguage();
258 if ( $msg->exists() && !$msg->isDisabled() ) {
259 $url = Skin::makeInternalOrExternalUrl( $msg->text() );
260 if ( $url ) {
261 return $url;
262 }
263 }
264 return null;
265 }
266
278 public static function tagDescription( $tag, MessageLocalizer $context ) {
279 $msg = self::tagShortDescriptionMessage( $tag, $context );
280 $link = self::tagHelpLink( $tag, $context );
281 if ( $msg && $link ) {
282 $label = $msg->parse();
283 // Avoid invalid HTML caused by link wrapping if the label already contains a link
284 if ( !str_contains( $label, '<a ' ) ) {
285 return Html::rawElement( 'a', [ 'href' => $link ], $label );
286 }
287 }
288 return $msg ? $msg->parse() : false;
289 }
290
303 public static function tagLongDescriptionMessage( $tag, MessageLocalizer $context ) {
304 $msg = $context->msg( "tag-$tag-description" );
305 if ( !$msg->exists() ) {
306 return false;
307 }
308 if ( $msg->isDisabled() ) {
309 // The message exists but is disabled, hide the description.
310 return false;
311 }
312
313 // Message exists and isn't disabled, use it.
314 return $msg;
315 }
316
331 public static function addTags( $tags, $rc_id = null, $rev_id = null,
332 $log_id = null, $params = null, RecentChange $rc = null
333 ) {
334 return MediaWikiServices::getInstance()->getChangeTagsStore()->addTags(
335 $tags, $rc_id, $rev_id, $log_id, $params, $rc
336 );
337 }
338
369 public static function updateTags( $tagsToAdd, $tagsToRemove, &$rc_id = null,
370 &$rev_id = null, &$log_id = null, $params = null, RecentChange $rc = null,
371 UserIdentity $user = null
372 ) {
373 return MediaWikiServices::getInstance()->getChangeTagsStore()->updateTags(
374 $tagsToAdd, $tagsToRemove, $rc_id, $rev_id, $log_id, $params, $rc, $user
375 );
376 }
377
390 public static function getTagsWithData(
391 IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null
392 ) {
393 return MediaWikiServices::getInstance()->getChangeTagsStore()->getTagsWithData( $db, $rc_id, $rev_id, $log_id );
394 }
395
407 public static function getTags( IReadableDatabase $db, $rc_id = null, $rev_id = null, $log_id = null ) {
408 return MediaWikiServices::getInstance()->getChangeTagsStore()->getTags( $db, $rc_id, $rev_id, $log_id );
409 }
410
421 protected static function restrictedTagError( $msgOne, $msgMulti, $tags ) {
422 $lang = RequestContext::getMain()->getLanguage();
423 $tags = array_values( $tags );
424 $count = count( $tags );
425 $status = Status::newFatal( ( $count > 1 ) ? $msgMulti : $msgOne,
426 $lang->commaList( $tags ), $count );
427 $status->value = $tags;
428 return $status;
429 }
430
445 public static function canAddTagsAccompanyingChange(
446 array $tags,
447 Authority $performer = null,
448 $checkBlock = true
449 ) {
450 $user = null;
451 $services = MediaWikiServices::getInstance();
452 if ( $performer !== null ) {
453 if ( !$performer->isAllowed( 'applychangetags' ) ) {
454 return Status::newFatal( 'tags-apply-no-permission' );
455 }
456
457 if ( $checkBlock && $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
458 return Status::newFatal(
459 'tags-apply-blocked',
460 $performer->getUser()->getName()
461 );
462 }
463
464 // ChangeTagsAllowedAdd hook still needs a full User object
465 $user = $services->getUserFactory()->newFromAuthority( $performer );
466 }
467
468 // to be applied, a tag has to be explicitly defined
469 $allowedTags = $services->getChangeTagsStore()->listExplicitlyDefinedTags();
470 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagsAllowedAdd( $allowedTags, $tags, $user );
471 $disallowedTags = array_diff( $tags, $allowedTags );
472 if ( $disallowedTags ) {
473 return self::restrictedTagError( 'tags-apply-not-allowed-one',
474 'tags-apply-not-allowed-multi', $disallowedTags );
475 }
476
477 return Status::newGood();
478 }
479
494 public static function canUpdateTags(
495 array $tagsToAdd,
496 array $tagsToRemove,
497 Authority $performer = null
498 ) {
499 if ( $performer !== null ) {
500 if ( !$performer->isDefinitelyAllowed( 'changetags' ) ) {
501 return Status::newFatal( 'tags-update-no-permission' );
502 }
503
504 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
505 return Status::newFatal(
506 'tags-update-blocked',
507 $performer->getUser()->getName()
508 );
509 }
510 }
511
512 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
513 if ( $tagsToAdd ) {
514 // to be added, a tag has to be explicitly defined
515 // @todo Allow extensions to define tags that can be applied by users...
516 $explicitlyDefinedTags = $changeTagStore->listExplicitlyDefinedTags();
517 $diff = array_diff( $tagsToAdd, $explicitlyDefinedTags );
518 if ( $diff ) {
519 return self::restrictedTagError( 'tags-update-add-not-allowed-one',
520 'tags-update-add-not-allowed-multi', $diff );
521 }
522 }
523
524 if ( $tagsToRemove ) {
525 // to be removed, a tag must not be defined by an extension, or equivalently it
526 // has to be either explicitly defined or not defined at all
527 // (assuming no edge case of a tag both explicitly-defined and extension-defined)
528 $softwareDefinedTags = $changeTagStore->listSoftwareDefinedTags();
529 $intersect = array_intersect( $tagsToRemove, $softwareDefinedTags );
530 if ( $intersect ) {
531 return self::restrictedTagError( 'tags-update-remove-not-allowed-one',
532 'tags-update-remove-not-allowed-multi', $intersect );
533 }
534 }
535
536 return Status::newGood();
537 }
538
569 public static function updateTagsWithChecks( $tagsToAdd, $tagsToRemove,
570 $rc_id, $rev_id, $log_id, $params, string $reason, Authority $performer
571 ) {
572 if ( !$tagsToAdd && !$tagsToRemove ) {
573 // no-op, don't bother
574 return Status::newGood( (object)[
575 'logId' => null,
576 'addedTags' => [],
577 'removedTags' => [],
578 ] );
579 }
580
581 $tagsToAdd ??= [];
582 $tagsToRemove ??= [];
583
584 // are we allowed to do this?
585 $result = self::canUpdateTags( $tagsToAdd, $tagsToRemove, $performer );
586 if ( !$result->isOK() ) {
587 $result->value = null;
588 return $result;
589 }
590
591 // basic rate limiting
592 $status = PermissionStatus::newEmpty();
593 if ( !$performer->authorizeAction( 'changetags', $status ) ) {
594 return Status::wrap( $status );
595 }
596
597 // do it!
598 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
599 [ $tagsAdded, $tagsRemoved, $initialTags ] = $changeTagStore->updateTags( $tagsToAdd,
600 $tagsToRemove, $rc_id, $rev_id, $log_id, $params, null, $performer->getUser() );
601 if ( !$tagsAdded && !$tagsRemoved ) {
602 // no-op, don't log it
603 return Status::newGood( (object)[
604 'logId' => null,
605 'addedTags' => [],
606 'removedTags' => [],
607 ] );
608 }
609
610 // log it
611 $logEntry = new ManualLogEntry( 'tag', 'update' );
612 $logEntry->setPerformer( $performer->getUser() );
613 $logEntry->setComment( $reason );
614
615 // find the appropriate target page
616 if ( $rev_id ) {
617 $revisionRecord = MediaWikiServices::getInstance()
618 ->getRevisionLookup()
619 ->getRevisionById( $rev_id );
620 if ( $revisionRecord ) {
621 $logEntry->setTarget( $revisionRecord->getPageAsLinkTarget() );
622 }
623 } elseif ( $log_id ) {
624 // This function is from revision deletion logic and has nothing to do with
625 // change tags, but it appears to be the only other place in core where we
626 // perform logged actions on log items.
627 $logEntry->setTarget( RevDelLogList::suggestTarget( null, [ $log_id ] ) );
628 }
629
630 if ( !$logEntry->getTarget() ) {
631 // target is required, so we have to set something
632 $logEntry->setTarget( SpecialPage::getTitleFor( 'Tags' ) );
633 }
634
635 $logParams = [
636 '4::revid' => $rev_id,
637 '5::logid' => $log_id,
638 '6:list:tagsAdded' => $tagsAdded,
639 '7:number:tagsAddedCount' => count( $tagsAdded ),
640 '8:list:tagsRemoved' => $tagsRemoved,
641 '9:number:tagsRemovedCount' => count( $tagsRemoved ),
642 'initialTags' => $initialTags,
643 ];
644 $logEntry->setParameters( $logParams );
645 $logEntry->setRelations( [ 'Tag' => array_merge( $tagsAdded, $tagsRemoved ) ] );
646
647 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
648 $logId = $logEntry->insert( $dbw );
649 // Only send this to UDP, not RC, similar to patrol events
650 $logEntry->publish( $logId, 'udp' );
651
652 return Status::newGood( (object)[
653 'logId' => $logId,
654 'addedTags' => $tagsAdded,
655 'removedTags' => $tagsRemoved,
656 ] );
657 }
658
680 public static function modifyDisplayQuery( &$tables, &$fields, &$conds,
681 &$join_conds, &$options, $filter_tag = '', bool $exclude = false
682 ) {
683 MediaWikiServices::getInstance()->getChangeTagsStore()->modifyDisplayQuery(
684 $tables,
685 $fields,
686 $conds,
687 $join_conds,
688 $options,
689 $filter_tag,
690 $exclude
691 );
692 }
693
703 public static function getDisplayTableName() {
704 return self::CHANGE_TAG;
705 }
706
715 public static function makeTagSummarySubquery( $tables ) {
716 return MediaWikiServices::getInstance()->getChangeTagsStore()->makeTagSummarySubquery( $tables );
717 }
718
730 public static function buildTagFilterSelector(
731 $selected = '', $ooui = false, IContextSource $context = null
732 ) {
733 if ( !$context ) {
734 $context = RequestContext::getMain();
735 }
736
737 $config = $context->getConfig();
738 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
739 if ( !$config->get( MainConfigNames::UseTagFilter ) ||
740 !count( $changeTagStore->listDefinedTags() ) ) {
741 return [];
742 }
743
744 $tags = self::getChangeTagList( $context, $context->getLanguage() );
745 $autocomplete = [];
746 foreach ( $tags as $tagInfo ) {
747 $autocomplete[ $tagInfo['label'] ] = $tagInfo['name'];
748 }
749
750 $data = [
751 Html::rawElement(
752 'label',
753 [ 'for' => 'tagfilter' ],
754 $context->msg( 'tag-filter' )->parse()
755 )
756 ];
757
758 if ( $ooui ) {
759 $options = Html::listDropdownOptionsOoui( $autocomplete );
760
761 $data[] = new OOUI\ComboBoxInputWidget( [
762 'id' => 'tagfilter',
763 'name' => 'tagfilter',
764 'value' => $selected,
765 'classes' => 'mw-tagfilter-input',
766 'options' => $options,
767 ] );
768 } else {
769 $datalist = new XmlSelect( false, 'tagfilter-datalist' );
770 $datalist->setTagName( 'datalist' );
771 $datalist->addOptions( $autocomplete );
772
773 $data[] = Html::input(
774 'tagfilter',
775 $selected,
776 'text',
777 [
778 'class' => [ 'mw-tagfilter-input', 'mw-ui-input', 'mw-ui-input-inline' ],
779 'size' => 20,
780 'id' => 'tagfilter',
781 'list' => 'tagfilter-datalist',
782 ]
783 ) . $datalist->getHTML();
784 }
785
786 return $data;
787 }
788
798 public static function defineTag( $tag ) {
799 MediaWikiServices::getInstance()->getChangeTagsStore()->defineTag( $tag );
800 }
801
811 public static function canActivateTag( $tag, Authority $performer = null ) {
812 if ( $performer !== null ) {
813 if ( !$performer->isAllowed( 'managechangetags' ) ) {
814 return Status::newFatal( 'tags-manage-no-permission' );
815 }
816 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
817 return Status::newFatal(
818 'tags-manage-blocked',
819 $performer->getUser()->getName()
820 );
821 }
822 }
823
824 // defined tags cannot be activated (a defined tag is either extension-
825 // defined, in which case the extension chooses whether or not to active it;
826 // or user-defined, in which case it is considered active)
827 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
828 $definedTags = $changeTagStore->listDefinedTags();
829 if ( in_array( $tag, $definedTags ) ) {
830 return Status::newFatal( 'tags-activate-not-allowed', $tag );
831 }
832
833 // non-existing tags cannot be activated
834 if ( !isset( $changeTagStore->tagUsageStatistics()[$tag] ) ) { // we already know the tag is undefined
835 return Status::newFatal( 'tags-activate-not-found', $tag );
836 }
837
838 return Status::newGood();
839 }
840
858 public static function activateTagWithChecks( string $tag, string $reason, Authority $performer,
859 bool $ignoreWarnings = false, array $logEntryTags = []
860 ) {
861 // are we allowed to do this?
862 $result = self::canActivateTag( $tag, $performer );
863 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
864 $result->value = null;
865 return $result;
866 }
867 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
868
869 $changeTagStore->defineTag( $tag );
870
871 $logId = $changeTagStore->logTagManagementAction( 'activate', $tag, $reason, $performer->getUser(),
872 null, $logEntryTags );
873
874 return Status::newGood( $logId );
875 }
876
886 public static function canDeactivateTag( $tag, Authority $performer = null ) {
887 if ( $performer !== null ) {
888 if ( !$performer->isAllowed( 'managechangetags' ) ) {
889 return Status::newFatal( 'tags-manage-no-permission' );
890 }
891 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
892 return Status::newFatal(
893 'tags-manage-blocked',
894 $performer->getUser()->getName()
895 );
896 }
897 }
898
899 // only explicitly-defined tags can be deactivated
900 $explicitlyDefinedTags = MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
901 if ( !in_array( $tag, $explicitlyDefinedTags ) ) {
902 return Status::newFatal( 'tags-deactivate-not-allowed', $tag );
903 }
904 return Status::newGood();
905 }
906
924 public static function deactivateTagWithChecks( string $tag, string $reason, Authority $performer,
925 bool $ignoreWarnings = false, array $logEntryTags = []
926 ) {
927 // are we allowed to do this?
928 $result = self::canDeactivateTag( $tag, $performer );
929 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
930 $result->value = null;
931 return $result;
932 }
933 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
934
935 $changeTagStore->undefineTag( $tag );
936
937 $logId = $changeTagStore->logTagManagementAction( 'deactivate', $tag, $reason,
938 $performer->getUser(), null, $logEntryTags );
939
940 return Status::newGood( $logId );
941 }
942
950 public static function isTagNameValid( $tag ) {
951 // no empty tags
952 if ( $tag === '' ) {
953 return Status::newFatal( 'tags-create-no-name' );
954 }
955
956 // tags cannot contain commas (used to be used as a delimiter in tag_summary table),
957 // pipe (used as a delimiter between multiple tags in
958 // SpecialRecentchanges and friends), or slashes (would break tag description messages in
959 // MediaWiki namespace)
960 if ( strpos( $tag, ',' ) !== false || strpos( $tag, '|' ) !== false
961 || strpos( $tag, '/' ) !== false ) {
962 return Status::newFatal( 'tags-create-invalid-chars' );
963 }
964
965 // could the MediaWiki namespace description messages be created?
966 $title = Title::makeTitleSafe( NS_MEDIAWIKI, "Tag-$tag-description" );
967 if ( $title === null ) {
968 return Status::newFatal( 'tags-create-invalid-title-chars' );
969 }
970
971 return Status::newGood();
972 }
973
986 public static function canCreateTag( $tag, Authority $performer = null ) {
987 $user = null;
988 $services = MediaWikiServices::getInstance();
989 if ( $performer !== null ) {
990 if ( !$performer->isAllowed( 'managechangetags' ) ) {
991 return Status::newFatal( 'tags-manage-no-permission' );
992 }
993 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
994 return Status::newFatal(
995 'tags-manage-blocked',
996 $performer->getUser()->getName()
997 );
998 }
999 // ChangeTagCanCreate hook still needs a full User object
1000 $user = $services->getUserFactory()->newFromAuthority( $performer );
1001 }
1002
1003 $status = self::isTagNameValid( $tag );
1004 if ( !$status->isGood() ) {
1005 return $status;
1006 }
1007
1008 // does the tag already exist?
1009 $changeTagStore = $services->getChangeTagsStore();
1010 if (
1011 isset( $changeTagStore->tagUsageStatistics()[$tag] ) ||
1012 in_array( $tag, $changeTagStore->listDefinedTags() )
1013 ) {
1014 return Status::newFatal( 'tags-create-already-exists', $tag );
1015 }
1016
1017 // check with hooks
1018 $canCreateResult = Status::newGood();
1019 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanCreate( $tag, $user, $canCreateResult );
1020 return $canCreateResult;
1021 }
1022
1042 public static function createTagWithChecks( string $tag, string $reason, Authority $performer,
1043 bool $ignoreWarnings = false, array $logEntryTags = []
1044 ) {
1045 // are we allowed to do this?
1046 $result = self::canCreateTag( $tag, $performer );
1047 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1048 $result->value = null;
1049 return $result;
1050 }
1051
1052 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1053 $changeTagStore->defineTag( $tag );
1054 $logId = $changeTagStore->logTagManagementAction( 'create', $tag, $reason,
1055 $performer->getUser(), null, $logEntryTags );
1056
1057 return Status::newGood( $logId );
1058 }
1059
1073 public static function deleteTagEverywhere( $tag ) {
1074 return MediaWikiServices::getInstance()->getChangeTagsStore()->deleteTagEverywhere( $tag );
1075 }
1076
1089 public static function canDeleteTag( $tag, Authority $performer = null, int $flags = 0 ) {
1090 $user = null;
1091 $services = MediaWikiServices::getInstance();
1092 if ( $performer !== null ) {
1093 if ( !$performer->isAllowed( 'deletechangetags' ) ) {
1094 return Status::newFatal( 'tags-delete-no-permission' );
1095 }
1096 if ( $performer->getBlock() && $performer->getBlock()->isSitewide() ) {
1097 return Status::newFatal(
1098 'tags-manage-blocked',
1099 $performer->getUser()->getName()
1100 );
1101 }
1102 // ChangeTagCanDelete hook still needs a full User object
1103 $user = $services->getUserFactory()->newFromAuthority( $performer );
1104 }
1105
1106 $changeTagStore = $services->getChangeTagsStore();
1107 $tagUsage = $changeTagStore->tagUsageStatistics();
1108 if (
1109 !isset( $tagUsage[$tag] ) &&
1110 !in_array( $tag, $changeTagStore->listDefinedTags() )
1111 ) {
1112 return Status::newFatal( 'tags-delete-not-found', $tag );
1113 }
1114
1115 if ( $flags !== self::BYPASS_MAX_USAGE_CHECK &&
1116 isset( $tagUsage[$tag] ) &&
1117 $tagUsage[$tag] > self::MAX_DELETE_USES
1118 ) {
1119 return Status::newFatal( 'tags-delete-too-many-uses', $tag, self::MAX_DELETE_USES );
1120 }
1121
1122 $softwareDefined = $changeTagStore->listSoftwareDefinedTags();
1123 if ( in_array( $tag, $softwareDefined ) ) {
1124 // extension-defined tags can't be deleted unless the extension
1125 // specifically allows it
1126 $status = Status::newFatal( 'tags-delete-not-allowed' );
1127 } else {
1128 // user-defined tags are deletable unless otherwise specified
1129 $status = Status::newGood();
1130 }
1131
1132 ( new HookRunner( $services->getHookContainer() ) )->onChangeTagCanDelete( $tag, $user, $status );
1133 return $status;
1134 }
1135
1153 public static function deleteTagWithChecks( string $tag, string $reason, Authority $performer,
1154 bool $ignoreWarnings = false, array $logEntryTags = []
1155 ) {
1156 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1157 // are we allowed to do this?
1158 $result = self::canDeleteTag( $tag, $performer );
1159 if ( $ignoreWarnings ? !$result->isOK() : !$result->isGood() ) {
1160 $result->value = null;
1161 return $result;
1162 }
1163
1164 // store the tag usage statistics
1165 $hitcount = $changeTagStore->tagUsageStatistics()[$tag] ?? 0;
1166
1167 // do it!
1168 $deleteResult = $changeTagStore->deleteTagEverywhere( $tag );
1169 if ( !$deleteResult->isOK() ) {
1170 return $deleteResult;
1171 }
1172
1173 // log it
1174 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1175 $logId = $changeTagStore->logTagManagementAction( 'delete', $tag, $reason, $performer->getUser(),
1176 $hitcount, $logEntryTags );
1177
1178 $deleteResult->value = $logId;
1179 return $deleteResult;
1180 }
1181
1189 public static function listSoftwareActivatedTags() {
1190 return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareActivatedTags();
1191 }
1192
1201 public static function listDefinedTags() {
1202 return MediaWikiServices::getInstance()->getChangeTagsStore()->listDefinedTags();
1203 }
1204
1214 public static function listExplicitlyDefinedTags() {
1215 return MediaWikiServices::getInstance()->getChangeTagsStore()->listExplicitlyDefinedTags();
1216 }
1217
1228 public static function listSoftwareDefinedTags() {
1229 return MediaWikiServices::getInstance()->getChangeTagsStore()->listSoftwareDefinedTags();
1230 }
1231
1238 public static function purgeTagCacheAll() {
1239 MediaWikiServices::getInstance()->getChangeTagsStore()->purgeTagCacheAll();
1240 }
1241
1250 public static function tagUsageStatistics() {
1251 return MediaWikiServices::getInstance()->getChangeTagsStore()->tagUsageStatistics();
1252 }
1253
1258 private const TAG_DESC_CHARACTER_LIMIT = 120;
1259
1285 public static function getChangeTagListSummary( MessageLocalizer $localizer, Language $lang ) {
1286 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
1287 return $cache->getWithSetCallback(
1288 $cache->makeKey( 'tags-list-summary', $lang->getCode() ),
1289 WANObjectCache::TTL_DAY,
1290 static function ( $oldValue, &$ttl, array &$setOpts ) use ( $localizer ) {
1291 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1292 $tagHitCounts = $changeTagStore->tagUsageStatistics();
1293
1294 $result = [];
1295 // Only list tags that are still actively defined
1296 foreach ( $changeTagStore->listDefinedTags() as $tagName ) {
1297 // Only list tags with more than 0 hits
1298 $hits = $tagHitCounts[$tagName] ?? 0;
1299 if ( $hits <= 0 ) {
1300 continue;
1301 }
1302
1303 $labelMsg = self::tagShortDescriptionMessage( $tagName, $localizer );
1304 $helpLink = self::tagHelpLink( $tagName, $localizer );
1305 $descriptionMsg = self::tagLongDescriptionMessage( $tagName, $localizer );
1306 // Don't cache the message object, use the correct MessageLocalizer to parse later.
1307 $result[] = [
1308 'name' => $tagName,
1309 'labelMsg' => (bool)$labelMsg,
1310 'label' => $labelMsg ? $labelMsg->plain() : $tagName,
1311 'descriptionMsg' => (bool)$descriptionMsg,
1312 'description' => $descriptionMsg ? $descriptionMsg->plain() : '',
1313 'helpLink' => $helpLink,
1314 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ),
1315 ];
1316 }
1317 return $result;
1318 }
1319 );
1320 }
1321
1334 public static function getChangeTagList( MessageLocalizer $localizer, Language $lang ) {
1335 $tags = self::getChangeTagListSummary( $localizer, $lang );
1336 foreach ( $tags as &$tagInfo ) {
1337 if ( $tagInfo['labelMsg'] ) {
1338 // Use localizer with the correct page title to parse plain message from the cache.
1339 $labelMsg = new RawMessage( $tagInfo['label'] );
1340 $tagInfo['label'] = Sanitizer::stripAllTags( $localizer->msg( $labelMsg )->parse() );
1341 } else {
1342 $tagInfo['label'] = $localizer->msg( 'tag-hidden', $tagInfo['name'] )->text();
1343 }
1344 if ( $tagInfo['descriptionMsg'] ) {
1345 $descriptionMsg = new RawMessage( $tagInfo['description'] );
1346 $tagInfo['description'] = $lang->truncateForVisual(
1347 Sanitizer::stripAllTags( $localizer->msg( $descriptionMsg )->parse() ),
1348 self::TAG_DESC_CHARACTER_LIMIT
1349 );
1350 }
1351 unset( $tagInfo['labelMsg'] );
1352 unset( $tagInfo['descriptionMsg'] );
1353 }
1354
1355 // Instead of sorting by hit count (disabled for now), sort by display name
1356 usort( $tags, static function ( $a, $b ) {
1357 return strcasecmp( $a['label'], $b['label'] );
1358 } );
1359 return $tags;
1360 }
1361
1376 public static function showTagEditingUI( Authority $performer ) {
1377 $changeTagStore = MediaWikiServices::getInstance()->getChangeTagsStore();
1378 return $performer->isAllowed( 'changetags' ) && (bool)$changeTagStore->listExplicitlyDefinedTags();
1379 }
1380}
const NS_MEDIAWIKI
Definition Defines.php:73
array $params
The job parameters.
Recent changes tagging.
static getTagsWithData(IReadableDatabase $db, $rc_id=null, $rev_id=null, $log_id=null)
Return all the tags associated with the given recent change ID, revision ID, and/or log entry ID,...
const TAG_MANUAL_REVERT
The tagged edit restores the page to an earlier revision.
const TAG_SERVER_SIDE_UPLOAD
This tagged edit was performed while importing media files using the importImages....
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='', bool $exclude=false)
Applies all tags-related changes to a query.
const TAG_REMOVED_REDIRECT
The tagged edit turns a redirect page into a non-redirect.
static listSoftwareDefinedTags()
Lists tags defined by core or extensions using the ListDefinedTags hook.
static canDeactivateTag( $tag, Authority $performer=null)
Is it OK to allow the user to deactivate this tag?
static buildTagFilterSelector( $selected='', $ooui=false, IContextSource $context=null)
Build a text box to select a change tag.
static canCreateTag( $tag, Authority $performer=null)
Is it OK to allow the user to create this tag?
const TAG_REPLACE
The tagged edit removes more than 90% of the content of the page.
const TAG_CONTENT_MODEL_CHANGE
The tagged edit changes the content model of the page.
static updateTagsWithChecks( $tagsToAdd, $tagsToRemove, $rc_id, $rev_id, $log_id, $params, string $reason, Authority $performer)
Adds and/or removes tags to/from a given change, checking whether it is allowed first,...
static tagLongDescriptionMessage( $tag, MessageLocalizer $context)
Get the message object for the tag's long description.
static deactivateTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Deactivates a tag, checking whether it is allowed first, and adding a log entry afterwards.
static showTagEditingUI(Authority $performer)
Indicate whether change tag editing UI is relevant.
static deleteTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Deletes a tag, checking whether it is allowed first, and adding a log entry afterwards.
static getSoftwareTags( $all=false)
Loads defined core tags, checks for invalid types (if not array), and filters for supported and enabl...
const TAG_CHANGED_REDIRECT_TARGET
The tagged edit changes the target of a redirect page.
const TAG_REVERTED
The tagged edit is reverted by a subsequent edit (which is tagged by one of TAG_ROLLBACK,...
static restrictedTagError( $msgOne, $msgMulti, $tags)
Helper function to generate a fatal status with a 'not-allowed' type error.
const TAG_ROLLBACK
The tagged edit is a rollback (undoes the previous edit and all immediately preceding edits by the sa...
static getChangeTagListSummary(MessageLocalizer $localizer, Language $lang)
Get information about change tags, without parsing messages, for tag filter dropdown menus.
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
static getChangeTagList(MessageLocalizer $localizer, Language $lang)
Get information about change tags for tag filter dropdown menus.
const BYPASS_MAX_USAGE_CHECK
Flag for canDeleteTag().
static tagHelpLink( $tag, MessageLocalizer $context)
Get the tag's help link.
static listSoftwareActivatedTags()
Lists those tags which core or extensions report as being "active".
static canActivateTag( $tag, Authority $performer=null)
Is it OK to allow the user to activate this tag?
static formatSummaryRow( $tags, $unused, MessageLocalizer $localizer=null)
Creates HTML for the given tags.
const DISPLAY_TABLE_ALIAS
static purgeTagCacheAll()
Invalidates the short-term cache of defined tags used by the list*DefinedTags functions,...
static addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null, RecentChange $rc=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
static canDeleteTag( $tag, Authority $performer=null, int $flags=0)
Is it OK to allow the user to delete this tag?
static getDisplayTableName()
Get the name of the change_tag table to use for modifyDisplayQuery().
static tagUsageStatistics()
Returns a map of any tags used on the wiki to number of edits tagged with them, ordered descending by...
static tagShortDescriptionMessage( $tag, MessageLocalizer $context)
Get the message object for the tag's short description.
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
static tagDescription( $tag, MessageLocalizer $context)
Get a short description for a tag.
static defineTag( $tag)
Set ctd_user_defined = 1 in change_tag_def without checking that the tag name is valid.
static createTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Creates a tag by adding it to change_tag_def table.
static getTags(IReadableDatabase $db, $rc_id=null, $rev_id=null, $log_id=null)
Return all the tags associated with the given recent change ID, revision ID, and/or log entry ID.
const TAG_UNDO
The tagged edit is was performed via the "undo" link.
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null, $checkBlock=true)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
const TAG_BLANK
The tagged edit blanks the page (replaces it with the empty string).
static isTagNameValid( $tag)
Is the tag name valid?
const REVERT_TAGS
List of tags which denote a revert of some sort.
static activateTagWithChecks(string $tag, string $reason, Authority $performer, bool $ignoreWarnings=false, array $logEntryTags=[])
Activates a tag, checking whether it is allowed first, and adding a log entry afterwards.
static deleteTagEverywhere( $tag)
Permanently removes all traces of a tag from the DB.
const TAG_NEW_REDIRECT
The tagged edit creates a new redirect (either by creating a new page or turning an existing page int...
static canUpdateTags(array $tagsToAdd, array $tagsToRemove, Authority $performer=null)
Is it OK to allow the user to adds and remove the given tags to/from a change?
static listExplicitlyDefinedTags()
Lists tags explicitly defined in the change_tag_def table of the database.
static updateTags( $tagsToAdd, $tagsToRemove, &$rc_id=null, &$rev_id=null, &$log_id=null, $params=null, RecentChange $rc=null, UserIdentity $user=null)
Add and remove tags to/from a change given its rc_id, rev_id and/or log_id, without verifying that th...
Class for creating new log entries and inserting them into the database.
Group all the pieces relevant to the context of a request into one instance.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Base class for language-specific code.
Definition Language.php:78
truncateForVisual( $string, $length, $ellipsis='...', $adjustLength=true)
Truncate a string to a specified number of characters, appending an optional string (e....
getCode()
Get the internal language code for this language object.
Variant of the Message class.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:150
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
A StatusValue for permission errors.
Parent class for all special pages.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:30
Utility class for creating and reading rows in the recentchanges table.
static suggestTarget( $target, array $ids)
Suggest a target for the revision deletion Optionally override this function.
static makeInternalOrExternalUrl( $name)
If url string starts with http, consider as external URL, else internal.
Definition Skin.php:1176
Multi-datacenter aware caching interface.
Interface for objects which can provide a MediaWiki context on request.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
authorizeAction(string $action, PermissionStatus $status=null)
Authorize an action.
getUser()
Returns the performer of the actions associated with this authority.
isAllowed(string $permission, PermissionStatus $status=null)
Checks whether this authority has the given permission in general.
Interface for objects representing user identity.
Interface for localizing messages in MediaWiki.
msg( $key,... $params)
This is the method for getting translated interface messages.
A database connection without write operations.