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