MediaWiki  REL1_24
ApiQueryPageProps.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryPageProps extends ApiQueryBase {
00033 
00034     private $params;
00035 
00036     public function __construct( ApiQuery $query, $moduleName ) {
00037         parent::__construct( $query, $moduleName, 'pp' );
00038     }
00039 
00040     public function execute() {
00041         # Only operate on existing pages
00042         $pages = $this->getPageSet()->getGoodTitles();
00043         if ( !count( $pages ) ) {
00044             # Nothing to do
00045             return;
00046         }
00047 
00048         $this->params = $this->extractRequestParams();
00049 
00050         $this->addTables( 'page_props' );
00051         $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
00052         $this->addWhereFld( 'pp_page', array_keys( $pages ) );
00053 
00054         if ( $this->params['continue'] ) {
00055             $this->addWhere( 'pp_page >=' . intval( $this->params['continue'] ) );
00056         }
00057 
00058         if ( $this->params['prop'] ) {
00059             $this->addWhereFld( 'pp_propname', $this->params['prop'] );
00060         }
00061 
00062         # Force a sort order to ensure that properties are grouped by page
00063         # But only if pp_page is not constant in the WHERE clause.
00064         if ( count( $pages ) > 1 ) {
00065             $this->addOption( 'ORDER BY', 'pp_page' );
00066         }
00067 
00068         $res = $this->select( __METHOD__ );
00069         $currentPage = 0; # Id of the page currently processed
00070         $props = array();
00071         $result = $this->getResult();
00072 
00073         foreach ( $res as $row ) {
00074             if ( $currentPage != $row->pp_page ) {
00075                 # Different page than previous row, so add the properties to
00076                 # the result and save the new page id
00077 
00078                 if ( $currentPage ) {
00079                     if ( !$this->addPageProps( $result, $currentPage, $props ) ) {
00080                         # addPageProps() indicated that the result did not fit
00081                         # so stop adding data. Reset props so that it doesn't
00082                         # get added again after loop exit
00083 
00084                         $props = array();
00085                         break;
00086                     }
00087 
00088                     $props = array();
00089                 }
00090 
00091                 $currentPage = $row->pp_page;
00092             }
00093 
00094             $props[$row->pp_propname] = $row->pp_value;
00095         }
00096 
00097         if ( count( $props ) ) {
00098             # Add any remaining properties to the results
00099             $this->addPageProps( $result, $currentPage, $props );
00100         }
00101     }
00102 
00112     private function addPageProps( $result, $page, $props ) {
00113         $fit = $result->addValue( array( 'query', 'pages', $page ), 'pageprops', $props );
00114 
00115         if ( !$fit ) {
00116             $this->setContinueEnumParameter( 'continue', $page );
00117         }
00118 
00119         return $fit;
00120     }
00121 
00122     public function getCacheMode( $params ) {
00123         return 'public';
00124     }
00125 
00126     public function getAllowedParams() {
00127         return array(
00128             'continue' => null,
00129             'prop' => array(
00130                 ApiBase::PARAM_ISMULTI => true,
00131             ),
00132         );
00133     }
00134 
00135     public function getParamDescription() {
00136         return array(
00137             'continue' => 'When more results are available, use this to continue',
00138             'prop' => 'Only list these props. Useful for checking whether a ' .
00139                 'certain page uses a certain page prop',
00140         );
00141     }
00142 
00143     public function getDescription() {
00144         return 'Get various properties defined in the page content.';
00145     }
00146 
00147     public function getExamples() {
00148         return array(
00149             'api.php?action=query&prop=pageprops&titles=Category:Foo',
00150         );
00151     }
00152 
00153     public function getHelpUrls() {
00154         return 'https://www.mediawiki.org/wiki/API:Properties#pageprops_.2F_pp';
00155     }
00156 }