[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/api/ -> ApiQueryRandom.php (source)

   1  <?php
   2  
   3  /**
   4   *
   5   *
   6   * Created on Monday, January 28, 2008
   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   * @file
  26   */
  27  
  28  /**
  29   * Query module to get list of random pages
  30   *
  31   * @ingroup API
  32   */
  33  class ApiQueryRandom extends ApiQueryGeneratorBase {
  34      private $pageIDs;
  35  
  36  	public function __construct( ApiQuery $query, $moduleName ) {
  37          parent::__construct( $query, $moduleName, 'rn' );
  38      }
  39  
  40  	public function execute() {
  41          $this->run();
  42      }
  43  
  44  	public function executeGenerator( $resultPageSet ) {
  45          $this->run( $resultPageSet );
  46      }
  47  
  48      /**
  49       * @param string $randstr
  50       * @param int $limit
  51       * @param int $namespace
  52       * @param ApiPageSet $resultPageSet
  53       * @param bool $redirect
  54       * @return void
  55       */
  56  	protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
  57          $this->resetQueryParams();
  58          $this->addTables( 'page' );
  59          $this->addOption( 'LIMIT', $limit );
  60          $this->addWhereFld( 'page_namespace', $namespace );
  61          $this->addWhereRange( 'page_random', 'newer', $randstr, null );
  62          $this->addWhereFld( 'page_is_redirect', $redirect );
  63          if ( is_null( $resultPageSet ) ) {
  64              $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
  65          } else {
  66              $this->addFields( $resultPageSet->getPageTableFields() );
  67          }
  68      }
  69  
  70      /**
  71       * @param ApiPageSet $resultPageSet
  72       * @return int
  73       */
  74  	protected function runQuery( $resultPageSet = null ) {
  75          $res = $this->select( __METHOD__ );
  76          $count = 0;
  77          foreach ( $res as $row ) {
  78              $count++;
  79              if ( is_null( $resultPageSet ) ) {
  80                  // Prevent duplicates
  81                  if ( !in_array( $row->page_id, $this->pageIDs ) ) {
  82                      $fit = $this->getResult()->addValue(
  83                          array( 'query', $this->getModuleName() ),
  84                          null, $this->extractRowInfo( $row ) );
  85                      if ( !$fit ) {
  86                          // We can't really query-continue a random list.
  87                          // Return an insanely high value so
  88                          // $count < $limit is false
  89                          return 1E9;
  90                      }
  91                      $this->pageIDs[] = $row->page_id;
  92                  }
  93              } else {
  94                  $resultPageSet->processDbRow( $row );
  95              }
  96          }
  97  
  98          return $count;
  99      }
 100  
 101      /**
 102       * @param ApiPageSet $resultPageSet
 103       * @return void
 104       */
 105  	public function run( $resultPageSet = null ) {
 106          $params = $this->extractRequestParams();
 107          $result = $this->getResult();
 108          $this->pageIDs = array();
 109  
 110          $this->prepareQuery(
 111              wfRandom(),
 112              $params['limit'],
 113              $params['namespace'],
 114              $resultPageSet,
 115              $params['redirect']
 116          );
 117          $count = $this->runQuery( $resultPageSet );
 118          if ( $count < $params['limit'] ) {
 119              /* We got too few pages, we probably picked a high value
 120               * for page_random. We'll just take the lowest ones, see
 121               * also the comment in Title::getRandomTitle()
 122               */
 123              $this->prepareQuery(
 124                  0,
 125                  $params['limit'] - $count,
 126                  $params['namespace'],
 127                  $resultPageSet,
 128                  $params['redirect']
 129              );
 130              $this->runQuery( $resultPageSet );
 131          }
 132  
 133          if ( is_null( $resultPageSet ) ) {
 134              $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
 135          }
 136      }
 137  
 138  	private function extractRowInfo( $row ) {
 139          $title = Title::makeTitle( $row->page_namespace, $row->page_title );
 140          $vals = array();
 141          $vals['id'] = intval( $row->page_id );
 142          ApiQueryBase::addTitleInfo( $vals, $title );
 143  
 144          return $vals;
 145      }
 146  
 147  	public function getCacheMode( $params ) {
 148          return 'public';
 149      }
 150  
 151  	public function getAllowedParams() {
 152          return array(
 153              'namespace' => array(
 154                  ApiBase::PARAM_TYPE => 'namespace',
 155                  ApiBase::PARAM_ISMULTI => true
 156              ),
 157              'limit' => array(
 158                  ApiBase::PARAM_TYPE => 'limit',
 159                  ApiBase::PARAM_DFLT => 1,
 160                  ApiBase::PARAM_MIN => 1,
 161                  ApiBase::PARAM_MAX => 10,
 162                  ApiBase::PARAM_MAX2 => 20
 163              ),
 164              'redirect' => false,
 165          );
 166      }
 167  
 168  	public function getParamDescription() {
 169          return array(
 170              'namespace' => 'Return pages in these namespaces only',
 171              'limit' => 'Limit how many random pages will be returned',
 172              'redirect' => 'Load a random redirect instead of a random page'
 173          );
 174      }
 175  
 176  	public function getDescription() {
 177          return array(
 178              'Get a set of random pages.',
 179              'NOTE: Pages are listed in a fixed sequence, only the starting point is random.',
 180              '      This means that if, for example, "Main Page" is the first random page on',
 181              '      your list, "List of fictional monkeys" will *always* be second, "List of',
 182              '      people on stamps of Vanuatu" third, etc.',
 183              'NOTE: If the number of pages in the namespace is lower than rnlimit, you will',
 184              '      get fewer pages. You will not get the same page twice.'
 185          );
 186      }
 187  
 188  	public function getExamples() {
 189          return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
 190      }
 191  
 192  	public function getHelpUrls() {
 193          return 'https://www.mediawiki.org/wiki/API:Random';
 194      }
 195  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1