Most of index.php now in Wiki.php
[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 function preliminaryChecks ( &$title , &$output , $request ) {
56
57 # Debug statement for user levels
58 // print_r($wgUser);
59
60 $search = $request->getText( 'search' );
61 if( !is_null( $search ) && $search !== '' ) {
62 // Compatibility with old search URLs which didn't use Special:Search
63 // Do this above the read whitelist check for security...
64 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
65 }
66 $this->setVal( "Search", $search );
67
68 # If the user is not logged in, the Namespace:title of the article must be in
69 # the Read array in order for the user to see it. (We have to check here to
70 # catch special pages etc. We check again in Article::view())
71 if ( !is_null( $title ) && !$title->userCanRead() ) {
72 $output->loginToUse();
73 $output->output();
74 exit;
75 }
76
77 }
78
79 /**
80 * Initialize the object to be known as $wgArticle for special cases
81 */
82 function initializeSpecialCases ( &$title, &$output, $request ) {
83
84 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
85
86 $search = $this->getVal('Search');
87 $action = $this->getVal('Action');
88 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
89 require_once( 'includes/SpecialSearch.php' );
90 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
91 wfSpecialSearch();
92 } else if( !$title or $title->getDBkey() == '' ) {
93 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
94 $output->errorpage( 'badtitle', 'badtitletext' );
95 } else if ( $title->getInterwiki() != '' ) {
96 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
97 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
98 } else {
99 $url = $title->getFullURL();
100 }
101 /* Check for a redirect loop */
102 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
103 $output->redirect( $url );
104 } else {
105 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
106 $output->errorpage( 'badtitle', 'badtitletext' );
107 }
108 } else if ( ( $action == 'view' ) &&
109 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
110 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
111 {
112 /* Redirect to canonical url, make it a 301 to allow caching */
113 $output->setSquidMaxage( 1200 );
114 $output->redirect( $title->getFullURL(), '301');
115 } else if ( NS_SPECIAL == $title->getNamespace() ) {
116 /* actions that need to be made when we have a special pages */
117 SpecialPage::executePath( $title );
118 } else {
119 /* No match to special cases */
120 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
121 return false;
122 }
123 /* Did match a special case */
124 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
125 return true;
126 }
127
128 /**
129 * Initialize the object to be known as $wgArticle for "standard" actions
130 */
131 function initializeArticle( &$title, $request ) {
132
133 wfProfileIn( 'MediaWiki::initializeArticle' );
134
135 $action = $this->getVal('Action');
136
137 if( NS_MEDIA == $title->getNamespace() ) {
138 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
139 }
140
141 $ns = $title->getNamespace();
142
143 /* Namespace might change when using redirects */
144 $article = new Article( $title );
145 if( $action == 'view' && !$request->getVal( 'oldid' ) ) {
146 $rTitle = Title::newFromRedirect( $article->fetchContent() );
147 if( $rTitle ) {
148 /* Reload from the page pointed to later */
149 $article->mContentLoaded = false;
150 $ns = $rTitle->getNamespace();
151 $wasRedirected = true;
152 }
153 }
154
155 /* Categories and images are handled by a different class */
156 if( $ns == NS_IMAGE ) {
157 $b4 = $title->getPrefixedText();
158 unset( $article );
159 require_once( 'includes/ImagePage.php' );
160 $article = new ImagePage( $title );
161 if( isset( $wasRedirected ) && $request->getVal( 'redirect' ) != 'no' ) {
162 $article->mTitle = $rTitle;
163 $article->mRedirectedFrom = $b4;
164 }
165 } elseif( $ns == NS_CATEGORY ) {
166 unset( $article );
167 require_once( 'includes/CategoryPage.php' );
168 $article = new CategoryPage( $title );
169 }
170 wfProfileOut( 'MediaWiki::initializeArticle' );
171 return $article;
172 }
173
174 /**
175 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
176 */
177 function finalCleanup ( &$deferredUpdates , &$loadBalancer , &$output ) {
178 wfProfileIn( 'MediaWiki::finalCleanup' );
179 $this->doUpdates( $deferredUpdates );
180 $loadBalancer->saveMasterPos();
181 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
182 $loadBalancer->commitAll();
183 $output->output();
184 wfProfileOut( 'MediaWiki::finalCleanup' );
185 }
186
187 /**
188 * Deferred updates aren't really deferred anymore. It's important to report errors to the
189 * user, and that means doing this before OutputPage::output(). Note that for page saves,
190 * the client will wait until the script exits anyway before following the redirect.
191 */
192 function doUpdates ( &$updates ) {
193 wfProfileIn( 'MediaWiki::doUpdates' );
194 foreach( $updates as $up ) {
195 $up->doUpdate();
196 }
197 wfProfileOut( 'MediaWiki::doUpdates' );
198 }
199
200 /**
201 * Ends this task peacefully
202 */
203 function restInPeace ( &$loadBalancer ) {
204 wfProfileClose();
205 logProfilingData();
206 $loadBalancer->closeAll();
207 wfDebug( "Request ended normally\n" );
208 }
209
210 /**
211 * Perform one of the "standard" actions
212 */
213 function performAction( &$output, &$article, &$title, &$user, &$request ) {
214
215 wfProfileIn( 'MediaWiki::performAction' );
216
217 $action = $this->getVal('Action');
218 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
219 /* No such action; this will switch to the default case */
220 $action = "nosuchaction";
221 }
222
223 switch( $action ) {
224 case 'view':
225 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
226 $article->view();
227 break;
228 case 'watch':
229 case 'unwatch':
230 case 'delete':
231 case 'revert':
232 case 'rollback':
233 case 'protect':
234 case 'unprotect':
235 case 'info':
236 case 'markpatrolled':
237 case 'validate':
238 case 'render':
239 case 'deletetrackback':
240 case 'purge':
241 $article->$action();
242 break;
243 case 'print':
244 $article->view();
245 break;
246 case 'dublincore':
247 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
248 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
249 } else {
250 require_once( 'includes/Metadata.php' );
251 wfDublinCoreRdf( $article );
252 }
253 break;
254 case 'creativecommons':
255 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
256 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
257 } else {
258 require_once( 'includes/Metadata.php' );
259 wfCreativeCommonsRdf( $article );
260 }
261 break;
262 case 'credits':
263 require_once( 'includes/Credits.php' );
264 showCreditsPage( $article );
265 break;
266 case 'submit':
267 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
268 /* Send a cookie so anons get talk message notifications */
269 User::SetupSession();
270 }
271 /* Continue... */
272 case 'edit':
273 $internal = $request->getVal( 'internaledit' );
274 $external = $request->getVal( 'externaledit' );
275 $section = $request->getVal( 'section' );
276 $oldid = $request->getVal( 'oldid' );
277 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
278 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
279 require_once( 'includes/EditPage.php' );
280 $editor = new EditPage( $article );
281 $editor->submit();
282 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
283 require_once( 'includes/ExternalEdit.php' );
284 $mode = $request->getVal( 'mode' );
285 $extedit = new ExternalEdit( $article, $mode );
286 $extedit->edit();
287 }
288 break;
289 case 'history':
290 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
291 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
292 }
293 require_once( 'includes/PageHistory.php' );
294 $history = new PageHistory( $article );
295 $history->history();
296 break;
297 case 'raw':
298 require_once( 'includes/RawPage.php' );
299 $raw = new RawPage( $article );
300 $raw->view();
301 break;
302 default:
303 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
304 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
305 }
306 wfProfileOut( 'MediaWiki::performAction' );
307 }
308
309
310 }
311
312 }; /* End of class MediaWiki */
313
314 ?>
315