MediaWiki  REL1_23
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         // Include in ORDER BY for uniqueness
00067         $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
00068         $this->addWhereRange( 'pt_title', $params['dir'], null, null );
00069 
00070         if ( !is_null( $params['continue'] ) ) {
00071             $cont = explode( '|', $params['continue'] );
00072             $this->dieContinueUsageIf( count( $cont ) != 3 );
00073             $op = ( $params['dir'] === 'newer' ? '>' : '<' );
00074             $db = $this->getDB();
00075             $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
00076             $continueNs = (int)$cont[1];
00077             $this->dieContinueUsageIf( $continueNs != $cont[1] );
00078             $continueTitle = $db->addQuotes( $cont[2] );
00079             $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
00080                 "(pt_timestamp = $continueTimestamp AND " .
00081                 "(pt_namespace $op $continueNs OR " .
00082                 "(pt_namespace = $continueNs AND " .
00083                 "pt_title $op= $continueTitle)))"
00084             );
00085         }
00086 
00087         if ( isset( $prop['user'] ) ) {
00088             $this->addTables( 'user' );
00089             $this->addFields( 'user_name' );
00090             $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
00091                 'user_id=pt_user'
00092             ) ) );
00093         }
00094 
00095         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00096         $res = $this->select( __METHOD__ );
00097 
00098         $count = 0;
00099         $result = $this->getResult();
00100 
00101         $titles = array();
00102 
00103         foreach ( $res as $row ) {
00104             if ( ++$count > $params['limit'] ) {
00105                 // We've reached the one extra which shows that there are
00106                 // additional pages to be had. Stop here...
00107                 $this->setContinueEnumParameter( 'continue',
00108                     "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
00109                 );
00110                 break;
00111             }
00112 
00113             $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
00114             if ( is_null( $resultPageSet ) ) {
00115                 $vals = array();
00116                 ApiQueryBase::addTitleInfo( $vals, $title );
00117                 if ( isset( $prop['timestamp'] ) ) {
00118                     $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
00119                 }
00120 
00121                 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
00122                     $vals['user'] = $row->user_name;
00123                 }
00124 
00125                 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
00126                     $vals['userid'] = $row->pt_user;
00127                 }
00128 
00129                 if ( isset( $prop['comment'] ) ) {
00130                     $vals['comment'] = $row->pt_reason;
00131                 }
00132 
00133                 if ( isset( $prop['parsedcomment'] ) ) {
00134                     $vals['parsedcomment'] = Linker::formatComment( $row->pt_reason, $title );
00135                 }
00136 
00137                 if ( isset( $prop['expiry'] ) ) {
00138                     global $wgContLang;
00139                     $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
00140                 }
00141 
00142                 if ( isset( $prop['level'] ) ) {
00143                     $vals['level'] = $row->pt_create_perm;
00144                 }
00145 
00146                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00147                 if ( !$fit ) {
00148                     $this->setContinueEnumParameter( 'continue',
00149                         "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
00150                     );
00151                     break;
00152                 }
00153             } else {
00154                 $titles[] = $title;
00155             }
00156         }
00157 
00158         if ( is_null( $resultPageSet ) ) {
00159             $result->setIndexedTagName_internal(
00160                 array( 'query', $this->getModuleName() ),
00161                 $this->getModulePrefix()
00162             );
00163         } else {
00164             $resultPageSet->populateFromTitles( $titles );
00165         }
00166     }
00167 
00168     public function getCacheMode( $params ) {
00169         if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00170             // formatComment() calls wfMessage() among other things
00171             return 'anon-public-user-private';
00172         } else {
00173             return 'public';
00174         }
00175     }
00176 
00177     public function getAllowedParams() {
00178         global $wgRestrictionLevels;
00179 
00180         return array(
00181             'namespace' => array(
00182                 ApiBase::PARAM_ISMULTI => true,
00183                 ApiBase::PARAM_TYPE => 'namespace',
00184             ),
00185             'level' => array(
00186                 ApiBase::PARAM_ISMULTI => true,
00187                 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
00188             ),
00189             'limit' => array(
00190                 ApiBase::PARAM_DFLT => 10,
00191                 ApiBase::PARAM_TYPE => 'limit',
00192                 ApiBase::PARAM_MIN => 1,
00193                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00194                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00195             ),
00196             'dir' => array(
00197                 ApiBase::PARAM_DFLT => 'older',
00198                 ApiBase::PARAM_TYPE => array(
00199                     'newer',
00200                     'older'
00201                 )
00202             ),
00203             'start' => array(
00204                 ApiBase::PARAM_TYPE => 'timestamp'
00205             ),
00206             'end' => array(
00207                 ApiBase::PARAM_TYPE => 'timestamp'
00208             ),
00209             'prop' => array(
00210                 ApiBase::PARAM_ISMULTI => true,
00211                 ApiBase::PARAM_DFLT => 'timestamp|level',
00212                 ApiBase::PARAM_TYPE => array(
00213                     'timestamp',
00214                     'user',
00215                     'userid',
00216                     'comment',
00217                     'parsedcomment',
00218                     'expiry',
00219                     'level'
00220                 )
00221             ),
00222             'continue' => null,
00223         );
00224     }
00225 
00226     public function getParamDescription() {
00227         return array(
00228             'namespace' => 'Only list titles in these namespaces',
00229             'start' => 'Start listing at this protection timestamp',
00230             'end' => 'Stop listing at this protection timestamp',
00231             'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
00232             'limit' => 'How many total pages to return',
00233             'prop' => array(
00234                 'Which properties to get',
00235                 ' timestamp      - Adds the timestamp of when protection was added',
00236                 ' user           - Adds the user that added the protection',
00237                 ' userid         - Adds the user id that added the protection',
00238                 ' comment        - Adds the comment for the protection',
00239                 ' parsedcomment  - Adds the parsed comment for the protection',
00240                 ' expiry         - Adds the timestamp of when the protection will be lifted',
00241                 ' level          - Adds the protection level',
00242             ),
00243             'level' => 'Only list titles with these protection levels',
00244             'continue' => 'When more results are available, use this to continue',
00245         );
00246     }
00247 
00248     public function getResultProperties() {
00249         global $wgRestrictionLevels;
00250 
00251         return array(
00252             '' => array(
00253                 'ns' => 'namespace',
00254                 'title' => 'string'
00255             ),
00256             'timestamp' => array(
00257                 'timestamp' => 'timestamp'
00258             ),
00259             'user' => array(
00260                 'user' => array(
00261                     ApiBase::PROP_TYPE => 'string',
00262                     ApiBase::PROP_NULLABLE => true
00263                 ),
00264                 'userid' => 'integer'
00265             ),
00266             'userid' => array(
00267                 'userid' => 'integer'
00268             ),
00269             'comment' => array(
00270                 'comment' => 'string'
00271             ),
00272             'parsedcomment' => array(
00273                 'parsedcomment' => 'string'
00274             ),
00275             'expiry' => array(
00276                 'expiry' => 'timestamp'
00277             ),
00278             'level' => array(
00279                 'level' => array(
00280                     ApiBase::PROP_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
00281                 )
00282             )
00283         );
00284     }
00285 
00286     public function getDescription() {
00287         return 'List all titles protected from creation.';
00288     }
00289 
00290     public function getExamples() {
00291         return array(
00292             'api.php?action=query&list=protectedtitles',
00293         );
00294     }
00295 
00296     public function getHelpUrls() {
00297         return 'https://www.mediawiki.org/wiki/API:Protectedtitles';
00298     }
00299 }