| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\RCFeed; |
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\WikiMap\WikiMap; |
| 27 |
use RecentChange; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
abstract class MachineReadableRCFeedFormatter implements RCFeedFormatter { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
abstract protected function formatArray( array $packet ); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function getLine( array $feed, RecentChange $rc, $actionComment ) { |
| 58 |
$mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 59 |
$canonicalServer = $mainConfig->get( MainConfigNames::CanonicalServer ); |
| 60 |
$serverName = $mainConfig->get( MainConfigNames::ServerName ); |
| 61 |
$scriptPath = $mainConfig->get( MainConfigNames::ScriptPath ); |
| 62 |
$packet = [ |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
'id' => $rc->getAttribute( 'rc_id' ), |
| 67 |
'type' => RecentChange::parseFromRCType( $rc->getAttribute( 'rc_type' ) ), |
| 68 |
'namespace' => $rc->getTitle()->getNamespace(), |
| 69 |
'title' => $rc->getTitle()->getPrefixedText(), |
| 70 |
'title_url' => $rc->getTitle()->getCanonicalURL(), |
| 71 |
'comment' => $rc->getAttribute( 'rc_comment' ), |
| 72 |
'timestamp' => (int)wfTimestamp( TS_UNIX, $rc->getAttribute( 'rc_timestamp' ) ), |
| 73 |
'user' => $rc->getAttribute( 'rc_user_text' ), |
| 74 |
'bot' => (bool)$rc->getAttribute( 'rc_bot' ), |
| 75 |
'notify_url' => $rc->getNotifyUrl(), |
| 76 |
]; |
| 77 |
|
| 78 |
if ( isset( $feed['channel'] ) ) { |
| 79 |
$packet['channel'] = $feed['channel']; |
| 80 |
} |
| 81 |
|
| 82 |
$type = $rc->getAttribute( 'rc_type' ); |
| 83 |
if ( $type == RC_EDIT || $type == RC_NEW ) { |
| 84 |
$useRCPatrol = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseRCPatrol ); |
| 85 |
$useNPPatrol = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseNPPatrol ); |
| 86 |
$packet['minor'] = (bool)$rc->getAttribute( 'rc_minor' ); |
| 87 |
if ( $useRCPatrol || ( $type == RC_NEW && $useNPPatrol ) ) { |
| 88 |
$packet['patrolled'] = (bool)$rc->getAttribute( 'rc_patrolled' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
switch ( $type ) { |
| 93 |
case RC_EDIT: |
| 94 |
$packet['length'] = [ |
| 95 |
'old' => $rc->getAttribute( 'rc_old_len' ), |
| 96 |
'new' => $rc->getAttribute( 'rc_new_len' ) |
| 97 |
]; |
| 98 |
$packet['revision'] = [ |
| 99 |
'old' => $rc->getAttribute( 'rc_last_oldid' ), |
| 100 |
'new' => $rc->getAttribute( 'rc_this_oldid' ) |
| 101 |
]; |
| 102 |
break; |
| 103 |
|
| 104 |
case RC_NEW: |
| 105 |
$packet['length'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_new_len' ) ]; |
| 106 |
$packet['revision'] = [ 'old' => null, 'new' => $rc->getAttribute( 'rc_this_oldid' ) ]; |
| 107 |
break; |
| 108 |
|
| 109 |
case RC_LOG: |
| 110 |
$packet['log_id'] = $rc->getAttribute( 'rc_logid' ); |
| 111 |
$packet['log_type'] = $rc->getAttribute( 'rc_log_type' ); |
| 112 |
$packet['log_action'] = $rc->getAttribute( 'rc_log_action' ); |
| 113 |
if ( $rc->getAttribute( 'rc_params' ) ) { |
| 114 |
$params = $rc->parseParams(); |
| 115 |
if ( |
| 116 |
|
| 117 |
$rc->getAttribute( 'rc_params' ) == serialize( false ) || |
| 118 |
|
| 119 |
$params !== false |
| 120 |
) { |
| 121 |
|
| 122 |
$logParams = []; |
| 123 |
|
| 124 |
foreach ( $params as $key => $value ) { |
| 125 |
if ( strpos( $key, ':' ) === false ) { |
| 126 |
$logParams[$key] = $value; |
| 127 |
continue; |
| 128 |
} |
| 129 |
$logParam = explode( ':', $key, 3 ); |
| 130 |
$logParams[$logParam[2]] = $value; |
| 131 |
} |
| 132 |
$packet['log_params'] = $logParams; |
| 133 |
} else { |
| 134 |
$packet['log_params'] = explode( "\n", $rc->getAttribute( 'rc_params' ) ); |
| 135 |
} |
| 136 |
} |
| 137 |
$packet['log_action_comment'] = $actionComment; |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
$packet['server_url'] = $canonicalServer; |
| 142 |
$packet['server_name'] = $serverName; |
| 143 |
|
| 144 |
$packet['server_script_path'] = $scriptPath ?: '/'; |
| 145 |
$packet['wiki'] = WikiMap::getCurrentWikiId(); |
| 146 |
|
| 147 |
return $this->formatArray( $packet ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
class_alias( MachineReadableRCFeedFormatter::class, 'MachineReadableRCFeedFormatter' ); |