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