Follow-up r81401: fix callback, apparently it was the other way around than I thought.
[lhc/web/wiklou.git] / includes / specials / SpecialRandompage.php
1 <?php
2 /**
3 * Implements Special:Randompage
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Rob Church <robchur@gmail.com>, Ilmari Karonen
23 */
24
25 /**
26 * Special page to direct the user to a random page
27 *
28 * @ingroup SpecialPage
29 */
30 class RandomPage extends SpecialPage {
31 private $namespaces; // namespaces to select pages from
32 protected $isRedir = false; // should the result be a redirect?
33 protected $extra = array(); // Extra SQL statements
34
35 public function __construct( $name = 'Randompage' ){
36 global $wgContentNamespaces;
37 $this->namespaces = $wgContentNamespaces;
38 parent::__construct( $name );
39 }
40
41 public function getNamespaces() {
42 return $this->namespaces;
43 }
44
45 public function setNamespace ( $ns ) {
46 if( !$ns || $ns < NS_MAIN ) {
47 $ns = NS_MAIN;
48 }
49 $this->namespaces = array( $ns );
50 }
51
52 // select redirects instead of normal pages?
53 public function isRedirect(){
54 return $this->isRedir;
55 }
56
57 public function execute( $par ) {
58 global $wgOut, $wgContLang, $wgRequest;
59
60 if ($par) {
61 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
62 }
63
64 $title = $this->getRandomTitle();
65
66 if( is_null( $title ) ) {
67 $this->setHeaders();
68 $wgOut->addWikiMsg( strtolower( $this->mName ) . '-nopages',
69 $this->getNsList(), count( $this->namespaces ) );
70 return;
71 }
72
73 $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
74 $query = array_merge( $wgRequest->getValues(), $redirectParam );
75 unset( $query['title'] );
76 $wgOut->redirect( $title->getFullUrl( $query ) );
77 }
78
79 /**
80 * Get a comma-delimited list of namespaces we don't have
81 * any pages in
82 * @return String
83 */
84 private function getNsList() {
85 global $wgContLang;
86 $nsNames = array();
87 foreach( $this->namespaces as $n ) {
88 if( $n === NS_MAIN ) {
89 $nsNames[] = wfMsgForContent( 'blanknamespace' );
90 } else {
91 $nsNames[] = $wgContLang->getNsText( $n );
92 }
93 }
94 return $wgContLang->commaList( $nsNames );
95 }
96
97 /**
98 * Choose a random title.
99 * @return Title object (or null if nothing to choose from)
100 */
101 public function getRandomTitle() {
102 $randstr = wfRandom();
103 $title = null;
104 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces,
105 &$this->extra, &$title ) ) ) {
106 return $title;
107 }
108 $row = $this->selectRandomPageFromDB( $randstr );
109
110 /* If we picked a value that was higher than any in
111 * the DB, wrap around and select the page with the
112 * lowest value instead! One might think this would
113 * skew the distribution, but in fact it won't cause
114 * any more bias than what the page_random scheme
115 * causes anyway. Trust me, I'm a mathematician. :)
116 */
117 if( !$row ) {
118 $row = $this->selectRandomPageFromDB( "0" );
119 }
120
121 if( $row ) {
122 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
123 } else {
124 return null;
125 }
126 }
127
128 protected function getQueryInfo( $randstr ) {
129 global $wgExtraRandompageSQL;
130 $redirect = $this->isRedirect() ? 1 : 0;
131
132 if ( $wgExtraRandompageSQL ) {
133 $this->extra[] = $wgExtraRandompageSQL;
134 }
135 if ( $this->addExtraSQL() ) {
136 $this->extra[] = $this->addExtraSQL();
137 }
138
139 return array(
140 'tables' => array( 'page' ),
141 'fields' => array( 'page_title', 'page_namespace' ),
142 'conds' => array_merge( array(
143 'page_namespace' => $this->namespaces,
144 'page_is_redirect' => $redirect,
145 'page_random >= ' . $randstr
146 ), $this->extra ),
147 'options' => array(
148 'ORDER BY' => 'page_random',
149 'USE INDEX' => 'page_random',
150 'LIMIT' => 1,
151 ),
152 'join_conds' => array()
153 );
154 }
155
156 private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
157 $dbr = wfGetDB( DB_SLAVE );
158
159 $query = $this->getQueryInfo( $randstr );
160 $res = $dbr->select(
161 $query['tables'],
162 $query['fields'],
163 $query['conds'],
164 $fname,
165 $query['options'],
166 $query['join_conds']
167 );
168
169 return $dbr->fetchObject( $res );
170 }
171
172 /* an alternative to $wgExtraRandompageSQL so subclasses
173 * can add their own SQL by overriding this function
174 * @deprecated, append to $this->extra instead
175 */
176 public function addExtraSQL() {
177 return '';
178 }
179 }