]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/SpecialRandompage.php
MediaWiki 1.16.1-scripts
[autoinstallsdev/mediawiki.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;
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                 $query = $this->isRedirect() ? 'redirect=no' : '';
52                 $wgOut->redirect( $title->getFullUrl( $query ) );
53         }
54
55         /**
56          * Get a comma-delimited list of namespaces we don't have
57          * any pages in
58          * @return String
59          */
60         private function getNsList() {
61                 global $wgContLang;
62                 $nsNames = array();
63                 foreach( $this->namespaces as $n ) {
64                         if( $n === NS_MAIN )
65                                 $nsNames[] = wfMsgForContent( 'blanknamespace' );
66                         else
67                                 $nsNames[] = $wgContLang->getNsText( $n );
68                 }
69                 return $wgContLang->commaList( $nsNames );
70         }
71
72
73         /**
74          * Choose a random title.
75          * @return Title object (or null if nothing to choose from)
76          */
77         public function getRandomTitle() {
78                 $randstr = wfRandom();
79                 $title = null;
80                 if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) ) ) {
81                         return $title;
82                 }
83                 $row = $this->selectRandomPageFromDB( $randstr );
84
85                 /* If we picked a value that was higher than any in
86                  * the DB, wrap around and select the page with the
87                  * lowest value instead!  One might think this would
88                  * skew the distribution, but in fact it won't cause
89                  * any more bias than what the page_random scheme
90                  * causes anyway.  Trust me, I'm a mathematician. :)
91                  */
92                 if( !$row )
93                         $row = $this->selectRandomPageFromDB( "0" );
94
95                 if( $row )
96                         return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
97                 else
98                         return null;
99         }
100
101         private function selectRandomPageFromDB( $randstr ) {
102                 global $wgExtraRandompageSQL;
103                 $dbr = wfGetDB( DB_SLAVE );
104
105                 $use_index = $dbr->useIndexClause( 'page_random' );
106                 $page = $dbr->tableName( 'page' );
107
108                 $ns = implode( ",", $this->namespaces );
109                 $redirect = $this->isRedirect() ? 1 : 0;
110                 
111                 if ( $wgExtraRandompageSQL ) {
112                         $this->extra[] = $wgExtraRandompageSQL;
113                 }
114                 if ( $this->addExtraSQL() ) {
115                         $this->extra[] = $this->addExtraSQL();
116                 }
117                 $extra = '';
118                 if ( $this->extra ) {
119                         $extra = 'AND (' . implode( ') AND (', $this->extra ) . ')';
120                 }
121                 $sql = "SELECT page_title, page_namespace
122                         FROM $page $use_index
123                         WHERE page_namespace IN ( $ns )
124                         AND page_is_redirect = $redirect
125                         AND page_random >= $randstr
126                         $extra
127                         ORDER BY page_random";
128
129                 $sql = $dbr->limitResult( $sql, 1, 0 );
130                 $res = $dbr->query( $sql, __METHOD__ );
131                 return $dbr->fetchObject( $res );
132         }
133
134         /* an alternative to $wgExtraRandompageSQL so subclasses
135          * can add their own SQL by overriding this function
136          * @deprecated, append to $this->extra instead
137          */
138         public function addExtraSQL() {
139                 return '';
140         }
141 }