make Special:Random retry (once, but more carefully) if the chosen random offset...
[lhc/web/wiklou.git] / includes / SpecialRandompage.php
1 <?php
2
3 /**
4 * Special page to direct the user to a random page
5 *
6 * @addtogroup SpecialPage
7 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
8 * @license GNU General Public Licence 2.0 or later
9 */
10
11 /**
12 * Main execution point
13 * @param $par Namespace to select the page from
14 */
15 function wfSpecialRandompage( $par = null ) {
16 global $wgOut, $wgContLang;
17
18 $rnd = new RandomPage();
19 $rnd->setNamespace( $wgContLang->getNsIndex( $par ) );
20 $rnd->setRedirect( false );
21
22 $title = $rnd->getRandomTitle();
23
24 if( is_null( $title ) ) {
25 $wgOut->addWikiText( wfMsg( 'randompage-nopages' ) );
26 return;
27 }
28
29 $wgOut->reportTime();
30 $wgOut->redirect( $title->getFullUrl() );
31 }
32
33
34 class RandomPage {
35 private $namespace = NS_MAIN; // namespace to select pages from
36 private $redirect = false; // select redirects instead of normal pages?
37
38 public function getNamespace ( ) {
39 return $this->namespace;
40 }
41 public function setNamespace ( $ns ) {
42 if( $ns < NS_MAIN ) $ns = NS_MAIN;
43 $this->namespace = $ns;
44 }
45 public function getRedirect ( ) {
46 return $this->redirect;
47 }
48 public function setRedirect ( $redirect ) {
49 $this->redirect = $redirect;
50 }
51
52 /**
53 * Choose a random title.
54 * @return Title object (or null if nothing to choose from)
55 */
56 public function getRandomTitle ( ) {
57 $randstr = wfRandom();
58 $row = $this->selectRandomPageFromDB( $randstr );
59
60 if( !$row ) {
61 // Try again with a normalized value
62 $randstr = wfRandom( $this->getMaxPageRandom() );
63 $row = $this->selectRandomPageFromDB( $randstr );
64 }
65
66 if( $row )
67 return Title::makeTitleSafe( $this->namespace, $row->page_title );
68 else
69 return null;
70 }
71
72 private function selectRandomPageFromDB ( $randstr ) {
73 global $wgExtraRandompageSQL;
74 $fname = 'RandomPage::selectRandomPageFromDB';
75
76 $dbr = wfGetDB( DB_SLAVE );
77
78 $from = $this->getSQLFrom( $dbr );
79 $where = $this->getSQLWhere( $dbr );
80
81 $sql = "SELECT page_title FROM $from
82 WHERE $where AND page_random > $randstr
83 ORDER BY page_random";
84
85 $sql = $dbr->limitResult( $sql, 1, 0 );
86 $res = $dbr->query( $sql, $fname );
87 return $dbr->fetchObject( $res );
88 }
89
90 private function getMaxPageRandom () {
91 $fname = 'RandomPage::getMaxPageRandom';
92
93 $dbr = wfGetDB( DB_SLAVE );
94
95 $from = $this->getSQLFrom( $dbr );
96 $where = $this->getSQLWhere( $dbr );
97
98 $sql = "SELECT MAX(page_random) AS max FROM $from WHERE $where";
99
100 $sql = $dbr->limitResult( $sql, 1, 0 );
101 $res = $dbr->query( $sql, $fname );
102 $row = $dbr->fetchObject( $res );
103
104 return $row ? $row->max : 0;
105 }
106
107 private function getSQLFrom ( $dbr ) {
108 $use_index = $dbr->useIndexClause( 'page_random' );
109 $page = $dbr->tableName( 'page' );
110 return "$page $use_index";
111 }
112
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;
119 }
120 }
121
122 ?>