Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreditsSourceAction
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 getName
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onView
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getCredits
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\CreditSource;
4
5use FormlessAction;
6use MediaWiki\Html\Html;
7
8class CreditsSourceAction extends FormlessAction {
9    /**
10     * @return string
11     */
12    public function getName() {
13        return 'credits';
14    }
15
16    /**
17     * @return string
18     */
19    protected function getDescription() {
20        return $this->msg( 'creditssource-creditpage' )->escaped();
21    }
22
23    /**
24     * @return string
25     */
26    public function onView() {
27        if ( $this->getWikiPage()->getID() == 0 ) {
28            $content = $this->msg( 'nocredits' )->parse();
29        } else {
30            global $wgMaxCredits, $wgShowCreditsIfMax;
31            $content = $this->getCredits(
32                $wgMaxCredits, $wgShowCreditsIfMax
33            );
34        }
35
36        return Html::rawElement( 'div', [ 'id' => 'mw-credits' ], $content );
37    }
38
39    /**
40     * @param int $maxCredits The max amount of credits to display
41     * @param bool $showCreditsIfMax Credit only the top authors if there are too many
42     * @return string
43     */
44    public function getCredits( $maxCredits, $showCreditsIfMax = true ) {
45        $maxCredits = $showCreditsIfMax ? $maxCredits : 9999999;
46
47        $return = '';
48        $pageId = $this->getTitle()->getArticleID();
49        $sourceWorks = SimpleSourceWork::newFromPageId( $pageId, $maxCredits );
50        $lang = $this->getLanguage();
51        $user = $this->getUser();
52
53        foreach ( $sourceWorks as $source ) {
54            // This is safe, since we don't allow writing to the swsite tables. If that
55            // changes in the future, mSiteShortName will need to be escaped here.
56            $return .= wfMessage( 'creditssource-credits' )->params(
57                // source link (absolute) url + title
58                $source->mUri,
59                $source->mTitle,
60                // site link (absolute) url + title
61                $source->mSiteUri,
62                $source->mSiteName,
63                // history link (absolute) url
64                $this->getTitle()->getFullURL( [ 'action' => 'history' ] ),
65
66                $source->mSiteShortName,
67                $lang->userTimeAndDate( $source->mTs, $user ),
68                $lang->userDate( $source->mTs, $user ),
69                $lang->userTime( $source->mTs, $user )
70            )->parse();
71        }
72
73        return $return;
74    }
75}