<?php
/**
- * API for MediaWiki 1.8+
+ *
*
* Created on Dec 1, 2007
*
- * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* @file
*/
-if ( !defined( 'MEDIAWIKI' ) ) {
- // Eclipse helper - will be ignored in production
- require_once( 'ApiQueryBase.php' );
-}
-
/**
* A query action to return messages from site message cache
*
* @ingroup API
*/
-class ApiQueryAllmessages extends ApiQueryBase {
+class ApiQueryAllMessages extends ApiQueryBase {
- public function __construct( $query, $moduleName ) {
+ public function __construct( ApiQuery $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'am' );
}
public function execute() {
$params = $this->extractRequestParams();
- global $wgLang;
+ if ( is_null( $params['lang'] ) ) {
+ $langObj = $this->getLanguage();
+ } elseif ( !Language::isValidCode( $params['lang'] ) ) {
+ $this->dieWithError(
+ [ 'apierror-invalidlang', $this->encodeParamName( 'lang' ) ], 'invalidlang'
+ );
+ } else {
+ $langObj = Language::factory( $params['lang'] );
+ }
- $oldLang = null;
- if ( !is_null( $params['lang'] ) ) {
- $oldLang = $wgLang; // Keep $wgLang for restore later
- $wgLang = Language::factory( $params['lang'] );
+ if ( $params['enableparser'] ) {
+ if ( !is_null( $params['title'] ) ) {
+ $title = Title::newFromText( $params['title'] );
+ if ( !$title || $title->isExternal() ) {
+ $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
+ }
+ } else {
+ $title = Title::newFromText( 'API' );
+ }
}
$prop = array_flip( (array)$params['prop'] );
// Determine which messages should we print
if ( in_array( '*', $params['messages'] ) ) {
- $message_names = array_keys( Language::getMessagesFor( 'en' ) );
+ $message_names = Language::getMessageKeysFor( $langObj->getCode() );
+ if ( $params['includelocal'] ) {
+ $message_names = array_unique( array_merge(
+ $message_names,
+ // Pass in the content language code so we get local messages that have a
+ // MediaWiki:msgkey page. We might theoretically miss messages that have no
+ // MediaWiki:msgkey page but do have a MediaWiki:msgkey/lang page, but that's
+ // just a stupid case.
+ MessageCache::singleton()->getAllMessageKeys( $this->getConfig()->get( 'LanguageCode' ) )
+ ) );
+ }
sort( $message_names );
$messages_target = $message_names;
} else {
$messages_target = $params['messages'];
}
- // Filter messages
+ // Filter messages that have the specified prefix
+ // Because we sorted the message array earlier, they will appear in a clump:
+ if ( isset( $params['prefix'] ) ) {
+ $skip = false;
+ $messages_filtered = [];
+ foreach ( $messages_target as $message ) {
+ // === 0: must be at beginning of string (position 0)
+ if ( strpos( $message, $params['prefix'] ) === 0 ) {
+ if ( !$skip ) {
+ $skip = true;
+ }
+ $messages_filtered[] = $message;
+ } elseif ( $skip ) {
+ break;
+ }
+ }
+ $messages_target = $messages_filtered;
+ }
+
+ // Filter messages that contain specified string
if ( isset( $params['filter'] ) ) {
- $messages_filtered = array();
+ $messages_filtered = [];
foreach ( $messages_target as $message ) {
// !== is used because filter can be at the beginning of the string
if ( strpos( $message, $params['filter'] ) !== false ) {
$messages_target = $messages_filtered;
}
+ // Whether we have any sort of message customisation filtering
+ $customiseFilterEnabled = $params['customised'] !== 'all';
+ if ( $customiseFilterEnabled ) {
+ global $wgContLang;
+
+ $customisedMessages = AllMessagesTablePager::getCustomisedStatuses(
+ array_map(
+ [ $langObj, 'ucfirst' ],
+ $messages_target
+ ),
+ $langObj->getCode(),
+ !$langObj->equals( $wgContLang )
+ );
+
+ $customised = $params['customised'] === 'modified';
+ }
+
// Get all requested messages and print the result
$skip = !is_null( $params['from'] );
$useto = !is_null( $params['to'] );
if ( $skip && $message === $params['from'] ) {
$skip = false;
}
-
- if( $useto && $message > $params['to'] ) {
+
+ if ( $useto && $message > $params['to'] ) {
break;
}
if ( !$skip ) {
- $a = array( 'name' => $message );
- $args = null;
+ $a = [
+ 'name' => $message,
+ 'normalizedname' => MessageCache::normalizeKey( $message ),
+ ];
+
+ $args = [];
if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
$args = $params['args'];
}
- // Check if the parser is enabled:
- if ( $params['enableparser'] ) {
- $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
- } elseif ( $args ) {
- $msgString = wfMsgGetKey( $message, true, false, false );
- $msg = wfMsgReplaceArgs( $msgString, $args );
- } else {
- $msg = wfMsgGetKey( $message, true, false, false );
+
+ if ( $customiseFilterEnabled ) {
+ $messageIsCustomised = isset( $customisedMessages['pages'][$langObj->ucfirst( $message )] );
+ if ( $customised === $messageIsCustomised ) {
+ if ( $customised ) {
+ $a['customised'] = true;
+ }
+ } else {
+ continue;
+ }
}
- if ( wfEmptyMsg( $message, $msg ) ) {
- $a['missing'] = '';
+ $msg = wfMessage( $message, $args )->inLanguage( $langObj );
+
+ if ( !$msg->exists() ) {
+ $a['missing'] = true;
} else {
- ApiResult::setContent( $a, $msg );
+ // Check if the parser is enabled:
+ if ( $params['enableparser'] ) {
+ $msgString = $msg->title( $title )->text();
+ } else {
+ $msgString = $msg->plain();
+ }
+ if ( !$params['nocontent'] ) {
+ ApiResult::setContentValue( $a, 'content', $msgString );
+ }
if ( isset( $prop['default'] ) ) {
- $default = wfMsgGetKey( $message, false, false, false );
- if ( $default !== $msg ) {
- if ( wfEmptyMsg( $message, $default ) ) {
- $a['defaultmissing'] = '';
- } else {
- $a['default'] = $default;
- }
+ $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
+ if ( !$default->exists() ) {
+ $a['defaultmissing'] = true;
+ } elseif ( $default->plain() != $msgString ) {
+ $a['default'] = $default->plain();
}
}
}
- $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $a );
+ $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $a );
if ( !$fit ) {
$this->setContinueEnumParameter( 'from', $message );
break;
}
}
}
- $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
-
- if ( !is_null( $oldLang ) ) {
- $wgLang = $oldLang; // Restore $oldLang
- }
+ $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'message' );
}
public function getCacheMode( $params ) {
}
public function getAllowedParams() {
- return array(
- 'messages' => array(
+ return [
+ 'messages' => [
ApiBase::PARAM_DFLT => '*',
ApiBase::PARAM_ISMULTI => true,
- ),
- 'prop' => array(
+ ],
+ 'prop' => [
ApiBase::PARAM_ISMULTI => true,
- ApiBase::PARAM_TYPE => array(
+ ApiBase::PARAM_TYPE => [
'default'
- )
- ),
+ ]
+ ],
'enableparser' => false,
- 'args' => array(
- ApiBase::PARAM_ISMULTI => true
- ),
- 'filter' => array(),
+ 'nocontent' => false,
+ 'includelocal' => false,
+ 'args' => [
+ ApiBase::PARAM_ISMULTI => true,
+ ApiBase::PARAM_ALLOW_DUPLICATES => true,
+ ],
+ 'filter' => [],
+ 'customised' => [
+ ApiBase::PARAM_DFLT => 'all',
+ ApiBase::PARAM_TYPE => [
+ 'all',
+ 'modified',
+ 'unmodified'
+ ]
+ ],
'lang' => null,
'from' => null,
'to' => null,
- );
- }
-
- public function getParamDescription() {
- return array(
- 'messages' => 'Which messages to output. "*" means all messages',
- 'prop' => 'Which properties to get',
- 'enableparser' => array( 'Set to enable parser, will preprocess the wikitext of message',
- 'Will substitute magic words, handle templates etc.' ),
- 'args' => 'Arguments to be substituted into message',
- 'filter' => 'Return only messages that contain this string',
- 'lang' => 'Return messages in this language',
- 'from' => 'Return messages starting at this message',
- 'to' => 'Return messages ending at this message',
- );
- }
-
- public function getDescription() {
- return 'Return messages from this site';
+ 'title' => null,
+ 'prefix' => null,
+ ];
}
- protected function getExamples() {
- return array(
- 'api.php?action=query&meta=allmessages&amfilter=ipb-',
- 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
- );
+ protected function getExamplesMessages() {
+ return [
+ 'action=query&meta=allmessages&refix=ipb-'
+ => 'apihelp-query+allmessages-example-ipb',
+ 'action=query&meta=allmessages&ammessages=august|mainpage&amlang=de'
+ => 'apihelp-query+allmessages-example-de',
+ ];
}
- public function getVersion() {
- return __CLASS__ . ': $Id$';
+ public function getHelpUrls() {
+ return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allmessages';
}
}