Merge "Add CASCADINGSOURCES parser function"
[lhc/web/wiklou.git] / includes / rcfeed / JSONRCFeedFormatter.php
1 <?php
2
3 /**
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.
8 *
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.
13 *
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
18 *
19 * @file
20 */
21
22 /**
23 * Formats a notification into the JSON format (http://www.json.org)
24 *
25 * @since 1.22
26 */
27
28 class JSONRCFeedFormatter implements RCFeedFormatter {
29 /**
30 * Generates a notification that can be easily interpreted by a machine.
31 * @see RCFeedFormatter::getLine
32 */
33 public function getLine( array $feed, RecentChange $rc, $actionComment ) {
34 global $wgCanonicalServer, $wgScriptPath, $wgDBname;
35 $attrib = $rc->getAttributes();
36
37 $packet = array(
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'],
49 );
50
51 if ( isset( $feed['channel'] ) ) {
52 $packet['channel'] = $feed['channel'];
53 }
54
55 $type = $attrib['rc_type'];
56 if ( $type == RC_EDIT || $type == RC_NEW ) {
57 global $wgUseRCPatrol, $wgUseNPPatrol;
58
59 $packet['minor'] = $attrib['rc_minor'];
60 if ( $wgUseRCPatrol || ( $type == RC_NEW && $wgUseNPPatrol ) ) {
61 $packet['patrolled'] = $attrib['rc_patrolled'];
62 }
63 }
64
65 switch ( $type ) {
66 case RC_EDIT:
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'] );
69 break;
70
71 case RC_NEW:
72 $packet['length'] = array( 'old' => null, 'new' => $attrib['rc_new_len'] );
73 $packet['revision'] = array( 'old' => null, 'new' => $attrib['rc_this_oldid'] );
74 break;
75
76 case RC_LOG:
77 $packet['log_type'] = $attrib['rc_log_type'];
78 $packet['log_action'] = $attrib['rc_log_action'];
79 if ( $attrib['rc_params'] ) {
80 wfSuppressWarnings();
81 $params = unserialize( $attrib['rc_params'] );
82 wfRestoreWarnings();
83 if (
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
87 $params !== false
88 ) {
89 // From ApiQueryLogEvents::addLogParams
90 $logParams = array();
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;
95 continue;
96 }
97 $logParam = explode( ':', $key, 3 );
98 $logParams[$logParam[2]] = $value;
99 }
100 $packet['log_params'] = $logParams;
101 } else {
102 $packet['log_params'] = explode( "\n", $attrib['rc_params'] );
103 }
104 }
105 $packet['log_action_comment'] = $actionComment;
106 break;
107 }
108
109 $packet['server_url'] = $wgCanonicalServer;
110 $packet['server_script_path'] = $wgScriptPath ?: '/';
111 $packet['wiki'] = $wgDBname;
112
113 return FormatJson::encode( $packet );
114 }
115 }