Rename all the special page class files back to their proper names.
[lhc/web/wiklou.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 $namespace = NS_MAIN; // namespace to select pages from
12
13 function __construct( $name = 'Randompage' ){
14 parent::__construct( $name );
15 }
16
17 public function getNamespace() {
18 return $this->namespace;
19 }
20
21 public function setNamespace ( $ns ) {
22 if( $ns < NS_MAIN ) $ns = NS_MAIN;
23 $this->namespace = $ns;
24 }
25
26 // select redirects instead of normal pages?
27 // Overriden by SpecialRandomredirect
28 public function isRedirect(){
29 return false;
30 }
31
32 public function execute( $par ) {
33 global $wgOut, $wgContLang;
34
35 if ($par)
36 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
37
38 $title = $this->getRandomTitle();
39
40 if( is_null( $title ) ) {
41 $this->setHeaders();
42 $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages' );
43 return;
44 }
45
46 $query = $this->isRedirect() ? 'redirect=no' : '';
47 $wgOut->redirect( $title->getFullUrl( $query ) );
48 }
49
50
51 /**
52 * Choose a random title.
53 * @return Title object (or null if nothing to choose from)
54 */
55 public function getRandomTitle() {
56 $randstr = wfRandom();
57 $row = $this->selectRandomPageFromDB( $randstr );
58
59 /* If we picked a value that was higher than any in
60 * the DB, wrap around and select the page with the
61 * lowest value instead! One might think this would
62 * skew the distribution, but in fact it won't cause
63 * any more bias than what the page_random scheme
64 * causes anyway. Trust me, I'm a mathematician. :)
65 */
66 if( !$row )
67 $row = $this->selectRandomPageFromDB( "0" );
68
69 if( $row )
70 return Title::makeTitleSafe( $this->namespace, $row->page_title );
71 else
72 return null;
73 }
74
75 private function selectRandomPageFromDB( $randstr ) {
76 global $wgExtraRandompageSQL;
77 $fname = 'RandomPage::selectRandomPageFromDB';
78
79 $dbr = wfGetDB( DB_SLAVE );
80
81 $use_index = $dbr->useIndexClause( 'page_random' );
82 $page = $dbr->tableName( 'page' );
83
84 $ns = (int) $this->namespace;
85 $redirect = $this->isRedirect() ? 1 : 0;
86
87 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
88 $sql = "SELECT page_title
89 FROM $page $use_index
90 WHERE page_namespace = $ns
91 AND page_is_redirect = $redirect
92 AND page_random >= $randstr
93 $extra
94 ORDER BY page_random";
95
96 $sql = $dbr->limitResult( $sql, 1, 0 );
97 $res = $dbr->query( $sql, $fname );
98 return $dbr->fetchObject( $res );
99 }
100 }