MediaWiki  master
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' ) {
38  $this->namespaces = MediaWikiServices::getInstance()->getNamespaceInfo()->
39  getContentNamespaces();
40  parent::__construct( $name );
41  }
42 
43  public function getNamespaces() {
44  return $this->namespaces;
45  }
46 
47  public function setNamespace( $ns ) {
48  if ( !$ns || $ns < NS_MAIN ) {
49  $ns = NS_MAIN;
50  }
51  $this->namespaces = [ $ns ];
52  }
53 
54  // select redirects instead of normal pages?
55  public function isRedirect() {
56  return $this->isRedir;
57  }
58 
59  public function execute( $par ) {
60  if ( is_string( $par ) ) {
61  // Testing for stringiness since we want to catch
62  // the empty string to mean main namespace only.
63  $this->setNamespace(
64  MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $par ) );
65  }
66 
67  $title = $this->getRandomTitle();
68 
69  if ( is_null( $title ) ) {
70  $this->setHeaders();
71  // Message: randompage-nopages, randomredirect-nopages
72  $this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
73  $this->getNsList(), count( $this->namespaces ) );
74 
75  return;
76  }
77 
78  $redirectParam = $this->isRedirect() ? [ 'redirect' => 'no' ] : [];
79  $query = array_merge( $this->getRequest()->getValues(), $redirectParam );
80  unset( $query['title'] );
81  $this->getOutput()->redirect( $title->getFullURL( $query ) );
82  }
83 
89  private function getNsList() {
90  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
91  $nsNames = [];
92  foreach ( $this->namespaces as $n ) {
93  if ( $n === NS_MAIN ) {
94  $nsNames[] = $this->msg( 'blanknamespace' )->plain();
95  } else {
96  $nsNames[] = $contLang->getNsText( $n );
97  }
98  }
99 
100  return $contLang->commaList( $nsNames );
101  }
102 
107  public function getRandomTitle() {
108  $randstr = wfRandom();
109  $title = null;
110 
111  if ( !Hooks::run(
112  'SpecialRandomGetRandomTitle',
113  [ &$randstr, &$this->isRedir, &$this->namespaces, &$this->extra, &$title ]
114  ) ) {
115  return $title;
116  }
117 
118  $row = $this->selectRandomPageFromDB( $randstr );
119 
120  /* If we picked a value that was higher than any in
121  * the DB, wrap around and select the page with the
122  * lowest value instead! One might think this would
123  * skew the distribution, but in fact it won't cause
124  * any more bias than what the page_random scheme
125  * causes anyway. Trust me, I'm a mathematician. :)
126  */
127  if ( !$row ) {
128  $row = $this->selectRandomPageFromDB( "0" );
129  }
130 
131  if ( $row ) {
132  return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
133  }
134 
135  return null;
136  }
137 
138  protected function getQueryInfo( $randstr ) {
139  $redirect = $this->isRedirect() ? 1 : 0;
140  $tables = [ 'page' ];
141  $conds = array_merge( [
142  'page_namespace' => $this->namespaces,
143  'page_is_redirect' => $redirect,
144  'page_random >= ' . $randstr
145  ], $this->extra );
146  $joinConds = [];
147 
148  // Allow extensions to modify the query
149  Hooks::run( 'RandomPageQuery', [ &$tables, &$conds, &$joinConds ] );
150 
151  return [
152  'tables' => $tables,
153  'fields' => [ 'page_title', 'page_namespace' ],
154  'conds' => $conds,
155  'options' => [
156  'ORDER BY' => 'page_random',
157  'LIMIT' => 1,
158  ],
159  'join_conds' => $joinConds
160  ];
161  }
162 
163  private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
164  $dbr = wfGetDB( DB_REPLICA );
165 
166  $query = $this->getQueryInfo( $randstr );
167  $res = $dbr->select(
168  $query['tables'],
169  $query['fields'],
170  $query['conds'],
171  $fname,
172  $query['options'],
173  $query['join_conds']
174  );
175 
176  return $dbr->fetchObject( $res );
177  }
178 
179  protected function getGroupName() {
180  return 'redirects';
181  }
182 }
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used $groups Array of ChangesListFilterGroup objects(added in 1.34) 'FileDeleteComplete' null for the local 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:1529
getQueryInfo( $randstr)
const NS_MAIN
Definition: Defines.php:60
to move a page</td >< td > &*You are moving the page across namespaces
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
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
getOutput()
Get the OutputPage being used for this instance.
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:960
getNsList()
Get a comma-delimited list of namespaces we don&#39;t have any pages in.
$res
Definition: database.txt:21
getRandomTitle()
Choose a random title.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
__construct( $name='Randompage')
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
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:131
selectRandomPageFromDB( $randstr, $fname=__METHOD__)
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:620
Special page to direct the user to a random page.
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
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
getName()
Get the name of this Special Page.
msg( $key)
Wrapper around wfMessage that sets the current context.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
const DB_REPLICA
Definition: defines.php:25
getRequest()
Get the WebRequest being used for this instance.
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200