More cleanup from r22859
[mediawiki.git] / includes / SpecialMostlinkedtemplates.php
blob874cc23c3074d610b8b9f52a6ac42d3994bf5b73
1 <?php
3 /**
4 * Special page lists templates with a large number of
5 * transclusion links, i.e. "most used" templates
7 * @addtogroup SpecialPage
8 * @author Rob Church <robchur@gmail.com>
9 */
10 class SpecialMostlinkedtemplates extends QueryPage {
12 /**
13 * Name of the report
15 * @return string
17 public function getName() {
18 return 'Mostlinkedtemplates';
21 /**
22 * Is this report expensive, i.e should it be cached?
24 * @return bool
26 public function isExpensive() {
27 return true;
30 /**
31 * Is there a feed available?
33 * @return bool
35 public function isSyndicated() {
36 return false;
39 /**
40 * Sort the results in descending order?
42 * @return bool
44 public function sortDescending() {
45 return true;
48 /**
49 * Generate SQL for the report
51 * @return string
53 public function getSql() {
54 $dbr = wfGetDB( DB_SLAVE );
55 $templatelinks = $dbr->tableName( 'templatelinks' );
56 $name = $dbr->addQuotes( $this->getName() );
57 return "SELECT {$name} AS type,
58 " . NS_TEMPLATE . " AS namespace,
59 tl_title AS title,
60 COUNT(*) AS value
61 FROM {$templatelinks}
62 WHERE tl_namespace = " . NS_TEMPLATE . "
63 GROUP BY 1, 2, 3";
66 /**
67 * Pre-cache page existence to speed up link generation
69 * @param Database $dbr Database connection
70 * @param int $res Result pointer
72 public function preprocessResults( $dbr, $res ) {
73 $batch = new LinkBatch();
74 while( $row = $dbr->fetchObject( $res ) ) {
75 $title = Title::makeTitleSafe( $row->namespace, $row->title );
76 $batch->addObj( $title );
78 $batch->execute();
79 if( $dbr->numRows( $res ) > 0 )
80 $dbr->dataSeek( $res, 0 );
83 /**
84 * Format a result row
86 * @param Skin $skin Skin to use for UI elements
87 * @param object $result Result row
88 * @return string
90 public function formatResult( $skin, $result ) {
91 $title = Title::makeTitleSafe( $result->namespace, $result->title );
92 if( $title instanceof Title ) {
93 return wfSpecialList(
94 $skin->makeLinkObj( $title ),
95 $this->makeWlhLink( $title, $skin, $result )
97 } else {
98 $tsafe = htmlspecialchars( $result->title );
99 return "Invalid title in result set; {$tsafe}";
104 * Make a "what links here" link for a given title
106 * @param Title $title Title to make the link for
107 * @param Skin $skin Skin to use
108 * @param object $result Result row
109 * @return string
111 private function makeWlhLink( $title, $skin, $result ) {
112 global $wgLang;
113 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
114 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
115 $wgLang->formatNum( $result->value ) );
116 return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
122 * Execution function
124 * @param mixed $par Parameters passed to the page
126 function wfSpecialMostlinkedtemplates( $par = false ) {
127 list( $limit, $offset ) = wfCheckLimits();
128 $mlt = new SpecialMostlinkedtemplates();
129 $mlt->doQuery( $offset, $limit );