MediaWiki  1.30.0
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 = []; // 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 = [ $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 ( 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() ? [ 'redirect' => 'no' ] : [];
77  $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
78  unset( $query['title'] );
79  $this->getOutput()->redirect( $title->getFullURL( $query ) );
80  }
81 
87  private function getNsList() {
89  $nsNames = [];
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 
105  public function getRandomTitle() {
106  $randstr = wfRandom();
107  $title = null;
108 
109  if ( !Hooks::run(
110  'SpecialRandomGetRandomTitle',
111  [ &$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  $tables = [ 'page' ];
139  $conds = array_merge( [
140  'page_namespace' => $this->namespaces,
141  'page_is_redirect' => $redirect,
142  'page_random >= ' . $randstr
143  ], $this->extra );
144  $joinConds = [];
145 
146  // Allow extensions to modify the query
147  Hooks::run( 'RandomPageQuery', [ &$tables, &$conds, &$joinConds ] );
148 
149  return [
150  'tables' => $tables,
151  'fields' => [ 'page_title', 'page_namespace' ],
152  'conds' => $conds,
153  'options' => [
154  'ORDER BY' => 'page_random',
155  'LIMIT' => 1,
156  ],
157  'join_conds' => $joinConds
158  ];
159  }
160 
161  private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
162  $dbr = wfGetDB( DB_REPLICA );
163 
164  $query = $this->getQueryInfo( $randstr );
165  $res = $dbr->select(
166  $query['tables'],
167  $query['fields'],
168  $query['conds'],
169  $fname,
170  $query['options'],
171  $query['join_conds']
172  );
173 
174  return $dbr->fetchObject( $res );
175  }
176 
177  protected function getGroupName() {
178  return 'redirects';
179  }
180 }
RandomPage\$isRedir
$isRedir
Definition: SpecialRandompage.php:32
RandomPage\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialRandompage.php:56
RandomPage\setNamespace
setNamespace( $ns)
Definition: SpecialRandompage.php:44
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:746
$tables
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition: hooks.txt:988
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:675
captcha-old.count
count
Definition: captcha-old.py:249
RandomPage\isRedirect
isRedirect()
Definition: SpecialRandompage.php:52
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:36
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:150
MWNamespace\getContentNamespaces
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: MWNamespace.php:353
RandomPage\__construct
__construct( $name='Randompage')
Definition: SpecialRandompage.php:35
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:65
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1581
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2670
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
RandomPage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialRandompage.php:177
RandomPage\selectRandomPageFromDB
selectRandomPageFromDB( $randstr, $fname=__METHOD__)
Definition: SpecialRandompage.php:161
RandomPage\getNsList
getNsList()
Get a comma-delimited list of namespaces we don't have any pages in.
Definition: SpecialRandompage.php:87
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:484
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:557
RandomPage\getNamespaces
getNamespaces()
Definition: SpecialRandompage.php:40
RandomPage\$extra
$extra
Definition: SpecialRandompage.php:33
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:36
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:665
RandomPage\$namespaces
$namespaces
Definition: SpecialRandompage.php:31
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:352
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
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:105
RandomPage
Special page to direct the user to a random page.
Definition: SpecialRandompage.php:30
RandomPage\getQueryInfo
getQueryInfo( $randstr)
Definition: SpecialRandompage.php:136
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
$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