4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 * Formats a notification into the JSON format (http://www.json.org)
28 class JSONRCFeedFormatter
implements RCFeedFormatter
{
30 * Generates a notification that can be easily interpreted by a machine.
31 * @see RCFeedFormatter::getLine
33 public function getLine( array $feed, RecentChange
$rc, $actionComment ) {
34 global $wgCanonicalServer, $wgScriptPath, $wgDBname;
35 $attrib = $rc->getAttributes();
38 // Usually, RC ID is exposed only for patrolling purposes,
39 // but there is no real reason not to expose it in other cases,
40 // and I can see how this may be potentially useful for clients.
41 'id' => $attrib['rc_id'],
42 'type' => $attrib['rc_type'],
43 'namespace' => $rc->getTitle()->getNamespace(),
44 'title' => $rc->getTitle()->getPrefixedText(),
45 'comment' => $attrib['rc_comment'],
46 'timestamp' => (int)wfTimestamp( TS_UNIX
, $attrib['rc_timestamp'] ),
47 'user' => $attrib['rc_user_text'],
48 'bot' => (bool)$attrib['rc_bot'],
51 if ( isset( $feed['channel'] ) ) {
52 $packet['channel'] = $feed['channel'];
55 $type = $attrib['rc_type'];
56 if ( $type == RC_EDIT ||
$type == RC_NEW
) {
57 global $wgUseRCPatrol, $wgUseNPPatrol;
59 $packet['minor'] = $attrib['rc_minor'];
60 if ( $wgUseRCPatrol ||
( $type == RC_NEW
&& $wgUseNPPatrol ) ) {
61 $packet['patrolled'] = $attrib['rc_patrolled'];
67 $packet['length'] = array( 'old' => $attrib['rc_old_len'], 'new' => $attrib['rc_new_len'] );
68 $packet['revision'] = array( 'old' => $attrib['rc_last_oldid'], 'new' => $attrib['rc_this_oldid'] );
72 $packet['length'] = array( 'old' => null, 'new' => $attrib['rc_new_len'] );
73 $packet['revision'] = array( 'old' => null, 'new' => $attrib['rc_this_oldid'] );
77 $packet['log_type'] = $attrib['rc_log_type'];
78 $packet['log_action'] = $attrib['rc_log_action'];
79 if ( $attrib['rc_params'] ) {
81 $params = unserialize( $attrib['rc_params'] );
84 // If it's an actual serialised false...
85 $attrib['rc_params'] == serialize( false ) ||
86 // Or if we did not get false back when trying to unserialise
89 // From ApiQueryLogEvents::addLogParams
91 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
92 foreach ( $params as $key => $value ) {
93 if ( strpos( $key, ':' ) === false ) {
94 $logParams[$key] = $value;
97 $logParam = explode( ':', $key, 3 );
98 $logParams[$logParam[2]] = $value;
100 $packet['log_params'] = $logParams;
102 $packet['log_params'] = explode( "\n", $attrib['rc_params'] );
105 $packet['log_action_comment'] = $actionComment;
109 $packet['server_url'] = $wgCanonicalServer;
110 $packet['server_script_path'] = $wgScriptPath ?
: '/';
111 $packet['wiki'] = $wgDBname;
113 return FormatJson
::encode( $packet );