Standardised file description headers; first path
[lhc/web/wiklou.git] / includes / specials / SpecialMostlinked.php
1 <?php
2 /**
3 * Implements Special:Mostlinked
4 *
5 * Copyright © 2005 Ævar Arnfjörð Bjarmason, 2006 Rob Church
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup SpecialPage
24 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
25 * @author Rob Church <robchur@gmail.com>
26 */
27
28 /**
29 * A special page to show pages ordered by the number of pages linking to them.
30 *
31 * @ingroup SpecialPage
32 */
33 class MostlinkedPage extends QueryPage {
34
35 function getName() { return 'Mostlinked'; }
36 function isExpensive() { return true; }
37 function isSyndicated() { return false; }
38
39 function getSQL() {
40 global $wgMiserMode;
41
42 $dbr = wfGetDB( DB_SLAVE );
43
44 # In miser mode, reduce the query cost by adding a threshold for large wikis
45 if ( $wgMiserMode ) {
46 $numPages = SiteStats::pages();
47 if ( $numPages > 10000 ) {
48 $cutoff = 100;
49 } elseif ( $numPages > 100 ) {
50 $cutoff = intval( sqrt( $numPages ) );
51 } else {
52 $cutoff = 1;
53 }
54 } else {
55 $cutoff = 1;
56 }
57
58 list( $pagelinks, $page ) = $dbr->tableNamesN( 'pagelinks', 'page' );
59 return
60 "SELECT 'Mostlinked' AS type,
61 pl_namespace AS namespace,
62 pl_title AS title,
63 COUNT(*) AS value
64 FROM $pagelinks
65 LEFT JOIN $page ON pl_namespace=page_namespace AND pl_title=page_title
66 GROUP BY pl_namespace, pl_title
67 HAVING COUNT(*) > $cutoff";
68 }
69
70 /**
71 * Pre-fill the link cache
72 */
73 function preprocessResults( $db, $res ) {
74 if( $db->numRows( $res ) > 0 ) {
75 $linkBatch = new LinkBatch();
76 while( $row = $db->fetchObject( $res ) )
77 $linkBatch->add( $row->namespace, $row->title );
78 $db->dataSeek( $res, 0 );
79 $linkBatch->execute();
80 }
81 }
82
83 /**
84 * Make a link to "what links here" for the specified title
85 *
86 * @param $title Title being queried
87 * @param $caption String: text to display on the link
88 * @param $skin Skin to use
89 * @return String
90 */
91 function makeWlhLink( &$title, $caption, &$skin ) {
92 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedDBkey() );
93 return $skin->linkKnown( $wlh, $caption );
94 }
95
96 /**
97 * Make links to the page corresponding to the item, and the "what links here" page for it
98 *
99 * @param $skin Skin to be used
100 * @param $result Result row
101 * @return string
102 */
103 function formatResult( $skin, $result ) {
104 global $wgLang;
105 $title = Title::makeTitleSafe( $result->namespace, $result->title );
106 if ( !$title ) {
107 return '<!-- ' . htmlspecialchars( "Invalid title: [[$title]]" ) . ' -->';
108 }
109 $link = $skin->link( $title );
110 $wlh = $this->makeWlhLink( $title,
111 wfMsgExt( 'nlinks', array( 'parsemag', 'escape'),
112 $wgLang->formatNum( $result->value ) ), $skin );
113 return wfSpecialList( $link, $wlh );
114 }
115 }
116
117 /**
118 * constructor
119 */
120 function wfSpecialMostlinked() {
121 list( $limit, $offset ) = wfCheckLimits();
122
123 $wpp = new MostlinkedPage();
124
125 $wpp->doQuery( $offset, $limit );
126 }