[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

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

   1  <?php
   2  /**
   3   *
   4   *
   5   * Created on Oct 4, 2008
   6   *
   7   * Copyright © 2008 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   * This query action allows clients to retrieve a list of pages
  29   * on the logged-in user's watchlist.
  30   *
  31   * @ingroup API
  32   */
  33  class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
  34  
  35  	public function __construct( ApiQuery $query, $moduleName ) {
  36          parent::__construct( $query, $moduleName, 'wr' );
  37      }
  38  
  39  	public function execute() {
  40          $this->run();
  41      }
  42  
  43  	public function executeGenerator( $resultPageSet ) {
  44          $this->run( $resultPageSet );
  45      }
  46  
  47      /**
  48       * @param ApiPageSet $resultPageSet
  49       * @return void
  50       */
  51  	private function run( $resultPageSet = null ) {
  52          $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
  53  
  54          $params = $this->extractRequestParams();
  55  
  56          $user = $this->getWatchlistUser( $params );
  57  
  58          $prop = array_flip( (array)$params['prop'] );
  59          $show = array_flip( (array)$params['show'] );
  60          if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) {
  61              $this->dieUsageMsg( 'show' );
  62          }
  63  
  64          $this->addTables( 'watchlist' );
  65          $this->addFields( array( 'wl_namespace', 'wl_title' ) );
  66          $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) );
  67          $this->addWhereFld( 'wl_user', $user->getId() );
  68          $this->addWhereFld( 'wl_namespace', $params['namespace'] );
  69          $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) );
  70          $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) );
  71  
  72          if ( isset( $params['continue'] ) ) {
  73              $cont = explode( '|', $params['continue'] );
  74              $this->dieContinueUsageIf( count( $cont ) != 2 );
  75              $ns = intval( $cont[0] );
  76              $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
  77              $title = $this->getDB()->addQuotes( $cont[1] );
  78              $op = $params['dir'] == 'ascending' ? '>' : '<';
  79              $this->addWhere(
  80                  "wl_namespace $op $ns OR " .
  81                  "(wl_namespace = $ns AND " .
  82                  "wl_title $op= $title)"
  83              );
  84          }
  85  
  86          $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
  87          // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
  88          if ( count( $params['namespace'] ) == 1 ) {
  89              $this->addOption( 'ORDER BY', 'wl_title' . $sort );
  90          } else {
  91              $this->addOption( 'ORDER BY', array(
  92                  'wl_namespace' . $sort,
  93                  'wl_title' . $sort
  94              ) );
  95          }
  96          $this->addOption( 'LIMIT', $params['limit'] + 1 );
  97          $res = $this->select( __METHOD__ );
  98  
  99          $titles = array();
 100          $count = 0;
 101          foreach ( $res as $row ) {
 102              if ( ++$count > $params['limit'] ) {
 103                  // We've reached the one extra which shows that there are
 104                  // additional pages to be had. Stop here...
 105                  $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title );
 106                  break;
 107              }
 108              $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
 109  
 110              if ( is_null( $resultPageSet ) ) {
 111                  $vals = array();
 112                  ApiQueryBase::addTitleInfo( $vals, $t );
 113                  if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) ) {
 114                      $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
 115                  }
 116                  $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
 117                  if ( !$fit ) {
 118                      $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title );
 119                      break;
 120                  }
 121              } else {
 122                  $titles[] = $t;
 123              }
 124          }
 125          if ( is_null( $resultPageSet ) ) {
 126              $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
 127          } else {
 128              $resultPageSet->populateFromTitles( $titles );
 129          }
 130      }
 131  
 132  	public function getAllowedParams() {
 133          return array(
 134              'continue' => null,
 135              'namespace' => array(
 136                  ApiBase::PARAM_ISMULTI => true,
 137                  ApiBase::PARAM_TYPE => 'namespace'
 138              ),
 139              'limit' => array(
 140                  ApiBase::PARAM_DFLT => 10,
 141                  ApiBase::PARAM_TYPE => 'limit',
 142                  ApiBase::PARAM_MIN => 1,
 143                  ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 144                  ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 145              ),
 146              'prop' => array(
 147                  ApiBase::PARAM_ISMULTI => true,
 148                  ApiBase::PARAM_TYPE => array(
 149                      'changed',
 150                  )
 151              ),
 152              'show' => array(
 153                  ApiBase::PARAM_ISMULTI => true,
 154                  ApiBase::PARAM_TYPE => array(
 155                      'changed',
 156                      '!changed',
 157                  )
 158              ),
 159              'owner' => array(
 160                  ApiBase::PARAM_TYPE => 'user'
 161              ),
 162              'token' => array(
 163                  ApiBase::PARAM_TYPE => 'string'
 164              ),
 165              'dir' => array(
 166                  ApiBase::PARAM_DFLT => 'ascending',
 167                  ApiBase::PARAM_TYPE => array(
 168                      'ascending',
 169                      'descending'
 170                  ),
 171              ),
 172          );
 173      }
 174  
 175  	public function getParamDescription() {
 176          return array(
 177              'continue' => 'When more results are available, use this to continue',
 178              'namespace' => 'Only list pages in the given namespace(s)',
 179              'limit' => 'How many total results to return per request',
 180              'prop' => array(
 181                  'Which additional properties to get (non-generator mode only)',
 182                  ' changed  - Adds timestamp of when the user was last notified about the edit',
 183              ),
 184              'show' => 'Only list items that meet these criteria',
 185              'owner' => 'The name of the user whose watchlist you\'d like to access',
 186              'token' => 'Give a security token (settable in preferences) to allow ' .
 187                  'access to another user\'s watchlist',
 188              'dir' => 'Direction to sort the titles and namespaces in',
 189          );
 190      }
 191  
 192  	public function getDescription() {
 193          return "Get all pages on the logged in user's watchlist.";
 194      }
 195  
 196  	public function getExamples() {
 197          return array(
 198              'api.php?action=query&list=watchlistraw',
 199              'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
 200          );
 201      }
 202  
 203  	public function getHelpUrls() {
 204          return 'https://www.mediawiki.org/wiki/API:Watchlistraw';
 205      }
 206  }


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