Removed the 'eclipse helper' bit on top of every API module
[lhc/web/wiklou.git] / includes / api / ApiQueryImages.php
index 4451985..b2a8af8 100644 (file)
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiQueryBase.php" );
-}
-
 /**
  * This query adds an <images> subelement to all pages with the list of images embedded into those pages.
  *
@@ -48,6 +43,9 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                $this->run( $resultPageSet );
        }
 
+       /**
+        * @param $resultPageSet ApiPageSet
+        */
        private function run( $resultPageSet = null ) {
                if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
                        return; // nothing to do
@@ -76,14 +74,31 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                        );
                }
 
+               $dir = ( $params['dir'] == 'descending' ? ' DESC' : '' );
                // Don't order by il_from if it's constant in the WHERE clause
                if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
-                       $this->addOption( 'ORDER BY', 'il_to' );
+                       $this->addOption( 'ORDER BY', 'il_to' . $dir );
                } else {
-                       $this->addOption( 'ORDER BY', 'il_from, il_to' );
+                       $this->addOption( 'ORDER BY', array(
+                                               'il_from' . $dir,
+                                               'il_to' . $dir
+                       ));
                }
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
 
+               if ( !is_null( $params['images'] ) ) {
+                       $images = array();
+                       foreach ( $params['images'] as $img ) {
+                               $title = Title::newFromText( $img );
+                               if ( !$title || $title->getNamespace() != NS_FILE ) {
+                                       $this->setWarning( "``$img'' is not a file" );
+                               } else {
+                                       $images[] = $title->getDBkey();
+                               }
+                       }
+                       $this->addWhereFld( 'il_to', $images );
+               }
+
                $res = $this->select( __METHOD__ );
 
                if ( is_null( $resultPageSet ) ) {
@@ -136,6 +151,16 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
                        ),
                        'continue' => null,
+                       'images' => array(
+                               ApiBase::PARAM_ISMULTI => true,
+                       ),
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               )
+                       ),
                );
        }
 
@@ -143,6 +168,8 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                return array(
                        'limit' => 'How many images to return',
                        'continue' => 'When more results are available, use this to continue',
+                       'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
+                       'dir' => 'The direction in which to list',
                );
        }
 
@@ -156,7 +183,7 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
                        'Get a list of images used in the [[Main Page]]:',
                        '  api.php?action=query&prop=images&titles=Main%20Page',
@@ -165,6 +192,10 @@ class ApiQueryImages extends ApiQueryGeneratorBase {
                );
        }
 
+       public function getHelpUrls() {
+               return 'http://www.mediawiki.org/wiki/API:Properties#images_.2F_im';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }