* (bug 829) Fix URL-escaping on block success
[lhc/web/wiklou.git] / includes / EditPage.php
1 <?php
2 /**
3 * Contain the EditPage class
4 * @package MediaWiki
5 */
6
7 /**
8 * Splitting edit page/HTML interface from Article...
9 * The actual database and text munging is still in Article,
10 * but it should get easier to call those from alternate
11 * interfaces.
12 *
13 * @package MediaWiki
14 */
15
16 class EditPage {
17 var $mArticle;
18 var $mTitle;
19 var $mMetaData = '';
20
21 # Form values
22 var $save = false, $preview = false;
23 var $minoredit = false, $watchthis = false;
24 var $textbox1 = '', $textbox2 = '', $summary = '';
25 var $edittime = '', $section = '';
26 var $oldid = 0;
27
28 /**
29 * @todo document
30 * @param $article
31 */
32 function EditPage( $article ) {
33 $this->mArticle =& $article;
34 global $wgTitle;
35 $this->mTitle =& $wgTitle;
36 }
37
38 /**
39 * This is the function that extracts metadata from the article body on the first view.
40 * To turn the feature on, set $wgUseMetadataEdit = true ; in LocalSettings
41 * and set $wgMetadataWhitelist to the *full* title of the template whitelist
42 */
43 function extractMetaDataFromArticle ()
44 {
45 global $wgUseMetadataEdit , $wgMetadataWhitelist , $wgLang ;
46 $this->mMetaData = "" ;
47 if ( !$wgUseMetadataEdit ) return ;
48 if ( $wgMetadataWhitelist == "" ) return ;
49 $s = "" ;
50 $t = $this->mArticle->getContent ( true ) ;
51
52 # MISSING : <nowiki> filtering
53
54 # Categories and language links
55 $t = explode ( "\n" , $t ) ;
56 $catlow = strtolower ( $wgLang->getNsText ( NS_CATEGORY ) ) ;
57 $cat = $ll = array() ;
58 foreach ( $t AS $key => $x )
59 {
60 $y = trim ( strtolower ( $x ) ) ;
61 while ( substr ( $y , 0 , 2 ) == "[[" )
62 {
63 $y = explode ( "]]" , trim ( $x ) ) ;
64 $first = array_shift ( $y ) ;
65 $first = explode ( ":" , $first ) ;
66 $ns = array_shift ( $first ) ;
67 $ns = trim ( str_replace ( "[" , "" , $ns ) ) ;
68 if ( strlen ( $ns ) == 2 OR strtolower ( $ns ) == $catlow )
69 {
70 $add = "[[" . $ns . ":" . implode ( ":" , $first ) . "]]" ;
71 if ( strtolower ( $ns ) == $catlow ) $cat[] = $add ;
72 else $ll[] = $add ;
73 $x = implode ( "]]" , $y ) ;
74 $t[$key] = $x ;
75 $y = trim ( strtolower ( $x ) ) ;
76 }
77 }
78 }
79 if ( count ( $cat ) ) $s .= implode ( " " , $cat ) . "\n" ;
80 if ( count ( $ll ) ) $s .= implode ( " " , $ll ) . "\n" ;
81 $t = implode ( "\n" , $t ) ;
82
83 # Load whitelist
84 $sat = array () ; # stand-alone-templates; must be lowercase
85 $wl_title = Title::newFromText ( $wgMetadataWhitelist ) ;
86 $wl_article = new Article ( $wl_title ) ;
87 $wl = explode ( "\n" , $wl_article->getContent(true) ) ;
88 foreach ( $wl AS $x )
89 {
90 $isentry = false ;
91 $x = trim ( $x ) ;
92 while ( substr ( $x , 0 , 1 ) == "*" )
93 {
94 $isentry = true ;
95 $x = trim ( substr ( $x , 1 ) ) ;
96 }
97 if ( $isentry )
98 {
99 $sat[] = strtolower ( $x ) ;
100 }
101
102 }
103
104 # Templates, but only some
105 $t = explode ( "{{" , $t ) ;
106 $tl = array () ;
107 foreach ( $t AS $key => $x )
108 {
109 $y = explode ( "}}" , $x , 2 ) ;
110 if ( count ( $y ) == 2 )
111 {
112 $z = $y[0] ;
113 $z = explode ( "|" , $z ) ;
114 $tn = array_shift ( $z ) ;
115 if ( in_array ( strtolower ( $tn ) , $sat ) )
116 {
117 $tl[] = "{{" . $y[0] . "}}" ;
118 $t[$key] = $y[1] ;
119 $y = explode ( "}}" , $y[1] , 2 ) ;
120 }
121 else $t[$key] = "{{" . $x ;
122 }
123 else if ( $key != 0 ) $t[$key] = "{{" . $x ;
124 else $t[$key] = $x ;
125 }
126 if ( count ( $tl ) ) $s .= implode ( " " , $tl ) ;
127 $t = implode ( "" , $t ) ;
128
129 $t = str_replace ( "\n\n\n" , "\n" , $t ) ;
130 $this->mArticle->mContent = $t ;
131 $this->mMetaData = $s ;
132 }
133
134 /**
135 * This is the function that gets called for "action=edit".
136 */
137 function edit() {
138 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
139 // this is not an article
140 $wgOut->setArticleFlag(false);
141
142 $this->importFormData( $wgRequest );
143
144 if( $this->live ) {
145 $this->livePreview();
146 return;
147 }
148
149 if ( ! $this->mTitle->userCanEdit() ) {
150 $wgOut->readOnlyPage( $this->mArticle->getContent( true ), true );
151 return;
152 }
153 if ( $wgUser->isBlocked() ) {
154 $this->blockedIPpage();
155 return;
156 }
157 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
158 $this->userNotLoggedInPage();
159 return;
160 }
161 if ( wfReadOnly() ) {
162 if( $this->save || $this->preview ) {
163 $this->editForm( 'preview' );
164 } else {
165 $wgOut->readOnlyPage( $this->mArticle->getContent( true ) );
166 }
167 return;
168 }
169 if ( $this->save ) {
170 $this->editForm( 'save' );
171 } else if ( $this->preview ) {
172 $this->editForm( 'preview' );
173 } else { # First time through
174 if( $wgUser->getOption('previewonfirst') ) {
175 $this->editForm( 'preview', true );
176 } else {
177 $this->extractMetaDataFromArticle () ;
178 $this->editForm( 'initial', true );
179 }
180 }
181 }
182
183 /**
184 * @todo document
185 */
186 function importFormData( &$request ) {
187 # These fields need to be checked for encoding.
188 # Also remove trailing whitespace, but don't remove _initial_
189 # whitespace from the text boxes. This may be significant formatting.
190 $this->textbox1 = rtrim( $request->getText( 'wpTextbox1' ) );
191 $this->textbox2 = rtrim( $request->getText( 'wpTextbox2' ) );
192 $this->mMetaData = rtrim( $request->getText( 'metadata' ) );
193 $this->summary = trim( $request->getText( 'wpSummary' ) );
194
195 $this->edittime = $request->getVal( 'wpEdittime' );
196 if( !preg_match( '/^\d{14}$/', $this->edittime )) $this->edittime = '';
197
198 $this->preview = $request->getCheck( 'wpPreview' );
199 $this->save = $request->wasPosted() && !$this->preview;
200 $this->minoredit = $request->getCheck( 'wpMinoredit' );
201 $this->watchthis = $request->getCheck( 'wpWatchthis' );
202
203 $this->oldid = $request->getInt( 'oldid' );
204
205 # Section edit can come from either the form or a link
206 $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
207
208 $this->live = $request->getCheck( 'live' );
209 }
210
211 /**
212 * Since there is only one text field on the edit form,
213 * pressing <enter> will cause the form to be submitted, but
214 * the submit button value won't appear in the query, so we
215 * Fake it here before going back to edit(). This is kind of
216 * ugly, but it helps some old URLs to still work.
217 */
218 function submit() {
219 if( !$this->preview ) $this->save = true;
220
221 $this->edit();
222 }
223
224 /**
225 * The edit form is self-submitting, so that when things like
226 * preview and edit conflicts occur, we get the same form back
227 * with the extra stuff added. Only when the final submission
228 * is made and all is well do we actually save and redirect to
229 * the newly-edited page.
230 *
231 * @param string $formtype Type of form either : save, initial or preview
232 * @param bool $firsttime True to load form data from db
233 */
234 function editForm( $formtype, $firsttime = false ) {
235 global $wgOut, $wgUser;
236 global $wgLang, $wgContLang, $wgParser, $wgTitle;
237 global $wgAllowAnonymousMinor;
238 global $wgWhitelistEdit;
239 global $wgSpamRegex, $wgFilterCallback;
240 global $wgUseLatin1;
241
242 $sk = $wgUser->getSkin();
243 $isConflict = false;
244 // css / js subpages of user pages get a special treatment
245 $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
246
247 if(!$this->mTitle->getArticleID()) { # new article
248 $wgOut->addWikiText(wfmsg('newarticletext'));
249 }
250
251 if( Namespace::isTalk( $this->mTitle->getNamespace() ) ) {
252 $wgOut->addWikiText(wfmsg('talkpagetext'));
253 }
254
255 # Attempt submission here. This will check for edit conflicts,
256 # and redundantly check for locked database, blocked IPs, etc.
257 # that edit() already checked just in case someone tries to sneak
258 # in the back door with a hand-edited submission URL.
259
260 if ( 'save' == $formtype ) {
261 # Reintegrate metadata
262 if ( $this->mMetaData != "" ) $this->textbox1 .= "\n" . $this->mMetaData ;
263 $this->mMetaData = "" ;
264
265 # Check for spam
266 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1, $matches ) ) {
267 $this->spamPage ( $matches[0] );
268 return;
269 }
270 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle, $this->textbox1, $this->section ) ) {
271 # Error messages or other handling should be performed by the filter function
272 return;
273 }
274 if ( $wgUser->isBlocked() ) {
275 $this->blockedIPpage();
276 return;
277 }
278 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
279 $this->userNotLoggedInPage();
280 return;
281 }
282 if ( wfReadOnly() ) {
283 $wgOut->readOnlyPage();
284 return;
285 }
286
287 # If article is new, insert it.
288 $aid = $this->mTitle->getArticleID( GAID_FOR_UPDATE );
289 if ( 0 == $aid ) {
290 # Don't save a new article if it's blank.
291 if ( ( '' == $this->textbox1 ) ||
292 ( wfMsg( 'newarticletext' ) == $this->textbox1 ) ) {
293 $wgOut->redirect( $this->mTitle->getFullURL() );
294 return;
295 }
296 if (wfRunHooks('ArticleSave', $this->mArticle, $wgUser, $this->textbox1,
297 $this->summary, $this->minoredit, $this->watchthis, NULL))
298 {
299 $this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
300 $this->minoredit, $this->watchthis );
301 wfRunHooks('ArticleSaveComplete', $this->mArticle, $wgUser, $this->textbox1,
302 $this->summary, $this->minoredit, $this->watchthis, NULL);
303 }
304 return;
305 }
306
307 # Article exists. Check for edit conflict.
308
309 $this->mArticle->clear(); # Force reload of dates, etc.
310 $this->mArticle->forUpdate( true ); # Lock the article
311
312 if( ( $this->section != 'new' ) &&
313 ($this->mArticle->getTimestamp() != $this->edittime ) ) {
314 $isConflict = true;
315 }
316 $userid = $wgUser->getID();
317
318 if ( $isConflict) {
319 $text = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
320 $this->section, $this->textbox1, $this->summary, $this->edittime);
321 }
322 else {
323 $text = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
324 $this->section, $this->textbox1, $this->summary);
325 }
326 # Suppress edit conflict with self
327
328 if ( ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
329 $isConflict = false;
330 } else {
331 # switch from section editing to normal editing in edit conflict
332 if($isConflict) {
333 # Attempt merge
334 if( $this->mergeChangesInto( $text ) ){
335 // Successful merge! Maybe we should tell the user the good news?
336 $isConflict = false;
337 } else {
338 $this->section = '';
339 $this->textbox1 = $text;
340 }
341 }
342 }
343 if ( ! $isConflict ) {
344 # All's well
345 $sectionanchor = '';
346 if( $this->section == 'new' ) {
347 if( $this->summary != '' ) {
348 $sectionanchor = $this->sectionAnchor( $this->summary );
349 }
350 } elseif( $this->section != '' ) {
351 # Try to get a section anchor from the section source, redirect to edited section if header found
352 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
353 # for duplicate heading checking and maybe parsing
354 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1, $matches );
355 # we can't deal with anchors, includes, html etc in the header for now,
356 # headline would need to be parsed to improve this
357 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
358 if($hasmatch and strlen($matches[2]) > 0) {
359 $sectionanchor = $this->sectionAnchor( $matches[2] );
360 }
361 }
362
363 if (wfRunHooks('ArticleSave', $this->mArticle, $wgUser, $text, $this->summary,
364 $this->minoredit, $this->watchthis, $sectionanchor))
365 {
366 # update the article here
367 if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
368 $this->watchthis, '', $sectionanchor ))
369 {
370 wfRunHooks('ArticleSaveComplete', $this->mArticle, $wgUser, $text, $this->summary,
371 $this->minoredit, $this->watchthis, $sectionanchor);
372 return;
373 }
374 else
375 $isConflict = true;
376 }
377 }
378 }
379 # First time through: get contents, set time for conflict
380 # checking, etc.
381
382 if ( 'initial' == $formtype || $firsttime ) {
383 $this->edittime = $this->mArticle->getTimestamp();
384 $this->textbox1 = $this->mArticle->getContent( true );
385 $this->summary = '';
386 $this->proxyCheck();
387 }
388 $wgOut->setRobotpolicy( 'noindex,nofollow' );
389
390 # Enabled article-related sidebar, toplinks, etc.
391 $wgOut->setArticleRelated( true );
392
393 if ( $isConflict ) {
394 $s = wfMsg( 'editconflict', $this->mTitle->getPrefixedText() );
395 $wgOut->setPageTitle( $s );
396 $wgOut->addHTML( wfMsg( 'explainconflict' ) );
397
398 $this->textbox2 = $this->textbox1;
399 $this->textbox1 = $this->mArticle->getContent( true );
400 $this->edittime = $this->mArticle->getTimestamp();
401 } else {
402
403 if( $this->section != '' ) {
404 if( $this->section == 'new' ) {
405 $s = wfMsg('editingcomment', $this->mTitle->getPrefixedText() );
406 } else {
407 $s = wfMsg('editingsection', $this->mTitle->getPrefixedText() );
408 }
409 if(!$this->preview) {
410 preg_match( "/^(=+)(.+)\\1/mi",
411 $this->textbox1,
412 $matches );
413 if( !empty( $matches[2] ) ) {
414 $this->summary = "/* ". trim($matches[2])." */ ";
415 }
416 }
417 } else {
418 $s = wfMsg( 'editing', $this->mTitle->getPrefixedText() );
419 }
420 $wgOut->setPageTitle( $s );
421 if ( !$wgUseLatin1 && !$this->checkUnicodeCompliantBrowser() ) {
422 $this->mArticle->setOldSubtitle();
423 $wgOut->addWikiText( wfMsg( 'nonunicodebrowser') );
424 }
425 if ( $this->oldid ) {
426 $this->mArticle->setOldSubtitle();
427 $wgOut->addHTML( wfMsg( 'editingold' ) );
428 }
429 }
430
431 if( wfReadOnly() ) {
432 $wgOut->addHTML( '<strong>' .
433 wfMsg( 'readonlywarning' ) .
434 "</strong>" );
435 } else if ( $isCssJsSubpage and 'preview' != $formtype) {
436 $wgOut->addHTML( wfMsg( 'usercssjsyoucanpreview' ));
437 }
438 if( $this->mTitle->isProtected('edit') ) {
439 $wgOut->addHTML( '<strong>' . wfMsg( 'protectedpagewarning' ) .
440 "</strong><br />\n" );
441 }
442
443 $kblength = (int)(strlen( $this->textbox1 ) / 1024);
444 if( $kblength > 29 ) {
445 $wgOut->addHTML( '<strong>' .
446 wfMsg( 'longpagewarning', $wgLang->formatNum( $kblength ) )
447 . '</strong>' );
448 }
449
450 $rows = $wgUser->getOption( 'rows' );
451 $cols = $wgUser->getOption( 'cols' );
452
453 $ew = $wgUser->getOption( 'editwidth' );
454 if ( $ew ) $ew = " style=\"width:100%\"";
455 else $ew = '';
456
457 $q = 'action=submit';
458 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
459 $action = $this->mTitle->escapeLocalURL( $q );
460
461 $summary = wfMsg('summary');
462 $subject = wfMsg('subject');
463 $minor = wfMsg('minoredit');
464 $watchthis = wfMsg ('watchthis');
465 $save = wfMsg('savearticle');
466 $prev = wfMsg('showpreview');
467
468 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedText(),
469 wfMsg('cancel') );
470 $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
471 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
472 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
473 htmlspecialchars( wfMsg( 'newwindow' ) );
474
475 global $wgRightsText;
476 $copywarn = "<div id=\"editpage-copywarn\">\n" .
477 wfMsg( $wgRightsText ? 'copyrightwarning' : 'copyrightwarning2',
478 '[[' . wfMsg( 'copyrightpage' ) . ']]',
479 $wgRightsText ) . "\n</div>";
480
481 if( $wgUser->getOption('showtoolbar') and !$isCssJsSubpage ) {
482 # prepare toolbar for edit buttons
483 $toolbar = $this->getEditToolbar();
484 } else {
485 $toolbar = '';
486 }
487
488 // activate checkboxes if user wants them to be always active
489 if( !$this->preview ) {
490 if( $wgUser->getOption( 'watchdefault' ) ) $this->watchthis = true;
491 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit = true;
492
493 // activate checkbox also if user is already watching the page,
494 // require wpWatchthis to be unset so that second condition is not
495 // checked unnecessarily
496 if( !$this->watchthis && $this->mTitle->userIsWatching() ) $this->watchthis = true;
497 }
498
499 $minoredithtml = '';
500
501 if ( 0 != $wgUser->getID() || $wgAllowAnonymousMinor ) {
502 $minoredithtml =
503 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?" checked='checked'":"").
504 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
505 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
506 }
507
508 $watchhtml = '';
509
510 if ( 0 != $wgUser->getID() ) {
511 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?" checked='checked'":"").
512 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
513 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
514 }
515
516 $checkboxhtml = $minoredithtml . $watchhtml . '<br />';
517
518 $wgOut->addHTML( '<div id="wikiPreview">' );
519 if ( 'preview' == $formtype) {
520 $previewOutput = $this->getPreviewText( $isConflict, $isCssJsSubpage );
521 if( $wgUser->getOption('previewontop' ) ) {
522 $wgOut->addHTML( $previewOutput );
523 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
524 }
525 }
526 $wgOut->addHTML( '</div>' );
527
528 # if this is a comment, show a subject line at the top, which is also the edit summary.
529 # Otherwise, show a summary field at the bottom
530 $summarytext = htmlspecialchars( $wgContLang->recodeForEdit( $this->summary ) ); # FIXME
531 if( $this->section == 'new' ) {
532 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
533 $editsummary = '';
534 } else {
535 $commentsubject = '';
536 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
537 }
538
539 if( !$this->preview ) {
540 # Don't select the edit box on preview; this interferes with seeing what's going on.
541 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
542 }
543 # Prepare a list of templates used by this page
544 $db =& wfGetDB( DB_SLAVE );
545 $page = $db->tableName( 'page' );
546 $links = $db->tableName( 'links' );
547 $id = $this->mTitle->getArticleID();
548 $sql = "SELECT page_namespace,page_title,page_id ".
549 "FROM $page,$links WHERE l_to=page_id AND l_from={$id} and page_namespace=".NS_TEMPLATE;
550 $res = $db->query( $sql, "EditPage::editform" );
551
552 if ( $db->numRows( $res ) ) {
553 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
554 while ( $row = $db->fetchObject( $res ) ) {
555 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
556 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
557 }
558 }
559 $templates .= '</ul>';
560 } else {
561 $templates = '';
562 }
563
564 global $wgLivePreview, $wgStylePath;
565 /**
566 * Live Preview lets us fetch rendered preview page content and
567 * add it to the page without refreshing the whole page.
568 * Set up the button for it; if not supported by the browser
569 * it will fall through to the normal form submission method.
570 */
571 if( $wgLivePreview ) {
572 $wgOut->addHTML( '<script type="text/javascript" src="' .
573 htmlspecialchars( $wgStylePath . '/common/preview.js' ) .
574 '"></script>' . "\n" );
575 $liveAction = $wgTitle->getLocalUrl( 'action=submit&wpPreview=true&live=true' );
576 $liveOnclick = 'onclick="return !livePreview('.
577 'getElementById(\'wikiPreview\'),' .
578 'editform.wpTextbox1.value,' .
579 htmlspecialchars( '"' . $liveAction . '"' ) . ')"';
580 } else {
581 $liveOnclick = '';
582 }
583
584 global $wgUseMetadataEdit ;
585 if ( $wgUseMetadataEdit )
586 {
587 $metadata = $this->mMetaData ;
588 $metadata = htmlspecialchars( $wgContLang->recodeForEdit( $metadata ) ) ;
589 $helppage = Title::newFromText ( wfmsg("metadata_page") ) ;
590 $top = str_replace ( "$1" , $helppage->getInternalURL() , wfmsg("metadata") ) ;
591 $metadata = $top . "<textarea name='metadata' rows='3' cols='{$cols}'{$ew}>{$metadata}</textarea>" ;
592 }
593 else $metadata = "" ;
594
595
596 $wgOut->addHTML( <<<END
597 {$toolbar}
598 <form id="editform" name="editform" method="post" action="$action"
599 enctype="multipart/form-data">
600 {$commentsubject}
601 <textarea tabindex='1' accesskey="," name="wpTextbox1" rows='{$rows}'
602 cols='{$cols}'{$ew}>
603 END
604 . htmlspecialchars( $wgContLang->recodeForEdit( $this->textbox1 ) ) .
605 "
606 </textarea>
607 {$metadata}
608 <br />{$editsummary}
609 {$checkboxhtml}
610 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
611 " title=\"".wfMsg('tooltip-save')."\"/>
612 <input tabindex='6' id='wpPreview' type='submit' $liveOnclick value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
613 " title=\"".wfMsg('tooltip-preview')."\"/>
614 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
615 $wgOut->addWikiText( $copywarn );
616 $wgOut->addHTML( "
617 <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
618 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
619
620 if ( $isConflict ) {
621 require_once( "DifferenceEngine.php" );
622 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
623 DifferenceEngine::showDiff( $this->textbox2, $this->textbox1,
624 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
625
626 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
627 <textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
628 . htmlspecialchars( $wgContLang->recodeForEdit( $this->textbox2 ) ) .
629 "
630 </textarea>" );
631 }
632 $wgOut->addHTML( "</form>\n" );
633 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
634 $wgOut->addHTML('<div id="wikiPreview">' . $previewOutput . '</div>');
635 }
636 }
637
638 function getPreviewText( $isConflict, $isCssJsSubpage ) {
639 global $wgOut, $wgUser, $wgTitle, $wgParser;
640 $previewhead='<h2>' . wfMsg( 'preview' ) . "</h2>\n<p><center><font color=\"#cc0000\">" .
641 wfMsg( 'note' ) . wfMsg( 'previewnote' ) . "</font></center></p>\n";
642 if ( $isConflict ) {
643 $previewhead.='<h2>' . wfMsg( 'previewconflict' ) .
644 "</h2>\n";
645 }
646
647 $parserOptions = ParserOptions::newFromUser( $wgUser );
648 $parserOptions->setEditSection( false );
649 $parserOptions->setEditSectionOnRightClick( false );
650
651 # don't parse user css/js, show message about preview
652 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
653
654 if ( $isCssJsSubpage ) {
655 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
656 $previewtext = wfMsg('usercsspreview');
657 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
658 $previewtext = wfMsg('userjspreview');
659 }
660 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
661 $wgOut->addHTML( $parserOutput->mText );
662 } else {
663 # if user want to see preview when he edit an article
664 if( $wgUser->getOption('previewonfirst') and ($this->textbox1 == '')) {
665 $this->textbox1 = $this->mArticle->getContent(true);
666 }
667
668 $toparse = $this->textbox1 ;
669 if ( $this->mMetaData != "" ) $toparse .= "\n" . $this->mMetaData ;
670
671 $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $toparse ) ."\n\n",
672 $wgTitle, $parserOptions );
673
674 $previewHTML = $parserOutput->mText;
675 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
676 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
677 }
678 return $previewhead . $previewHTML;
679 }
680
681 /**
682 * @todo document
683 */
684 function blockedIPpage() {
685 global $wgOut, $wgUser, $wgContLang, $wgIP;
686
687 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
688 $wgOut->setRobotpolicy( 'noindex,nofollow' );
689 $wgOut->setArticleRelated( false );
690
691 $id = $wgUser->blockedBy();
692 $reason = $wgUser->blockedFor();
693 $ip = $wgIP;
694
695 if ( is_numeric( $id ) ) {
696 $name = User::whoIs( $id );
697 } else {
698 $name = $id;
699 }
700 $link = '[[' . $wgContLang->getNsText( Namespace::getUser() ) .
701 ":{$name}|{$name}]]";
702
703 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
704 $wgOut->returnToMain( false );
705 }
706
707 /**
708 * @todo document
709 */
710 function userNotLoggedInPage() {
711 global $wgOut, $wgUser;
712
713 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
714 $wgOut->setRobotpolicy( 'noindex,nofollow' );
715 $wgOut->setArticleRelated( false );
716
717 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
718 $wgOut->returnToMain( false );
719 }
720
721 /**
722 * @todo document
723 */
724 function spamPage ( $match = false )
725 {
726 global $wgOut;
727 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
728 $wgOut->setRobotpolicy( 'noindex,nofollow' );
729 $wgOut->setArticleRelated( false );
730
731 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
732 if ( $match ) {
733 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$match}</nowiki>" ) );
734 }
735 $wgOut->returnToMain( false );
736 }
737
738 /**
739 * Forks processes to scan the originating IP for an open proxy server
740 * MemCached can be used to skip IPs that have already been scanned
741 */
742 function proxyCheck() {
743 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
744 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
745
746 if ( !$wgBlockOpenProxies ) {
747 return;
748 }
749
750 # Get MemCached key
751 $skip = false;
752 if ( $wgUseMemCached ) {
753 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
754 $mcValue = $wgMemc->get( $mcKey );
755 if ( $mcValue ) {
756 $skip = true;
757 }
758 }
759
760 # Fork the processes
761 if ( !$skip ) {
762 $title = Title::makeTitle( NS_SPECIAL, 'Blockme' );
763 $iphash = md5( $wgIP . $wgProxyKey );
764 $url = $title->getFullURL( 'ip='.$iphash );
765
766 foreach ( $wgProxyPorts as $port ) {
767 $params = implode( ' ', array(
768 escapeshellarg( $wgProxyScriptPath ),
769 escapeshellarg( $wgIP ),
770 escapeshellarg( $port ),
771 escapeshellarg( $url )
772 ));
773 exec( "php $params &>/dev/null &" );
774 }
775 # Set MemCached key
776 if ( $wgUseMemCached ) {
777 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
778 }
779 }
780 }
781
782 /**
783 * @access private
784 * @todo document
785 */
786 function mergeChangesInto( &$text ){
787 $yourtext = $this->mArticle->fetchRevisionText();
788
789 $db =& wfGetDB( DB_SLAVE );
790 $oldText = $this->mArticle->fetchRevisionText(
791 $db->timestamp( $this->edittime ),
792 'rev_timestamp' );
793
794 if(wfMerge($oldText, $text, $yourtext, $result)){
795 $text = $result;
796 return true;
797 } else {
798 return false;
799 }
800 }
801
802
803 function checkUnicodeCompliantBrowser() {
804 global $wgBrowserBlackList;
805 $currentbrowser = $_SERVER["HTTP_USER_AGENT"];
806 foreach ( $wgBrowserBlackList as $browser ) {
807 if ( preg_match($browser, $currentbrowser) ) {
808 return false;
809 }
810 }
811 return true;
812 }
813
814 /**
815 * Format an anchor fragment as it would appear for a given section name
816 * @param string $text
817 * @return string
818 * @access private
819 */
820 function sectionAnchor( $text ) {
821 global $wgInputEncoding;
822 $headline = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
823 # strip out HTML
824 $headline = preg_replace( '/<.*?' . '>/', '', $headline );
825 $headline = trim( $headline );
826 $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
827 $replacearray = array(
828 '%3A' => ':',
829 '%' => '.'
830 );
831 return str_replace(
832 array_keys( $replacearray ),
833 array_values( $replacearray ),
834 $sectionanchor );
835 }
836
837 /**
838 * Shows a bulletin board style toolbar for common editing functions.
839 * It can be disabled in the user preferences.
840 * The necessary JavaScript code can be found in style/wikibits.js.
841 */
842 function getEditToolbar() {
843 global $wgStylePath, $wgLang, $wgMimeType;
844
845 /**
846 * toolarray an array of arrays which each include the filename of
847 * the button image (without path), the opening tag, the closing tag,
848 * and optionally a sample text that is inserted between the two when no
849 * selection is highlighted.
850 * The tip text is shown when the user moves the mouse over the button.
851 *
852 * Already here are accesskeys (key), which are not used yet until someone
853 * can figure out a way to make them work in IE. However, we should make
854 * sure these keys are not defined on the edit page.
855 */
856 $toolarray=array(
857 array( 'image'=>'button_bold.png',
858 'open' => "\'\'\'",
859 'close' => "\'\'\'",
860 'sample'=> wfMsg('bold_sample'),
861 'tip' => wfMsg('bold_tip'),
862 'key' => 'B'
863 ),
864 array( 'image'=>'button_italic.png',
865 'open' => "\'\'",
866 'close' => "\'\'",
867 'sample'=> wfMsg('italic_sample'),
868 'tip' => wfMsg('italic_tip'),
869 'key' => 'I'
870 ),
871 array( 'image'=>'button_link.png',
872 'open' => '[[',
873 'close' => ']]',
874 'sample'=> wfMsg('link_sample'),
875 'tip' => wfMsg('link_tip'),
876 'key' => 'L'
877 ),
878 array( 'image'=>'button_extlink.png',
879 'open' => '[',
880 'close' => ']',
881 'sample'=> wfMsg('extlink_sample'),
882 'tip' => wfMsg('extlink_tip'),
883 'key' => 'X'
884 ),
885 array( 'image'=>'button_headline.png',
886 'open' => "\\n== ",
887 'close' => " ==\\n",
888 'sample'=> wfMsg('headline_sample'),
889 'tip' => wfMsg('headline_tip'),
890 'key' => 'H'
891 ),
892 array( 'image'=>'button_image.png',
893 'open' => '[['.$wgLang->getNsText(NS_IMAGE).":",
894 'close' => ']]',
895 'sample'=> wfMsg('image_sample'),
896 'tip' => wfMsg('image_tip'),
897 'key' => 'D'
898 ),
899 array( 'image' => 'button_media.png',
900 'open' => '[['.$wgLang->getNsText(NS_MEDIA).':',
901 'close' => ']]',
902 'sample'=> wfMsg('media_sample'),
903 'tip' => wfMsg('media_tip'),
904 'key' => 'M'
905 ),
906 array( 'image' => 'button_math.png',
907 'open' => "\\<math\\>",
908 'close' => "\\</math\\>",
909 'sample'=> wfMsg('math_sample'),
910 'tip' => wfMsg('math_tip'),
911 'key' => 'C'
912 ),
913 array( 'image' => 'button_nowiki.png',
914 'open' => "\\<nowiki\\>",
915 'close' => "\\</nowiki\\>",
916 'sample'=> wfMsg('nowiki_sample'),
917 'tip' => wfMsg('nowiki_tip'),
918 'key' => 'N'
919 ),
920 array( 'image' => 'button_sig.png',
921 'open' => '--~~~~',
922 'close' => '',
923 'sample'=> '',
924 'tip' => wfMsg('sig_tip'),
925 'key' => 'Y'
926 ),
927 array( 'image' => 'button_hr.png',
928 'open' => "\\n----\\n",
929 'close' => '',
930 'sample'=> '',
931 'tip' => wfMsg('hr_tip'),
932 'key' => 'R'
933 )
934 );
935 $toolbar ="<script type='text/javascript'>\n/*<![CDATA[*/\n";
936
937 $toolbar.="document.writeln(\"<div id='toolbar'>\");\n";
938 foreach($toolarray as $tool) {
939
940 $image=$wgStylePath.'/common/images/'.$tool['image'];
941 $open=$tool['open'];
942 $close=$tool['close'];
943 $sample = addslashes( $tool['sample'] );
944
945 // Note that we use the tip both for the ALT tag and the TITLE tag of the image.
946 // Older browsers show a "speedtip" type message only for ALT.
947 // Ideally these should be different, realistically they
948 // probably don't need to be.
949 $tip = addslashes( $tool['tip'] );
950
951 #$key = $tool["key"];
952
953 $toolbar.="addButton('$image','$tip','$open','$close','$sample');\n";
954 }
955
956 $toolbar.="addInfobox('" . addslashes( wfMsg( "infobox" ) ) . "','" . addslashes(wfMsg("infobox_alert")) . "');\n";
957 $toolbar.="document.writeln(\"</div>\");\n";
958
959 $toolbar.="/*]]>*/\n</script>";
960 return $toolbar;
961 }
962
963 /**
964 * Output preview text only. This can be sucked into the edit page
965 * via JavaScript, and saves the server time rendering the skin as
966 * well as theoretically being more robust on the client (doesn't
967 * disturb the edit box's undo history, won't eat your text on
968 * failure, etc).
969 *
970 * @todo This doesn't include category or interlanguage links.
971 * Would need to enhance it a bit, maybe wrap them in XML
972 * or something... that might also require more skin
973 * initialization, so check whether that's a problem.
974 */
975 function livePreview() {
976 global $wgOut;
977 $wgOut->disable();
978 header( 'Content-type: text/xml' );
979 header( 'Cache-control: no-cache' );
980 # FIXME
981 echo $this->getPreviewText( false, false );
982 }
983
984 }
985
986 ?>