Add a way for packagers to override some installation details
[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 $this->namespaces = MWNamespace::getContentNamespaces();
37 parent::__construct( $name );
38 }
39
40 public function getNamespaces() {
41 return $this->namespaces;
42 }
43
44 public function setNamespace ( $ns ) {
45 if( !$ns || $ns < NS_MAIN ) {
46 $ns = NS_MAIN;
47 }
48 $this->namespaces = array( $ns );
49 }
50
51 // select redirects instead of normal pages?
52 public function isRedirect(){
53 return $this->isRedir;
54 }
55
56 public function execute( $par ) {
57 global $wgContLang;
58
59 if ( $par ) {
60 $this->setNamespace( $wgContLang->getNsIndex( $par ) );
61 }
62
63 $title = $this->getRandomTitle();
64
65 if( is_null( $title ) ) {
66 $this->setHeaders();
67 $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
68 $this->getNsList(), count( $this->namespaces ) );
69 return;
70 }
71
72 $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
73 $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
74 unset( $query['title'] );
75 $this->getOutput()->redirect( $title->getFullUrl( $query ) );
76 }
77
78 /**
79 * Get a comma-delimited list of namespaces we don't have
80 * any pages in
81 * @return String
82 */
83 private function getNsList() {
84 global $wgContLang;
85 $nsNames = array();
86 foreach( $this->namespaces as $n ) {
87 if( $n === NS_MAIN ) {
88 $nsNames[] = $this->msg( 'blanknamespace' )->plain();
89 } else {
90 $nsNames[] = $wgContLang->getNsText( $n );
91 }
92 }
93 return $wgContLang->commaList( $nsNames );
94 }
95
96 /**
97 * Choose a random title.
98 * @return Title object (or null if nothing to choose from)
99 */
100 public function getRandomTitle() {
101 $randstr = wfRandom();
102 $title = null;
103 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces,
104 &$this->extra, &$title ) ) ) {
105 return $title;
106 }
107 $row = $this->selectRandomPageFromDB( $randstr );
108
109 /* If we picked a value that was higher than any in
110 * the DB, wrap around and select the page with the
111 * lowest value instead! One might think this would
112 * skew the distribution, but in fact it won't cause
113 * any more bias than what the page_random scheme
114 * causes anyway. Trust me, I'm a mathematician. :)
115 */
116 if( !$row ) {
117 $row = $this->selectRandomPageFromDB( "0" );
118 }
119
120 if( $row ) {
121 return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
122 } else {
123 return null;
124 }
125 }
126
127 protected function getQueryInfo( $randstr ) {
128 $redirect = $this->isRedirect() ? 1 : 0;
129
130 return array(
131 'tables' => array( 'page' ),
132 'fields' => array( 'page_title', 'page_namespace' ),
133 'conds' => array_merge( array(
134 'page_namespace' => $this->namespaces,
135 'page_is_redirect' => $redirect,
136 'page_random >= ' . $randstr
137 ), $this->extra ),
138 'options' => array(
139 'ORDER BY' => 'page_random',
140 'USE INDEX' => 'page_random',
141 'LIMIT' => 1,
142 ),
143 'join_conds' => array()
144 );
145 }
146
147 private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
148 $dbr = wfGetDB( DB_SLAVE );
149
150 $query = $this->getQueryInfo( $randstr );
151 $res = $dbr->select(
152 $query['tables'],
153 $query['fields'],
154 $query['conds'],
155 $fname,
156 $query['options'],
157 $query['join_conds']
158 );
159
160 return $dbr->fetchObject( $res );
161 }
162 }