4369ee4e7145c0fe88637c1c1dc205ed8a2cb5a2
[lhc/web/wiklou.git] / includes / specials / SpecialMostlinkedtemplates.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * @file
22 * @ingroup SpecialPage
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 * @author Rob Church <robchur@gmail.com>
31 */
32 class SpecialMostlinkedtemplates extends QueryPage {
33
34 /**
35 * Name of the report
36 *
37 * @return String
38 */
39 public function getName() {
40 return 'Mostlinkedtemplates';
41 }
42
43 /**
44 * Is this report expensive, i.e should it be cached?
45 *
46 * @return Boolean
47 */
48 public function isExpensive() {
49 return true;
50 }
51
52 /**
53 * Is there a feed available?
54 *
55 * @return Boolean
56 */
57 public function isSyndicated() {
58 return false;
59 }
60
61 /**
62 * Sort the results in descending order?
63 *
64 * @return Boolean
65 */
66 public function sortDescending() {
67 return true;
68 }
69
70 /**
71 * Generate SQL for the report
72 *
73 * @return String
74 */
75 public function getSql() {
76 $dbr = wfGetDB( DB_SLAVE );
77 $templatelinks = $dbr->tableName( 'templatelinks' );
78 $name = $dbr->addQuotes( $this->getName() );
79 return "SELECT {$name} AS type,
80 " . NS_TEMPLATE . " AS namespace,
81 tl_title AS title,
82 COUNT(*) AS value
83 FROM {$templatelinks}
84 WHERE tl_namespace = " . NS_TEMPLATE . "
85 GROUP BY tl_title";
86 }
87
88 /**
89 * Pre-cache page existence to speed up link generation
90 *
91 * @param $db Database connection
92 * @param $res ResultWrapper
93 */
94 public function preprocessResults( $db, $res ) {
95 $batch = new LinkBatch();
96 while( $row = $db->fetchObject( $res ) ) {
97 $batch->add( $row->namespace, $row->title );
98 }
99 $batch->execute();
100 if( $db->numRows( $res ) > 0 )
101 $db->dataSeek( $res, 0 );
102 }
103
104 /**
105 * Format a result row
106 *
107 * @param $skin Skin to use for UI elements
108 * @param $result Result row
109 * @return String
110 */
111 public function formatResult( $skin, $result ) {
112 $title = Title::makeTitleSafe( $result->namespace, $result->title );
113
114 return wfSpecialList(
115 $skin->link( $title ),
116 $this->makeWlhLink( $title, $skin, $result )
117 );
118 }
119
120 /**
121 * Make a "what links here" link for a given title
122 *
123 * @param $title Title to make the link for
124 * @param $skin Skin to use
125 * @param $result Result row
126 * @return String
127 */
128 private function makeWlhLink( $title, $skin, $result ) {
129 global $wgLang;
130 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
131 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
132 $wgLang->formatNum( $result->value ) );
133 return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
134 }
135 }
136
137 /**
138 * Execution function
139 *
140 * @param $par Mixed: parameters passed to the page
141 */
142 function wfSpecialMostlinkedtemplates( $par = false ) {
143 list( $limit, $offset ) = wfCheckLimits();
144 $mlt = new SpecialMostlinkedtemplates();
145 $mlt->doQuery( $offset, $limit );
146 }