Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialMostLinkedTemplates
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 9
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 isExpensive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSyndicated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sortDescending
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getQueryInfo
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 preprocessResults
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatResult
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 makeWlhLink
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Implements Special:Mostlinkedtemplates
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 * @ingroup SpecialPage
22 * @author Rob Church <robchur@gmail.com>
23 */
24
25namespace MediaWiki\Specials;
26
27use MediaWiki\Cache\LinkBatchFactory;
28use MediaWiki\Html\Html;
29use MediaWiki\Linker\Linker;
30use MediaWiki\Linker\LinksMigration;
31use MediaWiki\SpecialPage\QueryPage;
32use MediaWiki\SpecialPage\SpecialPage;
33use MediaWiki\Title\Title;
34use Skin;
35use stdClass;
36use Wikimedia\Rdbms\IConnectionProvider;
37use Wikimedia\Rdbms\IDatabase;
38use Wikimedia\Rdbms\IResultWrapper;
39
40/**
41 * Special page lists templates with a large number of
42 * transclusion links, i.e. "most used" templates
43 *
44 * @ingroup SpecialPage
45 */
46class SpecialMostLinkedTemplates extends QueryPage {
47
48    private LinksMigration $linksMigration;
49
50    /**
51     * @param IConnectionProvider $dbProvider
52     * @param LinkBatchFactory $linkBatchFactory
53     * @param LinksMigration $linksMigration
54     */
55    public function __construct(
56        IConnectionProvider $dbProvider,
57        LinkBatchFactory $linkBatchFactory,
58        LinksMigration $linksMigration
59    ) {
60        parent::__construct( 'Mostlinkedtemplates' );
61        $this->setDatabaseProvider( $dbProvider );
62        $this->setLinkBatchFactory( $linkBatchFactory );
63        $this->linksMigration = $linksMigration;
64    }
65
66    /**
67     * Is this report expensive, i.e should it be cached?
68     *
69     * @return bool
70     */
71    public function isExpensive() {
72        return true;
73    }
74
75    /**
76     * Is there a feed available?
77     *
78     * @return bool
79     */
80    public function isSyndicated() {
81        return false;
82    }
83
84    /**
85     * Sort the results in descending order?
86     *
87     * @return bool
88     */
89    public function sortDescending() {
90        return true;
91    }
92
93    public function getQueryInfo() {
94        $queryInfo = $this->linksMigration->getQueryInfo( 'templatelinks' );
95        [ $ns, $title ] = $this->linksMigration->getTitleFields( 'templatelinks' );
96        return [
97            'tables' => $queryInfo['tables'],
98            'fields' => [
99                'namespace' => $ns,
100                'title' => $title,
101                'value' => 'COUNT(*)'
102            ],
103            'options' => [ 'GROUP BY' => [ $ns, $title ] ],
104            'join_conds' => $queryInfo['joins']
105        ];
106    }
107
108    /**
109     * Pre-cache page existence to speed up link generation
110     *
111     * @param IDatabase $db
112     * @param IResultWrapper $res
113     */
114    public function preprocessResults( $db, $res ) {
115        $this->executeLBFromResultWrapper( $res );
116    }
117
118    /**
119     * Format a result row
120     *
121     * @param Skin $skin
122     * @param stdClass $result Result row
123     * @return string
124     */
125    public function formatResult( $skin, $result ) {
126        $title = Title::makeTitleSafe( $result->namespace, $result->title );
127        if ( !$title ) {
128            return Html::element(
129                'span',
130                [ 'class' => 'mw-invalidtitle' ],
131                Linker::getInvalidTitleDescription(
132                    $this->getContext(),
133                    $result->namespace,
134                    $result->title
135                )
136            );
137        }
138
139        return $this->getLanguage()->specialList(
140            $this->getLinkRenderer()->makeLink( $title ),
141            $this->makeWlhLink( $title, $result )
142        );
143    }
144
145    /**
146     * Make a "what links here" link for a given title
147     *
148     * @param Title $title Title to make the link for
149     * @param stdClass $result Result row
150     * @return string
151     */
152    private function makeWlhLink( $title, $result ) {
153        $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
154        $label = $this->msg( 'ntransclusions' )->numParams( $result->value )->text();
155
156        return $this->getLinkRenderer()->makeLink( $wlh, $label );
157    }
158
159    protected function getGroupName() {
160        return 'highuse';
161    }
162}
163
164/**
165 * Retain the old class name for backwards compatibility.
166 * @deprecated since 1.41
167 */
168class_alias( SpecialMostLinkedTemplates::class, 'SpecialMostLinkedTemplates' );