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