4 * Special page to direct the user to a random page
6 * @addtogroup SpecialPage
7 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
8 * @license GNU General Public Licence 2.0 or later
12 * Main execution point
13 * @param $par Namespace to select the page from
15 function wfSpecialRandompage( $par = null ) {
16 global $wgOut, $wgContLang;
18 $rnd = new RandomPage();
19 $rnd->setNamespace( $wgContLang->getNsIndex( $par ) );
20 $rnd->setRedirect( false );
22 $title = $rnd->getRandomTitle();
24 if( is_null( $title ) ) {
25 $wgOut->addWikiText( wfMsg( 'randompage-nopages' ) );
30 $wgOut->redirect( $title->getFullUrl() );
35 private $namespace = NS_MAIN
; // namespace to select pages from
36 private $redirect = false; // select redirects instead of normal pages?
38 public function getNamespace ( ) {
39 return $this->namespace;
41 public function setNamespace ( $ns ) {
42 if( $ns < NS_MAIN
) $ns = NS_MAIN
;
43 $this->namespace = $ns;
45 public function getRedirect ( ) {
46 return $this->redirect
;
48 public function setRedirect ( $redirect ) {
49 $this->redirect
= $redirect;
53 * Choose a random title.
54 * @return Title object (or null if nothing to choose from)
56 public function getRandomTitle ( ) {
57 $randstr = wfRandom();
58 $row = $this->selectRandomPageFromDB( $randstr );
61 // Try again with a normalized value
62 $randstr = wfRandom( $this->getMaxPageRandom() );
63 $row = $this->selectRandomPageFromDB( $randstr );
67 return Title
::makeTitleSafe( $this->namespace, $row->page_title
);
72 private function selectRandomPageFromDB ( $randstr ) {
73 global $wgExtraRandompageSQL;
74 $fname = 'RandomPage::selectRandomPageFromDB';
76 $dbr = wfGetDB( DB_SLAVE
);
78 $from = $this->getSQLFrom( $dbr );
79 $where = $this->getSQLWhere( $dbr );
81 $sql = "SELECT page_title FROM $from
82 WHERE $where AND page_random > $randstr
83 ORDER BY page_random";
85 $sql = $dbr->limitResult( $sql, 1, 0 );
86 $res = $dbr->query( $sql, $fname );
87 return $dbr->fetchObject( $res );
90 private function getMaxPageRandom () {
91 $fname = 'RandomPage::getMaxPageRandom';
93 $dbr = wfGetDB( DB_SLAVE
);
95 $from = $this->getSQLFrom( $dbr );
96 $where = $this->getSQLWhere( $dbr );
98 $sql = "SELECT MAX(page_random) AS max FROM $from WHERE $where";
100 $sql = $dbr->limitResult( $sql, 1, 0 );
101 $res = $dbr->query( $sql, $fname );
102 $row = $dbr->fetchObject( $res );
104 return $row ?
$row->max
: 0;
107 private function getSQLFrom ( $dbr ) {
108 $use_index = $dbr->useIndexClause( 'page_random' );
109 $page = $dbr->tableName( 'page' );
110 return "$page $use_index";
113 private function getSQLWhere ( $dbr ) {
114 global $wgExtraRandompageSQL;
115 $ns = (int) $this->namespace;
116 $redirect = $this->redirect ?
1 : 0;
117 $extra = $wgExtraRandompageSQL ?
" AND ($wgExtraRandompageSQL)" : "";
118 return "page_namespace = $ns AND page_is_redirect = $redirect" . $extra;