Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangesFeed
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 3
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeedObject
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 buildItems
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
210
1<?php
2/**
3 * Feed for list of changes.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23use MediaWiki\Feed\ChannelFeed;
24use MediaWiki\Feed\FeedItem;
25use MediaWiki\Feed\FeedUtils;
26use MediaWiki\MainConfigNames;
27use MediaWiki\MediaWikiServices;
28use MediaWiki\Revision\RevisionRecord;
29use MediaWiki\Title\Title;
30use Wikimedia\Rdbms\IResultWrapper;
31
32/**
33 * Feed to Special:RecentChanges and Special:RecentChangesLinked.
34 *
35 * @ingroup Feed
36 */
37class ChangesFeed {
38    private $format;
39
40    /**
41     * @param string $format Feed's format (either 'rss' or 'atom')
42     */
43    public function __construct( $format ) {
44        $this->format = $format;
45    }
46
47    /**
48     * Get a MediaWiki\Feed\ChannelFeed subclass object to use
49     *
50     * @param string $title Feed's title
51     * @param string $description Feed's description
52     * @param string $url Url of origin page
53     * @return ChannelFeed|bool MediaWiki\Feed\ChannelFeed subclass or false on failure
54     */
55    public function getFeedObject( $title, $description, $url ) {
56        $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
57        $sitename = $mainConfig->get( MainConfigNames::Sitename );
58        $languageCode = $mainConfig->get( MainConfigNames::LanguageCode );
59        $feedClasses = $mainConfig->get( MainConfigNames::FeedClasses );
60        if ( !isset( $feedClasses[$this->format] ) ) {
61            return false;
62        }
63
64        if ( !array_key_exists( $this->format, $feedClasses ) ) {
65            // falling back to atom
66            $this->format = 'atom';
67        }
68
69        $feedTitle = "{$sitename}  - {$title} [{$languageCode}]";
70        return new $feedClasses[$this->format](
71            $feedTitle, htmlspecialchars( $description ), $url );
72    }
73
74    /**
75     * Generate the feed items given a row from the database.
76     * @param IResultWrapper $rows IDatabase resource with recentchanges rows
77     * @return array
78     * @suppress PhanTypeInvalidDimOffset False positives in the foreach
79     */
80    public static function buildItems( $rows ) {
81        $items = [];
82
83        # Merge adjacent edits by one user
84        $sorted = [];
85        $n = 0;
86        foreach ( $rows as $obj ) {
87            if ( $obj->rc_type == RC_EXTERNAL ) {
88                continue;
89            }
90
91            if ( $n > 0 &&
92                $obj->rc_type == RC_EDIT &&
93                $obj->rc_namespace >= 0 &&
94                $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
95                $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
96                $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
97            } else {
98                $sorted[$n] = $obj;
99                $n++;
100            }
101        }
102
103        $services = MediaWikiServices::getInstance();
104        $commentFormatter = $services->getRowCommentFormatter();
105        $formattedComments = $commentFormatter->formatItems(
106            $commentFormatter->rows( $rows )
107                ->commentKey( 'rc_comment' )
108                ->indexField( 'rc_id' )
109        );
110
111        $nsInfo = $services->getNamespaceInfo();
112        foreach ( $sorted as $obj ) {
113            $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
114            $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) && $title->canExist()
115                ? $title->getTalkPage()->getFullURL()
116                : '';
117
118            // Skip items with deleted content (avoids partially complete/inconsistent output)
119            if ( $obj->rc_deleted ) {
120                continue;
121            }
122
123            if ( $obj->rc_this_oldid ) {
124                $url = $title->getFullURL( [
125                    'diff' => $obj->rc_this_oldid,
126                    'oldid' => $obj->rc_last_oldid,
127                ] );
128            } else {
129                // log entry or something like that.
130                $url = $title->getFullURL();
131            }
132
133            $items[] = new FeedItem(
134                $title->getPrefixedText(),
135                FeedUtils::formatDiff( $obj, $formattedComments[$obj->rc_id] ),
136                $url,
137                $obj->rc_timestamp,
138                ( $obj->rc_deleted & RevisionRecord::DELETED_USER )
139                    ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
140                $talkpage
141            );
142        }
143
144        return $items;
145    }
146}