2008-01-28 19:05:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
2010-02-24 14:45:19 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright © 2008 Brent Garber
|
2008-01-28 19:05:26 +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:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-01-28 19:05:26 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2008-01-28 19:05:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query module to get list of random pages
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
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 API
|
2008-01-28 19:05:26 +00:00
|
|
|
*/
|
2011-02-19 00:30:18 +00:00
|
|
|
class ApiQueryRandom extends ApiQueryGeneratorBase {
|
2014-03-25 17:22:11 +00:00
|
|
|
public function __construct( ApiQuery $query, $moduleName ) {
|
2010-02-24 14:45:19 +00:00
|
|
|
parent::__construct( $query, $moduleName, 'rn' );
|
2008-01-28 19:05:26 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function execute() {
|
|
|
|
|
$this->run();
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function executeGenerator( $resultPageSet ) {
|
|
|
|
|
$this->run( $resultPageSet );
|
2008-01-28 19:05:26 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-02-19 00:30:18 +00:00
|
|
|
/**
|
2015-07-07 16:28:42 +00:00
|
|
|
* Actually perform the query and add pages to the result.
|
|
|
|
|
* @param ApiPageSet|null $resultPageSet
|
|
|
|
|
* @param int $limit Number of pages to fetch
|
|
|
|
|
* @param string|null $start Starting page_random
|
2017-06-06 14:15:49 +00:00
|
|
|
* @param int $startId Starting page_id
|
2015-07-07 16:28:42 +00:00
|
|
|
* @param string|null $end Ending page_random
|
|
|
|
|
* @return array (int, string|null) Number of pages left to query and continuation string
|
2011-02-19 00:30:18 +00:00
|
|
|
*/
|
2015-07-07 16:28:42 +00:00
|
|
|
protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
2008-02-01 23:05:19 +00:00
|
|
|
$this->resetQueryParams();
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->addTables( 'page' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addFields( [ 'page_id', 'page_random' ] );
|
2010-02-24 14:45:19 +00:00
|
|
|
if ( is_null( $resultPageSet ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addFields( [ 'page_title', 'page_namespace' ] );
|
2010-02-24 14:45:19 +00:00
|
|
|
} else {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->addFields( $resultPageSet->getPageTableFields() );
|
2010-02-24 14:45:19 +00:00
|
|
|
}
|
2015-07-07 16:28:42 +00:00
|
|
|
$this->addWhereFld( 'page_namespace', $params['namespace'] );
|
|
|
|
|
if ( $params['redirect'] || $params['filterredir'] === 'redirects' ) {
|
|
|
|
|
$this->addWhereFld( 'page_is_redirect', 1 );
|
|
|
|
|
} elseif ( $params['filterredir'] === 'nonredirects' ) {
|
|
|
|
|
$this->addWhereFld( 'page_is_redirect', 0 );
|
|
|
|
|
} elseif ( is_null( $resultPageSet ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addFields( [ 'page_is_redirect' ] );
|
2015-07-07 16:28:42 +00:00
|
|
|
}
|
|
|
|
|
$this->addOption( 'LIMIT', $limit + 1 );
|
|
|
|
|
|
|
|
|
|
if ( $start !== null ) {
|
|
|
|
|
$start = $this->getDB()->addQuotes( $start );
|
2017-06-06 14:15:49 +00:00
|
|
|
if ( $startId > 0 ) {
|
|
|
|
|
$startId = (int)$startId; // safety
|
2015-07-07 16:28:42 +00:00
|
|
|
$this->addWhere( "page_random = $start AND page_id >= $startId OR page_random > $start" );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addWhere( "page_random >= $start" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $end !== null ) {
|
|
|
|
|
$this->addWhere( 'page_random < ' . $this->getDB()->addQuotes( $end ) );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addOption( 'ORDER BY', [ 'page_random', 'page_id' ] );
|
2015-07-07 16:28:42 +00:00
|
|
|
|
|
|
|
|
$result = $this->getResult();
|
2016-02-17 09:09:32 +00:00
|
|
|
$path = [ 'query', $this->getModuleName() ];
|
2008-01-28 19:05:26 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$res = $this->select( __METHOD__ );
|
2008-02-01 23:05:19 +00:00
|
|
|
$count = 0;
|
2010-06-20 18:48:34 +00:00
|
|
|
foreach ( $res as $row ) {
|
2015-07-07 16:28:42 +00:00
|
|
|
if ( $count++ >= $limit ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 0, "{$row->page_random}|{$row->page_id}" ];
|
2015-07-07 16:28:42 +00:00
|
|
|
}
|
2010-02-24 14:45:19 +00:00
|
|
|
if ( is_null( $resultPageSet ) ) {
|
2015-07-07 16:28:42 +00:00
|
|
|
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
|
2016-02-17 09:09:32 +00:00
|
|
|
$page = [
|
2015-07-07 16:28:42 +00:00
|
|
|
'id' => (int)$row->page_id,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-07-07 16:28:42 +00:00
|
|
|
ApiQueryBase::addTitleInfo( $page, $title );
|
|
|
|
|
if ( isset( $row->page_is_redirect ) ) {
|
|
|
|
|
$page['redirect'] = (bool)$row->page_is_redirect;
|
|
|
|
|
}
|
|
|
|
|
$fit = $result->addValue( $path, null, $page );
|
|
|
|
|
if ( !$fit ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ 0, "{$row->page_random}|{$row->page_id}" ];
|
2008-02-01 23:05:19 +00:00
|
|
|
}
|
2010-02-24 14:45:19 +00:00
|
|
|
} else {
|
2010-01-11 15:55:52 +00:00
|
|
|
$resultPageSet->processDbRow( $row );
|
2010-02-24 14:45:19 +00:00
|
|
|
}
|
2008-01-28 19:05:26 +00:00
|
|
|
}
|
2010-06-20 17:48:21 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $limit - $count, null ];
|
2008-02-01 23:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-24 23:03:00 +00:00
|
|
|
/**
|
2015-07-07 16:28:42 +00:00
|
|
|
* @param ApiPageSet|null $resultPageSet
|
2011-02-24 23:03:00 +00:00
|
|
|
*/
|
2010-01-11 15:55:52 +00:00
|
|
|
public function run( $resultPageSet = null ) {
|
2008-02-01 23:05:19 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2015-07-07 16:28:42 +00:00
|
|
|
|
|
|
|
|
// Since 'filterredir" will always be set in $params, we have to dig
|
|
|
|
|
// into the WebRequest to see if it was actually passed.
|
|
|
|
|
$request = $this->getMain()->getRequest();
|
|
|
|
|
if ( $request->getCheck( $this->encodeParamName( 'filterredir' ) ) ) {
|
|
|
|
|
$this->requireMaxOneParameter( $params, 'filterredir', 'redirect' );
|
2008-02-01 23:05:19 +00:00
|
|
|
}
|
2008-01-28 19:05:26 +00:00
|
|
|
|
2015-07-07 16:28:42 +00:00
|
|
|
if ( isset( $params['continue'] ) ) {
|
|
|
|
|
$cont = explode( '|', $params['continue'] );
|
|
|
|
|
$this->dieContinueUsageIf( count( $cont ) != 4 );
|
|
|
|
|
$rand = $cont[0];
|
|
|
|
|
$start = $cont[1];
|
|
|
|
|
$startId = (int)$cont[2];
|
|
|
|
|
$end = $cont[3] ? $rand : null;
|
|
|
|
|
$this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $rand ) );
|
|
|
|
|
$this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $start ) );
|
|
|
|
|
$this->dieContinueUsageIf( $cont[2] !== (string)$startId );
|
|
|
|
|
$this->dieContinueUsageIf( $cont[3] !== '0' && $cont[3] !== '1' );
|
|
|
|
|
} else {
|
|
|
|
|
$rand = wfRandom();
|
|
|
|
|
$start = $rand;
|
2017-06-06 14:15:49 +00:00
|
|
|
$startId = 0;
|
2015-07-07 16:28:42 +00:00
|
|
|
$end = null;
|
2008-04-14 07:45:50 +00:00
|
|
|
}
|
2008-01-28 19:05:26 +00:00
|
|
|
|
2017-06-06 14:15:49 +00:00
|
|
|
// Set the non-continue if this is being used as a generator
|
|
|
|
|
// (as a list it doesn't matter because lists never non-continue)
|
|
|
|
|
if ( $resultPageSet !== null ) {
|
|
|
|
|
$endFlag = $end === null ? 0 : 1;
|
|
|
|
|
$this->getContinuationManager()->addGeneratorNonContinueParam(
|
|
|
|
|
$this, 'continue', "$rand|$start|$startId|$endFlag"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-28 11:15:17 +00:00
|
|
|
list( $left, $continue ) =
|
|
|
|
|
$this->runQuery( $resultPageSet, $params['limit'], $start, $startId, $end );
|
2015-07-07 16:28:42 +00:00
|
|
|
if ( $end === null && $continue === null ) {
|
|
|
|
|
// Wrap around. We do this even if $left === 0 for continuation
|
|
|
|
|
// (saving a DB query in this rare case probably isn't worth the
|
|
|
|
|
// added code complexity it would require).
|
|
|
|
|
$end = $rand;
|
|
|
|
|
list( $left, $continue ) = $this->runQuery( $resultPageSet, $left, null, null, $end );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $continue !== null ) {
|
|
|
|
|
$endFlag = $end === null ? 0 : 1;
|
|
|
|
|
$this->setContinueEnumParameter( 'continue', "$rand|$continue|$endFlag" );
|
|
|
|
|
}
|
2013-11-14 12:56:06 +00:00
|
|
|
|
2015-07-07 16:28:42 +00:00
|
|
|
if ( is_null( $resultPageSet ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
|
2015-07-07 16:28:42 +00:00
|
|
|
}
|
2008-01-28 19:05:26 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
public function getCacheMode( $params ) {
|
|
|
|
|
return 'public';
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'namespace' => [
|
2010-02-24 14:45:19 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'namespace',
|
|
|
|
|
ApiBase::PARAM_ISMULTI => true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'filterredir' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'all', 'redirects', 'nonredirects' ],
|
2015-07-07 16:28:42 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'nonredirects', // for BC
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'redirect' => [
|
2015-07-07 16:28:42 +00:00
|
|
|
ApiBase::PARAM_DEPRECATED => true,
|
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'limit' => [
|
2010-02-24 14:45:19 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
|
ApiBase::PARAM_DFLT => 1,
|
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
2015-07-07 16:28:42 +00:00
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
|
|
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'continue' => [
|
2015-07-07 16:28:42 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2008-01-28 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=query&list=random&rnnamespace=0&rnlimit=2'
|
|
|
|
|
=> 'apihelp-query+random-example-simple',
|
|
|
|
|
'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
|
|
|
|
|
=> 'apihelp-query+random-example-generator',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2008-01-28 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 07:08:18 +00:00
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
|
2013-05-16 07:08:18 +00:00
|
|
|
}
|
2010-07-23 07:17:56 +00:00
|
|
|
}
|