MediaWiki  master
ApiQueryFilearchive.php
Go to the documentation of this file.
00001 <?php
00034 class ApiQueryFilearchive extends ApiQueryBase {
00035 
00036         public function __construct( $query, $moduleName ) {
00037                 parent::__construct( $query, $moduleName, 'fa' );
00038         }
00039 
00040         public function execute() {
00041                 $user = $this->getUser();
00042                 // Before doing anything at all, let's check permissions
00043                 if ( !$user->isAllowed( 'deletedhistory' ) ) {
00044                         $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
00045                 }
00046 
00047                 $db = $this->getDB();
00048 
00049                 $params = $this->extractRequestParams();
00050 
00051                 $prop = array_flip( $params['prop'] );
00052                 $fld_sha1 = isset( $prop['sha1'] );
00053                 $fld_timestamp = isset( $prop['timestamp'] );
00054                 $fld_user = isset( $prop['user'] );
00055                 $fld_size = isset( $prop['size'] );
00056                 $fld_dimensions = isset( $prop['dimensions'] );
00057                 $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
00058                 $fld_mime = isset( $prop['mime'] );
00059                 $fld_mediatype = isset( $prop['mediatype'] );
00060                 $fld_metadata = isset( $prop['metadata'] );
00061                 $fld_bitdepth = isset( $prop['bitdepth'] );
00062                 $fld_archivename = isset( $prop['archivename'] );
00063 
00064                 $this->addTables( 'filearchive' );
00065 
00066                 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
00067                 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
00068                 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
00069                 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
00070                 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
00071                 $this->addFieldsIf( 'fa_description', $fld_description );
00072                 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
00073                 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
00074                 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
00075                 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
00076                 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
00077 
00078                 if ( !is_null( $params['continue'] ) ) {
00079                         $cont = explode( '|', $params['continue'] );
00080                         if ( count( $cont ) != 1 ) {
00081                                 $this->dieUsage( "Invalid continue param. You should pass the " .
00082                                         "original value returned by the previous query", "_badcontinue" );
00083                         }
00084                         $op = $params['dir'] == 'descending' ? '<' : '>';
00085                         $cont_from = $db->addQuotes( $cont[0] );
00086                         $this->addWhere( "fa_name $op= $cont_from" );
00087                 }
00088 
00089                 // Image filters
00090                 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00091                 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
00092                 if ( !is_null( $params['continue'] ) ) {
00093                         $from = $params['continue'];
00094                 }
00095                 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
00096                 $this->addWhereRange( 'fa_name', $dir, $from, $to );
00097                 if ( isset( $params['prefix'] ) ) {
00098                         $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00099                 }
00100 
00101                 $sha1Set = isset( $params['sha1'] );
00102                 $sha1base36Set = isset( $params['sha1base36'] );
00103                 if ( $sha1Set || $sha1base36Set ) {
00104                         $sha1 = false;
00105                         if ( $sha1Set ) {
00106                                 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
00107                                         $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
00108                                 }
00109                                 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
00110                         } elseif ( $sha1base36Set ) {
00111                                 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
00112                                         $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
00113                                 }
00114                                 $sha1 = $params['sha1base36'];
00115                         }
00116                         if ( $sha1 ) {
00117                                 $this->addWhereFld( 'fa_sha1', $sha1 );
00118                         }
00119                 }
00120 
00121                 if ( !$user->isAllowed( 'suppressrevision' ) ) {
00122                         // Filter out revisions that the user is not allowed to see. There
00123                         // is no way to indicate that we have skipped stuff because the
00124                         // continuation parameter is fa_name
00125 
00126                         // Note that this field is unindexed. This should however not be
00127                         // a big problem as files with fa_deleted are rare
00128                         $this->addWhereFld( 'fa_deleted', 0 );
00129                 }
00130 
00131                 $limit = $params['limit'];
00132                 $this->addOption( 'LIMIT', $limit + 1 );
00133                 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00134                 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
00135 
00136                 $res = $this->select( __METHOD__ );
00137 
00138                 $count = 0;
00139                 $result = $this->getResult();
00140                 foreach ( $res as $row ) {
00141                         if ( ++$count > $limit ) {
00142                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00143                                 $this->setContinueEnumParameter( 'continue', $row->fa_name );
00144                                 break;
00145                         }
00146 
00147                         $file = array();
00148                         $file['name'] = $row->fa_name;
00149                         $title = Title::makeTitle( NS_FILE, $row->fa_name );
00150                         self::addTitleInfo( $file, $title );
00151 
00152                         if ( $fld_sha1 ) {
00153                                 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
00154                         }
00155                         if ( $fld_timestamp ) {
00156                                 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
00157                         }
00158                         if ( $fld_user ) {
00159                                 $file['userid'] = $row->fa_user;
00160                                 $file['user'] = $row->fa_user_text;
00161                         }
00162                         if ( $fld_size || $fld_dimensions ) {
00163                                 $file['size'] = $row->fa_size;
00164 
00165                                 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
00166                                 if ( $pageCount !== false ) {
00167                                         $vals['pagecount'] = $pageCount;
00168                                 }
00169 
00170                                 $file['height'] = $row->fa_height;
00171                                 $file['width'] = $row->fa_width;
00172                         }
00173                         if ( $fld_description ) {
00174                                 $file['description'] = $row->fa_description;
00175                                 if ( isset( $prop['parseddescription'] ) ) {
00176                                         $file['parseddescription'] = Linker::formatComment(
00177                                                 $row->fa_description, $title );
00178                                 }
00179                         }
00180                         if ( $fld_mediatype ) {
00181                                 $file['mediatype'] = $row->fa_media_type;
00182                         }
00183                         if ( $fld_metadata ) {
00184                                 $file['metadata'] = $row->fa_metadata
00185                                                 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
00186                                                 : null;
00187                         }
00188                         if ( $fld_bitdepth ) {
00189                                 $file['bitdepth'] = $row->fa_bits;
00190                         }
00191                         if ( $fld_mime ) {
00192                                 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
00193                         }
00194                         if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
00195                                 $file['archivename'] = $row->fa_archive_name;
00196                         }
00197 
00198                         if ( $row->fa_deleted & File::DELETED_FILE ) {
00199                                 $file['filehidden'] = '';
00200                         }
00201                         if ( $row->fa_deleted & File::DELETED_COMMENT ) {
00202                                 $file['commenthidden'] = '';
00203                         }
00204                         if ( $row->fa_deleted & File::DELETED_USER ) {
00205                                 $file['userhidden'] = '';
00206                         }
00207                         if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
00208                                 // This file is deleted for normal admins
00209                                 $file['suppressed'] = '';
00210                         }
00211 
00212 
00213                         $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
00214                         if ( !$fit ) {
00215                                 $this->setContinueEnumParameter( 'continue', $row->fa_name );
00216                                 break;
00217                         }
00218                 }
00219 
00220                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
00221         }
00222 
00223         public function getAllowedParams() {
00224                 return array (
00225                         'from' => null,
00226                         'continue' => null,
00227                         'to' => null,
00228                         'prefix' => null,
00229                         'limit' => array(
00230                                 ApiBase::PARAM_DFLT => 10,
00231                                 ApiBase::PARAM_TYPE => 'limit',
00232                                 ApiBase::PARAM_MIN => 1,
00233                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00234                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00235                         ),
00236                         'dir' => array(
00237                                 ApiBase::PARAM_DFLT => 'ascending',
00238                                 ApiBase::PARAM_TYPE => array(
00239                                         'ascending',
00240                                         'descending'
00241                                 )
00242                         ),
00243                         'sha1' => null,
00244                         'sha1base36' => null,
00245                         'prop' => array(
00246                                 ApiBase::PARAM_DFLT => 'timestamp',
00247                                 ApiBase::PARAM_ISMULTI => true,
00248                                 ApiBase::PARAM_TYPE => array(
00249                                         'sha1',
00250                                         'timestamp',
00251                                         'user',
00252                                         'size',
00253                                         'dimensions',
00254                                         'description',
00255                                         'parseddescription',
00256                                         'mime',
00257                                         'mediatype',
00258                                         'metadata',
00259                                         'bitdepth',
00260                                         'archivename',
00261                                 ),
00262                         ),
00263                 );
00264         }
00265 
00266         public function getParamDescription() {
00267                 return array(
00268                         'from' => 'The image title to start enumerating from',
00269                         'continue' => 'When more results are available, use this to continue',
00270                         'to' => 'The image title to stop enumerating at',
00271                         'prefix' => 'Search for all image titles that begin with this value',
00272                         'dir' => 'The direction in which to list',
00273                         'limit' => 'How many images to return in total',
00274                         'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
00275                         'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
00276                         'prop' => array(
00277                                 'What image information to get:',
00278                                 ' sha1              - Adds SHA-1 hash for the image',
00279                                 ' timestamp         - Adds timestamp for the uploaded version',
00280                                 ' user              - Adds user who uploaded the image version',
00281                                 ' size              - Adds the size of the image in bytes and the height, width and page count (if applicable)',
00282                                 ' dimensions        - Alias for size',
00283                                 ' description       - Adds description the image version',
00284                                 ' parseddescription - Parse the description on the version',
00285                                 ' mime              - Adds MIME of the image',
00286                                 ' mediatype         - Adds the media type of the image',
00287                                 ' metadata          - Lists EXIF metadata for the version of the image',
00288                                 ' bitdepth          - Adds the bit depth of the version',
00289                                 ' archivename       - Adds the file name of the archive version for non-latest versions'
00290                         ),
00291                 );
00292         }
00293 
00294         public function getResultProperties() {
00295                 return array(
00296                         '' => array(
00297                                 'name' => 'string',
00298                                 'ns' => 'namespace',
00299                                 'title' => 'string',
00300                                 'filehidden' => 'boolean',
00301                                 'commenthidden' => 'boolean',
00302                                 'userhidden' => 'boolean',
00303                                 'suppressed' => 'boolean'
00304                         ),
00305                         'sha1' => array(
00306                                 'sha1' => 'string'
00307                         ),
00308                         'timestamp' => array(
00309                                 'timestamp' => 'timestamp'
00310                         ),
00311                         'user' => array(
00312                                 'userid' => 'integer',
00313                                 'user' => 'string'
00314                         ),
00315                         'size' => array(
00316                                 'size' => 'integer',
00317                                 'pagecount' => array(
00318                                         ApiBase::PROP_TYPE => 'integer',
00319                                         ApiBase::PROP_NULLABLE => true
00320                                 ),
00321                                 'height' => 'integer',
00322                                 'width' => 'integer'
00323                         ),
00324                         'dimensions' => array(
00325                                 'size' => 'integer',
00326                                 'pagecount' => array(
00327                                         ApiBase::PROP_TYPE => 'integer',
00328                                         ApiBase::PROP_NULLABLE => true
00329                                 ),
00330                                 'height' => 'integer',
00331                                 'width' => 'integer'
00332                         ),
00333                         'description' => array(
00334                                 'description' => 'string'
00335                         ),
00336                         'parseddescription' => array(
00337                                 'description' => 'string',
00338                                 'parseddescription' => 'string'
00339                         ),
00340                         'metadata' => array(
00341                                 'metadata' => 'string'
00342                         ),
00343                         'bitdepth' => array(
00344                                 'bitdepth' => 'integer'
00345                         ),
00346                         'mime' => array(
00347                                 'mime' => 'string'
00348                         ),
00349                         'mediatype' => array(
00350                                 'mediatype' => 'string'
00351                         ),
00352                         'archivename' => array(
00353                                 'archivename' => 'string'
00354                         ),
00355                 );
00356         }
00357 
00358         public function getDescription() {
00359                 return 'Enumerate all deleted files sequentially';
00360         }
00361 
00362         public function getPossibleErrors() {
00363                 return array_merge( parent::getPossibleErrors(), array(
00364                         array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
00365                         array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
00366                         array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
00367                         array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
00368                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00369                 ) );
00370         }
00371 
00372         public function getExamples() {
00373                 return array(
00374                         'api.php?action=query&list=filearchive' => array(
00375                                 'Simple Use',
00376                                 'Show a list of all deleted files',
00377                         ),
00378                 );
00379         }
00380 
00381         public function getVersion() {
00382                 return __CLASS__ . ': $Id$';
00383         }
00384 }