MediaWiki  REL1_22
ApiQueryAllMessages.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryAllMessages extends ApiQueryBase {
00033 
00034     public function __construct( $query, $moduleName ) {
00035         parent::__construct( $query, $moduleName, 'am' );
00036     }
00037 
00038     public function execute() {
00039         $params = $this->extractRequestParams();
00040 
00041         if ( is_null( $params['lang'] ) ) {
00042             $langObj = $this->getLanguage();
00043         } elseif ( !Language::isValidCode( $params['lang'] ) ) {
00044             $this->dieUsage( 'Invalid language code for parameter lang', 'invalidlang' );
00045         } else {
00046             $langObj = Language::factory( $params['lang'] );
00047         }
00048 
00049         if ( $params['enableparser'] ) {
00050             if ( !is_null( $params['title'] ) ) {
00051                 $title = Title::newFromText( $params['title'] );
00052                 if ( !$title || $title->isExternal() ) {
00053                     $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00054                 }
00055             } else {
00056                 $title = Title::newFromText( 'API' );
00057             }
00058         }
00059 
00060         $prop = array_flip( (array)$params['prop'] );
00061 
00062         // Determine which messages should we print
00063         if ( in_array( '*', $params['messages'] ) ) {
00064             $message_names = Language::getMessageKeysFor( $langObj->getCode() );
00065             if ( $params['includelocal'] ) {
00066                 global $wgLanguageCode;
00067                 $message_names = array_unique( array_merge(
00068                     $message_names,
00069                     // Pass in the content language code so we get local messages that have a
00070                     // MediaWiki:msgkey page. We might theoretically miss messages that have no
00071                     // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
00072                     // just a stupid case.
00073                     MessageCache::singleton()->getAllMessageKeys( $wgLanguageCode )
00074                 ) );
00075             }
00076             sort( $message_names );
00077             $messages_target = $message_names;
00078         } else {
00079             $messages_target = $params['messages'];
00080         }
00081 
00082         // Filter messages that have the specified prefix
00083         // Because we sorted the message array earlier, they will appear in a clump:
00084         if ( isset( $params['prefix'] ) ) {
00085             $skip = false;
00086             $messages_filtered = array();
00087             foreach ( $messages_target as $message ) {
00088                 // === 0: must be at beginning of string (position 0)
00089                 if ( strpos( $message, $params['prefix'] ) === 0 ) {
00090                     if ( !$skip ) {
00091                         $skip = true;
00092                     }
00093                     $messages_filtered[] = $message;
00094                 } elseif ( $skip ) {
00095                     break;
00096                 }
00097             }
00098             $messages_target = $messages_filtered;
00099         }
00100 
00101         // Filter messages that contain specified string
00102         if ( isset( $params['filter'] ) ) {
00103             $messages_filtered = array();
00104             foreach ( $messages_target as $message ) {
00105                 // !== is used because filter can be at the beginning of the string
00106                 if ( strpos( $message, $params['filter'] ) !== false ) {
00107                     $messages_filtered[] = $message;
00108                 }
00109             }
00110             $messages_target = $messages_filtered;
00111         }
00112 
00113         // Whether we have any sort of message customisation filtering
00114         $customiseFilterEnabled = $params['customised'] !== 'all';
00115         if ( $customiseFilterEnabled ) {
00116             global $wgContLang;
00117             $lang = $langObj->getCode();
00118 
00119             $customisedMessages = AllmessagesTablePager::getCustomisedStatuses(
00120                 array_map( array( $langObj, 'ucfirst' ), $messages_target ), $lang, $lang != $wgContLang->getCode() );
00121 
00122             $customised = $params['customised'] === 'modified';
00123         }
00124 
00125         // Get all requested messages and print the result
00126         $skip = !is_null( $params['from'] );
00127         $useto = !is_null( $params['to'] );
00128         $result = $this->getResult();
00129         foreach ( $messages_target as $message ) {
00130             // Skip all messages up to $params['from']
00131             if ( $skip && $message === $params['from'] ) {
00132                 $skip = false;
00133             }
00134 
00135             if ( $useto && $message > $params['to'] ) {
00136                 break;
00137             }
00138 
00139             if ( !$skip ) {
00140                 $a = array( 'name' => $message );
00141                 $args = array();
00142                 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
00143                     $args = $params['args'];
00144                 }
00145 
00146                 if ( $customiseFilterEnabled ) {
00147                     $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
00148                     if ( $customised === $messageIsCustomised ) {
00149                         if ( $customised ) {
00150                             $a['customised'] = '';
00151                         }
00152                     } else {
00153                         continue;
00154                     }
00155                 }
00156 
00157                 $msg = wfMessage( $message, $args )->inLanguage( $langObj );
00158 
00159                 if ( !$msg->exists() ) {
00160                     $a['missing'] = '';
00161                 } else {
00162                     // Check if the parser is enabled:
00163                     if ( $params['enableparser'] ) {
00164                         $msgString = $msg->title( $title )->text();
00165                     } else {
00166                         $msgString = $msg->plain();
00167                     }
00168                     if ( !$params['nocontent'] ) {
00169                         ApiResult::setContent( $a, $msgString );
00170                     }
00171                     if ( isset( $prop['default'] ) ) {
00172                         $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
00173                         if ( !$default->exists() ) {
00174                             $a['defaultmissing'] = '';
00175                         } elseif ( $default->plain() != $msgString ) {
00176                             $a['default'] = $default->plain();
00177                         }
00178                     }
00179                 }
00180                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
00181                 if ( !$fit ) {
00182                     $this->setContinueEnumParameter( 'from', $message );
00183                     break;
00184                 }
00185             }
00186         }
00187         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
00188     }
00189 
00190     public function getCacheMode( $params ) {
00191         if ( is_null( $params['lang'] ) ) {
00192             // Language not specified, will be fetched from preferences
00193             return 'anon-public-user-private';
00194         } elseif ( $params['enableparser'] ) {
00195             // User-specific parser options will be used
00196             return 'anon-public-user-private';
00197         } else {
00198             // OK to cache
00199             return 'public';
00200         }
00201     }
00202 
00203     public function getAllowedParams() {
00204         return array(
00205             'messages' => array(
00206                 ApiBase::PARAM_DFLT => '*',
00207                 ApiBase::PARAM_ISMULTI => true,
00208             ),
00209             'prop' => array(
00210                 ApiBase::PARAM_ISMULTI => true,
00211                 ApiBase::PARAM_TYPE => array(
00212                     'default'
00213                 )
00214             ),
00215             'enableparser' => false,
00216             'nocontent' => false,
00217             'includelocal' => false,
00218             'args' => array(
00219                 ApiBase::PARAM_ISMULTI => true,
00220                 ApiBase::PARAM_ALLOW_DUPLICATES => true,
00221             ),
00222             'filter' => array(),
00223             'customised' => array(
00224                 ApiBase::PARAM_DFLT => 'all',
00225                 ApiBase::PARAM_TYPE => array(
00226                     'all',
00227                     'modified',
00228                     'unmodified'
00229                 )
00230             ),
00231             'lang' => null,
00232             'from' => null,
00233             'to' => null,
00234             'title' => null,
00235             'prefix' => null,
00236         );
00237     }
00238 
00239     public function getParamDescription() {
00240         return array(
00241             'messages' => 'Which messages to output. "*" (default) means all messages',
00242             'prop' => 'Which properties to get',
00243             'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
00244                             'Will substitute magic words, handle templates etc.' ),
00245             'nocontent' => 'If set, do not include the content of the messages in the output.',
00246             'includelocal' => array( "Also include local messages, i.e. messages that don't exist in the software but do exist as a MediaWiki: page.",
00247                             "This lists all MediaWiki: pages, so it will also list those that aren't 'really' messages such as Common.js",
00248             ),
00249             'title' => 'Page name to use as context when parsing message (for enableparser option)',
00250             'args' => 'Arguments to be substituted into message',
00251             'prefix' => 'Return messages with this prefix',
00252             'filter' => 'Return only messages with names that contain this string',
00253             'customised' => 'Return only messages in this customisation state',
00254             'lang' => 'Return messages in this language',
00255             'from' => 'Return messages starting at this message',
00256             'to' => 'Return messages ending at this message',
00257         );
00258     }
00259 
00260     public function getPossibleErrors() {
00261         return array_merge( parent::getPossibleErrors(), array(
00262             array( 'code' => 'invalidlang', 'info' => 'Invalid language code for parameter lang' ),
00263         ) );
00264     }
00265 
00266     public function getResultProperties() {
00267         return array(
00268             '' => array(
00269                 'name' => 'string',
00270                 'customised' => 'boolean',
00271                 'missing' => 'boolean',
00272                 '*' => array(
00273                     ApiBase::PROP_TYPE => 'string',
00274                     ApiBase::PROP_NULLABLE => true
00275                 )
00276             ),
00277             'default' => array(
00278                 'defaultmissing' => 'boolean',
00279                 'default' => array(
00280                     ApiBase::PROP_TYPE => 'string',
00281                     ApiBase::PROP_NULLABLE => true
00282                 )
00283             )
00284         );
00285     }
00286 
00287     public function getDescription() {
00288         return 'Return messages from this site';
00289     }
00290 
00291     public function getExamples() {
00292         return array(
00293             'api.php?action=query&meta=allmessages&amprefix=ipb-',
00294             'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
00295         );
00296     }
00297 
00298     public function getHelpUrls() {
00299         return 'https://www.mediawiki.org/wiki/API:Meta#allmessages_.2F_am';
00300     }
00301 }