Cleanup
[lhc/web/wiklou.git] / includes / SpecialMostlinkedtemplates.php
1 <?php
2
3 /**
4 * Special page lists templates with a large number of
5 * transclusion links, i.e. "most used" templates
6 *
7 * @addtogroup SpecialPage
8 * @author Rob Church <robchur@gmail.com>
9 */
10 class SpecialMostlinkedtemplates extends QueryPage {
11
12 /**
13 * Name of the report
14 *
15 * @return string
16 */
17 public function getName() {
18 return 'Mostlinkedtemplates';
19 }
20
21 /**
22 * Is this report expensive, i.e should it be cached?
23 *
24 * @return bool
25 */
26 public function isExpensive() {
27 return true;
28 }
29
30 /**
31 * Is there a feed available?
32 *
33 * @return bool
34 */
35 public function isSyndicated() {
36 return false;
37 }
38
39 /**
40 * Sort the results in descending order?
41 *
42 * @return bool
43 */
44 public function sortDescending() {
45 return true;
46 }
47
48 /**
49 * Generate SQL for the report
50 *
51 * @return string
52 */
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";
64 }
65
66 /**
67 * Pre-cache page existence to speed up link generation
68 *
69 * @param Database $dbr Database connection
70 * @param int $res Result pointer
71 */
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 );
77 }
78 $batch->execute();
79 if( $dbr->numRows( $res ) > 0 )
80 $dbr->dataSeek( $res, 0 );
81 }
82
83 /**
84 * Format a result row
85 *
86 * @param Skin $skin Skin to use for UI elements
87 * @param object $result Result row
88 * @return string
89 */
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 )
96 );
97 } else {
98 $tsafe = htmlspecialchars( $result->title );
99 return "Invalid title in result set; {$tsafe}";
100 }
101 }
102
103 /**
104 * Make a "what links here" link for a given title
105 *
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
110 */
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() );
117 }
118 }
119
120 /**
121 * Execution function
122 *
123 * @param mixed $par Parameters passed to the page
124 */
125 function wfSpecialMostlinkedtemplates( $par = false ) {
126 list( $limit, $offset ) = wfCheckLimits();
127 $mlt = new SpecialMostlinkedtemplates();
128 $mlt->doQuery( $offset, $limit );
129 }