]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/specials/SpecialMostlinkedtemplates.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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 SpecialMostlinkedtemplates extends QueryPage {
32
33         /**
34          * Name of the report
35          *
36          * @return String
37          */
38         public function getName() {
39                 return 'Mostlinkedtemplates';
40         }
41
42         /**
43          * Is this report expensive, i.e should it be cached?
44          *
45          * @return Boolean
46          */
47         public function isExpensive() {
48                 return true;
49         }
50
51         /**
52          * Is there a feed available?
53          *
54          * @return Boolean
55          */
56         public function isSyndicated() {
57                 return false;
58         }
59
60         /**
61          * Sort the results in descending order?
62          *
63          * @return Boolean
64          */
65         public function sortDescending() {
66                 return true;
67         }
68
69         /**
70          * Generate SQL for the report
71          *
72          * @return String
73          */
74         public function getSql() {
75                 $dbr = wfGetDB( DB_SLAVE );
76                 $templatelinks = $dbr->tableName( 'templatelinks' );
77                 $name = $dbr->addQuotes( $this->getName() );
78                 return "SELECT {$name} AS type,
79                         " . NS_TEMPLATE . " AS namespace,
80                         tl_title AS title,
81                         COUNT(*) AS value
82                         FROM {$templatelinks}
83                         WHERE tl_namespace = " . NS_TEMPLATE . "
84                         GROUP BY tl_title";
85         }
86
87         /**
88          * Pre-cache page existence to speed up link generation
89          *
90          * @param $db Database connection
91          * @param $res ResultWrapper
92          */
93         public function preprocessResults( $db, $res ) {
94                 $batch = new LinkBatch();
95                 foreach ( $res as $row ) {
96                         $batch->add( $row->namespace, $row->title );
97                 }
98                 $batch->execute();
99                 if( $db->numRows( $res ) > 0 )
100                         $db->dataSeek( $res, 0 );
101         }
102
103         /**
104          * Format a result row
105          *
106          * @param $skin Skin to use for UI elements
107          * @param $result Result row
108          * @return String
109          */
110         public function formatResult( $skin, $result ) {
111                 $title = Title::makeTitleSafe( $result->namespace, $result->title );
112
113                 return wfSpecialList(
114                         $skin->link( $title ),
115                         $this->makeWlhLink( $title, $skin, $result )
116                 );
117         }
118
119         /**
120          * Make a "what links here" link for a given title
121          *
122          * @param $title Title to make the link for
123          * @param $skin Skin to use
124          * @param $result Result row
125          * @return String
126          */
127         private function makeWlhLink( $title, $skin, $result ) {
128                 global $wgLang;
129                 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
130                 $label = wfMsgExt( 'ntransclusions', array( 'parsemag', 'escape' ),
131                         $wgLang->formatNum( $result->value ) );
132                 return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
133         }
134 }
135
136 /**
137  * Execution function
138  *
139  * @param $par Mixed: parameters passed to the page
140  */
141 function wfSpecialMostlinkedtemplates( $par = false ) {
142         list( $limit, $offset ) = wfCheckLimits();
143         $mlt = new SpecialMostlinkedtemplates();
144         $mlt->doQuery( $offset, $limit );
145 }