[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

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

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on Dec 22, 2010
   6   *
   7   * Copyright © 2010 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
   8   *
   9   * This program is free software; you can redistribute it and/or modify
  10   * it under the terms of the GNU General Public License as published by
  11   * the Free Software Foundation; either version 2 of the License, or
  12   * (at your option) any later version.
  13   *
  14   * This program is distributed in the hope that it will be useful,
  15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17   * GNU General Public License for more details.
  18   *
  19   * You should have received a copy of the GNU General Public License along
  20   * with this program; if not, write to the Free Software Foundation, Inc.,
  21   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22   * http://www.gnu.org/copyleft/gpl.html
  23   *
  24   * @file
  25   */
  26  
  27  /**
  28   * Query module to get the results of a QueryPage-based special page
  29   *
  30   * @ingroup API
  31   */
  32  class ApiQueryQueryPage extends ApiQueryGeneratorBase {
  33      private $qpMap;
  34  
  35  	public function __construct( ApiQuery $query, $moduleName ) {
  36          parent::__construct( $query, $moduleName, 'qp' );
  37          // Build mapping from special page names to QueryPage classes
  38          $uselessQueryPages = $this->getConfig()->get( 'APIUselessQueryPages' );
  39          $this->qpMap = array();
  40          foreach ( QueryPage::getPages() as $page ) {
  41              if ( !in_array( $page[1], $uselessQueryPages ) ) {
  42                  $this->qpMap[$page[1]] = $page[0];
  43              }
  44          }
  45      }
  46  
  47  	public function execute() {
  48          $this->run();
  49      }
  50  
  51  	public function executeGenerator( $resultPageSet ) {
  52          $this->run( $resultPageSet );
  53      }
  54  
  55      /**
  56       * @param ApiPageSet $resultPageSet
  57       */
  58  	public function run( $resultPageSet = null ) {
  59          $params = $this->extractRequestParams();
  60          $result = $this->getResult();
  61  
  62          /** @var $qp QueryPage */
  63          $qp = new $this->qpMap[$params['page']]();
  64          if ( !$qp->userCanExecute( $this->getUser() ) ) {
  65              $this->dieUsageMsg( 'specialpage-cantexecute' );
  66          }
  67  
  68          $r = array( 'name' => $params['page'] );
  69          if ( $qp->isCached() ) {
  70              if ( !$qp->isCacheable() ) {
  71                  $r['disabled'] = '';
  72              } else {
  73                  $r['cached'] = '';
  74                  $ts = $qp->getCachedTimestamp();
  75                  if ( $ts ) {
  76                      $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
  77                  }
  78                  $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
  79              }
  80          }
  81          $result->addValue( array( 'query' ), $this->getModuleName(), $r );
  82  
  83          if ( $qp->isCached() && !$qp->isCacheable() ) {
  84              // Disabled query page, don't run the query
  85              return;
  86          }
  87  
  88          $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
  89          $count = 0;
  90          $titles = array();
  91          foreach ( $res as $row ) {
  92              if ( ++$count > $params['limit'] ) {
  93                  // We've had enough
  94                  $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
  95                  break;
  96              }
  97  
  98              $title = Title::makeTitle( $row->namespace, $row->title );
  99              if ( is_null( $resultPageSet ) ) {
 100                  $data = array( 'value' => $row->value );
 101                  if ( $qp->usesTimestamps() ) {
 102                      $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
 103                  }
 104                  self::addTitleInfo( $data, $title );
 105  
 106                  foreach ( $row as $field => $value ) {
 107                      if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
 108                          $data['databaseResult'][$field] = $value;
 109                      }
 110                  }
 111  
 112                  $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
 113                  if ( !$fit ) {
 114                      $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
 115                      break;
 116                  }
 117              } else {
 118                  $titles[] = $title;
 119              }
 120          }
 121          if ( is_null( $resultPageSet ) ) {
 122              $result->setIndexedTagName_internal(
 123                  array( 'query', $this->getModuleName(), 'results' ),
 124                  'page'
 125              );
 126          } else {
 127              $resultPageSet->populateFromTitles( $titles );
 128          }
 129      }
 130  
 131  	public function getCacheMode( $params ) {
 132          /** @var $qp QueryPage */
 133          $qp = new $this->qpMap[$params['page']]();
 134          if ( $qp->getRestriction() != '' ) {
 135              return 'private';
 136          }
 137  
 138          return 'public';
 139      }
 140  
 141  	public function getAllowedParams() {
 142          return array(
 143              'page' => array(
 144                  ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
 145                  ApiBase::PARAM_REQUIRED => true
 146              ),
 147              'offset' => 0,
 148              'limit' => array(
 149                  ApiBase::PARAM_DFLT => 10,
 150                  ApiBase::PARAM_TYPE => 'limit',
 151                  ApiBase::PARAM_MIN => 1,
 152                  ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 153                  ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 154              ),
 155          );
 156      }
 157  
 158  	public function getParamDescription() {
 159          return array(
 160              'page' => 'The name of the special page. Note, this is case sensitive',
 161              'offset' => 'When more results are available, use this to continue',
 162              'limit' => 'Number of results to return',
 163          );
 164      }
 165  
 166  	public function getDescription() {
 167          return 'Get a list provided by a QueryPage-based special page.';
 168      }
 169  
 170  	public function getExamples() {
 171          return array(
 172              'api.php?action=query&list=querypage&qppage=Ancientpages'
 173          );
 174      }
 175  
 176  	public function getHelpUrls() {
 177          return 'https://www.mediawiki.org/wiki/API:Querypage';
 178      }
 179  }


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