MediaWiki  1.23.13
SpecialRandompage.php
Go to the documentation of this file.
1 <?php
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' ) {
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 ) {
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  // Message: randompage-nopages, randomredirect-nopages
68  $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
69  $this->getNsList(), count( $this->namespaces ) );
70 
71  return;
72  }
73 
74  $redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
75  $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
76  unset( $query['title'] );
77  $this->getOutput()->redirect( $title->getFullURL( $query ) );
78  }
79 
85  private function getNsList() {
87  $nsNames = array();
88  foreach ( $this->namespaces as $n ) {
89  if ( $n === NS_MAIN ) {
90  $nsNames[] = $this->msg( 'blanknamespace' )->plain();
91  } else {
92  $nsNames[] = $wgContLang->getNsText( $n );
93  }
94  }
95 
96  return $wgContLang->commaList( $nsNames );
97  }
98 
103  public function getRandomTitle() {
104  $randstr = wfRandom();
105  $title = null;
106 
107  if ( !wfRunHooks(
108  'SpecialRandomGetRandomTitle',
109  array( &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title )
110  ) ) {
111  return $title;
112  }
113 
114  $row = $this->selectRandomPageFromDB( $randstr );
115 
116  /* If we picked a value that was higher than any in
117  * the DB, wrap around and select the page with the
118  * lowest value instead! One might think this would
119  * skew the distribution, but in fact it won't cause
120  * any more bias than what the page_random scheme
121  * causes anyway. Trust me, I'm a mathematician. :)
122  */
123  if ( !$row ) {
124  $row = $this->selectRandomPageFromDB( "0" );
125  }
126 
127  if ( $row ) {
128  return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
129  }
130 
131  return null;
132  }
133 
134  protected function getQueryInfo( $randstr ) {
135  $redirect = $this->isRedirect() ? 1 : 0;
136 
137  return array(
138  'tables' => array( 'page' ),
139  'fields' => array( 'page_title', 'page_namespace' ),
140  'conds' => array_merge( array(
141  'page_namespace' => $this->namespaces,
142  'page_is_redirect' => $redirect,
143  'page_random >= ' . $randstr
144  ), $this->extra ),
145  'options' => array(
146  'ORDER BY' => 'page_random',
147  'LIMIT' => 1,
148  ),
149  'join_conds' => array()
150  );
151  }
152 
153  private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
154  $dbr = wfGetDB( DB_SLAVE );
155 
156  $query = $this->getQueryInfo( $randstr );
157  $res = $dbr->select(
158  $query['tables'],
159  $query['fields'],
160  $query['conds'],
161  $fname,
162  $query['options'],
163  $query['join_conds']
164  );
165 
166  return $dbr->fetchObject( $res );
167  }
168 
169  protected function getGroupName() {
170  return 'redirects';
171  }
172 }
RandomPage\$isRedir
$isRedir
Definition: SpecialRandompage.php:32
RandomPage\execute
execute( $par)
Default execute method Checks user permissions, calls the function given in mFunction.
Definition: SpecialRandompage.php:56
RandomPage\setNamespace
setNamespace( $ns)
Definition: SpecialRandompage.php:44
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
RandomPage\isRedirect
isRedirect()
Definition: SpecialRandompage.php:52
$n
$n
Definition: RandomTest.php:76
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:35
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:139
MWNamespace\getContentNamespaces
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: Namespace.php:334
RandomPage\__construct
__construct( $name='Randompage')
Definition: SpecialRandompage.php:35
$dbr
$dbr
Definition: testCompression.php:48
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2677
RandomPage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialRandompage.php:169
RandomPage\selectRandomPageFromDB
selectRandomPageFromDB( $randstr, $fname=__METHOD__)
Definition: SpecialRandompage.php:153
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4058
RandomPage\getNsList
getNsList()
Get a comma-delimited list of namespaces we don't have any pages in.
Definition: SpecialRandompage.php:85
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:352
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
RandomPage\getNamespaces
getNamespaces()
Definition: SpecialRandompage.php:40
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
RandomPage\$extra
$extra
Definition: SpecialRandompage.php:33
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:33
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:525
RandomPage\$namespaces
$namespaces
Definition: SpecialRandompage.php:31
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
wfRandom
wfRandom()
Get a random decimal value between 0 and 1, in a way not likely to give duplicate values for any real...
Definition: GlobalFunctions.php:328
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
RandomPage\getRandomTitle
getRandomTitle()
Choose a random title.
Definition: SpecialRandompage.php:103
RandomPage
Special page to direct the user to a random page.
Definition: SpecialRandompage.php:30
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
RandomPage\getQueryInfo
getQueryInfo( $randstr)
Definition: SpecialRandompage.php:134
$res
$res
Definition: database.txt:21