fixed infinite recursion in SVG error path
[lhc/web/wiklou.git] / includes / Wiki.php
1 <?php
2 /**
3 * MediaWiki is the to-be base class for this whole project
4 */
5
6 class MediaWiki {
7
8 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
9 var $params = array();
10
11 /**
12 * Constructor
13 */
14 function MediaWiki () {
15 $this->GET = $_GET;
16 }
17
18 /**
19 * Stores key/value pairs to circumvent global variables
20 * Note that keys are case-insensitive!
21 */
22 function setVal( $key, &$value ) {
23 $key = strtolower( $key );
24 $this->params[$key] =& $value;
25 }
26
27 /**
28 * Retieves key/value pairs to circumvent global variables
29 * Note that keys are case-insensitive!
30 */
31 function getVal( $key, $default = "" ) {
32 $key = strtolower( $key );
33 if( isset( $this->params[$key] ) ) {
34 return $this->params[$key];
35 }
36 return $default;
37 }
38
39 /**
40 * Initialization of ... everything
41 @return Article either the object to become $wgArticle, or NULL
42 */
43 function initialize ( &$title, &$output, &$user, $request) {
44 wfProfileIn( 'MediaWiki::initialize' );
45 $this->preliminaryChecks ( $title, $output, $request ) ;
46 $article = NULL;
47 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
48 $article = $this->initializeArticle( $title, $request );
49 $this->performAction( $output, $article, $title, $user, $request );
50 }
51 wfProfileOut( 'MediaWiki::initialize' );
52 return $article;
53 }
54
55 /**
56 * Checks some initial queries
57 * Note that $title here is *not* a Title object, but a string!
58 */
59 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
60 if ($request->getVal( 'printable' ) == 'yes') {
61 $output->setPrintable();
62 }
63
64 $ret = NULL ;
65
66
67 if ( '' == $title && 'delete' != $action ) {
68 $ret = Title::newFromText( wfMsgForContent( 'mainpage' ) );
69 } elseif ( $curid = $request->getInt( 'curid' ) ) {
70 # URLs like this are generated by RC, because rc_title isn't always accurate
71 $ret = Title::newFromID( $curid );
72 } else {
73 $ret = Title::newFromURL( $title );
74 /* check variant links so that interwiki links don't have to worry about
75 the possible different language variants
76 */
77 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
78 $lang->findVariantLink( $title, $ret );
79
80 }
81 return $ret ;
82 }
83
84 /**
85 * Checks for search query and anon-cannot-read case
86 */
87 function preliminaryChecks ( &$title, &$output, $request ) {
88
89 # Debug statement for user levels
90 // print_r($wgUser);
91
92 $search = $request->getText( 'search' );
93 if( !is_null( $search ) && $search !== '' ) {
94 // Compatibility with old search URLs which didn't use Special:Search
95 // Do this above the read whitelist check for security...
96 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
97 }
98 $this->setVal( "Search", $search );
99
100 # If the user is not logged in, the Namespace:title of the article must be in
101 # the Read array in order for the user to see it. (We have to check here to
102 # catch special pages etc. We check again in Article::view())
103 if ( !is_null( $title ) && !$title->userCanRead() ) {
104 $output->loginToUse();
105 $output->output();
106 exit;
107 }
108
109 }
110
111 /**
112 * Initialize the object to be known as $wgArticle for special cases
113 */
114 function initializeSpecialCases ( &$title, &$output, $request ) {
115
116 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
117
118 $search = $this->getVal('Search');
119 $action = $this->getVal('Action');
120 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
121 require_once( 'includes/SpecialSearch.php' );
122 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
123 wfSpecialSearch();
124 } else if( !$title or $title->getDBkey() == '' ) {
125 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
126 $output->errorpage( 'badtitle', 'badtitletext' );
127 } else if ( $title->getInterwiki() != '' ) {
128 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
129 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
130 } else {
131 $url = $title->getFullURL();
132 }
133 /* Check for a redirect loop */
134 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
135 $output->redirect( $url );
136 } else {
137 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
138 $output->errorpage( 'badtitle', 'badtitletext' );
139 }
140 } else if ( ( $action == 'view' ) &&
141 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
142 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
143 {
144 /* Redirect to canonical url, make it a 301 to allow caching */
145 $output->setSquidMaxage( 1200 );
146 $output->redirect( $title->getFullURL(), '301');
147 } else if ( NS_SPECIAL == $title->getNamespace() ) {
148 /* actions that need to be made when we have a special pages */
149 SpecialPage::executePath( $title );
150 } else {
151 /* No match to special cases */
152 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
153 return false;
154 }
155 /* Did match a special case */
156 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
157 return true;
158 }
159
160 /**
161 * Initialize the object to be known as $wgArticle for "standard" actions
162 */
163 function initializeArticle( &$title, $request ) {
164
165 wfProfileIn( 'MediaWiki::initializeArticle' );
166
167 $action = $this->getVal('Action');
168
169 if( NS_MEDIA == $title->getNamespace() ) {
170 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
171 }
172
173 $ns = $title->getNamespace();
174
175 /* Namespace might change when using redirects */
176 $article = new Article( $title );
177 if( $action == 'view' && !$request->getVal( 'oldid' ) ) {
178 $rTitle = Title::newFromRedirect( $article->fetchContent() );
179 if( $rTitle ) {
180 /* Reload from the page pointed to later */
181 $article->mContentLoaded = false;
182 $ns = $rTitle->getNamespace();
183 $wasRedirected = true;
184 }
185 }
186
187 /* Categories and images are handled by a different class */
188 if( $ns == NS_IMAGE ) {
189 $b4 = $title->getPrefixedText();
190 unset( $article );
191 require_once( 'includes/ImagePage.php' );
192 $article = new ImagePage( $title );
193 if( isset( $wasRedirected ) && $request->getVal( 'redirect' ) != 'no' ) {
194 $article->mTitle = $rTitle;
195 $article->mRedirectedFrom = $b4;
196 }
197 } elseif( $ns == NS_CATEGORY ) {
198 unset( $article );
199 require_once( 'includes/CategoryPage.php' );
200 $article = new CategoryPage( $title );
201 }
202 wfProfileOut( 'MediaWiki::initializeArticle' );
203 return $article;
204 }
205
206 /**
207 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
208 */
209 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
210 wfProfileIn( 'MediaWiki::finalCleanup' );
211 $this->doUpdates( $deferredUpdates );
212 $loadBalancer->saveMasterPos();
213 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
214 $loadBalancer->commitAll();
215 $output->output();
216 wfProfileOut( 'MediaWiki::finalCleanup' );
217 }
218
219 /**
220 * Deferred updates aren't really deferred anymore. It's important to report errors to the
221 * user, and that means doing this before OutputPage::output(). Note that for page saves,
222 * the client will wait until the script exits anyway before following the redirect.
223 */
224 function doUpdates ( &$updates ) {
225 wfProfileIn( 'MediaWiki::doUpdates' );
226 foreach( $updates as $up ) {
227 $up->doUpdate();
228 }
229 wfProfileOut( 'MediaWiki::doUpdates' );
230 }
231
232 /**
233 * Ends this task peacefully
234 */
235 function restInPeace ( &$loadBalancer ) {
236 wfProfileClose();
237 logProfilingData();
238 $loadBalancer->closeAll();
239 wfDebug( "Request ended normally\n" );
240 }
241
242 /**
243 * Perform one of the "standard" actions
244 */
245 function performAction( &$output, &$article, &$title, &$user, &$request ) {
246
247 wfProfileIn( 'MediaWiki::performAction' );
248
249 $action = $this->getVal('Action');
250 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
251 /* No such action; this will switch to the default case */
252 $action = "nosuchaction";
253 }
254
255 switch( $action ) {
256 case 'view':
257 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
258 $article->view();
259 break;
260 case 'watch':
261 case 'unwatch':
262 case 'delete':
263 case 'revert':
264 case 'rollback':
265 case 'protect':
266 case 'unprotect':
267 case 'info':
268 case 'markpatrolled':
269 case 'validate':
270 case 'render':
271 case 'deletetrackback':
272 case 'purge':
273 $article->$action();
274 break;
275 case 'print':
276 $article->view();
277 break;
278 case 'dublincore':
279 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
280 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
281 } else {
282 require_once( 'includes/Metadata.php' );
283 wfDublinCoreRdf( $article );
284 }
285 break;
286 case 'creativecommons':
287 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
288 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
289 } else {
290 require_once( 'includes/Metadata.php' );
291 wfCreativeCommonsRdf( $article );
292 }
293 break;
294 case 'credits':
295 require_once( 'includes/Credits.php' );
296 showCreditsPage( $article );
297 break;
298 case 'submit':
299 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
300 /* Send a cookie so anons get talk message notifications */
301 User::SetupSession();
302 }
303 /* Continue... */
304 case 'edit':
305 $internal = $request->getVal( 'internaledit' );
306 $external = $request->getVal( 'externaledit' );
307 $section = $request->getVal( 'section' );
308 $oldid = $request->getVal( 'oldid' );
309 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
310 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
311 require_once( 'includes/EditPage.php' );
312 $editor = new EditPage( $article );
313 $editor->submit();
314 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
315 require_once( 'includes/ExternalEdit.php' );
316 $mode = $request->getVal( 'mode' );
317 $extedit = new ExternalEdit( $article, $mode );
318 $extedit->edit();
319 }
320 break;
321 case 'history':
322 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
323 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
324 }
325 require_once( 'includes/PageHistory.php' );
326 $history = new PageHistory( $article );
327 $history->history();
328 break;
329 case 'raw':
330 require_once( 'includes/RawPage.php' );
331 $raw = new RawPage( $article );
332 $raw->view();
333 break;
334 default:
335 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
336 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
337 }
338 wfProfileOut( 'MediaWiki::performAction' );
339 }
340
341
342 }
343
344 }; /* End of class MediaWiki */
345
346 ?>
347