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 * Abstract class so there can be multiple formatters outputting the same data
27 abstract class MachineReadableRCFeedFormatter
implements RCFeedFormatter
{
30 * Take the packet and return the formatted string
31 * @param array $packet
34 abstract protected function formatArray( array $packet );
37 * Generates a notification that can be easily interpreted by a machine.
38 * @see RCFeedFormatter::getLine
40 public function getLine( array $feed, RecentChange
$rc, $actionComment ) {
41 global $wgCanonicalServer, $wgServerName, $wgScriptPath;
42 $attrib = $rc->getAttributes();
45 // Usually, RC ID is exposed only for patrolling purposes,
46 // but there is no real reason not to expose it in other cases,
47 // and I can see how this may be potentially useful for clients.
48 'id' => $attrib['rc_id'],
49 'type' => RecentChange
::parseFromRCType( $attrib['rc_type'] ),
50 'namespace' => $rc->getTitle()->getNamespace(),
51 'title' => $rc->getTitle()->getPrefixedText(),
52 'comment' => $attrib['rc_comment'],
53 'timestamp' => (int)wfTimestamp( TS_UNIX
, $attrib['rc_timestamp'] ),
54 'user' => $attrib['rc_user_text'],
55 'bot' => (bool)$attrib['rc_bot'],
58 if ( isset( $feed['channel'] ) ) {
59 $packet['channel'] = $feed['channel'];
62 $type = $attrib['rc_type'];
63 if ( $type == RC_EDIT ||
$type == RC_NEW
) {
64 global $wgUseRCPatrol, $wgUseNPPatrol;
66 $packet['minor'] = (bool)$attrib['rc_minor'];
67 if ( $wgUseRCPatrol ||
( $type == RC_NEW
&& $wgUseNPPatrol ) ) {
68 $packet['patrolled'] = (bool)$attrib['rc_patrolled'];
74 $packet['length'] = array(
75 'old' => $attrib['rc_old_len'],
76 'new' => $attrib['rc_new_len']
78 $packet['revision'] = array(
79 'old' => $attrib['rc_last_oldid'],
80 'new' => $attrib['rc_this_oldid']
85 $packet['length'] = array( 'old' => null, 'new' => $attrib['rc_new_len'] );
86 $packet['revision'] = array( 'old' => null, 'new' => $attrib['rc_this_oldid'] );
90 $packet['log_id'] = $attrib['rc_logid'];
91 $packet['log_type'] = $attrib['rc_log_type'];
92 $packet['log_action'] = $attrib['rc_log_action'];
93 if ( $attrib['rc_params'] ) {
95 $params = unserialize( $attrib['rc_params'] );
98 // If it's an actual serialised false...
99 $attrib['rc_params'] == serialize( false ) ||
100 // Or if we did not get false back when trying to unserialise
103 // From ApiQueryLogEvents::addLogParams
104 $logParams = array();
105 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
106 foreach ( $params as $key => $value ) {
107 if ( strpos( $key, ':' ) === false ) {
108 $logParams[$key] = $value;
111 $logParam = explode( ':', $key, 3 );
112 $logParams[$logParam[2]] = $value;
114 $packet['log_params'] = $logParams;
116 $packet['log_params'] = explode( "\n", $attrib['rc_params'] );
119 $packet['log_action_comment'] = $actionComment;
123 $packet['server_url'] = $wgCanonicalServer;
124 $packet['server_name'] = $wgServerName;
126 $packet['server_script_path'] = $wgScriptPath ?
: '/';
127 $packet['wiki'] = wfWikiID();
129 return $this->formatArray( $packet );