MediaWiki  REL1_19
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_metadata = isset( $prop['metadata'] );
00060                 $fld_bitdepth = isset( $prop['bitdepth'] );
00061 
00062                 $this->addTables( 'filearchive' );
00063 
00064                 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
00065                 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
00066                 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
00067                 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
00068                 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
00069                 $this->addFieldsIf( 'fa_description', $fld_description );
00070                 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
00071                 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
00072                 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
00073 
00074                 // Image filters
00075                 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00076                 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
00077                 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
00078                 $this->addWhereRange( 'fa_name', $dir, $from, $to );
00079                 if ( isset( $params['prefix'] ) ) {
00080                         $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00081                 }
00082 
00083                 $sha1Set = isset( $params['sha1'] );
00084                 $sha1base36Set = isset( $params['sha1base36'] );
00085                 if ( $sha1Set || $sha1base36Set ) {
00086                         global $wgMiserMode;
00087                         if ( $wgMiserMode  ) {
00088                                 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
00089                         }
00090 
00091                         $sha1 = false;
00092                         if ( $sha1Set ) {
00093                                 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
00094                                         $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
00095                                 }
00096                                 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
00097                         } elseif ( $sha1base36Set ) {
00098                                 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
00099                                         $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
00100                                 }
00101                                 $sha1 = $params['sha1base36'];
00102                         }
00103                         if ( $sha1 ) {
00104                                 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
00105                         }
00106                 }
00107 
00108                 if ( !$user->isAllowed( 'suppressrevision' ) ) {
00109                         // Filter out revisions that the user is not allowed to see. There
00110                         // is no way to indicate that we have skipped stuff because the
00111                         // continuation parameter is fa_name
00112 
00113                         // Note that this field is unindexed. This should however not be
00114                         // a big problem as files with fa_deleted are rare
00115                         $this->addWhereFld( 'fa_deleted', 0 );
00116                 }
00117 
00118                 $limit = $params['limit'];
00119                 $this->addOption( 'LIMIT', $limit + 1 );
00120                 $this->addOption( 'ORDER BY', 'fa_name' .
00121                                                 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
00122 
00123                 $res = $this->select( __METHOD__ );
00124 
00125                 $count = 0;
00126                 $result = $this->getResult();
00127                 foreach ( $res as $row ) {
00128                         if ( ++$count > $limit ) {
00129                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00130                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
00131                                 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
00132                                 break;
00133                         }
00134 
00135                         $file = array();
00136                         $file['name'] = $row->fa_name;
00137                         $title = Title::makeTitle( NS_FILE, $row->fa_name );
00138                         self::addTitleInfo( $file, $title );
00139 
00140                         if ( $fld_sha1 ) {
00141                                 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
00142                         }
00143                         if ( $fld_timestamp ) {
00144                                 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
00145                         }
00146                         if ( $fld_user ) {
00147                                 $file['userid'] = $row->fa_user;
00148                                 $file['user'] = $row->fa_user_text;
00149                         }
00150                         if ( $fld_size || $fld_dimensions ) {
00151                                 $file['size'] = $row->fa_size;
00152 
00153                                 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
00154                                 if ( $pageCount !== false ) {
00155                                         $vals['pagecount'] = $pageCount;
00156                                 }
00157 
00158                                 $file['height'] = $row->fa_height;
00159                                 $file['width'] = $row->fa_width;
00160                         }
00161                         if ( $fld_description ) {
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_metadata ) {
00169                                 $file['metadata'] = $row->fa_metadata
00170                                                 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
00171                                                 : null;
00172                         }
00173                         if ( $fld_bitdepth ) {
00174                                 $file['bitdepth'] = $row->fa_bits;
00175                         }
00176                         if ( $fld_mime ) {
00177                                 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
00178                         }
00179 
00180                         if ( $row->fa_deleted & File::DELETED_FILE ) {
00181                                 $file['filehidden'] = '';
00182                         }
00183                         if ( $row->fa_deleted & File::DELETED_COMMENT ) {
00184                                 $file['commenthidden'] = '';
00185                         }
00186                         if ( $row->fa_deleted & File::DELETED_USER ) {
00187                                 $file['userhidden'] = '';
00188                         }
00189                         if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
00190                                 // This file is deleted for normal admins
00191                                 $file['suppressed'] = '';
00192                         }
00193 
00194 
00195                         $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
00196                         if ( !$fit ) {
00197                                 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
00198                                 break;
00199                         }
00200                 }
00201 
00202                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
00203         }
00204 
00205         public function getAllowedParams() {
00206                 return array (
00207                         'from' => null,
00208                         'to' => null,
00209                         'prefix' => null,
00210                         'limit' => array(
00211                                 ApiBase::PARAM_DFLT => 10,
00212                                 ApiBase::PARAM_TYPE => 'limit',
00213                                 ApiBase::PARAM_MIN => 1,
00214                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00215                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00216                         ),
00217                         'dir' => array(
00218                                 ApiBase::PARAM_DFLT => 'ascending',
00219                                 ApiBase::PARAM_TYPE => array(
00220                                         'ascending',
00221                                         'descending'
00222                                 )
00223                         ),
00224                         'sha1' => null,
00225                         'sha1base36' => null,
00226                         'prop' => array(
00227                                 ApiBase::PARAM_DFLT => 'timestamp',
00228                                 ApiBase::PARAM_ISMULTI => true,
00229                                 ApiBase::PARAM_TYPE => array(
00230                                         'sha1',
00231                                         'timestamp',
00232                                         'user',
00233                                         'size',
00234                                         'dimensions',
00235                                         'description',
00236                                         'parseddescription',
00237                                         'mime',
00238                                         'metadata',
00239                                         'bitdepth'
00240                                 ),
00241                         ),
00242                 );
00243         }
00244 
00245         public function getParamDescription() {
00246                 return array(
00247                         'from' => 'The image title to start enumerating from',
00248                         'to' => 'The image title to stop enumerating at',
00249                         'prefix' => 'Search for all image titles that begin with this value',
00250                         'dir' => 'The direction in which to list',
00251                         'limit' => 'How many images to return in total',
00252                         'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
00253                         'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
00254                         'prop' => array(
00255                                 'What image information to get:',
00256                                 ' sha1              - Adds SHA-1 hash for the image',
00257                                 ' timestamp         - Adds timestamp for the uploaded version',
00258                                 ' user              - Adds user who uploaded the image version',
00259                                 ' size              - Adds the size of the image in bytes and the height, width and page count (if applicable)',
00260                                 ' dimensions        - Alias for size',
00261                                 ' description       - Adds description the image version',
00262                                 ' parseddescription - Parse the description on the version',
00263                                 ' mime              - Adds MIME of the image',
00264                                 ' metadata          - Lists EXIF metadata for the version of the image',
00265                                 ' bitdepth          - Adds the bit depth of the version',
00266             ),
00267                 );
00268         }
00269 
00270         public function getDescription() {
00271                 return 'Enumerate all deleted files sequentially';
00272         }
00273 
00274         public function getPossibleErrors() {
00275                 return array_merge( parent::getPossibleErrors(), array(
00276                         array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
00277                         array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
00278                         array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
00279                         array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
00280                 ) );
00281         }
00282 
00283         public function getExamples() {
00284                 return array(
00285                         'api.php?action=query&list=filearchive' => array(
00286                                 'Simple Use',
00287                                 'Show a list of all deleted files',
00288                         ),
00289                 );
00290         }
00291 
00292         public function getVersion() {
00293                 return __CLASS__ . ': $Id$';
00294         }
00295 }