* Port tests from t/inc/
[mediawiki.git] / includes / specials / SpecialMostlinked.php
blob173015997b78b18b1b06d032b189ae62b6b3f8f9
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
7 /**
8 * A special page to show pages ordered by the number of pages linking to them.
9 * Implements Special:Mostlinked
11 * @ingroup SpecialPage
13 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
14 * @author Rob Church <robchur@gmail.com>
15 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
16 * @copyright © 2006 Rob Church
17 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
19 class MostlinkedPage extends QueryPage {
21 function getName() { return 'Mostlinked'; }
22 function isExpensive() { return true; }
23 function isSyndicated() { return false; }
25 function getSQL() {
26 global $wgMiserMode;
28 $dbr = wfGetDB( DB_SLAVE );
30 # In miser mode, reduce the query cost by adding a threshold for large wikis
31 if ( $wgMiserMode ) {
32 $numPages = SiteStats::pages();
33 if ( $numPages > 10000 ) {
34 $cutoff = 100;
35 } elseif ( $numPages > 100 ) {
36 $cutoff = intval( sqrt( $numPages ) );
37 } else {
38 $cutoff = 1;
40 } else {
41 $cutoff = 1;
44 list( $pagelinks, $page ) = $dbr->tableNamesN( 'pagelinks', 'page' );
45 return
46 "SELECT 'Mostlinked' AS type,
47 pl_namespace AS namespace,
48 pl_title AS title,
49 COUNT(*) AS value
50 FROM $pagelinks
51 LEFT JOIN $page ON pl_namespace=page_namespace AND pl_title=page_title
52 GROUP BY pl_namespace, pl_title
53 HAVING COUNT(*) > $cutoff";
56 /**
57 * Pre-fill the link cache
59 function preprocessResults( $db, $res ) {
60 if( $db->numRows( $res ) > 0 ) {
61 $linkBatch = new LinkBatch();
62 while( $row = $db->fetchObject( $res ) )
63 $linkBatch->add( $row->namespace, $row->title );
64 $db->dataSeek( $res, 0 );
65 $linkBatch->execute();
69 /**
70 * Make a link to "what links here" for the specified title
72 * @param $title Title being queried
73 * @param $skin Skin to use
74 * @return string
76 function makeWlhLink( &$title, $caption, &$skin ) {
77 $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedDBkey() );
78 return $skin->linkKnown( $wlh, $caption );
81 /**
82 * Make links to the page corresponding to the item, and the "what links here" page for it
84 * @param $skin Skin to be used
85 * @param $result Result row
86 * @return string
88 function formatResult( $skin, $result ) {
89 global $wgLang;
90 $title = Title::makeTitleSafe( $result->namespace, $result->title );
91 if ( !$title ) {
92 return '<!-- ' . htmlspecialchars( "Invalid title: [[$title]]" ) . ' -->';
94 $link = $skin->link( $title );
95 $wlh = $this->makeWlhLink( $title,
96 wfMsgExt( 'nlinks', array( 'parsemag', 'escape'),
97 $wgLang->formatNum( $result->value ) ), $skin );
98 return wfSpecialList( $link, $wlh );
103 * constructor
105 function wfSpecialMostlinked() {
106 list( $limit, $offset ) = wfCheckLimits();
108 $wpp = new MostlinkedPage();
110 $wpp->doQuery( $offset, $limit );