HTML 2???
[lhc/web/wiklou.git] / includes / api / ApiQueryQueryPage.php
index bc4008f..5eba0de 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 /**
- * API for MediaWiki 1.8+
  *
- * Created on Sep 10, 2007
+ *
+ * Created on Dec 22, 2010
  *
  * Copyright © 2010 Roan Kattouw <Firstname>.<Lastname>@gmail.com
  *
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( 'ApiQueryBase.php' );
-}
-
 /**
  * Query module to get the results of a QueryPage-based special page
  *
@@ -36,41 +31,53 @@ if ( !defined( 'MEDIAWIKI' ) ) {
  */
 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
        private $qpMap;
-       
+
+       /**
+        * Some query pages are useless because they're available elsewhere in the API
+        */
+       private $uselessQueryPages = array(
+               'MIMEsearch', // aiprop=mime
+               'LinkSearch', // list=exturlusage
+               'FileDuplicateSearch', // prop=duplicatefiles
+       );
+
        public function __construct( $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'qp' );
-               
                // We need to do this to make sure $wgQueryPages is set up
                // This SUCKS
                global $IP;
                require_once( "$IP/includes/QueryPage.php" );
-               
+
                // Build mapping from special page names to QueryPage classes
                global $wgQueryPages;
                $this->qpMap = array();
                foreach ( $wgQueryPages as $page ) {
-                       $this->qpMap[$page[1]] = $page[0];
+                       if( !in_array( $page[1], $this->uselessQueryPages ) ) {
+                               $this->qpMap[$page[1]] = $page[0];
+                       }
                }
        }
-       
+
        public function execute() {
                $this->run();
        }
-       
+
        public function executeGenerator( $resultPageSet ) {
                $this->run( $resultPageSet );
        }
 
+       /**
+        * @param $resultPageSet ApiPageSet
+        */
        public function run( $resultPageSet = null ) {
-               global $wgUser;
                $params = $this->extractRequestParams();
                $result = $this->getResult();
-               
+
                $qp = new $this->qpMap[$params['page']]();
-               if ( !$qp->userCanExecute( $wgUser ) ) {
-                       $this->dieUsageMsg( array( 'specialpage-cantexecute' ) );
+               if ( !$qp->userCanExecute( $this->getUser() ) ) {
+                       $this->dieUsageMsg( 'specialpage-cantexecute' );
                }
-               
+
                $r = array( 'name' => $params['page'] );
                if ( $qp->isCached() ) {
                        if ( !$qp->isCacheable() ) {
@@ -79,13 +86,18 @@ class ApiQueryQueryPage extends ApiQueryGeneratorBase {
                                $r['cached'] = '';
                                $ts = $qp->getCachedTimestamp();
                                if ( $ts ) {
-                                       $r['cachedTimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
+                                       $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
                                }
                        }
                }
                $result->addValue( array( 'query' ), $this->getModuleName(), $r );
-               
-               $res = $qp->doQuery( $params['limit'] + 1, $params['offset'] );
+
+               if ( $qp->isCached() && !$qp->isCacheable() ) {
+                       // Disabled query page, don't run the query
+                       return;
+               }
+
+               $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
                $count = 0;
                $titles = array();
                foreach ( $res as $row ) {
@@ -94,7 +106,7 @@ class ApiQueryQueryPage extends ApiQueryGeneratorBase {
                                $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
                                break;
                        }
-                       
+
                        $title = Title::makeTitle( $row->namespace, $row->title );
                        if ( is_null( $resultPageSet ) ) {
                                $data = array( 'value' => $row->value );
@@ -102,13 +114,13 @@ class ApiQueryQueryPage extends ApiQueryGeneratorBase {
                                        $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
                                }
                                self::addTitleInfo( $data, $title );
-                               
+
                                foreach ( $row as $field => $value ) {
                                        if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
                                                $data['databaseResult'][$field] = $value;
                                        }
                                }
-                               
+
                                $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
                                if ( !$fit ) {
                                        $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
@@ -124,7 +136,7 @@ class ApiQueryQueryPage extends ApiQueryGeneratorBase {
                        $resultPageSet->populateFromTitles( $titles );
                }
        }
-       
+
        public function getCacheMode( $params ) {
                $qp = new $this->qpMap[$params['page']]();
                if ( $qp->getRestriction() != '' ) {
@@ -149,27 +161,28 @@ class ApiQueryQueryPage extends ApiQueryGeneratorBase {
                        ),
                );
        }
-       
+
        public function getParamDescription() {
                return array(
-                       'page' => 'The name of the special page',
+                       'page' => 'The name of the special page. Note, this is case sensitive',
                        'offset' => 'When more results are available, use this to continue',
                        'limit' => 'Number of results to return',
                );
        }
 
        public function getDescription() {
-               return 'Get a list provide by a QueryPage-based special page';
+               return 'Get a list provided by a QueryPage-based special page';
        }
 
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
+                        array( 'specialpage-cantexecute' )
                ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
-                       
+                       'api.php?action=query&list=querypage&qppage=Ancientpages'
                );
        }