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