ApiQueryAllmessages.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * Created on Dec 1, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * A query action to return messages from site message cache
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryAllmessages extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'am');
  36. }
  37. public function execute() {
  38. global $wgMessageCache;
  39. $params = $this->extractRequestParams();
  40. if(!is_null($params['lang']))
  41. {
  42. global $wgLang;
  43. $wgLang = Language::factory($params['lang']);
  44. }
  45. //Determine which messages should we print
  46. $messages_target = array();
  47. if( $params['messages'] == '*' ) {
  48. $wgMessageCache->loadAllMessages();
  49. $message_names = array_keys( array_merge( Language::getMessagesFor( 'en' ), $wgMessageCache->getExtensionMessagesFor( 'en' ) ) );
  50. sort( $message_names );
  51. $messages_target = $message_names;
  52. } else {
  53. $messages_target = explode( '|', $params['messages'] );
  54. }
  55. //Filter messages
  56. if( isset( $params['filter'] ) ) {
  57. $messages_filtered = array();
  58. foreach( $messages_target as $message ) {
  59. if( strpos( $message, $params['filter'] ) !== false ) { //!== is used because filter can be at the beginnig of the string
  60. $messages_filtered[] = $message;
  61. }
  62. }
  63. $messages_target = $messages_filtered;
  64. }
  65. //Get all requested messages
  66. $messages = array();
  67. $skip = !is_null($params['from']);
  68. foreach( $messages_target as $message ) {
  69. // Skip all messages up to $params['from']
  70. if($skip && $message === $params['from'])
  71. $skip = false;
  72. if(!$skip)
  73. $messages[$message] = wfMsg( $message );
  74. }
  75. //Print the result
  76. $result = $this->getResult();
  77. $messages_out = array();
  78. foreach( $messages as $name => $value ) {
  79. $message = array();
  80. $message['name'] = $name;
  81. if( wfEmptyMsg( $name, $value ) ) {
  82. $message['missing'] = '';
  83. } else {
  84. $result->setContent( $message, $value );
  85. }
  86. $fit = $result->addValue(array('query', $this->getModuleName()), null, $message);
  87. if(!$fit)
  88. {
  89. $this->setContinueEnumParameter('from', $name);
  90. break;
  91. }
  92. }
  93. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'message');
  94. }
  95. public function getAllowedParams() {
  96. return array (
  97. 'messages' => array (
  98. ApiBase :: PARAM_DFLT => '*',
  99. ),
  100. 'filter' => array(),
  101. 'lang' => null,
  102. 'from' => null,
  103. );
  104. }
  105. public function getParamDescription() {
  106. return array (
  107. 'messages' => 'Which messages to output. "*" means all messages',
  108. 'filter' => 'Return only messages that contain this string',
  109. 'lang' => 'Return messages in this language',
  110. 'from' => 'Return messages starting at this message',
  111. );
  112. }
  113. public function getDescription() {
  114. return 'Return messages from this site.';
  115. }
  116. protected function getExamples() {
  117. return array(
  118. 'api.php?action=query&meta=allmessages&amfilter=ipb-',
  119. 'api.php?action=query&meta=allmessages&ammessages=august|mainpage&amlang=de',
  120. );
  121. }
  122. public function getVersion() {
  123. return __CLASS__ . ': $Id: ApiQueryAllmessages.php 47048 2009-02-09 19:24:28Z catrope $';
  124. }
  125. }