00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( "ApiQueryBase.php" );
00029 }
00030
00036 class ApiQueryDuplicateFiles extends ApiQueryGeneratorBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'df' );
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function getCacheMode( $params ) {
00047 return 'public';
00048 }
00049
00050 public function executeGenerator( $resultPageSet ) {
00051 $this->run( $resultPageSet );
00052 }
00053
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 {
00079 $cont = explode( '|', $params['continue'] );
00080 if ( count( $cont ) != 2 )
00081 $this->dieUsage( "Invalid continue param. You should pass the " .
00082 "original value returned by the previous query", "_badcontinue" );
00083 $orig = $this->getDB()->strencode( $this->titleTokey( $cont[0] ) );
00084 $dup = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
00085 $this->addWhere( "i1.img_name > '$orig' OR " .
00086 "(i1.img_name = '$orig' AND " .
00087 "i2.img_name >= '$dup')" );
00088 }
00089
00090 $this->addOption( 'ORDER BY', 'i1.img_name' );
00091 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00092
00093 $res = $this->select( __METHOD__ );
00094 $db = $this->getDB();
00095 $count = 0;
00096 $titles = array();
00097 while ( $row = $db->fetchObject( $res ) )
00098 {
00099 if ( ++$count > $params['limit'] )
00100 {
00101
00102
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 {
00112 $r = array(
00113 'name' => $row->dup_name,
00114 'user' => $row->dup_user_text,
00115 'timestamp' => wfTimestamp( TS_ISO_8601, $row->dup_timestamp )
00116 );
00117 $fit = $this->addPageSubItem( $images[$row->orig_name], $r );
00118 if ( !$fit )
00119 {
00120 $this->setContinueEnumParameter( 'continue',
00121 $this->keyToTitle( $row->orig_name ) . '|' .
00122 $this->keyToTitle( $row->dup_name ) );
00123 break;
00124 }
00125 }
00126 }
00127 if ( !is_null( $resultPageSet ) )
00128 $resultPageSet->populateFromTitles( $titles );
00129 $db->freeResult( $res );
00130 }
00131
00132 public function getAllowedParams() {
00133 return array (
00134 'limit' => array(
00135 ApiBase :: PARAM_DFLT => 10,
00136 ApiBase :: PARAM_TYPE => 'limit',
00137 ApiBase :: PARAM_MIN => 1,
00138 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00139 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00140 ),
00141 'continue' => null,
00142 );
00143 }
00144
00145 public function getParamDescription() {
00146 return array (
00147 'limit' => 'How many files to return',
00148 'continue' => 'When more results are available, use this to continue',
00149 );
00150 }
00151
00152 public function getDescription() {
00153 return 'List all files that are duplicates of the given file(s).';
00154 }
00155
00156 public function getPossibleErrors() {
00157 return array_merge( parent::getPossibleErrors(), array(
00158 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00159 ) );
00160 }
00161
00162 protected function getExamples() {
00163 return array ( 'api.php?action=query&titles=File:Albert_Einstein_Head.jpg&prop=duplicatefiles',
00164 'api.php?action=query&generator=allimages&prop=duplicatefiles',
00165 );
00166 }
00167
00168 public function getVersion() {
00169 return __CLASS__ . ': $Id: ApiQueryDuplicateFiles.php 69932 2010-07-26 08:03:21Z tstarling $';
00170 }
00171 }