Added job table, for deferred processing of jobs. The immediate application is to...
[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 if( is_object( $article ) ) {
50 $this->performAction( $output, $article, $title, $user, $request );
51 } elseif( is_string( $article ) ) {
52 $output->redirect( $article );
53 } else {
54 wfDebugDieBacktrace( "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::newFromText( wfMsgForContent( 'badtitle' ) );
132 $output->errorpage( 'badtitle', 'badtitletext' );
133 } else if ( $title->getInterwiki() != '' ) {
134 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
135 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
136 } else {
137 $url = $title->getFullURL();
138 }
139 /* Check for a redirect loop */
140 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
141 $output->redirect( $url );
142 } else {
143 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
144 $output->errorpage( 'badtitle', 'badtitletext' );
145 }
146 } else if ( ( $action == 'view' ) &&
147 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
148 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
149 {
150 /* Redirect to canonical url, make it a 301 to allow caching */
151 $output->setSquidMaxage( 1200 );
152 $output->redirect( $title->getFullURL(), '301');
153 } else if ( NS_SPECIAL == $title->getNamespace() ) {
154 /* actions that need to be made when we have a special pages */
155 SpecialPage::executePath( $title );
156 } else {
157 /* No match to special cases */
158 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
159 return false;
160 }
161 /* Did match a special case */
162 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
163 return true;
164 }
165
166 /**
167 * Create an Article object of the appropriate class for the given page.
168 * @param Title $title
169 * @return Article
170 */
171 function articleFromTitle( $title ) {
172 if( NS_MEDIA == $title->getNamespace() ) {
173 // FIXME: where should this go?
174 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
175 }
176
177 switch( $title->getNamespace() ) {
178 case NS_IMAGE:
179 require_once( 'includes/ImagePage.php' );
180 return new ImagePage( $title );
181 case NS_CATEGORY:
182 require_once( 'includes/CategoryPage.php' );
183 return new CategoryPage( $title );
184 default:
185 return new Article( $title );
186 }
187 }
188
189 /**
190 * Initialize the object to be known as $wgArticle for "standard" actions
191 * Create an Article object for the page, following redirects if needed.
192 * @param Title $title
193 * @param Request $request
194 * @param string $action
195 * @return mixed an Article, or a string to redirect to another URL
196 */
197 function initializeArticle( $title, $request ) {
198 global $wgTitle;
199 wfProfileIn( 'MediaWiki::initializeArticle' );
200
201 $action = $this->getVal('Action');
202 $article = $this->articleFromTitle( $title );
203
204 // Namespace might change when using redirects
205 if( $action == 'view' && !$request->getVal( 'oldid' ) &&
206 $request->getVal( 'redirect' ) != 'no' ) {
207 $dbr=&wfGetDB(DB_SLAVE);
208
209 // If we don't check for existance we'll get "Trying to get
210 // property of non-object" E_NOTICE in Article::loadPageData() when
211 // viewing a page that does not exist
212 if ( $article->exists() ) {
213 $article->loadPageData($article->pageDataFromTitle($dbr,$title));
214 }
215
216 /* Follow redirects only for... redirects */
217 if ($article->mIsRedirect) {
218 $target = $article->followRedirect();
219 if( is_string( $target ) ) {
220 global $wgDisableHardRedirects;
221 if( !$wgDisableHardRedirects ) {
222 // we'll need to redirect
223 return $target;
224 }
225 }
226 if( is_object( $target ) ) {
227 /* Rewrite environment to redirected article */
228 $rarticle = $this->articleFromTitle($target);
229 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
230 if ($rarticle->mTitle->mArticleID) {
231 $article = $rarticle;
232 $wgTitle = $target;
233 $article->setRedirectedFrom( $title );
234 } else {
235 $wgTitle = $title;
236 }
237 }
238 } else {
239 $wgTitle = $article->mTitle;
240 }
241 }
242 wfProfileOut( 'MediaWiki::initializeArticle' );
243 return $article;
244 }
245
246 /**
247 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
248 */
249 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
250 wfProfileIn( 'MediaWiki::finalCleanup' );
251 $this->doUpdates( $deferredUpdates );
252 $this->doJobs();
253 $loadBalancer->saveMasterPos();
254 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
255 $loadBalancer->commitAll();
256 $output->output();
257 wfProfileOut( 'MediaWiki::finalCleanup' );
258 }
259
260 /**
261 * Deferred updates aren't really deferred anymore. It's important to report errors to the
262 * user, and that means doing this before OutputPage::output(). Note that for page saves,
263 * the client will wait until the script exits anyway before following the redirect.
264 */
265 function doUpdates ( &$updates ) {
266 wfProfileIn( 'MediaWiki::doUpdates' );
267 foreach( $updates as $up ) {
268 $up->doUpdate();
269 }
270 wfProfileOut( 'MediaWiki::doUpdates' );
271 }
272
273 /**
274 * Do a job from the job queue
275 */
276 function doJobs() {
277 global $wgJobLogFile, $wgJobRunRate;
278
279 if ( $wgJobRunRate <= 0 ) {
280 return;
281 }
282 if ( $wgJobRunRate < 1 ) {
283 $max = mt_getrandmax();
284 if ( mt_rand( 0, $max ) < $max * $wgJobRunRate ) {
285 return;
286 }
287 $n = 1;
288 } else {
289 $n = intval( $wgJobRunRate );
290 }
291
292 require_once( 'JobQueue.php' );
293
294 while ( $n-- && false != ($job = Job::pop())) {
295 $output = $job->toString() . "\n";
296 if ( !$job->run() ) {
297 $output .= "Error: " . $job->getLastError() . "\n";
298 }
299 if ( $wgJobLogFile ) {
300 error_log( $output, 3, $wgJobLogFile );
301 }
302 }
303 }
304
305 /**
306 * Ends this task peacefully
307 */
308 function restInPeace ( &$loadBalancer ) {
309 wfProfileClose();
310 logProfilingData();
311 $loadBalancer->closeAll();
312 wfDebug( "Request ended normally\n" );
313 }
314
315 /**
316 * Perform one of the "standard" actions
317 */
318 function performAction( &$output, &$article, &$title, &$user, &$request ) {
319
320 wfProfileIn( 'MediaWiki::performAction' );
321
322 $action = $this->getVal('Action');
323 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
324 /* No such action; this will switch to the default case */
325 $action = 'nosuchaction';
326 }
327
328 switch( $action ) {
329 case 'view':
330 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
331 $article->view();
332 break;
333 case 'watch':
334 case 'unwatch':
335 case 'delete':
336 case 'revert':
337 case 'rollback':
338 case 'protect':
339 case 'unprotect':
340 case 'info':
341 case 'markpatrolled':
342 case 'validate':
343 case 'render':
344 case 'deletetrackback':
345 case 'purge':
346 $article->$action();
347 break;
348 case 'print':
349 $article->view();
350 break;
351 case 'dublincore':
352 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
353 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
354 } else {
355 require_once( 'includes/Metadata.php' );
356 wfDublinCoreRdf( $article );
357 }
358 break;
359 case 'creativecommons':
360 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
361 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
362 } else {
363 require_once( 'includes/Metadata.php' );
364 wfCreativeCommonsRdf( $article );
365 }
366 break;
367 case 'credits':
368 require_once( 'includes/Credits.php' );
369 showCreditsPage( $article );
370 break;
371 case 'submit':
372 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
373 /* Send a cookie so anons get talk message notifications */
374 User::SetupSession();
375 }
376 /* Continue... */
377 case 'edit':
378 $internal = $request->getVal( 'internaledit' );
379 $external = $request->getVal( 'externaledit' );
380 $section = $request->getVal( 'section' );
381 $oldid = $request->getVal( 'oldid' );
382 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
383 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
384 require_once( 'includes/EditPage.php' );
385 $editor = new EditPage( $article );
386 $editor->submit();
387 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
388 require_once( 'includes/ExternalEdit.php' );
389 $mode = $request->getVal( 'mode' );
390 $extedit = new ExternalEdit( $article, $mode );
391 $extedit->edit();
392 }
393 break;
394 case 'history':
395 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
396 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
397 }
398 require_once( 'includes/PageHistory.php' );
399 $history = new PageHistory( $article );
400 $history->history();
401 break;
402 case 'raw':
403 require_once( 'includes/RawPage.php' );
404 $raw = new RawPage( $article );
405 $raw->view();
406 break;
407 default:
408 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
409 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
410 }
411 wfProfileOut( 'MediaWiki::performAction' );
412 }
413
414
415 }
416
417 }; /* End of class MediaWiki */
418
419 ?>