Log job execution time
[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 * Retrieves 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 if( is_object( $article ) ) {
50 $this->performAction( $output, $article, $title, $user, $request );
51 } elseif( is_string( $article ) ) {
52 $output->redirect( $article );
53 } else {
54 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
55 }
56 }
57 wfProfileOut( 'MediaWiki::initialize' );
58 return $article;
59 }
60
61 /**
62 * Checks some initial queries
63 * Note that $title here is *not* a Title object, but a string!
64 */
65 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
66 if ($request->getVal( 'printable' ) == 'yes') {
67 $output->setPrintable();
68 }
69
70 $ret = NULL ;
71
72
73 if ( '' == $title && 'delete' != $action ) {
74 $ret = Title::newFromText( wfMsgForContent( 'mainpage' ) );
75 } elseif ( $curid = $request->getInt( 'curid' ) ) {
76 # URLs like this are generated by RC, because rc_title isn't always accurate
77 $ret = Title::newFromID( $curid );
78 } else {
79 $ret = Title::newFromURL( $title );
80 /* check variant links so that interwiki links don't have to worry about
81 the possible different language variants
82 */
83 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
84 $lang->findVariantLink( $title, $ret );
85
86 }
87 return $ret ;
88 }
89
90 /**
91 * Checks for search query and anon-cannot-read case
92 */
93 function preliminaryChecks ( &$title, &$output, $request ) {
94
95 # Debug statement for user levels
96 // print_r($wgUser);
97
98 $search = $request->getText( 'search' );
99 if( !is_null( $search ) && $search !== '' ) {
100 // Compatibility with old search URLs which didn't use Special:Search
101 // Do this above the read whitelist check for security...
102 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
103 }
104 $this->setVal( 'Search', $search );
105
106 # If the user is not logged in, the Namespace:title of the article must be in
107 # the Read array in order for the user to see it. (We have to check here to
108 # catch special pages etc. We check again in Article::view())
109 if ( !is_null( $title ) && !$title->userCanRead() ) {
110 $output->loginToUse();
111 $output->output();
112 exit;
113 }
114
115 }
116
117 /**
118 * Initialize the object to be known as $wgArticle for special cases
119 */
120 function initializeSpecialCases ( &$title, &$output, $request ) {
121
122 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
123
124 $search = $this->getVal('Search');
125 $action = $this->getVal('Action');
126 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
127 require_once( 'includes/SpecialSearch.php' );
128 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
129 wfSpecialSearch();
130 } else if( !$title or $title->getDBkey() == '' ) {
131 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle' );
132 # Die now before we mess up $wgArticle and the skin stops working
133 throw new ErrorPageError( 'badtitle', 'badtitletext' );
134 } else if ( $title->getInterwiki() != '' ) {
135 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
136 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
137 } else {
138 $url = $title->getFullURL();
139 }
140 /* Check for a redirect loop */
141 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
142 $output->redirect( $url );
143 } else {
144 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle' );
145 throw new ErrorPageError( 'badtitle', 'badtitletext' );
146 }
147 } else if ( ( $action == 'view' ) &&
148 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
149 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
150 {
151 /* Redirect to canonical url, make it a 301 to allow caching */
152 $output->setSquidMaxage( 1200 );
153 $output->redirect( $title->getFullURL(), '301');
154 } else if ( NS_SPECIAL == $title->getNamespace() ) {
155 /* actions that need to be made when we have a special pages */
156 SpecialPage::executePath( $title );
157 } else {
158 /* No match to special cases */
159 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
160 return false;
161 }
162 /* Did match a special case */
163 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
164 return true;
165 }
166
167 /**
168 * Create an Article object of the appropriate class for the given page.
169 * @param Title $title
170 * @return Article
171 */
172 function articleFromTitle( $title ) {
173 if( NS_MEDIA == $title->getNamespace() ) {
174 // FIXME: where should this go?
175 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
176 }
177
178 switch( $title->getNamespace() ) {
179 case NS_IMAGE:
180 return new ImagePage( $title );
181 case NS_CATEGORY:
182 return new CategoryPage( $title );
183 default:
184 return new Article( $title );
185 }
186 }
187
188 /**
189 * Initialize the object to be known as $wgArticle for "standard" actions
190 * Create an Article object for the page, following redirects if needed.
191 * @param Title $title
192 * @param Request $request
193 * @param string $action
194 * @return mixed an Article, or a string to redirect to another URL
195 */
196 function initializeArticle( $title, $request ) {
197 global $wgTitle;
198 wfProfileIn( 'MediaWiki::initializeArticle' );
199
200 $action = $this->getVal('Action');
201 $article = $this->articleFromTitle( $title );
202
203 // Namespace might change when using redirects
204 if( $action == 'view' && !$request->getVal( 'oldid' ) &&
205 $request->getVal( 'redirect' ) != 'no' ) {
206
207 $dbr =& wfGetDB(DB_SLAVE);
208 $article->loadPageData($article->pageDataFromTitle($dbr, $title));
209
210 /* Follow redirects only for... redirects */
211 if ($article->mIsRedirect) {
212 $target = $article->followRedirect();
213 if( is_string( $target ) ) {
214 global $wgDisableHardRedirects;
215 if( !$wgDisableHardRedirects ) {
216 // we'll need to redirect
217 return $target;
218 }
219 }
220 if( is_object( $target ) ) {
221 /* Rewrite environment to redirected article */
222 $rarticle = $this->articleFromTitle($target);
223 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
224 if ($rarticle->mTitle->mArticleID) {
225 $article = $rarticle;
226 $wgTitle = $target;
227 $article->setRedirectedFrom( $title );
228 } else {
229 $wgTitle = $title;
230 }
231 }
232 } else {
233 $wgTitle = $article->mTitle;
234 }
235 }
236 wfProfileOut( 'MediaWiki::initializeArticle' );
237 return $article;
238 }
239
240 /**
241 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
242 */
243 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
244 wfProfileIn( 'MediaWiki::finalCleanup' );
245 $this->doUpdates( $deferredUpdates );
246 $this->doJobs();
247 $loadBalancer->saveMasterPos();
248 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
249 $loadBalancer->commitAll();
250 $output->output();
251 wfProfileOut( 'MediaWiki::finalCleanup' );
252 }
253
254 /**
255 * Deferred updates aren't really deferred anymore. It's important to report errors to the
256 * user, and that means doing this before OutputPage::output(). Note that for page saves,
257 * the client will wait until the script exits anyway before following the redirect.
258 */
259 function doUpdates ( &$updates ) {
260 wfProfileIn( 'MediaWiki::doUpdates' );
261 foreach( $updates as $up ) {
262 $up->doUpdate();
263 }
264 wfProfileOut( 'MediaWiki::doUpdates' );
265 }
266
267 /**
268 * Do a job from the job queue
269 */
270 function doJobs() {
271 global $wgJobLogFile, $wgJobRunRate;
272
273 if ( $wgJobRunRate <= 0 ) {
274 return;
275 }
276 if ( $wgJobRunRate < 1 ) {
277 $max = mt_getrandmax();
278 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
279 return;
280 }
281 $n = 1;
282 } else {
283 $n = intval( $wgJobRunRate );
284 }
285
286 while ( $n-- && false != ($job = Job::pop())) {
287 $output = $job->toString() . "\n";
288 $t = -wfTime();
289 $success = $job->run();
290 $t += wfTime();
291 $t = round( $t*1000 );
292 if ( !$success ) {
293 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
294 } else {
295 $output .= "Success, Time: $t ms\n";
296 }
297 if ( $wgJobLogFile ) {
298 error_log( $output, 3, $wgJobLogFile );
299 }
300 }
301 }
302
303 /**
304 * Ends this task peacefully
305 */
306 function restInPeace ( &$loadBalancer ) {
307 wfProfileClose();
308 logProfilingData();
309 $loadBalancer->closeAll();
310 wfDebug( "Request ended normally\n" );
311 }
312
313 /**
314 * Perform one of the "standard" actions
315 */
316 function performAction( &$output, &$article, &$title, &$user, &$request ) {
317
318 wfProfileIn( 'MediaWiki::performAction' );
319
320 $action = $this->getVal('Action');
321 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
322 /* No such action; this will switch to the default case */
323 $action = 'nosuchaction';
324 }
325
326 switch( $action ) {
327 case 'view':
328 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
329 $article->view();
330 break;
331 case 'watch':
332 case 'unwatch':
333 case 'delete':
334 case 'revert':
335 case 'rollback':
336 case 'protect':
337 case 'unprotect':
338 case 'info':
339 case 'markpatrolled':
340 case 'render':
341 case 'deletetrackback':
342 case 'purge':
343 $article->$action();
344 break;
345 case 'print':
346 $article->view();
347 break;
348 case 'dublincore':
349 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
350 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
351 } else {
352 require_once( 'includes/Metadata.php' );
353 wfDublinCoreRdf( $article );
354 }
355 break;
356 case 'creativecommons':
357 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
358 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
359 } else {
360 require_once( 'includes/Metadata.php' );
361 wfCreativeCommonsRdf( $article );
362 }
363 break;
364 case 'credits':
365 require_once( 'includes/Credits.php' );
366 showCreditsPage( $article );
367 break;
368 case 'submit':
369 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
370 /* Send a cookie so anons get talk message notifications */
371 User::SetupSession();
372 }
373 /* Continue... */
374 case 'edit':
375 $internal = $request->getVal( 'internaledit' );
376 $external = $request->getVal( 'externaledit' );
377 $section = $request->getVal( 'section' );
378 $oldid = $request->getVal( 'oldid' );
379 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
380 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
381 $editor = new EditPage( $article );
382 $editor->submit();
383 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
384 $mode = $request->getVal( 'mode' );
385 $extedit = new ExternalEdit( $article, $mode );
386 $extedit->edit();
387 }
388 break;
389 case 'history':
390 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
391 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
392 }
393 $history = new PageHistory( $article );
394 $history->history();
395 break;
396 case 'raw':
397 $raw = new RawPage( $article );
398 $raw->view();
399 break;
400 default:
401 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
402 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
403 }
404 }
405 wfProfileOut( 'MediaWiki::performAction' );
406
407
408 }
409
410 }; /* End of class MediaWiki */
411
412 ?>