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