Remove double globals.
[lhc/web/wiklou.git] / includes / api / ApiQueryRandom.php
1 <?php
2
3 /**
4 * Created on Monday, January 28, 2008
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2008 Brent Garber
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to get list of random pages
33 *
34 * @ingroup API
35 */
36
37 class ApiQueryRandom extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rn' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
52 $this->resetQueryParams();
53 $this->addTables( 'page' );
54 $this->addOption( 'LIMIT', $limit );
55 $this->addWhereFld( 'page_namespace', $namespace );
56 $this->addWhereRange( 'page_random', 'newer', $randstr, null );
57 $this->addWhereFld( 'page_is_redirect', $redirect );
58 $this->addOption( 'USE INDEX', 'page_random' );
59 if ( is_null( $resultPageSet ) ) {
60 $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
61 } else {
62 $this->addFields( $resultPageSet->getPageTableFields() );
63 }
64 }
65
66 protected function runQuery( &$resultPageSet ) {
67 $res = $this->select( __METHOD__ );
68 $count = 0;
69 foreach ( $res as $row ) {
70 $count++;
71 if ( is_null( $resultPageSet ) ) {
72 // Prevent duplicates
73 if ( !in_array( $row->page_id, $this->pageIDs ) ) {
74 $fit = $this->getResult()->addValue(
75 array( 'query', $this->getModuleName() ),
76 null, $this->extractRowInfo( $row ) );
77 if ( !$fit ) {
78 // We can't really query-continue a random list.
79 // Return an insanely high value so
80 // $count < $limit is false
81 return 1E9;
82 }
83 $this->pageIDs[] = $row->page_id;
84 }
85 } else {
86 $resultPageSet->processDbRow( $row );
87 }
88 }
89
90 return $count;
91 }
92
93 public function run( $resultPageSet = null ) {
94 $params = $this->extractRequestParams();
95 $result = $this->getResult();
96 $this->pageIDs = array();
97
98 $this->prepareQuery( wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect'] );
99 $count = $this->runQuery( $resultPageSet );
100 if ( $count < $params['limit'] ) {
101 /* We got too few pages, we probably picked a high value
102 * for page_random. We'll just take the lowest ones, see
103 * also the comment in Title::getRandomTitle()
104 */
105 $this->prepareQuery( 0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect'] );
106 $this->runQuery( $resultPageSet );
107 }
108
109 if ( is_null( $resultPageSet ) ) {
110 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
111 }
112 }
113
114 private function extractRowInfo( $row ) {
115 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
116 $vals = array();
117 $vals['id'] = intval( $row->page_id );
118 ApiQueryBase::addTitleInfo( $vals, $title );
119 return $vals;
120 }
121
122 public function getCacheMode( $params ) {
123 return 'public';
124 }
125
126 public function getAllowedParams() {
127 return array(
128 'namespace' => array(
129 ApiBase::PARAM_TYPE => 'namespace',
130 ApiBase::PARAM_ISMULTI => true
131 ),
132 'limit' => array(
133 ApiBase::PARAM_TYPE => 'limit',
134 ApiBase::PARAM_DFLT => 1,
135 ApiBase::PARAM_MIN => 1,
136 ApiBase::PARAM_MAX => 10,
137 ApiBase::PARAM_MAX2 => 20
138 ),
139 'redirect' => false,
140 );
141 }
142
143 public function getParamDescription() {
144 return array(
145 'namespace' => 'Return pages in these namespaces only',
146 'limit' => 'Limit how many random pages will be returned',
147 'redirect' => 'Load a random redirect instead of a random page'
148 );
149 }
150
151 public function getDescription() {
152 return array(
153 'Get a set of random pages',
154 'NOTE: Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, "Main Page" is the first ',
155 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc',
156 'NOTE: If the number of pages in the namespace is lower than rnlimit, you will get fewer pages. You will not get the same page twice'
157 );
158 }
159
160 protected function getExamples() {
161 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
162 }
163
164 public function getVersion() {
165 return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$';
166 }
167 }