[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 <[email protected]>, 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 ( is_string( $par ) ) { 60 // Testing for stringiness since we want to catch 61 // the empty string to mean main namespace only. 62 $this->setNamespace( $wgContLang->getNsIndex( $par ) ); 63 } 64 65 $title = $this->getRandomTitle(); 66 67 if ( is_null( $title ) ) { 68 $this->setHeaders(); 69 // Message: randompage-nopages, randomredirect-nopages 70 $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages', 71 $this->getNsList(), count( $this->namespaces ) ); 72 73 return; 74 } 75 76 $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array(); 77 $query = array_merge( $this->getRequest()->getValues(), $redirectParam ); 78 unset( $query['title'] ); 79 $this->getOutput()->redirect( $title->getFullURL( $query ) ); 80 } 81 82 /** 83 * Get a comma-delimited list of namespaces we don't have 84 * any pages in 85 * @return string 86 */ 87 private function getNsList() { 88 global $wgContLang; 89 $nsNames = array(); 90 foreach ( $this->namespaces as $n ) { 91 if ( $n === NS_MAIN ) { 92 $nsNames[] = $this->msg( 'blanknamespace' )->plain(); 93 } else { 94 $nsNames[] = $wgContLang->getNsText( $n ); 95 } 96 } 97 98 return $wgContLang->commaList( $nsNames ); 99 } 100 101 /** 102 * Choose a random title. 103 * @return Title|null Title object (or null if nothing to choose from) 104 */ 105 public function getRandomTitle() { 106 $randstr = wfRandom(); 107 $title = null; 108 109 if ( !wfRunHooks( 110 'SpecialRandomGetRandomTitle', 111 array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ) 112 ) ) { 113 return $title; 114 } 115 116 $row = $this->selectRandomPageFromDB( $randstr ); 117 118 /* If we picked a value that was higher than any in 119 * the DB, wrap around and select the page with the 120 * lowest value instead! One might think this would 121 * skew the distribution, but in fact it won't cause 122 * any more bias than what the page_random scheme 123 * causes anyway. Trust me, I'm a mathematician. :) 124 */ 125 if ( !$row ) { 126 $row = $this->selectRandomPageFromDB( "0" ); 127 } 128 129 if ( $row ) { 130 return Title::makeTitleSafe( $row->page_namespace, $row->page_title ); 131 } 132 133 return null; 134 } 135 136 protected function getQueryInfo( $randstr ) { 137 $redirect = $this->isRedirect() ? 1 : 0; 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 'LIMIT' => 1, 150 ), 151 'join_conds' => array() 152 ); 153 } 154 155 private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) { 156 $dbr = wfGetDB( DB_SLAVE ); 157 158 $query = $this->getQueryInfo( $randstr ); 159 $res = $dbr->select( 160 $query['tables'], 161 $query['fields'], 162 $query['conds'], 163 $fname, 164 $query['options'], 165 $query['join_conds'] 166 ); 167 168 return $dbr->fetchObject( $res ); 169 } 170 171 protected function getGroupName() { 172 return 'redirects'; 173 } 174 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |