GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.mediawiki.org — Notifications/Developer guide - MediaWiki
Notifications, as enabled by the Echo Extension, give MediaWiki users quick updates about actions that affect them. These notifications can help users become more aware of events that relate to them and take quick action if they want to. This document is intended for MediaWiki extension developers and describes how to extend a MediaWiki extension to use the Notifications system. For more information about the Notification system, please see the project home page. See also: Echo (Notifications)/API Implementing notifications: Step-by-step
Notification categories
Every notification must be assigned to a category, which defines the notification preferences (e.g., opt-in or opt-out; on-wiki or email notification). The notification category can also be used to specify user groups that are eligible to receive the notification, the priority of the notification, and a help tip. Note that multiple notifications can be assigned to a single category. The assigned category can be one that is predefined by the Echo extension, or a customized category. Predefined categories include:
Creating a new categoryEach category definition consists of an array of keys and values. The following is an example of a category definition used by the Thanks extension: $notificationCategories['edit-thank'] = [ 'priority' => 3, 'tooltip' => 'echo-pref-tooltip-edit-thank', ]; You'll also need to set the title of the category in your i18n file - the message key must be of the form "echo-category-title-<category-key>": $messages['en'] = [ /* ... */ 'echo-category-title-edit-thank' => 'Thanks', 'echo-pref-tooltip-edit-thank' => 'Notify me when someone thanks me for an edit I made.', /* ... */ ]; $messages['qqq'] = [ /* ... */ 'echo-category-title-edit-thank' => 'Name of the notification category for the Thanks extension.', 'echo-pref-tooltip-edit-thank' => 'Further explanation of the user preference for enabling notifications on Thanks events.', /* ... */ ]; Notification category parametersNote that all category parameters are optional.
Defining a notificationEach notification consists of, at minimum, a title and a timestamp. In addition, a notification may also include a payload and primary and secondary action links. Title: The title is a simple string that conveys a piece of information to a user. It may include links, or may have no data or link associated. Timestamp: The timestamp reflects the time the notification was generated. Primary action link: A link designated as the main action that the user should take in response to a notification. In the flyout interface (the overlay revealed when you click the red notifications badge), the entire notification is clickable and the destination the user is sent to when clicking the notification is defined as the primary link. Secondary action link: An additional link that may be needed for a notification. Most notifications will not have a secondary link and the use of secondary links is discouraged (to keep the interface simple). Payload: The payload contains secondary data that augments the notification, e.g., an edit summary or a snippet of content such as the first few words of a contribution. The notification appears in the flyout menu beside the user name as well as on the user’s Notification page and, if email notification preference is set to on, as an email message. Note that the notification can be customized for each of these instances via the notification parameters. TODO: Explain more about customizing for different views Each notification definition consists of an array of keys and values. The following is an example of the Thanks notification definition. Note that the notification and the category have the same name (‘edit-thank’), as the Thanks notification is the only notification for this category: $notifications['edit-thank'] = array( 'primary-link' => array( 'message' => 'notification-link-text-respond-to-user', 'destination' => 'agent' ), 'secondary-link' => array( 'message' => 'notification-link-text-view-edit', 'destination' => 'diff' ), 'category' => 'edit-thank', 'group' => 'positive', 'formatter-class' => 'EchoThanksFormatter', 'title-message' => 'notification-thanks', 'title-params' => array( 'agent', 'difflink', 'title' ), 'flyout-message' => 'notification-thanks-flyout2', 'flyout-params' => array( 'agent', 'title' ), 'payload' => array( 'summary' ), 'email-subject-message' => 'notification-thanks-email-subject', 'email-subject-params' => array( 'agent' ), 'email-body-batch-message' => 'notification-thanks-email-batch-body', 'email-body-batch-params' => array( 'agent', 'title' ), 'icon' => 'thanks', ); Notification parameters
* Will become optional soon IconsPredefined iconsThe following icons are defined by the Echo extension and may be used by additional notifications. From: https://github.com/wikimedia/mediawiki-extensions-Echo/tree/master/modules/icons Additional iconsThe following icons are used by other extensions deployed on Wikimedia sites.
Creating a new iconNotification icons should be 30x30 pixel monochrome graphics that use the following color scheme:
Adding an iconAdd icons in hook public static function onBeforeCreateEchoEvent( &$notifications, &$notificationCategories, &$icons ) { // You can use either a path or a url, but not both. // The value of 'path' is relative to $wgExtensionAssetsPath. // // The value of 'url' should be a URL. //adding a custom icon $icons += [ 'myext-customicon' => [ 'path' => "my/icon/path/customicon.png" ], 'myext-anothericon' => [ 'url' => 'http://www.example.org/images/anothericon.png' ] ]; // register some echo events return true; } Add icons in global variable $wgEchoNotificationIcons['myext-customicon'] = [ 'path' => "my/icon/path/customicon.png" ]; PayloadsA payload adds more descriptive detail to a notification, it is usually located below the notification title in flyout/archive page/email. Predefined payloads
Creating/Overwriting a payloadclass MyCoolExtensionEchoFormatter extends EchoEventFormatter { protected function formatPayload( $payload, $event, $user ) { switch ( $payload ) { case 'summary': // generate the $summary return $summary; break; default: return parent::formatPayload( $payload, $event, $user ); break; } } } Notification formatter classesThe formatter class is the engine that defines how to turn an event into a message that the end-user reads. The value should be set to the name of a PHP class (either predefined or customized). Predefined formatter classesFormatter class provides an abstraction to format and display notification message for all distribution types: flyout, archive page, email and any other future distribution types. A notification only needs to define the messages and parameter tokens for each distribution type, all the formatting work will be handled automatically by the formatter class. There are three pre-defined concrete formatter classes: EchoBasicFormatter, EchoEditFormatter and EchoCommentFormatter, the basic formatter defines common and general parameter tokens for flytout, archive, and email while edit and comment formatter have more specific tokens for edit and comment related actions
Creating a new formatter classNew formatter classes can be developed as part of your extension and referred to via the notification’s 'formatter-class' parameter. New classes should extend one of the predefined formatter classes and should have a processParam function, which defines how to handles the message parameters unique to your notification. The following example is from the Thanks extension: class EchoThanksFormatter extends EchoBasicFormatter { /** * @param EchoEvent $event * @param string $param * @param Message $message * @param User $user */ protected function processParam( $event, $param, $message, $user ) { if ( $param === 'difflink' ) { $eventData = $event->getExtra(); if ( !isset( $eventData['revid'] ) ) { $message->params( '' ); return; } $this->setTitleLink( $event, $message, [ 'class' => 'mw-echo-diff', 'linkText' => wfMessage( 'notification-thanks-diff-link' )->text(), 'param' => [ 'oldid' => $eventData['revid'], 'diff' => 'prev', ] ] ); } else { parent::processParam( $event, $param, $message, $user ); } } } Email notificationIndividual email notificationA typical individual email notification consists of four major components: intro, summary, call-to-action and footer
HTML email also contains an icon component Batched email notificationA user can request to receive notification summary in email once in a day or a week. For the summary section in the notification, the message defined in 'email-body-batch-message' is used. For a bundled message, the message defined in 'email-body-batch-bundle-message' is used Bundled notificationsNotification can be triggered anywhere from the code. We definitely do not want to spam the user with notifications. In this case, we may want to enable notification bundling. For example, page-link notification is triggered when a page is linked from another page, a popular page may trigger a lot of unwanted notifications. Bundling will help resolving the problem Define bundling rulepublic static function onEchoGetBundleRules( $event, &$bundleString ) { switch ( $event->getType() ) { case 'edit-user-talk': $bundleString = 'edit-user-talk'; if ( $event->getTitle() ) { $bundleString .= '-' . $event->getTitle()->getNamespace() . '-' . $event->getTitle()->getDBkey(); } break; } } Hook the notification into the Echo extensionTo hook your notification to the Echo extension, use the following two hooks: BeforeCreateEchoEvent - Used to pass Echo your definition for the notification category and the notification itself (as well as any custom icons). Example from Thanks: public static function onBeforeCreateEchoEvent( &$notifications, &$notificationCategories, &$icons ) { $notificationCategories['edit-thank'] = [ 'priority' => 3, 'tooltip' => 'echo-pref-tooltip-edit-thank', ]; $notifications['edit-thank'] = [ 'category' => 'edit-thank', 'group' => 'positive', 'formatter-class' => 'EchoThanksFormatter', 'title-message' => 'notification-thanks', 'title-params' => [ 'agent', 'difflink', 'title' ], 'flyout-message' => 'notification-thanks-flyout', 'flyout-params' => [ 'agent', 'difflink', 'title' ], 'payload' => [ 'summary' ], 'email-subject-message' => 'notification-thanks-email-subject', 'email-subject-params' => [ 'agent' ], 'email-body-batch-message' => 'notification-thanks-email-batch-body', 'email-body-batch-params' => [ 'agent', 'title' ], 'icon' => 'gratitude', ]; return true; } EchoGetDefaultNotifiedUsers - Used to define who gets the notifications (for example, the user who performed the edit) Example from Thanks: public static function onEchoGetDefaultNotifiedUsers( $event, &$users ) { switch ( $event->getType() ) { case 'edit-thank': $extra = $event->getExtra(); if ( !$extra || !isset( $extra['thanked-user-id'] ) ) { break; } $recipientId = $extra['thanked-user-id']; $recipient = User::newFromId( $recipientId ); $users[$recipientId] = $recipient; break; } return true; } Remember to register the above hooks to your myCoolExtension.php file. Example from BounceHandler: $wgHooks['BeforeCreateEchoEvent'][] = 'BounceHandlerHooks::onBeforeCreateEchoEvent'; $wgHooks['EchoGetDefaultNotifiedUsers'][] = 'BounceHandlerHooks::onEchoGetDefaultNotifiedUsers'; Create the notification via the Echo extensionTo trigger the notification, use the EchoEvent::create function call, either as part of the extension code (if the extension creates an interface) or in a hook to an existing part of the MediaWiki software. The EchoEvent::create function defines what data gets passed in to the notification. The following example is used by the Thanks extension: EchoEvent::create( [ 'type' => 'edit-thank', 'title' => $title, 'extra' => [ 'revid' => $rev->getId(), 'thanked-user-id' => $recipient, 'source' => $source, ], 'agent' => $agent, ] ); All create function calls must have a 'type' parameter; the rest of the parameters are optional and depend on the notification. EchoEvent::create parameters
ConventionsOpt-in is bestIn general, notifications should be opt-in unless they are a critical component of site building and it’s imperative that everyone get them. You can set defaults for opt-in or opt-out via $wgDefaultUserOptions . For example your category has the name myCategory the definition would look like: Categorydefinition $wgEchoNotificationCategories['myCategory'] = [ 'priority' => 3, 'tooltip' => 'message-key-for-myCategory-tooltip' ]; The value for useroptions is echo-subscriptions-typeOfNotification-categoryOfNotification. Opt-out $wgDefaultUserOptions['echo-subscriptions-web-myCategory'] = true; Opt-in $wgDefaultUserOptions['echo-subscriptions-email-myCategory'] = false; Rate-limit notification-generating eventsTo prevent users from getting spammed by your notification, you can rate-limit the event that generates the notification (e.g., limit a user to 10 or fewer thanks events per minute). MonitoringEcho uses EventLogging to track data about notifications. Some graphs of notification data might be available, check m:Statistics.
Add notification to preferenceTo add your extension to
The following code would add the notification preference for public static function onBeforeCreateEchoEvent( &$notifications, &$notificationCategories, &$icons ) { $notificationCategories['edit-thank'] = [ 'priority' => 3, 'tooltip' => 'echo-pref-tooltip-edit-thank', ]; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |