MediaWiki  1.33.0
SpecialRandompage.php
Go to the documentation of this file.
1 <?php
26 
32 class RandomPage extends SpecialPage {
33  private $namespaces; // namespaces to select pages from
34  protected $isRedir = false; // should the result be a redirect?
35  protected $extra = []; // Extra SQL statements
36 
37  public function __construct( $name = 'Randompage' ) {
39  parent::__construct( $name );
40  }
41 
42  public function getNamespaces() {
43  return $this->namespaces;
44  }
45 
46  public function setNamespace( $ns ) {
47  if ( !$ns || $ns < NS_MAIN ) {
48  $ns = NS_MAIN;
49  }
50  $this->namespaces = [ $ns ];
51  }
52 
53  // select redirects instead of normal pages?
54  public function isRedirect() {
55  return $this->isRedir;
56  }
57 
58  public function execute( $par ) {
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(
63  MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $par ) );
64  }
65 
66  $title = $this->getRandomTitle();
67 
68  if ( is_null( $title ) ) {
69  $this->setHeaders();
70  // Message: randompage-nopages, randomredirect-nopages
71  $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
72  $this->getNsList(), count( $this->namespaces ) );
73 
74  return;
75  }
76 
77  $redirectParam = $this->isRedirect() ? [ 'redirect' => 'no' ] : [];
78  $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
79  unset( $query['title'] );
80  $this->getOutput()->redirect( $title->getFullURL( $query ) );
81  }
82 
88  private function getNsList() {
89  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
90  $nsNames = [];
91  foreach ( $this->namespaces as $n ) {
92  if ( $n === NS_MAIN ) {
93  $nsNames[] = $this->msg( 'blanknamespace' )->plain();
94  } else {
95  $nsNames[] = $contLang->getNsText( $n );
96  }
97  }
98 
99  return $contLang->commaList( $nsNames );
100  }
101 
106  public function getRandomTitle() {
107  $randstr = wfRandom();
108  $title = null;
109 
110  if ( !Hooks::run(
111  'SpecialRandomGetRandomTitle',
112  [ &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ]
113  ) ) {
114  return $title;
115  }
116 
117  $row = $this->selectRandomPageFromDB( $randstr );
118 
119  /* If we picked a value that was higher than any in
120  * the DB, wrap around and select the page with the
121  * lowest value instead! One might think this would
122  * skew the distribution, but in fact it won't cause
123  * any more bias than what the page_random scheme
124  * causes anyway. Trust me, I'm a mathematician. :)
125  */
126  if ( !$row ) {
127  $row = $this->selectRandomPageFromDB( "0" );
128  }
129 
130  if ( $row ) {
131  return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
132  }
133 
134  return null;
135  }
136 
137  protected function getQueryInfo( $randstr ) {
138  $redirect = $this->isRedirect() ? 1 : 0;
139  $tables = [ 'page' ];
140  $conds = array_merge( [
141  'page_namespace' => $this->namespaces,
142  'page_is_redirect' => $redirect,
143  'page_random >= ' . $randstr
144  ], $this->extra );
145  $joinConds = [];
146 
147  // Allow extensions to modify the query
148  Hooks::run( 'RandomPageQuery', [ &$tables, &$conds, &$joinConds ] );
149 
150  return [
151  'tables' => $tables,
152  'fields' => [ 'page_title', 'page_namespace' ],
153  'conds' => $conds,
154  'options' => [
155  'ORDER BY' => 'page_random',
156  'LIMIT' => 1,
157  ],
158  'join_conds' => $joinConds
159  ];
160  }
161 
162  private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
163  $dbr = wfGetDB( DB_REPLICA );
164 
165  $query = $this->getQueryInfo( $randstr );
166  $res = $dbr->select(
167  $query['tables'],
168  $query['fields'],
169  $query['conds'],
170  $fname,
171  $query['options'],
172  $query['join_conds']
173  );
174 
175  return $dbr->fetchObject( $res );
176  }
177 
178  protected function getGroupName() {
179  return 'redirects';
180  }
181 }
RandomPage\$isRedir
$isRedir
Definition: SpecialRandompage.php:34
RandomPage\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialRandompage.php:58
RandomPage\setNamespace
setNamespace( $ns)
Definition: SpecialRandompage.php:46
SpecialPage\msg
msg( $key)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:796
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:725
captcha-old.count
count
Definition: captcha-old.py:249
RandomPage\isRedirect
isRedirect()
Definition: SpecialRandompage.php:54
$tables
this hook is for auditing only 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:979
$res
$res
Definition: database.txt:21
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:152
MWNamespace\getContentNamespaces
static getContentNamespaces()
Get a list of all namespace indices which are considered to contain content.
Definition: MWNamespace.php:375
RandomPage\__construct
__construct( $name='Randompage')
Definition: SpecialRandompage.php:37
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
$dbr
$dbr
Definition: testCompression.php:50
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$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:1588
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:925
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
RandomPage\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialRandompage.php:178
RandomPage\selectRandomPageFromDB
selectRandomPageFromDB( $randstr, $fname=__METHOD__)
Definition: SpecialRandompage.php:162
RandomPage\getNsList
getNsList()
Get a comma-delimited list of namespaces we don't have any pages in.
Definition: SpecialRandompage.php:88
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:531
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:123
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:604
RandomPage\getNamespaces
getNamespaces()
Definition: SpecialRandompage.php:42
RandomPage\$extra
$extra
Definition: SpecialRandompage.php:35
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:715
RandomPage\$namespaces
$namespaces
Definition: SpecialRandompage.php:33
wfRandom
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
Definition: GlobalFunctions.php:280
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:106
RandomPage
Special page to direct the user to a random page.
Definition: SpecialRandompage.php:32
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
RandomPage\getQueryInfo
getQueryInfo( $randstr)
Definition: SpecialRandompage.php:137
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200