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