Debian Codegraph @

includes/recentchanges/RCFeed/MachineReadableRCFeedFormatter.php

raw symbols SCIP
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 namespace MediaWiki\RCFeed;
23
24 use MediaWiki\MainConfigNames;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\WikiMap\WikiMap;
27 use RecentChange;
28
29 /**
30 * Abstract class so there can be multiple formatters outputting the same data
31 *
32 * @since 1.23
33 * @ingroup RecentChanges
34 */
35 abstract class MachineReadableRCFeedFormatter implements RCFeedFormatter {
36
37 /**
38 * Take the packet and return the formatted string
39 *
40 * @param array $packet
41 *
42 * @return string
43 */
44 abstract protected function formatArray( array $packet );
45
46 /**
47 * Generates a notification that can be easily interpreted by a machine.
48 *
49 * @see RCFeedFormatter::getLine
50 *
51 * @param array $feed
52 * @param RecentChange $rc
53 * @param string|null $actionComment
54 *
55 * @return string|null
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 // Usually, RC ID is exposed only for patrolling purposes,
64 // but there is no real reason not to expose it in other cases,
65 // and I can see how this may be potentially useful for clients.
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 // If it's an actual serialised false...
117 $rc->getAttribute( 'rc_params' ) == serialize( false ) ||
118 // Or if we did not get false back when trying to unserialise
119 $params !== false
120 ) {
121 // From ApiQueryLogEvents::addLogParams
122 $logParams = [];
123 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
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 /** @deprecated class alias since 1.43 */
151 class_alias( MachineReadableRCFeedFormatter::class, 'MachineReadableRCFeedFormatter' );