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