Merge "Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups()."
[lhc/web/wiklou.git] / includes / specials / SpecialMostlinkedtemplates.php
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
25 /**
26 * Special page lists templates with a large number of
27 * transclusion links, i.e. "most used" templates
28 *
29 * @ingroup SpecialPage
30 */
31 class MostlinkedTemplatesPage extends QueryPage {
32
33 function __construct( $name = 'Mostlinkedtemplates' ) {
34 parent::__construct( $name );
35 }
36
37 /**
38 * Is this report expensive, i.e should it be cached?
39 *
40 * @return Boolean
41 */
42 public function isExpensive() {
43 return true;
44 }
45
46 /**
47 * Is there a feed available?
48 *
49 * @return Boolean
50 */
51 public function isSyndicated() {
52 return false;
53 }
54
55 /**
56 * Sort the results in descending order?
57 *
58 * @return Boolean
59 */
60 public function sortDescending() {
61 return true;
62 }
63
64 public function getQueryInfo() {
65 return array (
66 'tables' => array ( 'templatelinks' ),
67 'fields' => array ( 'namespace' => 'tl_namespace',
68 'title' => 'tl_title',
69 'value' => 'COUNT(*)' ),
70 'conds' => array ( 'tl_namespace' => NS_TEMPLATE ),
71 'options' => array( 'GROUP BY' => array( 'tl_namespace', 'tl_title' ) )
72 );
73 }
74
75 /**
76 * Pre-cache page existence to speed up link generation
77 *
78 * @param $db DatabaseBase connection
79 * @param $res ResultWrapper
80 */
81 public function preprocessResults( $db, $res ) {
82 if ( !$res->numRows() ) {
83 return;
84 }
85
86 $batch = new LinkBatch();
87 foreach ( $res as $row ) {
88 $batch->add( $row->namespace, $row->title );
89 }
90 $batch->execute();
91
92 $res->seek( 0 );
93 }
94
95 /**
96 * Format a result row
97 *
98 * @param $skin Skin to use for UI elements
99 * @param $result Result row
100 * @return String
101 */
102 public function formatResult( $skin, $result ) {
103 $title = Title::makeTitleSafe( $result->namespace, $result->title );
104 if ( !$title ) {
105 return Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
106 Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
107 }
108
109 return $this->getLanguage()->specialList(
110 Linker::link( $title ),
111 $this->makeWlhLink( $title, $result )
112 );
113 }
114
115 /**
116 * Make a "what links here" link for a given title
117 *
118 * @param $title Title to make the link for
119 * @param $result Result row
120 * @return String
121 */
122 private function makeWlhLink( $title, $result ) {
123 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
124 $label = $this->msg( 'ntransclusions' )->numParams( $result->value )->escaped();
125 return Linker::link( $wlh, $label );
126 }
127 }
128