MediaWiki  REL1_24
ApiQueryFilearchive.php
Go to the documentation of this file.
00001 <?php
00034 class ApiQueryFilearchive extends ApiQueryBase {
00035 
00036     public function __construct( ApiQuery $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(
00045                 'You don\'t have permission to view deleted file information',
00046                 'permissiondenied'
00047             );
00048         }
00049 
00050         $db = $this->getDB();
00051 
00052         $params = $this->extractRequestParams();
00053 
00054         $prop = array_flip( $params['prop'] );
00055         $fld_sha1 = isset( $prop['sha1'] );
00056         $fld_timestamp = isset( $prop['timestamp'] );
00057         $fld_user = isset( $prop['user'] );
00058         $fld_size = isset( $prop['size'] );
00059         $fld_dimensions = isset( $prop['dimensions'] );
00060         $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
00061         $fld_mime = isset( $prop['mime'] );
00062         $fld_mediatype = isset( $prop['mediatype'] );
00063         $fld_metadata = isset( $prop['metadata'] );
00064         $fld_bitdepth = isset( $prop['bitdepth'] );
00065         $fld_archivename = isset( $prop['archivename'] );
00066 
00067         $this->addTables( 'filearchive' );
00068 
00069         $this->addFields( ArchivedFile::selectFields() );
00070         $this->addFields( array( 'fa_id', 'fa_name', 'fa_timestamp', 'fa_deleted' ) );
00071         $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
00072         $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
00073         $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
00074         $this->addFieldsIf( 'fa_description', $fld_description );
00075         $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
00076         $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
00077         $this->addFieldsIf( 'fa_metadata', $fld_metadata );
00078         $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
00079         $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
00080 
00081         if ( !is_null( $params['continue'] ) ) {
00082             $cont = explode( '|', $params['continue'] );
00083             $this->dieContinueUsageIf( count( $cont ) != 3 );
00084             $op = $params['dir'] == 'descending' ? '<' : '>';
00085             $cont_from = $db->addQuotes( $cont[0] );
00086             $cont_timestamp = $db->addQuotes( $db->timestamp( $cont[1] ) );
00087             $cont_id = (int)$cont[2];
00088             $this->dieContinueUsageIf( $cont[2] !== (string)$cont_id );
00089             $this->addWhere( "fa_name $op $cont_from OR " .
00090                 "(fa_name = $cont_from AND " .
00091                 "(fa_timestamp $op $cont_timestamp OR " .
00092                 "(fa_timestamp = $cont_timestamp AND " .
00093                 "fa_id $op= $cont_id )))"
00094             );
00095         }
00096 
00097         // Image filters
00098         $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00099         $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
00100         $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
00101         $this->addWhereRange( 'fa_name', $dir, $from, $to );
00102         if ( isset( $params['prefix'] ) ) {
00103             $this->addWhere( 'fa_name' . $db->buildLike(
00104                 $this->titlePartToKey( $params['prefix'], NS_FILE ),
00105                 $db->anyString() ) );
00106         }
00107 
00108         $sha1Set = isset( $params['sha1'] );
00109         $sha1base36Set = isset( $params['sha1base36'] );
00110         if ( $sha1Set || $sha1base36Set ) {
00111             $sha1 = false;
00112             if ( $sha1Set ) {
00113                 $sha1 = strtolower( $params['sha1'] );
00114                 if ( !$this->validateSha1Hash( $sha1 ) ) {
00115                     $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
00116                 }
00117                 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
00118             } elseif ( $sha1base36Set ) {
00119                 $sha1 = strtolower( $params['sha1base36'] );
00120                 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
00121                     $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
00122                 }
00123             }
00124             if ( $sha1 ) {
00125                 $this->addWhereFld( 'fa_sha1', $sha1 );
00126             }
00127         }
00128 
00129         // Exclude files this user can't view.
00130         if ( !$user->isAllowed( 'deletedtext' ) ) {
00131             $bitmask = File::DELETED_FILE;
00132         } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
00133             $bitmask = File::DELETED_FILE | File::DELETED_RESTRICTED;
00134         } else {
00135             $bitmask = 0;
00136         }
00137         if ( $bitmask ) {
00138             $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
00139         }
00140 
00141         $limit = $params['limit'];
00142         $this->addOption( 'LIMIT', $limit + 1 );
00143         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00144         $this->addOption( 'ORDER BY', array(
00145             'fa_name' . $sort,
00146             'fa_timestamp' . $sort,
00147             'fa_id' . $sort,
00148         ) );
00149 
00150         $res = $this->select( __METHOD__ );
00151 
00152         $count = 0;
00153         $result = $this->getResult();
00154         foreach ( $res as $row ) {
00155             if ( ++$count > $limit ) {
00156                 // We've reached the one extra which shows that there are
00157                 // additional pages to be had. Stop here...
00158                 $this->setContinueEnumParameter(
00159                     'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
00160                 );
00161                 break;
00162             }
00163 
00164             $file = array();
00165             $file['id'] = $row->fa_id;
00166             $file['name'] = $row->fa_name;
00167             $title = Title::makeTitle( NS_FILE, $row->fa_name );
00168             self::addTitleInfo( $file, $title );
00169 
00170             if ( $fld_description &&
00171                 Revision::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user )
00172             ) {
00173                 $file['description'] = $row->fa_description;
00174                 if ( isset( $prop['parseddescription'] ) ) {
00175                     $file['parseddescription'] = Linker::formatComment(
00176                         $row->fa_description, $title );
00177                 }
00178             }
00179             if ( $fld_user &&
00180                 Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
00181             ) {
00182                 $file['userid'] = $row->fa_user;
00183                 $file['user'] = $row->fa_user_text;
00184             }
00185             if ( $fld_sha1 ) {
00186                 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
00187             }
00188             if ( $fld_timestamp ) {
00189                 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
00190             }
00191             if ( $fld_size || $fld_dimensions ) {
00192                 $file['size'] = $row->fa_size;
00193 
00194                 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
00195                 if ( $pageCount !== false ) {
00196                     $vals['pagecount'] = $pageCount;
00197                 }
00198 
00199                 $file['height'] = $row->fa_height;
00200                 $file['width'] = $row->fa_width;
00201             }
00202             if ( $fld_mediatype ) {
00203                 $file['mediatype'] = $row->fa_media_type;
00204             }
00205             if ( $fld_metadata ) {
00206                 $file['metadata'] = $row->fa_metadata
00207                     ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
00208                     : null;
00209             }
00210             if ( $fld_bitdepth ) {
00211                 $file['bitdepth'] = $row->fa_bits;
00212             }
00213             if ( $fld_mime ) {
00214                 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
00215             }
00216             if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
00217                 $file['archivename'] = $row->fa_archive_name;
00218             }
00219 
00220             if ( $row->fa_deleted & File::DELETED_FILE ) {
00221                 $file['filehidden'] = '';
00222             }
00223             if ( $row->fa_deleted & File::DELETED_COMMENT ) {
00224                 $file['commenthidden'] = '';
00225             }
00226             if ( $row->fa_deleted & File::DELETED_USER ) {
00227                 $file['userhidden'] = '';
00228             }
00229             if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
00230                 // This file is deleted for normal admins
00231                 $file['suppressed'] = '';
00232             }
00233 
00234             $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
00235             if ( !$fit ) {
00236                 $this->setContinueEnumParameter(
00237                     'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
00238                 );
00239                 break;
00240             }
00241         }
00242 
00243         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
00244     }
00245 
00246     public function getAllowedParams() {
00247         return array(
00248             'from' => null,
00249             'continue' => null,
00250             'to' => null,
00251             'prefix' => null,
00252             'limit' => array(
00253                 ApiBase::PARAM_DFLT => 10,
00254                 ApiBase::PARAM_TYPE => 'limit',
00255                 ApiBase::PARAM_MIN => 1,
00256                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00257                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00258             ),
00259             'dir' => array(
00260                 ApiBase::PARAM_DFLT => 'ascending',
00261                 ApiBase::PARAM_TYPE => array(
00262                     'ascending',
00263                     'descending'
00264                 )
00265             ),
00266             'sha1' => null,
00267             'sha1base36' => null,
00268             'prop' => array(
00269                 ApiBase::PARAM_DFLT => 'timestamp',
00270                 ApiBase::PARAM_ISMULTI => true,
00271                 ApiBase::PARAM_TYPE => array(
00272                     'sha1',
00273                     'timestamp',
00274                     'user',
00275                     'size',
00276                     'dimensions',
00277                     'description',
00278                     'parseddescription',
00279                     'mime',
00280                     'mediatype',
00281                     'metadata',
00282                     'bitdepth',
00283                     'archivename',
00284                 ),
00285             ),
00286         );
00287     }
00288 
00289     public function getParamDescription() {
00290         return array(
00291             'from' => 'The image title to start enumerating from',
00292             'continue' => 'When more results are available, use this to continue',
00293             'to' => 'The image title to stop enumerating at',
00294             'prefix' => 'Search for all image titles that begin with this value',
00295             'dir' => 'The direction in which to list',
00296             'limit' => 'How many images to return in total',
00297             'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
00298             'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
00299             'prop' => array(
00300                 'What image information to get:',
00301                 ' sha1              - Adds SHA-1 hash for the image',
00302                 ' timestamp         - Adds timestamp for the uploaded version',
00303                 ' user              - Adds user who uploaded the image version',
00304                 ' size              - Adds the size of the image in bytes and the height, ' .
00305                     'width and page count (if applicable)',
00306                 ' dimensions        - Alias for size',
00307                 ' description       - Adds description the image version',
00308                 ' parseddescription - Parse the description on the version',
00309                 ' mime              - Adds MIME of the image',
00310                 ' mediatype         - Adds the media type of the image',
00311                 ' metadata          - Lists Exif metadata for the version of the image',
00312                 ' bitdepth          - Adds the bit depth of the version',
00313                 ' archivename       - Adds the file name of the archive version for non-latest versions'
00314             ),
00315         );
00316     }
00317 
00318     public function getDescription() {
00319         return 'Enumerate all deleted files sequentially.';
00320     }
00321 
00322     public function getExamples() {
00323         return array(
00324             'api.php?action=query&list=filearchive' => array(
00325                 'Simple Use',
00326                 'Show a list of all deleted files',
00327             ),
00328         );
00329     }
00330 
00331     public function getHelpUrls() {
00332         return 'https://www.mediawiki.org/wiki/API:Filearchive';
00333     }
00334 }