[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

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

   1  <?php
   2  /**
   3   * API for MediaWiki 1.12+
   4   *
   5   * Created on May 10, 2010
   6   *
   7   * Copyright © 2010 Sam Reed
   8   * Copyright © 2008 Vasiliev Victor [email protected],
   9   * based on ApiQueryAllPages.php
  10   *
  11   * This program is free software; you can redistribute it and/or modify
  12   * it under the terms of the GNU General Public License as published by
  13   * the Free Software Foundation; either version 2 of the License, or
  14   * (at your option) any later version.
  15   *
  16   * This program is distributed in the hope that it will be useful,
  17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19   * GNU General Public License for more details.
  20   *
  21   * You should have received a copy of the GNU General Public License along
  22   * with this program; if not, write to the Free Software Foundation, Inc.,
  23   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24   * http://www.gnu.org/copyleft/gpl.html
  25   *
  26   * @file
  27   */
  28  
  29  /**
  30   * Query module to enumerate all deleted files.
  31   *
  32   * @ingroup API
  33   */
  34  class ApiQueryFilearchive extends ApiQueryBase {
  35  
  36  	public function __construct( ApiQuery $query, $moduleName ) {
  37          parent::__construct( $query, $moduleName, 'fa' );
  38      }
  39  
  40  	public function execute() {
  41          $user = $this->getUser();
  42          // Before doing anything at all, let's check permissions
  43          if ( !$user->isAllowed( 'deletedhistory' ) ) {
  44              $this->dieUsage(
  45                  'You don\'t have permission to view deleted file information',
  46                  'permissiondenied'
  47              );
  48          }
  49  
  50          $db = $this->getDB();
  51  
  52          $params = $this->extractRequestParams();
  53  
  54          $prop = array_flip( $params['prop'] );
  55          $fld_sha1 = isset( $prop['sha1'] );
  56          $fld_timestamp = isset( $prop['timestamp'] );
  57          $fld_user = isset( $prop['user'] );
  58          $fld_size = isset( $prop['size'] );
  59          $fld_dimensions = isset( $prop['dimensions'] );
  60          $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
  61          $fld_mime = isset( $prop['mime'] );
  62          $fld_mediatype = isset( $prop['mediatype'] );
  63          $fld_metadata = isset( $prop['metadata'] );
  64          $fld_bitdepth = isset( $prop['bitdepth'] );
  65          $fld_archivename = isset( $prop['archivename'] );
  66  
  67          $this->addTables( 'filearchive' );
  68  
  69          $this->addFields( ArchivedFile::selectFields() );
  70          $this->addFields( array( 'fa_id', 'fa_name', 'fa_timestamp', 'fa_deleted' ) );
  71          $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
  72          $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
  73          $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
  74          $this->addFieldsIf( 'fa_description', $fld_description );
  75          $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
  76          $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
  77          $this->addFieldsIf( 'fa_metadata', $fld_metadata );
  78          $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
  79          $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
  80  
  81          if ( !is_null( $params['continue'] ) ) {
  82              $cont = explode( '|', $params['continue'] );
  83              $this->dieContinueUsageIf( count( $cont ) != 3 );
  84              $op = $params['dir'] == 'descending' ? '<' : '>';
  85              $cont_from = $db->addQuotes( $cont[0] );
  86              $cont_timestamp = $db->addQuotes( $db->timestamp( $cont[1] ) );
  87              $cont_id = (int)$cont[2];
  88              $this->dieContinueUsageIf( $cont[2] !== (string)$cont_id );
  89              $this->addWhere( "fa_name $op $cont_from OR " .
  90                  "(fa_name = $cont_from AND " .
  91                  "(fa_timestamp $op $cont_timestamp OR " .
  92                  "(fa_timestamp = $cont_timestamp AND " .
  93                  "fa_id $op= $cont_id )))"
  94              );
  95          }
  96  
  97          // Image filters
  98          $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
  99          $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
 100          $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
 101          $this->addWhereRange( 'fa_name', $dir, $from, $to );
 102          if ( isset( $params['prefix'] ) ) {
 103              $this->addWhere( 'fa_name' . $db->buildLike(
 104                  $this->titlePartToKey( $params['prefix'], NS_FILE ),
 105                  $db->anyString() ) );
 106          }
 107  
 108          $sha1Set = isset( $params['sha1'] );
 109          $sha1base36Set = isset( $params['sha1base36'] );
 110          if ( $sha1Set || $sha1base36Set ) {
 111              $sha1 = false;
 112              if ( $sha1Set ) {
 113                  $sha1 = strtolower( $params['sha1'] );
 114                  if ( !$this->validateSha1Hash( $sha1 ) ) {
 115                      $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
 116                  }
 117                  $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
 118              } elseif ( $sha1base36Set ) {
 119                  $sha1 = strtolower( $params['sha1base36'] );
 120                  if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
 121                      $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
 122                  }
 123              }
 124              if ( $sha1 ) {
 125                  $this->addWhereFld( 'fa_sha1', $sha1 );
 126              }
 127          }
 128  
 129          // Exclude files this user can't view.
 130          if ( !$user->isAllowed( 'deletedtext' ) ) {
 131              $bitmask = File::DELETED_FILE;
 132          } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
 133              $bitmask = File::DELETED_FILE | File::DELETED_RESTRICTED;
 134          } else {
 135              $bitmask = 0;
 136          }
 137          if ( $bitmask ) {
 138              $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
 139          }
 140  
 141          $limit = $params['limit'];
 142          $this->addOption( 'LIMIT', $limit + 1 );
 143          $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
 144          $this->addOption( 'ORDER BY', array(
 145              'fa_name' . $sort,
 146              'fa_timestamp' . $sort,
 147              'fa_id' . $sort,
 148          ) );
 149  
 150          $res = $this->select( __METHOD__ );
 151  
 152          $count = 0;
 153          $result = $this->getResult();
 154          foreach ( $res as $row ) {
 155              if ( ++$count > $limit ) {
 156                  // We've reached the one extra which shows that there are
 157                  // additional pages to be had. Stop here...
 158                  $this->setContinueEnumParameter(
 159                      'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
 160                  );
 161                  break;
 162              }
 163  
 164              $file = array();
 165              $file['id'] = $row->fa_id;
 166              $file['name'] = $row->fa_name;
 167              $title = Title::makeTitle( NS_FILE, $row->fa_name );
 168              self::addTitleInfo( $file, $title );
 169  
 170              if ( $fld_description &&
 171                  Revision::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user )
 172              ) {
 173                  $file['description'] = $row->fa_description;
 174                  if ( isset( $prop['parseddescription'] ) ) {
 175                      $file['parseddescription'] = Linker::formatComment(
 176                          $row->fa_description, $title );
 177                  }
 178              }
 179              if ( $fld_user &&
 180                  Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
 181              ) {
 182                  $file['userid'] = $row->fa_user;
 183                  $file['user'] = $row->fa_user_text;
 184              }
 185              if ( $fld_sha1 ) {
 186                  $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
 187              }
 188              if ( $fld_timestamp ) {
 189                  $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
 190              }
 191              if ( $fld_size || $fld_dimensions ) {
 192                  $file['size'] = $row->fa_size;
 193  
 194                  $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
 195                  if ( $pageCount !== false ) {
 196                      $vals['pagecount'] = $pageCount;
 197                  }
 198  
 199                  $file['height'] = $row->fa_height;
 200                  $file['width'] = $row->fa_width;
 201              }
 202              if ( $fld_mediatype ) {
 203                  $file['mediatype'] = $row->fa_media_type;
 204              }
 205              if ( $fld_metadata ) {
 206                  $file['metadata'] = $row->fa_metadata
 207                      ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
 208                      : null;
 209              }
 210              if ( $fld_bitdepth ) {
 211                  $file['bitdepth'] = $row->fa_bits;
 212              }
 213              if ( $fld_mime ) {
 214                  $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
 215              }
 216              if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
 217                  $file['archivename'] = $row->fa_archive_name;
 218              }
 219  
 220              if ( $row->fa_deleted & File::DELETED_FILE ) {
 221                  $file['filehidden'] = '';
 222              }
 223              if ( $row->fa_deleted & File::DELETED_COMMENT ) {
 224                  $file['commenthidden'] = '';
 225              }
 226              if ( $row->fa_deleted & File::DELETED_USER ) {
 227                  $file['userhidden'] = '';
 228              }
 229              if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
 230                  // This file is deleted for normal admins
 231                  $file['suppressed'] = '';
 232              }
 233  
 234              $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
 235              if ( !$fit ) {
 236                  $this->setContinueEnumParameter(
 237                      'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
 238                  );
 239                  break;
 240              }
 241          }
 242  
 243          $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
 244      }
 245  
 246  	public function getAllowedParams() {
 247          return array(
 248              'from' => null,
 249              'continue' => null,
 250              'to' => null,
 251              'prefix' => null,
 252              'limit' => array(
 253                  ApiBase::PARAM_DFLT => 10,
 254                  ApiBase::PARAM_TYPE => 'limit',
 255                  ApiBase::PARAM_MIN => 1,
 256                  ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 257                  ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 258              ),
 259              'dir' => array(
 260                  ApiBase::PARAM_DFLT => 'ascending',
 261                  ApiBase::PARAM_TYPE => array(
 262                      'ascending',
 263                      'descending'
 264                  )
 265              ),
 266              'sha1' => null,
 267              'sha1base36' => null,
 268              'prop' => array(
 269                  ApiBase::PARAM_DFLT => 'timestamp',
 270                  ApiBase::PARAM_ISMULTI => true,
 271                  ApiBase::PARAM_TYPE => array(
 272                      'sha1',
 273                      'timestamp',
 274                      'user',
 275                      'size',
 276                      'dimensions',
 277                      'description',
 278                      'parseddescription',
 279                      'mime',
 280                      'mediatype',
 281                      'metadata',
 282                      'bitdepth',
 283                      'archivename',
 284                  ),
 285              ),
 286          );
 287      }
 288  
 289  	public function getParamDescription() {
 290          return array(
 291              'from' => 'The image title to start enumerating from',
 292              'continue' => 'When more results are available, use this to continue',
 293              'to' => 'The image title to stop enumerating at',
 294              'prefix' => 'Search for all image titles that begin with this value',
 295              'dir' => 'The direction in which to list',
 296              'limit' => 'How many images to return in total',
 297              'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
 298              'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
 299              'prop' => array(
 300                  'What image information to get:',
 301                  ' sha1              - Adds SHA-1 hash for the image',
 302                  ' timestamp         - Adds timestamp for the uploaded version',
 303                  ' user              - Adds user who uploaded the image version',
 304                  ' size              - Adds the size of the image in bytes and the height, ' .
 305                      'width and page count (if applicable)',
 306                  ' dimensions        - Alias for size',
 307                  ' description       - Adds description the image version',
 308                  ' parseddescription - Parse the description on the version',
 309                  ' mime              - Adds MIME of the image',
 310                  ' mediatype         - Adds the media type of the image',
 311                  ' metadata          - Lists Exif metadata for the version of the image',
 312                  ' bitdepth          - Adds the bit depth of the version',
 313                  ' archivename       - Adds the file name of the archive version for non-latest versions'
 314              ),
 315          );
 316      }
 317  
 318  	public function getDescription() {
 319          return 'Enumerate all deleted files sequentially.';
 320      }
 321  
 322  	public function getExamples() {
 323          return array(
 324              'api.php?action=query&list=filearchive' => array(
 325                  'Simple Use',
 326                  'Show a list of all deleted files',
 327              ),
 328          );
 329      }
 330  
 331  	public function getHelpUrls() {
 332          return 'https://www.mediawiki.org/wiki/API:Filearchive';
 333      }
 334  }


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