2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2010-06-21 12:59:04 +00:00
|
|
|
/**
|
2010-08-15 07:16:58 +00:00
|
|
|
* Implements Special:Randompage
|
2010-06-21 12:59:04 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:16:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2010-06-21 12:59:04 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-15 07:16:58 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup SpecialPage
|
|
|
|
|
* @author Rob Church <robchur@gmail.com>, Ilmari Karonen
|
2010-06-21 12:59:04 +00:00
|
|
|
*/
|
2007-04-19 10:19:27 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2007-04-19 10:19:27 +00:00
|
|
|
* Special page to direct the user to a random page
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup SpecialPage
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2008-01-14 10:23:48 +00:00
|
|
|
class RandomPage extends SpecialPage {
|
2009-01-06 23:25:03 +00:00
|
|
|
private $namespaces; // namespaces to select pages from
|
2009-08-17 15:23:51 +00:00
|
|
|
protected $isRedir = false; // should the result be a redirect?
|
|
|
|
|
protected $extra = array(); // Extra SQL statements
|
2007-04-19 10:19:27 +00:00
|
|
|
|
2009-08-16 06:23:07 +00:00
|
|
|
public function __construct( $name = 'Randompage' ){
|
2011-08-19 23:11:12 +00:00
|
|
|
$this->namespaces = MWNamespace::getContentNamespaces();
|
2008-04-14 07:45:50 +00:00
|
|
|
parent::__construct( $name );
|
2008-01-14 10:23:48 +00:00
|
|
|
}
|
2008-01-14 12:50:20 +00:00
|
|
|
|
2009-01-06 23:25:03 +00:00
|
|
|
public function getNamespaces() {
|
|
|
|
|
return $this->namespaces;
|
2008-01-14 10:23:48 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-04-19 10:19:27 +00:00
|
|
|
public function setNamespace ( $ns ) {
|
2011-01-31 23:06:36 +00:00
|
|
|
if( !$ns || $ns < NS_MAIN ) {
|
|
|
|
|
$ns = NS_MAIN;
|
|
|
|
|
}
|
2009-01-06 23:25:03 +00:00
|
|
|
$this->namespaces = array( $ns );
|
2007-04-19 10:19:27 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-01-14 12:50:20 +00:00
|
|
|
// select redirects instead of normal pages?
|
|
|
|
|
public function isRedirect(){
|
2009-08-17 15:23:51 +00:00
|
|
|
return $this->isRedir;
|
2007-04-19 10:19:27 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-01-14 12:50:20 +00:00
|
|
|
public function execute( $par ) {
|
2011-08-20 20:12:46 +00:00
|
|
|
global $wgContLang;
|
2008-01-14 10:23:48 +00:00
|
|
|
|
2011-08-20 20:12:46 +00:00
|
|
|
if ( $par ) {
|
2008-01-14 10:23:48 +00:00
|
|
|
$this->setNamespace( $wgContLang->getNsIndex( $par ) );
|
2009-04-27 07:20:23 +00:00
|
|
|
}
|
2008-01-14 10:23:48 +00:00
|
|
|
|
|
|
|
|
$title = $this->getRandomTitle();
|
|
|
|
|
|
|
|
|
|
if( is_null( $title ) ) {
|
2008-01-14 12:50:20 +00:00
|
|
|
$this->setHeaders();
|
2011-08-20 20:12:46 +00:00
|
|
|
$this->getOutput()->addWikiMsg( strtolower( $this->getName() ) . '-nopages',
|
2009-08-16 06:23:07 +00:00
|
|
|
$this->getNsList(), count( $this->namespaces ) );
|
2008-01-14 10:23:48 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-15 11:54:16 +00:00
|
|
|
$redirectParam = $this->isRedirect() ? array( 'redirect' => 'no' ) : array();
|
2011-08-20 20:12:46 +00:00
|
|
|
$query = array_merge( $this->getRequest()->getValues(), $redirectParam );
|
2010-04-15 11:54:16 +00:00
|
|
|
unset( $query['title'] );
|
2011-08-20 20:12:46 +00:00
|
|
|
$this->getOutput()->redirect( $title->getFullUrl( $query ) );
|
2008-01-14 10:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-16 06:23:07 +00:00
|
|
|
/**
|
|
|
|
|
* Get a comma-delimited list of namespaces we don't have
|
|
|
|
|
* any pages in
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
private function getNsList() {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$nsNames = array();
|
|
|
|
|
foreach( $this->namespaces as $n ) {
|
2011-01-31 23:06:36 +00:00
|
|
|
if( $n === NS_MAIN ) {
|
2012-05-11 09:09:48 +00:00
|
|
|
$nsNames[] = $this->msg( 'blanknamespace' )->plain();
|
2011-01-31 23:06:36 +00:00
|
|
|
} else {
|
2009-08-16 06:23:07 +00:00
|
|
|
$nsNames[] = $wgContLang->getNsText( $n );
|
2011-01-31 23:06:36 +00:00
|
|
|
}
|
2009-08-16 06:23:07 +00:00
|
|
|
}
|
|
|
|
|
return $wgContLang->commaList( $nsNames );
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-19 10:19:27 +00:00
|
|
|
/**
|
|
|
|
|
* Choose a random title.
|
|
|
|
|
* @return Title object (or null if nothing to choose from)
|
|
|
|
|
*/
|
2008-01-14 12:50:20 +00:00
|
|
|
public function getRandomTitle() {
|
2007-04-19 10:19:27 +00:00
|
|
|
$randstr = wfRandom();
|
2009-08-17 15:23:51 +00:00
|
|
|
$title = null;
|
2011-01-31 23:06:36 +00:00
|
|
|
if ( !wfRunHooks( 'SpecialRandomGetRandomTitle', array( &$randstr, &$this->isRedir, &$this->namespaces,
|
|
|
|
|
&$this->extra, &$title ) ) ) {
|
2009-08-17 15:23:51 +00:00
|
|
|
return $title;
|
|
|
|
|
}
|
2007-04-19 10:19:27 +00:00
|
|
|
$row = $this->selectRandomPageFromDB( $randstr );
|
|
|
|
|
|
2007-04-19 10:54:48 +00:00
|
|
|
/* If we picked a value that was higher than any in
|
|
|
|
|
* the DB, wrap around and select the page with the
|
|
|
|
|
* lowest value instead! One might think this would
|
|
|
|
|
* skew the distribution, but in fact it won't cause
|
|
|
|
|
* any more bias than what the page_random scheme
|
|
|
|
|
* causes anyway. Trust me, I'm a mathematician. :)
|
|
|
|
|
*/
|
2011-01-31 23:06:36 +00:00
|
|
|
if( !$row ) {
|
2007-04-19 10:54:48 +00:00
|
|
|
$row = $this->selectRandomPageFromDB( "0" );
|
2011-01-31 23:06:36 +00:00
|
|
|
}
|
2007-04-19 10:19:27 +00:00
|
|
|
|
2011-01-31 23:06:36 +00:00
|
|
|
if( $row ) {
|
2009-01-06 23:25:03 +00:00
|
|
|
return Title::makeTitleSafe( $row->page_namespace, $row->page_title );
|
2011-01-31 23:06:36 +00:00
|
|
|
} else {
|
2007-04-19 10:19:27 +00:00
|
|
|
return null;
|
2011-01-31 23:06:36 +00:00
|
|
|
}
|
2007-04-19 10:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-31 23:41:38 +00:00
|
|
|
protected function getQueryInfo( $randstr ) {
|
2008-01-14 12:50:20 +00:00
|
|
|
$redirect = $this->isRedirect() ? 1 : 0;
|
2011-01-31 23:41:38 +00:00
|
|
|
|
|
|
|
|
return array(
|
|
|
|
|
'tables' => array( 'page' ),
|
|
|
|
|
'fields' => array( 'page_title', 'page_namespace' ),
|
|
|
|
|
'conds' => array_merge( array(
|
2011-01-31 23:06:36 +00:00
|
|
|
'page_namespace' => $this->namespaces,
|
|
|
|
|
'page_is_redirect' => $redirect,
|
|
|
|
|
'page_random >= ' . $randstr
|
|
|
|
|
), $this->extra ),
|
2011-01-31 23:41:38 +00:00
|
|
|
'options' => array(
|
2011-01-31 23:06:36 +00:00
|
|
|
'ORDER BY' => 'page_random',
|
2011-01-31 23:41:38 +00:00
|
|
|
'USE INDEX' => 'page_random',
|
|
|
|
|
'LIMIT' => 1,
|
|
|
|
|
),
|
|
|
|
|
'join_conds' => array()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function selectRandomPageFromDB( $randstr, $fname = __METHOD__ ) {
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
|
|
|
|
|
$query = $this->getQueryInfo( $randstr );
|
|
|
|
|
$res = $dbr->select(
|
|
|
|
|
$query['tables'],
|
|
|
|
|
$query['fields'],
|
|
|
|
|
$query['conds'],
|
|
|
|
|
$fname,
|
|
|
|
|
$query['options'],
|
|
|
|
|
$query['join_conds']
|
2011-01-31 23:06:36 +00:00
|
|
|
);
|
|
|
|
|
|
2007-04-19 10:19:27 +00:00
|
|
|
return $dbr->fetchObject( $res );
|
|
|
|
|
}
|
|
|
|
|
}
|