]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialRandompage.php
MediaWiki 1.15.4-scripts
[autoinstallsdev/mediawiki.git] / includes / specials / SpecialRandompage.php
1 <?php
2
3 /**
4  * Special page to direct the user to a random page
5  *
6  * @ingroup SpecialPage
7  * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
8  * @license GNU General Public Licence 2.0 or later
9  */
10 class RandomPage extends SpecialPage {
11         private $namespaces;  // namespaces to select pages from
12
13         function __construct( $name = 'Randompage' ){
14                 global $wgContentNamespaces;
15
16                 $this->namespaces = $wgContentNamespaces;
17
18                 parent::__construct( $name );
19         }
20
21         public function getNamespaces() {
22                 return $this->namespaces;
23         }
24
25         public function setNamespace ( $ns ) {
26                 if( !$ns || $ns < NS_MAIN ) $ns = NS_MAIN;
27                 $this->namespaces = array( $ns );
28         }
29
30         // select redirects instead of normal pages?
31         // Overriden by SpecialRandomredirect
32         public function isRedirect(){
33                 return false;
34         }
35
36         public function execute( $par ) {
37                 global $wgOut, $wgContLang;
38
39                 if ($par)
40                         $this->setNamespace( $wgContLang->getNsIndex( $par ) );
41
42                 $title = $this->getRandomTitle();
43
44                 if( is_null( $title ) ) {
45                         $this->setHeaders();
46                         $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages',  $wgContLang->getNsText( $this->namespace ) );
47                         return;
48                 }
49
50                 $query = $this->isRedirect() ? 'redirect=no' : '';
51                 $wgOut->redirect( $title->getFullUrl( $query ) );
52         }
53
54
55         /**
56          * Choose a random title.
57          * @return Title object (or null if nothing to choose from)
58          */
59         public function getRandomTitle() {
60                 $randstr = wfRandom();
61                 $row = $this->selectRandomPageFromDB( $randstr );
62
63                 /* If we picked a value that was higher than any in
64                  * the DB, wrap around and select the page with the
65                  * lowest value instead!  One might think this would
66                  * skew the distribution, but in fact it won't cause
67                  * any more bias than what the page_random scheme
68                  * causes anyway.  Trust me, I'm a mathematician. :)
69                  */
70                 if( !$row )
71                         $row = $this->selectRandomPageFromDB( "0" );
72
73                 if( $row )
74                         return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
75                 else
76                         return null;
77         }
78
79         private function selectRandomPageFromDB( $randstr ) {
80                 global $wgExtraRandompageSQL;
81                 $fname = 'RandomPage::selectRandomPageFromDB';
82
83                 $dbr = wfGetDB( DB_SLAVE );
84
85                 $use_index = $dbr->useIndexClause( 'page_random' );
86                 $page = $dbr->tableName( 'page' );
87
88                 $ns = implode( ",", $this->namespaces );
89                 $redirect = $this->isRedirect() ? 1 : 0;
90
91                 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
92                 $sql = "SELECT page_title, page_namespace
93                         FROM $page $use_index
94                         WHERE page_namespace IN ( $ns )
95                         AND page_is_redirect = $redirect
96                         AND page_random >= $randstr
97                         $extra
98                         ORDER BY page_random";
99
100                 $sql = $dbr->limitResult( $sql, 1, 0 );
101                 $res = $dbr->query( $sql, $fname );
102                 return $dbr->fetchObject( $res );
103         }
104 }