MediaWiki  REL1_19
ApiQueryProtectedTitles.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'pt' );
00036         }
00037 
00038         public function execute() {
00039                 $this->run();
00040         }
00041 
00042         public function executeGenerator( $resultPageSet ) {
00043                 $this->run( $resultPageSet );
00044         }
00045 
00050         private function run( $resultPageSet = null ) {
00051                 $params = $this->extractRequestParams();
00052 
00053                 $this->addTables( 'protected_titles' );
00054                 $this->addFields( array( 'pt_namespace', 'pt_title', 'pt_timestamp' ) );
00055 
00056                 $prop = array_flip( $params['prop'] );
00057                 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
00058                 $this->addFieldsIf( 'pt_reason', isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) );
00059                 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
00060                 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
00061 
00062                 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
00063                 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
00064                 $this->addWhereFld( 'pt_create_perm', $params['level'] );
00065 
00066                 if ( isset( $prop['user'] ) ) {
00067                         $this->addTables( 'user' );
00068                         $this->addFields( 'user_name' );
00069                         $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
00070                                 'user_id=pt_user'
00071                         ) ) );
00072                 }
00073 
00074                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00075                 $res = $this->select( __METHOD__ );
00076 
00077                 $count = 0;
00078                 $result = $this->getResult();
00079 
00080                 $titles = array();
00081 
00082                 foreach ( $res as $row ) {
00083                         if ( ++ $count > $params['limit'] ) {
00084                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00085                                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
00086                                 break;
00087                         }
00088 
00089                         $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
00090                         if ( is_null( $resultPageSet ) ) {
00091                                 $vals = array();
00092                                 ApiQueryBase::addTitleInfo( $vals, $title );
00093                                 if ( isset( $prop['timestamp'] ) ) {
00094                                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
00095                                 }
00096 
00097                                 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
00098                                         $vals['user'] = $row->user_name;
00099                                 }
00100 
00101                                 if ( isset( $prop['user'] ) ) {
00102                                         $vals['userid'] = $row->pt_user;
00103                                 }
00104 
00105                                 if ( isset( $prop['comment'] ) ) {
00106                                         $vals['comment'] = $row->pt_reason;
00107                                 }
00108 
00109                                 if ( isset( $prop['parsedcomment'] ) ) {
00110                                         $vals['parsedcomment'] = Linker::formatComment( $row->pt_reason, $title );
00111                                 }
00112 
00113                                 if ( isset( $prop['expiry'] ) ) {
00114                                         global $wgContLang;
00115                                         $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
00116                                 }
00117 
00118                                 if ( isset( $prop['level'] ) ) {
00119                                         $vals['level'] = $row->pt_create_perm;
00120                                 }
00121 
00122                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00123                                 if ( !$fit ) {
00124                                         $this->setContinueEnumParameter( 'start',
00125                                                 wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
00126                                         break;
00127                                 }
00128                         } else {
00129                                 $titles[] = $title;
00130                         }
00131                 }
00132 
00133                 if ( is_null( $resultPageSet ) ) {
00134                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
00135                 } else {
00136                         $resultPageSet->populateFromTitles( $titles );
00137                 }
00138         }
00139 
00140         public function getCacheMode( $params ) {
00141                 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00142                         // formatComment() calls wfMsg() among other things
00143                         return 'anon-public-user-private';
00144                 } else {
00145                         return 'public';
00146                 }
00147         }
00148 
00149         public function getAllowedParams() {
00150                 global $wgRestrictionLevels;
00151                 return array(
00152                         'namespace' => array(
00153                                 ApiBase::PARAM_ISMULTI => true,
00154                                 ApiBase::PARAM_TYPE => 'namespace',
00155                         ),
00156                         'level' => array(
00157                                 ApiBase::PARAM_ISMULTI => true,
00158                                 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
00159                         ),
00160                         'limit' => array (
00161                                 ApiBase::PARAM_DFLT => 10,
00162                                 ApiBase::PARAM_TYPE => 'limit',
00163                                 ApiBase::PARAM_MIN => 1,
00164                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00165                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00166                         ),
00167                         'dir' => array(
00168                                 ApiBase::PARAM_DFLT => 'older',
00169                                 ApiBase::PARAM_TYPE => array(
00170                                         'newer',
00171                                         'older'
00172                                 )
00173                         ),
00174                         'start' => array(
00175                                 ApiBase::PARAM_TYPE => 'timestamp'
00176                         ),
00177                         'end' => array(
00178                                 ApiBase::PARAM_TYPE => 'timestamp'
00179                         ),
00180                         'prop' => array(
00181                                 ApiBase::PARAM_ISMULTI => true,
00182                                 ApiBase::PARAM_DFLT => 'timestamp|level',
00183                                 ApiBase::PARAM_TYPE => array(
00184                                         'timestamp',
00185                                         'user',
00186                                         'userid',
00187                                         'comment',
00188                                         'parsedcomment',
00189                                         'expiry',
00190                                         'level'
00191                                 )
00192                         ),
00193                 );
00194         }
00195 
00196         public function getParamDescription() {
00197                 return array(
00198                         'namespace' => 'Only list titles in these namespaces',
00199                         'start' => 'Start listing at this protection timestamp',
00200                         'end' => 'Stop listing at this protection timestamp',
00201                         'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
00202                         'limit' => 'How many total pages to return',
00203                         'prop' => array(
00204                                 'Which properties to get',
00205                                 ' timestamp      - Adds the timestamp of when protection was added',
00206                                 ' user           - Adds the user that added the protection',
00207                                 ' userid         - Adds the user id that added the protection',
00208                                 ' comment        - Adds the comment for the protection',
00209                                 ' parsedcomment  - Adds the parsed comment for the protection',
00210                                 ' expiry         - Adds the timestamp of when the protection will be lifted',
00211                                 ' level          - Adds the protection level',
00212                         ),
00213                         'level' => 'Only list titles with these protection levels',
00214                 );
00215         }
00216 
00217         public function getDescription() {
00218                 return 'List all titles protected from creation';
00219         }
00220 
00221         public function getExamples() {
00222                 return array(
00223                         'api.php?action=query&list=protectedtitles',
00224                 );
00225         }
00226 
00227         public function getHelpUrls() {
00228                 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';
00229         }
00230 
00231         public function getVersion() {
00232                 return __CLASS__ . ': $Id$';
00233         }
00234 }