Consolidation of mediaWIki calls into 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 $article = NULL;
46 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
47 $article = $this->initializeArticle( $title, $request );
48 $this->performAction( $output, $article, $title, $user, $request );
49 }
50 wfProfileOut( 'MediaWiki::initialize' );
51 return $article;
52 }
53
54 /**
55 * Initialize the object to be known as $wgArticle for special cases
56 */
57 function initializeSpecialCases ( &$title, &$output, $request ) {
58
59 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
60
61 $search = $this->getVal('Search');
62 $action = $this->getVal('Action');
63 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
64 require_once( 'includes/SpecialSearch.php' );
65 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
66 wfSpecialSearch();
67 } else if( !$title or $title->getDBkey() == '' ) {
68 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
69 $output->errorpage( 'badtitle', 'badtitletext' );
70 } else if ( $title->getInterwiki() != '' ) {
71 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
72 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
73 } else {
74 $url = $title->getFullURL();
75 }
76 /* Check for a redirect loop */
77 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
78 $output->redirect( $url );
79 } else {
80 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
81 $output->errorpage( 'badtitle', 'badtitletext' );
82 }
83 } else if ( ( $action == 'view' ) &&
84 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
85 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
86 {
87 /* Redirect to canonical url, make it a 301 to allow caching */
88 $output->setSquidMaxage( 1200 );
89 $output->redirect( $title->getFullURL(), '301');
90 } else if ( NS_SPECIAL == $title->getNamespace() ) {
91 /* actions that need to be made when we have a special pages */
92 SpecialPage::executePath( $title );
93 } else {
94 /* No match to special cases */
95 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
96 return false;
97 }
98 /* Did match a special case */
99 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
100 return true;
101 }
102
103 /**
104 * Initialize the object to be known as $wgArticle for "standard" actions
105 */
106 function initializeArticle( &$title, $request ) {
107
108 wfProfileIn( 'MediaWiki::initializeArticle' );
109
110 $action = $this->getVal('Action');
111
112 if( NS_MEDIA == $title->getNamespace() ) {
113 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
114 }
115
116 $ns = $title->getNamespace();
117
118 /* Namespace might change when using redirects */
119 $article = new Article( $title );
120 if( $action == 'view' && !$request->getVal( 'oldid' ) ) {
121 $rTitle = Title::newFromRedirect( $article->fetchContent() );
122 if( $rTitle ) {
123 /* Reload from the page pointed to later */
124 $article->mContentLoaded = false;
125 $ns = $rTitle->getNamespace();
126 $wasRedirected = true;
127 }
128 }
129
130 /* Categories and images are handled by a different class */
131 if( $ns == NS_IMAGE ) {
132 $b4 = $title->getPrefixedText();
133 unset( $article );
134 require_once( 'includes/ImagePage.php' );
135 $article = new ImagePage( $title );
136 if( isset( $wasRedirected ) && $request->getVal( 'redirect' ) != 'no' ) {
137 $article->mTitle = $rTitle;
138 $article->mRedirectedFrom = $b4;
139 }
140 } elseif( $ns == NS_CATEGORY ) {
141 unset( $article );
142 require_once( 'includes/CategoryPage.php' );
143 $article = new CategoryPage( $title );
144 }
145 wfProfileOut( 'MediaWiki::initializeArticle' );
146 return $article;
147 }
148
149 /**
150 * Perform one of the "standard" actions
151 */
152 function performAction( &$output, &$article, &$title, &$user, &$request ) {
153
154 wfProfileIn( 'MediaWiki::performAction' );
155
156 $action = $this->getVal('Action');
157 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
158 /* No such action; this will switch to the default case */
159 $action = "nosuchaction";
160 }
161
162 switch( $action ) {
163 case 'view':
164 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
165 $article->view();
166 break;
167 case 'watch':
168 case 'unwatch':
169 case 'delete':
170 case 'revert':
171 case 'rollback':
172 case 'protect':
173 case 'unprotect':
174 case 'info':
175 case 'markpatrolled':
176 case 'validate':
177 case 'render':
178 case 'deletetrackback':
179 case 'purge':
180 $article->$action();
181 break;
182 case 'print':
183 $article->view();
184 break;
185 case 'dublincore':
186 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
187 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
188 } else {
189 require_once( 'includes/Metadata.php' );
190 wfDublinCoreRdf( $article );
191 }
192 break;
193 case 'creativecommons':
194 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
195 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
196 } else {
197 require_once( 'includes/Metadata.php' );
198 wfCreativeCommonsRdf( $article );
199 }
200 break;
201 case 'credits':
202 require_once( 'includes/Credits.php' );
203 showCreditsPage( $article );
204 break;
205 case 'submit':
206 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
207 /* Send a cookie so anons get talk message notifications */
208 User::SetupSession();
209 }
210 /* Continue... */
211 case 'edit':
212 $internal = $request->getVal( 'internaledit' );
213 $external = $request->getVal( 'externaledit' );
214 $section = $request->getVal( 'section' );
215 $oldid = $request->getVal( 'oldid' );
216 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
217 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
218 require_once( 'includes/EditPage.php' );
219 $editor = new EditPage( $article );
220 $editor->submit();
221 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
222 require_once( 'includes/ExternalEdit.php' );
223 $mode = $request->getVal( 'mode' );
224 $extedit = new ExternalEdit( $article, $mode );
225 $extedit->edit();
226 }
227 break;
228 case 'history':
229 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
230 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
231 }
232 require_once( 'includes/PageHistory.php' );
233 $history = new PageHistory( $article );
234 $history->history();
235 break;
236 case 'raw':
237 require_once( 'includes/RawPage.php' );
238 $raw = new RawPage( $article );
239 $raw->view();
240 break;
241 default:
242 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
243 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
244 }
245 wfProfileOut( 'MediaWiki::performAction' );
246
247 }
248 }
249
250 }; /* End of class MediaWiki */
251
252 ?>
253