fix syntax error
[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 $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
43 $title = $this->getRandomTitle();
44
45 if( is_null( $title ) ) {
46 $this->setHeaders();
47 $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages', $wgContLang->getNsText( $this->namespace ) );
48 return;
49 }
50
51 $query = $this->isRedirect() ? 'redirect=no' : '';
52 $wgOut->redirect( $title->getFullUrl( $query ) );
53 }
54
55
56 /**
57 * Choose a random title.
58 * @return Title object (or null if nothing to choose from)
59 */
60 public function getRandomTitle() {
61 $randstr = wfRandom();
62 $row = $this->selectRandomPageFromDB( $randstr );
63
64 /* If we picked a value that was higher than any in
65 * the DB, wrap around and select the page with the
66 * lowest value instead! One might think this would
67 * skew the distribution, but in fact it won't cause
68 * any more bias than what the page_random scheme
69 * causes anyway. Trust me, I'm a mathematician. :)
70 */
71 if( !$row )
72 $row = $this->selectRandomPageFromDB( "0" );
73
74 if( $row )
75 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
76 else
77 return null;
78 }
79
80 private function selectRandomPageFromDB( $randstr ) {
81 global $wgExtraRandompageSQL;
82 $dbr = wfGetDB( DB_SLAVE );
83
84 $use_index = $dbr->useIndexClause( 'page_random' );
85 $page = $dbr->tableName( 'page' );
86
87 $ns = implode( ",", $this->namespaces );
88 $redirect = $this->isRedirect() ? 1 : 0;
89
90 $extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : "";
91 $extra .= $this->addExtraSQL() ? "AND (".$this->addExtraSQL().")" : "";
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, __METHOD__ );
102 return $dbr->fetchObject( $res );
103 }
104
105 // an alternative to $wgExtraRandompageSQL so extensions
106 // can add their own SQL by overriding this function
107 public function addExtraSQL() {
108 return '';
109 }
110 }