Same for $this->user
[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( $db, $res ) {
73 $batch = new LinkBatch();
74 while( $row = $db->fetchObject( $res ) ) {
75 $batch->add( $row->namespace, $row->title );
76 }
77 $batch->execute();
78 if( $db->numRows( $res ) > 0 )
79 $db->dataSeek( $res, 0 );
80 }
81
82 /**
83 * Format a result row
84 *
85 * @param Skin $skin Skin to use for UI elements
86 * @param object $result Result row
87 * @return string
88 */
89 public function formatResult( $skin, $result ) {
90 $title = Title::makeTitleSafe( $result->namespace, $result->title );
91 if( $title instanceof Title ) {
92 return wfSpecialList(
93 $skin->makeLinkObj( $title ),
94 $this->makeWlhLink( $title, $skin, $result )
95 );
96 } else {
97 $tsafe = htmlspecialchars( $result->title );
98 return "Invalid title in result set; {$tsafe}";
99 }
100 }
101
102 /**
103 * Make a "what links here" link for a given title
104 *
105 * @param Title $title Title to make the link for
106 * @param Skin $skin Skin to use
107 * @param object $result Result row
108 * @return string
109 */
110 private function makeWlhLink( $title, $skin, $result ) {
111 global $wgLang;
112 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
113 $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
114 $wgLang->formatNum( $result->value ) );
115 return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
116 }
117 }
118
119 /**
120 * Execution function
121 *
122 * @param mixed $par Parameters passed to the page
123 */
124 function wfSpecialMostlinkedtemplates( $par = false ) {
125 list( $limit, $offset ) = wfCheckLimits();
126 $mlt = new SpecialMostlinkedtemplates();
127 $mlt->doQuery( $offset, $limit );
128 }