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