]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialMostlinked.php
MediaWiki 1.17.3-scripts
[autoinstallsdev/mediawiki.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                         foreach ( $res as $row ) {
77                                 $linkBatch->add( $row->namespace, $row->title );
78                         }
79                         $db->dataSeek( $res, 0 );
80                         $linkBatch->execute();
81                 }
82         }
83
84         /**
85          * Make a link to "what links here" for the specified title
86          *
87          * @param $title Title being queried
88          * @param $caption String: text to display on the link
89          * @param $skin Skin to use
90          * @return String
91          */
92         function makeWlhLink( &$title, $caption, &$skin ) {
93                 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedDBkey() );
94                 return $skin->linkKnown( $wlh, $caption );
95         }
96
97         /**
98          * Make links to the page corresponding to the item, and the "what links here" page for it
99          *
100          * @param $skin Skin to be used
101          * @param $result Result row
102          * @return string
103          */
104         function formatResult( $skin, $result ) {
105                 global $wgLang;
106                 $title = Title::makeTitleSafe( $result->namespace, $result->title );
107                 if ( !$title ) {
108                         return '<!-- ' . htmlspecialchars( "Invalid title: [[$title]]" ) . ' -->';
109                 }
110                 $link = $skin->link( $title );
111                 $wlh = $this->makeWlhLink( $title,
112                         wfMsgExt( 'nlinks', array( 'parsemag', 'escape'),
113                                 $wgLang->formatNum( $result->value ) ), $skin );
114                 return wfSpecialList( $link, $wlh );
115         }
116 }
117
118 /**
119  * constructor
120  */
121 function wfSpecialMostlinked() {
122         list( $limit, $offset ) = wfCheckLimits();
123
124         $wpp = new MostlinkedPage();
125
126         $wpp->doQuery( $offset, $limit );
127 }