3 * MediaWiki is the to-be base class for this whole project
8 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
14 function MediaWiki () {
19 * Stores key/value pairs to circumvent global variables
20 * Note that keys are case-insensitive!
22 function setVal( $key, &$value ) {
23 $key = strtolower( $key );
24 $this->params
[$key] =& $value;
28 * Retieves key/value pairs to circumvent global variables
29 * Note that keys are case-insensitive!
31 function getVal( $key, $default = "" ) {
32 $key = strtolower( $key );
33 if( isset( $this->params
[$key] ) ) {
34 return $this->params
[$key];
40 * Wrapper for getrusage, if it exists
41 * getrusage() does not exist on the Window$ platform, catching this
43 function getRUsage() {
44 if ( function_exists ( 'getrusage' ) ) {
52 * CHeck for $GLOBALS vulnerability
54 function ckeckGlobalsVulnerability() {
55 @ini_set
( 'allow_url_fopen', 0 ); # For security...
56 if ( isset( $_REQUEST['GLOBALS'] ) ) {
57 die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>');
62 * Checks if the wiki is set up at all, or configured but not activated
64 function checkSetup() {
65 if ( file_exists( './LocalSettings.php' ) ) {
66 /* LocalSettings exists, commerce normally */
70 /* LocalSettings is not in the right place, do something */
72 require_once( 'includes/DefaultSettings.php' ); # used for printing the version
73 $out = file_get_contents( "./setup_message.html" );
74 $out = str_replace( "$1", $wgVersion, $out );
75 if ( file_exists( 'config/LocalSettings.php' ) ) {
76 $msg = "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.";
78 $msg = "Please <a href='config/index.php' title='setup'>setup the wiki</a> first.";
80 $out = str_replace( "$2", $msg, $out );
86 * Reads title and action values from request
88 function initializeActionTitle () {
89 $request = $this->getVal( 'Request' );
90 $this->setVal( 'action', $request->getVal( 'action', 'view' ) );
91 $this->setVal( 'urltitle', $request->getVal( 'title' ) );
95 * Initialization of ... everything
96 @return Article either the object to become $wgArticle, or NULL
98 function initialize ( &$title, &$output, &$user ) {
99 wfProfileIn( 'MediaWiki::initialize' );
100 $request = $this->getVal( 'Request' );
101 $this->preliminaryChecks ( $title, $output );
103 if ( !$this->initializeSpecialCases( $title, $output ) ) {
104 $article = $this->initializeArticle( $title );
105 if( is_object( $article ) ) {
106 $this->performAction( $output, $article, $title, $user );
107 } elseif( is_string( $article ) ) {
108 $output->redirect( $article );
110 wfDebugDieBacktrace( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
113 wfProfileOut( 'MediaWiki::initialize' );
118 * Checks some initial queries
120 function checkInitialQueries( &$output, $lang) {
121 wfProfileIn( 'MediaWiki::checkInitialQueries' );
122 $request = $this->getVal( 'Request' );
123 $action = $this->getVal( 'action' );
124 $title = $this->getVal( 'urltitle' );
125 if ($request->getVal( 'printable' ) == 'yes') {
126 $output->setPrintable();
132 if ( '' == $title && 'delete' != $action ) {
133 $ret = Title
::newFromText( wfMsgForContent( 'mainpage' ) );
134 } elseif ( $curid = $request->getInt( 'curid' ) ) {
135 # URLs like this are generated by RC, because rc_title isn't always accurate
136 $ret = Title
::newFromID( $curid );
138 $ret = Title
::newFromURL( $title );
139 /* check variant links so that interwiki links don't have to worry about
140 the possible different language variants
142 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
143 $lang->findVariantLink( $title, $ret );
146 wfProfileOut( 'MediaWiki::checkInitialQueries' );
151 * Checks for search query and anon-cannot-read case
153 function preliminaryChecks ( &$title, &$output ) {
154 $request = $this->getVal( 'Request' );
155 # Debug statement for user levels
158 $search = $request->getText( 'search' );
159 if( !is_null( $search ) && $search !== '' ) {
160 // Compatibility with old search URLs which didn't use Special:Search
161 // Do this above the read whitelist check for security...
162 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
164 $this->setVal( "Search", $search );
166 # If the user is not logged in, the Namespace:title of the article must be in
167 # the Read array in order for the user to see it. (We have to check here to
168 # catch special pages etc. We check again in Article::view())
169 if ( !is_null( $title ) && !$title->userCanRead() ) {
170 $output->loginToUse();
178 * Initialize the object to be known as $wgArticle for special cases
180 function initializeSpecialCases ( &$title, &$output ) {
181 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
182 $request = $this->getVal( 'Request' );
184 $search = $this->getVal('Search');
185 $action = $this->getVal('Action');
186 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
187 require_once( 'includes/SpecialSearch.php' );
188 $title = Title
::makeTitle( NS_SPECIAL
, 'Search' );
190 } else if( !$title or $title->getDBkey() == '' ) {
191 $title = Title
::newFromText( wfMsgForContent( 'badtitle' ) );
192 $output->errorpage( 'badtitle', 'badtitletext' );
193 } else if ( $title->getInterwiki() != '' ) {
194 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
195 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
197 $url = $title->getFullURL();
199 /* Check for a redirect loop */
200 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
201 $output->redirect( $url );
203 $title = Title
::newFromText( wfMsgForContent( 'badtitle' ) );
204 $output->errorpage( 'badtitle', 'badtitletext' );
206 } else if ( ( $action == 'view' ) &&
207 (!isset( $this->GET
['title'] ) ||
$title->getPrefixedDBKey() != $this->GET
['title'] ) &&
208 !count( array_diff( array_keys( $this->GET
), array( 'action', 'title' ) ) ) )
210 /* Redirect to canonical url, make it a 301 to allow caching */
211 $output->setSquidMaxage( 1200 );
212 $output->redirect( $title->getFullURL(), '301');
213 } else if ( NS_SPECIAL
== $title->getNamespace() ) {
214 /* actions that need to be made when we have a special pages */
215 SpecialPage
::executePath( $title );
217 /* No match to special cases */
218 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
221 /* Did match a special case */
222 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
227 * Create an Article object of the appropriate class for the given page.
228 * @param Title $title
231 function articleFromTitle( $title ) {
232 if( NS_MEDIA
== $title->getNamespace() ) {
233 // FIXME: where should this go?
234 $title = Title
::makeTitle( NS_IMAGE
, $title->getDBkey() );
237 switch( $title->getNamespace() ) {
239 require_once( 'includes/ImagePage.php' );
240 return new ImagePage( $title );
242 require_once( 'includes/CategoryPage.php' );
243 return new CategoryPage( $title );
245 return new Article( $title );
250 * Initialize the object to be known as $wgArticle for "standard" actions
251 * Create an Article object for the page, following redirects if needed.
252 * @param Title $title
253 * @return mixed an Article, or a string to redirect to another URL
255 function initializeArticle( $title ) {
256 wfProfileIn( 'MediaWiki::initializeArticle' );
258 $request = $this->getVal( 'Request' );
259 $action = $this->getVal('Action');
260 $article = $this->articleFromTitle( $title );
262 // Namespace might change when using redirects
263 if( $action == 'view' && !$request->getVal( 'oldid' ) && $request->getVal( 'redirect' ) != 'no' ) {
264 $target = $article->followRedirect();
265 if( is_string( $target ) ) {
266 global $wgDisableHardRedirects;
267 if( !$wgDisableHardRedirects ) {
268 // we'll need to redirect
272 if( is_object( $target ) ) {
273 // evil globals hack!
277 $article = $this->articleFromTitle( $target );
278 $article->setRedirectedFrom( $title );
281 wfProfileOut( 'MediaWiki::initializeArticle' );
286 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
288 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
289 wfProfileIn( 'MediaWiki::finalCleanup' );
290 $this->doUpdates( $deferredUpdates );
291 $loadBalancer->saveMasterPos();
292 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
293 $loadBalancer->commitAll();
295 wfProfileOut( 'MediaWiki::finalCleanup' );
299 * Deferred updates aren't really deferred anymore. It's important to report errors to the
300 * user, and that means doing this before OutputPage::output(). Note that for page saves,
301 * the client will wait until the script exits anyway before following the redirect.
303 function doUpdates ( &$updates ) {
304 wfProfileIn( 'MediaWiki::doUpdates' );
305 foreach( $updates as $up ) {
308 wfProfileOut( 'MediaWiki::doUpdates' );
312 * Ends this task peacefully
314 function restInPeace ( &$loadBalancer ) {
317 $loadBalancer->closeAll();
318 wfDebug( "Request ended normally\n" );
322 * Perform one of the "standard" actions
324 function performAction( &$output, &$article, &$title, &$user ) {
325 wfProfileIn( 'MediaWiki::performAction' );
327 $request = $this->getVal( 'Request' );
328 $action = $this->getVal('Action');
329 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
330 /* No such action; this will switch to the default case */
331 $action = "nosuchaction";
336 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
347 case 'markpatrolled':
350 case 'deletetrackback':
358 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
359 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
361 require_once( 'includes/Metadata.php' );
362 wfDublinCoreRdf( $article );
365 case 'creativecommons':
366 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
367 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
369 require_once( 'includes/Metadata.php' );
370 wfCreativeCommonsRdf( $article );
374 require_once( 'includes/Credits.php' );
375 showCreditsPage( $article );
378 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
379 /* Send a cookie so anons get talk message notifications */
380 User
::SetupSession();
384 $internal = $request->getVal( 'internaledit' );
385 $external = $request->getVal( 'externaledit' );
386 $section = $request->getVal( 'section' );
387 $oldid = $request->getVal( 'oldid' );
388 if( !$this->getVal( 'UseExternalEditor' ) ||
$action=='submit' ||
$internal ||
389 $section ||
$oldid ||
( !$user->getOption( 'externaleditor' ) && !$external ) ) {
390 require_once( 'includes/EditPage.php' );
391 $editor = new EditPage( $article );
393 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external ||
$user->getOption( 'externaleditor' ) ) ) {
394 require_once( 'includes/ExternalEdit.php' );
395 $mode = $request->getVal( 'mode' );
396 $extedit = new ExternalEdit( $article, $mode );
401 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
402 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
404 require_once( 'includes/PageHistory.php' );
405 $history = new PageHistory( $article );
409 require_once( 'includes/RawPage.php' );
410 $raw = new RawPage( $article );
414 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
415 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
417 wfProfileOut( 'MediaWiki::performAction' );
423 }; /* End of class MediaWiki */