2013-01-15 23:21:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom formatter for 'page-link' notifications
|
|
|
|
*/
|
|
|
|
class EchoPageLinkFormatter extends EchoBasicFormatter {
|
|
|
|
|
2013-05-09 18:50:05 +00:00
|
|
|
/**
|
|
|
|
* This is a workaround for backwards compatibility.
|
|
|
|
* In https://gerrit.wikimedia.org/r/#/c/63076 we changed
|
|
|
|
* the schema to save link-from-page-id instead of
|
|
|
|
* link-from-namespace & link-from-title
|
|
|
|
*/
|
|
|
|
protected function extractExtra( $extra ) {
|
|
|
|
if ( isset( $extra['link-from-namespace'], $extra['link-from-title'] )
|
|
|
|
&& !isset( $extra['link-from-page-id'] )
|
|
|
|
) {
|
|
|
|
$title = Title::makeTitleSafe(
|
|
|
|
$extra['link-from-namespace'],
|
|
|
|
$extra['link-from-title']
|
|
|
|
);
|
|
|
|
if ( $title ) {
|
|
|
|
$extra['link-from-page-id'] = $title->getArticleId();
|
|
|
|
unset(
|
|
|
|
$extra['link-from-namespace'],
|
|
|
|
$extra['link-from-title']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extra;
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
|
|
|
* This method overwrite parent method and construct the bundle iterator
|
|
|
|
* based on link from, it will be used in a message like this: Page A was
|
|
|
|
* link from Page B and X other pages
|
2014-07-18 03:58:21 +00:00
|
|
|
*
|
2013-01-15 23:21:39 +00:00
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
2013-11-23 05:31:26 +00:00
|
|
|
* @param $type string deprecated
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
protected function generateBundleData( $event, $user, $type ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoMaxNotificationCount;
|
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
$data = $this->getRawBundleData( $event, $user, $type );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
if ( !$data ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-09 18:50:05 +00:00
|
|
|
$extra = self::extractExtra( $event->getExtra() );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
2013-05-09 18:50:05 +00:00
|
|
|
if ( !$this->isTitleSet( $extra ) ) {
|
|
|
|
// Link from title is required for bundling notification
|
|
|
|
return;
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$count = 1;
|
2013-05-08 18:11:44 +00:00
|
|
|
$linkFrom = array(
|
|
|
|
$extra['link-from-page-id'] => true
|
|
|
|
);
|
2014-07-18 03:58:21 +00:00
|
|
|
foreach ( $data as $bundledEvent ) {
|
|
|
|
$extra = $bundledEvent->getExtra();
|
2013-04-12 18:56:32 +00:00
|
|
|
if ( !$extra ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
if ( $this->isTitleSet( $extra ) ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
$pageId = $extra['link-from-page-id'];
|
2013-01-15 23:21:39 +00:00
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
if ( !isset( $linkFrom[$pageId] ) ) {
|
|
|
|
$linkFrom[$pageId] = true;
|
2013-01-15 23:21:39 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $count > $wgEchoMaxNotificationCount + 1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->bundleData['link-from-page-other-count'] = $count - 1;
|
|
|
|
if ( $count > 1 ) {
|
|
|
|
$this->bundleData['use-bundle'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-08 18:11:44 +00:00
|
|
|
* Internal function to check if link from page id key is set
|
2013-01-15 23:21:39 +00:00
|
|
|
* @param $extra array
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isTitleSet( $extra ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
return isset( $extra['link-from-page-id'] ) && $extra['link-from-page-id'];
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $param string
|
|
|
|
* @param $message Message
|
|
|
|
* @param $user User
|
|
|
|
*/
|
|
|
|
protected function processParam( $event, $param, $message, $user ) {
|
2013-05-09 18:50:05 +00:00
|
|
|
$extra = self::extractExtra( $event->getExtra() );
|
2013-01-15 23:21:39 +00:00
|
|
|
switch ( $param ) {
|
|
|
|
// 'A' part in this message: link from page A and X others
|
|
|
|
case 'link-from-page':
|
2013-07-24 20:59:54 +00:00
|
|
|
$title = null;
|
2013-01-15 23:21:39 +00:00
|
|
|
if ( $this->isTitleSet( $extra ) ) {
|
2013-05-09 18:50:05 +00:00
|
|
|
$title = Title::newFromId( $extra['link-from-page-id'] );
|
2013-08-22 21:51:01 +00:00
|
|
|
// Link-from page could be a brand new page and page_id would not be replicated
|
|
|
|
// to slave db yet. If job queue is enabled to process web and email notification,
|
|
|
|
// the check against master database is not necessary since there is already a
|
|
|
|
// delay in the job queue
|
|
|
|
if ( !$title ) {
|
|
|
|
global $wgEchoUseJobQueue;
|
|
|
|
$diff = wfTimestamp() - wfTimestamp( TS_UNIX, $event->getTimestamp() );
|
|
|
|
if ( !$wgEchoUseJobQueue && $diff < 5 ) {
|
|
|
|
$title = Title::newFromID( $extra['link-from-page-id'], Title::GAID_FOR_UPDATE );
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 20:59:54 +00:00
|
|
|
if ( $title ) {
|
|
|
|
if ( $this->outputFormat === 'htmlemail' ) {
|
|
|
|
$message->rawParams(
|
|
|
|
Linker::link(
|
|
|
|
$title,
|
|
|
|
$this->formatTitle( $title ),
|
|
|
|
array( 'style' => $this->getHTMLLinkStyle() ),
|
|
|
|
array(),
|
|
|
|
array( 'https' )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$message->params( $this->formatTitle( $title ) );
|
|
|
|
}
|
2013-05-09 18:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-24 20:59:54 +00:00
|
|
|
|
|
|
|
if ( !$title ) {
|
2013-08-27 00:55:45 +00:00
|
|
|
$message->params( $this->getMessage( 'echo-no-title' ) );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// example: {7} other page, {99+} other pages
|
|
|
|
case 'link-from-page-other-display':
|
|
|
|
global $wgEchoMaxNotificationCount;
|
|
|
|
|
|
|
|
if ( $this->bundleData['link-from-page-other-count'] > $wgEchoMaxNotificationCount ) {
|
|
|
|
$message->params(
|
2013-08-27 00:55:45 +00:00
|
|
|
$this->getMessage( 'echo-notification-count' )
|
2013-11-25 14:10:44 +00:00
|
|
|
->numParams( $wgEchoMaxNotificationCount )
|
2013-01-15 23:21:39 +00:00
|
|
|
->text()
|
|
|
|
);
|
|
|
|
} else {
|
2013-11-25 14:10:44 +00:00
|
|
|
$message->numParams( $this->bundleData['link-from-page-other-count'] );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// the number used for plural support
|
|
|
|
case 'link-from-page-other-count':
|
|
|
|
$message->params( $this->bundleData['link-from-page-other-count'] );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
parent::processParam( $event, $param, $message, $user );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 23:18:26 +00:00
|
|
|
/**
|
|
|
|
* Helper function for getLink()
|
|
|
|
*
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @param User $user The user receiving the notification
|
|
|
|
* @param String $destination The destination type for the link
|
|
|
|
* @return Array including target and query parameters
|
|
|
|
*/
|
|
|
|
protected function getLinkParams( $event, $user, $destination ) {
|
|
|
|
$target = null;
|
|
|
|
$query = array();
|
|
|
|
// Set up link parameters based on the destination (or pass to parent)
|
|
|
|
switch ( $destination ) {
|
|
|
|
case 'link-from-page':
|
|
|
|
if ( $this->bundleData['use-bundle'] ) {
|
|
|
|
if ( $event->getTitle() ) {
|
|
|
|
$target = SpecialPage::getTitleFor( 'WhatLinksHere', $event->getTitle()->getPrefixedText() );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$extra = self::extractExtra( $event->getExtra() );
|
|
|
|
if ( $this->isTitleSet( $extra ) ) {
|
|
|
|
$target = Title::newFromId( $extra['link-from-page-id'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return parent::getLinkParams( $event, $user, $destination );
|
|
|
|
}
|
|
|
|
return array( $target, $query );
|
|
|
|
}
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|