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