Add a new searchmenu-new-nocreate message
[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 protected $isRedir = false; // should the result be a redirect?
13 protected $extra = array(); // Extra SQL statements
14
15 public function __construct( $name = 'Randompage' ){
16 global $wgContentNamespaces;
17 $this->namespaces = $wgContentNamespaces;
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 public function isRedirect(){
32 return $this->isRedir;
33 }
34
35 public function execute( $par ) {
36 global $wgOut, $wgContLang, $wgRequest;
37
38 if ($par) {
39 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
40 }
41
42 $title = $this->getRandomTitle();
43
44 if( is_null( $title ) ) {
45 $this->setHeaders();
46 $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages',
47 $this->getNsList(), count( $this->namespaces ) );
48 return;
49 }
50
51 $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
52 $query = array_merge( $wgRequest->getValues(), $redirectParam );
53 unset( $query['title'] );
54 $wgOut->redirect( $title->getFullUrl( $query ) );
55 }
56
57 /**
58 * Get a comma-delimited list of namespaces we don't have
59 * any pages in
60 * @return String
61 */
62 private function getNsList() {
63 global $wgContLang;
64 $nsNames = array();
65 foreach( $this->namespaces as $n ) {
66 if( $n === NS_MAIN )
67 $nsNames[] = wfMsgForContent( 'blanknamespace' );
68 else
69 $nsNames[] = $wgContLang->getNsText( $n );
70 }
71 return $wgContLang->commaList( $nsNames );
72 }
73
74
75 /**
76 * Choose a random title.
77 * @return Title object (or null if nothing to choose from)
78 */
79 public function getRandomTitle() {
80 $randstr = wfRandom();
81 $title = null;
82 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) {
83 return $title;
84 }
85 $row = $this->selectRandomPageFromDB( $randstr );
86
87 /* If we picked a value that was higher than any in
88 * the DB, wrap around and select the page with the
89 * lowest value instead! One might think this would
90 * skew the distribution, but in fact it won't cause
91 * any more bias than what the page_random scheme
92 * causes anyway. Trust me, I'm a mathematician. :)
93 */
94 if( !$row )
95 $row = $this->selectRandomPageFromDB( "0" );
96
97 if( $row )
98 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
99 else
100 return null;
101 }
102
103 private function selectRandomPageFromDB( $randstr ) {
104 global $wgExtraRandompageSQL;
105 $dbr = wfGetDB( DB_SLAVE );
106
107 $use_index = $dbr->useIndexClause( 'page_random' );
108 $page = $dbr->tableName( 'page' );
109
110 $ns = implode( ",", $this->namespaces );
111 $redirect = $this->isRedirect() ? 1 : 0;
112
113 if ( $wgExtraRandompageSQL ) {
114 $this->extra[] = $wgExtraRandompageSQL;
115 }
116 if ( $this->addExtraSQL() ) {
117 $this->extra[] = $this->addExtraSQL();
118 }
119 $extra = '';
120 if ( $this->extra ) {
121 $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')';
122 }
123 $sql = "SELECT page_title, page_namespace
124 FROM $page $use_index
125 WHERE page_namespace IN ( $ns )
126 AND page_is_redirect = $redirect
127 AND page_random >= $randstr
128 $extra
129 ORDER BY page_random";
130
131 $sql = $dbr->limitResult( $sql, 1, 0 );
132 $res = $dbr->query( $sql, __METHOD__ );
133 return $dbr->fetchObject( $res );
134 }
135
136 /* an alternative to $wgExtraRandompageSQL so subclasses
137 * can add their own SQL by overriding this function
138 * @deprecated, append to $this->extra instead
139 */
140 public function addExtraSQL() {
141 return '';
142 }
143 }