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