[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Created on December 31, 2012 4 * 5 * Copyright © 2012 Brad Jorsch <[email protected]> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * http://www.gnu.org/copyleft/gpl.html 21 * 22 * @file 23 * @since 1.21 24 * @author Brad Jorsch 25 */ 26 27 /** 28 * A query module to enumerate pages that use a particular prop 29 * 30 * @ingroup API 31 * @since 1.21 32 */ 33 class ApiQueryPagesWithProp extends ApiQueryGeneratorBase { 34 35 public function __construct( ApiQuery $query, $moduleName ) { 36 parent::__construct( $query, $moduleName, 'pwp' ); 37 } 38 39 public function execute() { 40 $this->run(); 41 } 42 43 public function getCacheMode( $params ) { 44 return 'public'; 45 } 46 47 public function executeGenerator( $resultPageSet ) { 48 $this->run( $resultPageSet ); 49 } 50 51 /** 52 * @param ApiPageSet $resultPageSet 53 * @return void 54 */ 55 private function run( $resultPageSet = null ) { 56 $params = $this->extractRequestParams(); 57 58 $prop = array_flip( $params['prop'] ); 59 $fld_ids = isset( $prop['ids'] ); 60 $fld_title = isset( $prop['title'] ); 61 $fld_value = isset( $prop['value'] ); 62 63 if ( $resultPageSet === null ) { 64 $this->addFields( array( 'page_id' ) ); 65 $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title ); 66 $this->addFieldsIf( 'pp_value', $fld_value ); 67 } else { 68 $this->addFields( $resultPageSet->getPageTableFields() ); 69 } 70 $this->addTables( array( 'page_props', 'page' ) ); 71 $this->addWhere( 'pp_page=page_id' ); 72 $this->addWhereFld( 'pp_propname', $params['propname'] ); 73 74 $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older'; 75 76 if ( $params['continue'] ) { 77 $cont = explode( '|', $params['continue'] ); 78 $this->dieContinueUsageIf( count( $cont ) != 1 ); 79 80 // Add a WHERE clause 81 $from = (int)$cont[0]; 82 $this->addWhereRange( 'pp_page', $dir, $from, null ); 83 } 84 85 $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' ); 86 $this->addOption( 'ORDER BY', 'pp_page' . $sort ); 87 88 $limit = $params['limit']; 89 $this->addOption( 'LIMIT', $limit + 1 ); 90 91 $result = $this->getResult(); 92 $count = 0; 93 foreach ( $this->select( __METHOD__ ) as $row ) { 94 if ( ++$count > $limit ) { 95 // We've reached the one extra which shows that there are 96 // additional pages to be had. Stop here... 97 $this->setContinueEnumParameter( 'continue', $row->page_id ); 98 break; 99 } 100 101 if ( $resultPageSet === null ) { 102 $vals = array(); 103 if ( $fld_ids ) { 104 $vals['pageid'] = (int)$row->page_id; 105 } 106 if ( $fld_title ) { 107 $title = Title::makeTitle( $row->page_namespace, $row->page_title ); 108 ApiQueryBase::addTitleInfo( $vals, $title ); 109 } 110 if ( $fld_value ) { 111 $vals['value'] = $row->pp_value; 112 } 113 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals ); 114 if ( !$fit ) { 115 $this->setContinueEnumParameter( 'continue', $row->page_id ); 116 break; 117 } 118 } else { 119 $resultPageSet->processDbRow( $row ); 120 } 121 } 122 123 if ( $resultPageSet === null ) { 124 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' ); 125 } 126 } 127 128 public function getAllowedParams() { 129 return array( 130 'propname' => array( 131 ApiBase::PARAM_TYPE => 'string', 132 ApiBase::PARAM_REQUIRED => true, 133 ), 134 'prop' => array( 135 ApiBase::PARAM_DFLT => 'ids|title', 136 ApiBase::PARAM_ISMULTI => true, 137 ApiBase::PARAM_TYPE => array( 138 'ids', 139 'title', 140 'value', 141 ) 142 ), 143 'continue' => null, 144 'limit' => array( 145 ApiBase::PARAM_TYPE => 'limit', 146 ApiBase::PARAM_DFLT => 10, 147 ApiBase::PARAM_MIN => 1, 148 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 149 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 150 ), 151 'dir' => array( 152 ApiBase::PARAM_DFLT => 'ascending', 153 ApiBase::PARAM_TYPE => array( 154 'ascending', 155 'descending', 156 ) 157 ), 158 ); 159 } 160 161 public function getParamDescription() { 162 return array( 163 'propname' => 'Page prop for which to enumerate pages', 164 'prop' => array( 165 'What pieces of information to include', 166 ' ids - Adds the page ID', 167 ' title - Adds the title and namespace ID of the page', 168 ' value - Adds the value of the page prop', 169 ), 170 'dir' => 'In which direction to sort', 171 'continue' => 'When more results are available, use this to continue', 172 'limit' => 'The maximum number of pages to return', 173 ); 174 } 175 176 public function getDescription() { 177 return 'List all pages using a given page prop.'; 178 } 179 180 public function getExamples() { 181 return array( 182 'api.php?action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value' 183 => 'Get first 10 pages using {{DISPLAYTITLE:}}', 184 'api.php?action=query&generator=pageswithprop&gpwppropname=notoc&prop=info' 185 => 'Get page info about first 10 pages using __NOTOC__', 186 ); 187 } 188 189 public function getHelpUrls() { 190 return 'https://www.mediawiki.org/wiki/API:Pageswithprop'; 191 } 192 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |