Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeedSMItem
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
 newFromFeedItem
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getLastMod
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getKeywords
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\GoogleNewsSitemap;
4
5use ApiMain;
6use InvalidArgumentException;
7use LogicException;
8use MediaWiki\Feed\FeedItem;
9use MediaWiki\Request\FauxRequest;
10use MediaWiki\Title\Title;
11
12/**
13 * FeedSMItem Class
14 *
15 * Base class for basic SiteMap support, for building url containers.
16 */
17class FeedSMItem extends FeedItem {
18
19    private $keywords = [];
20
21    /**
22     * @var Title
23     */
24    private $titleObj;
25
26    /**
27     * @param Title $title Title object that this entry is for.
28     * @param string $pubDate Publish date formattable by wfTimestamp.
29     * @param string[] $keywords list of keywords
30     * @param bool|int $comment Namespace containing comments page for entry.
31     *   True for the corresponding talk page of $title
32     *   False for none
33     *   An integer for the page name of $title in the specific namespace denoted by that integer.
34     */
35    public function __construct( $title, $pubDate, $keywords = [], $comment = true ) {
36        if ( !$title || !$title instanceof Title ) {
37            // Paranoia
38            throw new InvalidArgumentException( 'Invalid title object passed to FeedSMItem' );
39        }
40
41        $commentsURL = '';
42        if ( $comment === true && $title->canHaveTalkPage() ) {
43            // The comment ns is this article's talk namespace.
44            $commentsURL = $title->getTalkPage()->getCanonicalURL();
45        } elseif ( is_int( $comment ) ) {
46            // There's a specific comments namespace.
47            $commentsTitle = Title::makeTitle( $comment, $title->getDBkey() );
48            if ( $commentsTitle ) {
49                $commentsURL = $commentsTitle->getCanonicalURL();
50            }
51        }
52
53        $this->keywords = $keywords;
54        $this->titleObj = $title;
55
56        parent::__construct(
57            $title->getText(),
58            '', /* Description */
59            $title->getCanonicalURL(),
60            $pubDate,
61            '', /* Author */
62            $commentsURL
63        );
64    }
65
66    /**
67     * Convert a FeedItem to an FeedSMItem.
68     * This is to make sitemap feed get along with normal MediaWiki feeds.
69     * @param FeedItem $item Original item.
70     * @return FeedSMItem Converted item.
71     */
72    public static function newFromFeedItem( FeedItem $item ) {
73        // @todo FIXME: This is borked (esp. on history), but better than a fatal (not by much).
74        // maybe try and get title from url?
75        $title = Title::newFromText( $item->getTitle() );
76        if ( !$title ) {
77            throw new LogicException( 'Error getting title object from string in FeedItem.' );
78        }
79        $date = $item->getDate();
80        return new FeedSMItem( $title, $date );
81    }
82
83    public function getLastMod() {
84        return $this->titleObj->getTouched();
85    }
86
87    public function getKeywords() {
88        // Note, not using Language::commaList(), as this is for
89        // computers not humans, so we don't want to vary with
90        // language conventions.
91        return $this->xmlEncode( implode( ', ', $this->keywords ) );
92    }
93
94    /**
95     * Overrides parent class. Meant to be used in rss feed.
96     * Currently return the article, its debatable if thats a good idea
97     * or not, but not sure of what better to do. Could regex the wikitext
98     * and try to return the first paragraph, but thats iffy.
99     *
100     * Note, this is only called by the atom/rss feed output, not by
101     * the sitemap output.
102     * @return string
103     */
104    public function getDescription() {
105        // This is probably rather inefficient to do for several pages
106        // but not much worse than the rest of this extension.
107
108        $result = '';
109        $req = new FauxRequest( [
110            'action' => 'parse',
111            'page' => $this->titleObj->getPrefixedDBKey(),
112            'prop' => 'text',
113        ] );
114        $main = new ApiMain( $req );
115        $main->execute();
116        $data = $main->getResult()->getResultData( null, [ 'BC' => [] ] );
117
118        if ( isset( $data['parse']['text']['*'] ) ) {
119            $result = $this->xmlEncode(
120                $data['parse']['text']['*']
121            );
122        }
123        return $result;
124    }
125}