MediaWiki  REL1_19
ApiQueryDuplicateFiles.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'df' );
00036         }
00037 
00038         public function execute() {
00039                 $this->run();
00040         }
00041 
00042         public function getCacheMode( $params ) {
00043                 return 'public';
00044         }
00045 
00046         public function executeGenerator( $resultPageSet ) {
00047                 $this->run( $resultPageSet );
00048         }
00049 
00054         private function run( $resultPageSet = null ) {
00055                 $params = $this->extractRequestParams();
00056                 $namespaces = $this->getPageSet()->getAllTitlesByNamespace();
00057                 if ( empty( $namespaces[NS_FILE] ) ) {
00058                         return;
00059                 }
00060                 $images = $namespaces[NS_FILE];
00061 
00062                 $this->addTables( 'image', 'i1' );
00063                 $this->addTables( 'image', 'i2' );
00064                 $this->addFields( array(
00065                         'i1.img_name AS orig_name',
00066                         'i2.img_name AS dup_name',
00067                         'i2.img_user_text AS dup_user_text',
00068                         'i2.img_timestamp AS dup_timestamp'
00069                 ) );
00070 
00071                 $this->addWhere( array(
00072                         'i1.img_name' => array_keys( $images ),
00073                         'i1.img_sha1 = i2.img_sha1',
00074                         'i1.img_name != i2.img_name',
00075                 ) );
00076 
00077                 if ( isset( $params['continue'] ) ) {
00078                         $cont = explode( '|', $params['continue'] );
00079                         if ( count( $cont ) != 2 ) {
00080                                 $this->dieUsage( 'Invalid continue param. You should pass the ' .
00081                                         'original value returned by the previous query', '_badcontinue' );
00082                         }
00083                         $orig = $this->getDB()->strencode( $this->titleTokey( $cont[0] ) );
00084                         $dup = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
00085                         $this->addWhere(
00086                                 "i1.img_name > '$orig' OR " .
00087                                 "(i1.img_name = '$orig' AND " .
00088                                 "i2.img_name >= '$dup')"
00089                         );
00090                 }
00091 
00092                 $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00093                 $this->addOption( 'ORDER BY', 'i1.img_name' . $dir );
00094                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00095 
00096                 $res = $this->select( __METHOD__ );
00097                 $count = 0;
00098                 $titles = array();
00099                 foreach ( $res as $row ) {
00100                         if ( ++$count > $params['limit'] ) {
00101                                 // We've reached the one extra which shows that
00102                                 // there are additional pages to be had. Stop here...
00103                                 $this->setContinueEnumParameter( 'continue',
00104                                         $this->keyToTitle( $row->orig_name ) . '|' .
00105                                         $this->keyToTitle( $row->dup_name ) );
00106                                 break;
00107                         }
00108                         if ( !is_null( $resultPageSet ) ) {
00109                                 $titles[] = Title::makeTitle( NS_FILE, $row->dup_name );
00110                         } else {
00111                                 $r = array(
00112                                         'name' => $row->dup_name,
00113                                         'user' => $row->dup_user_text,
00114                                         'timestamp' => wfTimestamp( TS_ISO_8601, $row->dup_timestamp )
00115                                 );
00116                                 $fit = $this->addPageSubItem( $images[$row->orig_name], $r );
00117                                 if ( !$fit ) {
00118                                         $this->setContinueEnumParameter( 'continue',
00119                                                         $this->keyToTitle( $row->orig_name ) . '|' .
00120                                                         $this->keyToTitle( $row->dup_name ) );
00121                                         break;
00122                                 }
00123                         }
00124                 }
00125                 if ( !is_null( $resultPageSet ) ) {
00126                         $resultPageSet->populateFromTitles( $titles );
00127                 }
00128         }
00129 
00130         public function getAllowedParams() {
00131                 return array(
00132                         'limit' => array(
00133                                 ApiBase::PARAM_DFLT => 10,
00134                                 ApiBase::PARAM_TYPE => 'limit',
00135                                 ApiBase::PARAM_MIN => 1,
00136                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00137                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00138                         ),
00139                         'continue' => null,
00140                         'dir' => array(
00141                                 ApiBase::PARAM_DFLT => 'ascending',
00142                                 ApiBase::PARAM_TYPE => array(
00143                                         'ascending',
00144                                         'descending'
00145                                 )
00146                         ),
00147                 );
00148         }
00149 
00150         public function getParamDescription() {
00151                 return array(
00152                         'limit' => 'How many files to return',
00153                         'continue' => 'When more results are available, use this to continue',
00154                         'dir' => 'The direction in which to list',
00155                 );
00156         }
00157 
00158         public function getDescription() {
00159                 return 'List all files that are duplicates of the given file(s)';
00160         }
00161 
00162         public function getPossibleErrors() {
00163                 return array_merge( parent::getPossibleErrors(), array(
00164                         array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00165                 ) );
00166         }
00167 
00168         public function getExamples() {
00169                 return array(
00170                         'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
00171                         'api.php?action=query&generator=allimages&prop=duplicatefiles',
00172                 );
00173         }
00174 
00175         public function getHelpUrls() {
00176                 return 'https://www.mediawiki.org/wiki/API:Properties#duplicatefiles_.2F_df';
00177         }
00178 
00179         public function getVersion() {
00180                 return __CLASS__ . ': $Id$';
00181         }
00182 }