truncate summary for multibyte chars
[lhc/web/wiklou.git] / includes / EditPage.php
index 941cef9..b6b1a77 100644 (file)
@@ -22,6 +22,7 @@ class EditPage {
        var $deletedSinceEdit = false;
        var $formtype;
        var $firsttime;
+       var $lastDelete;
 
        # Form values
        var $save = false, $preview = false, $diff = false;
@@ -152,6 +153,10 @@ class EditPage {
         */
        function edit() {
                global $wgOut, $wgUser, $wgRequest, $wgTitle;
+
+               $fname = 'EditPage::edit';
+               wfProfileIn( $fname );
+
                // this is not an article
                $wgOut->setArticleFlag(false);
 
@@ -160,25 +165,30 @@ class EditPage {
 
                if( $this->live ) {
                        $this->livePreview();
+                       wfProfileOut( $fname );
                        return;
                }
 
                if ( ! $this->mTitle->userCanEdit() ) {
                        $wgOut->readOnlyPage( $this->mArticle->getContent( true ), true );
+                       wfProfileOut( $fname );
                        return;
                }
                if ( !$this->preview && !$this->diff && $wgUser->isBlockedFrom( $this->mTitle, !$this->save ) ) {
                        # When previewing, don't check blocked state - will get caught at save time.
                        # Also, check when starting edition is done against slave to improve performance.
                        $this->blockedIPpage();
+                       wfProfileOut( $fname );
                        return;
                }
                if ( !$wgUser->isAllowed('edit') ) {
                        if ( $wgUser->isAnon() ) {
                                $this->userNotLoggedInPage();
+                               wfProfileOut( $fname );
                                return;
                        } else {
                                $wgOut->readOnlyPage( $this->mArticle->getContent( true ), true );
+                               wfProfileOut( $fname );
                                return;
                        }
                }
@@ -189,6 +199,7 @@ class EditPage {
                                $this->formtype = 'diff';
                        } else {
                                $wgOut->readOnlyPage( $this->mArticle->getContent( true ) );
+                               wfProfileOut( $fname );
                                return;
                        }
                } else {
@@ -208,6 +219,8 @@ class EditPage {
                                }
                        }
                }
+
+               wfProfileIn( "$fname-business-end" );
                
                $this->isConflict = false;
                // css / js subpages of user pages get a special treatment
@@ -224,9 +237,9 @@ class EditPage {
                         * to only perform it on saves
                         */
                        if ( $this->mTitle->isDeleted() ) {
-                               $query = $this->getLastDelete();
-                               if ( !is_null($query) ) {
-                                       $deletetime = $query->log_timestamp;
+                               $this->lastDelete = $this->getLastDelete();
+                               if ( !is_null($this->lastDelete) ) {
+                                       $deletetime = $this->lastDelete->log_timestamp;
                                        if ( ($deletetime - $this->starttime) > 0 ) {
                                                $this->deletedSinceEdit = true;
                                        }
@@ -248,6 +261,8 @@ class EditPage {
 
                if ( 'save' == $this->formtype ) {
                        if ( !$this->attemptSave() ) {
+                               wfProfileOut( "$fname-business-end" );
+                               wfProfileOut( $fname );
                                return;
                        }
                }
@@ -259,6 +274,8 @@ class EditPage {
                }
 
                $this->showEditForm();
+               wfProfileOut( "$fname-business-end" );
+               wfProfileOut( $fname );
        }
 
        /**
@@ -278,6 +295,10 @@ class EditPage {
         * @todo document
         */
        function importFormData( &$request ) {
+               global $wgLang ;
+               $fname = 'EditPage::importFormData';
+               wfProfileIn( $fname );
+
                if( $request->wasPosted() ) {
                        # These fields need to be checked for encoding.
                        # Also remove trailing whitespace, but don't remove _initial_
@@ -285,7 +306,8 @@ class EditPage {
                        $this->textbox1 = $this->safeUnicodeInput( $request, 'wpTextbox1' );
                        $this->textbox2 = $this->safeUnicodeInput( $request, 'wpTextbox2' );
                        $this->mMetaData = rtrim( $request->getText( 'metadata'   ) );
-                       $this->summary   =        $request->getText( 'wpSummary'  );
+                       # Truncate for whole multibyte characters. +5 bytes for ellipsis
+                       $this->summary   = $wgLang->truncate( $request->getText( 'wpSummary'  ), 250 );
 
                        $this->edittime = $request->getVal( 'wpEdittime' );
                        $this->starttime = $request->getVal( 'wpStarttime' );
@@ -343,6 +365,7 @@ class EditPage {
                $this->live = $request->getCheck( 'live' );
                $this->editintro = $request->getText( 'editintro' );
                
+               wfProfileOut( $fname );
        }
 
        /**
@@ -364,7 +387,7 @@ class EditPage {
        }
 
        function showIntro() {
-               global $wgOut;
+               global $wgOut, $wgUser;
                $addstandardintro=true;
                if($this->editintro) {
                        $introtitle=Title::newFromText($this->editintro);
@@ -377,7 +400,10 @@ class EditPage {
                        }
                }
                if($addstandardintro) {
-                       $wgOut->addWikiText( wfMsg( 'newarticletext' ) );                               
+                       if ( $wgUser->isLoggedIn() ) 
+                               $wgOut->addWikiText( wfMsg( 'newarticletext' ) );
+                       else
+                               $wgOut->addWikiText( wfMsg( 'newarticletextanon' ) );
                }
        }
 
@@ -388,6 +414,10 @@ class EditPage {
        function attemptSave() {
                global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut;
                
+               $fname = 'EditPage::attemptSave';
+               wfProfileIn( $fname );
+               wfProfileIn( "$fname-checks" );
+
                # Reintegrate metadata
                if ( $this->mMetaData != '' ) $this->textbox1 .= "\n" . $this->mMetaData ;
                $this->mMetaData = '' ;
@@ -395,64 +425,83 @@ class EditPage {
                # Check for spam
                if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1, $matches ) ) {
                        $this->spamPage ( $matches[0] );
+                       wfProfileOut( "$fname-checks" );
+                       wfProfileOut( $fname );
                        return false;
                }
                if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle, $this->textbox1, $this->section ) ) {
                        # Error messages or other handling should be performed by the filter function
+                       wfProfileOut( $fname );
+                       wfProfileOut( "$fname-checks" );
+                       return false;
+               }
+               if ( !wfRunHooks( 'EditFilter', array( &$this, $this->textbox1, $this->section ) ) ) {
+                       # Error messages or other handling should be performed by the filter function
+                       wfProfileOut( $fname );
+                       wfProfileOut( "$fname-checks" );
                        return false;
                }
                if ( $wgUser->isBlockedFrom( $this->mTitle, false ) ) {
                        # Check block state against master, thus 'false'.
                        $this->blockedIPpage();
+                       wfProfileOut( "$fname-checks" );
+                       wfProfileOut( $fname );
                        return false;
                }
 
                if ( !$wgUser->isAllowed('edit') ) {
                        if ( $wgUser->isAnon() ) {
                                $this->userNotLoggedInPage();
+                               wfProfileOut( "$fname-checks" );
+                               wfProfileOut( $fname );
                                return false;
                        }
                        else {
                                $wgOut->readOnlyPage();
+                               wfProfileOut( "$fname-checks" );
+                               wfProfileOut( $fname );
                                return false;
                        }
                }
 
                if ( wfReadOnly() ) {
                        $wgOut->readOnlyPage();
+                       wfProfileOut( "$fname-checks" );
+                       wfProfileOut( $fname );
                        return false;
                }
                if ( $wgUser->pingLimiter() ) {
                        $wgOut->rateLimited();
+                       wfProfileOut( "$fname-checks" );
+                       wfProfileOut( $fname );
                        return false;
                }
 
                # If the article has been deleted while editing, don't save it without
                # confirmation
                if ( $this->deletedSinceEdit && !$this->recreate ) {
+                       wfProfileOut( "$fname-checks" );
+                       wfProfileOut( $fname );
                        return true;
                }
                
+               wfProfileOut( "$fname-checks" );
+               
                # If article is new, insert it.
                $aid = $this->mTitle->getArticleID( GAID_FOR_UPDATE );
                if ( 0 == $aid ) {
                        # Don't save a new article if it's blank.
-                       if ( ( '' == $this->textbox1 ) ||
-                               ( wfMsg( 'newarticletext' ) == $this->textbox1 ) ) {
+                       if ( ( '' == $this->textbox1 ) ) {
                                        $wgOut->redirect( $this->mTitle->getFullURL() );
+                                       wfProfileOut( $fname );
                                        return false;
-                               }
-                       if (wfRunHooks('ArticleSave', array(&$this->mArticle, &$wgUser, &$this->textbox1,
-                               &$this->summary, &$this->minoredit, &$this->watchthis, NULL)))
-                       {                                       
-
-                               $isComment=($this->section=='new');
-                               $this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
-                                       $this->minoredit, $this->watchthis, false, $isComment);
-                               wfRunHooks('ArticleSaveComplete', array(&$this->mArticle, &$wgUser, $this->textbox1,
-                                       $this->summary, $this->minoredit,
-                                       $this->watchthis, NULL));
                        }
+
+                       $isComment=($this->section=='new');
+                       $this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
+                               $this->minoredit, $this->watchthis, false, $isComment);
+                       
+                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -471,17 +520,15 @@ class EditPage {
                if ( $this->isConflict) {
                        wfDebug( "EditPage::editForm conflict! getting section '$this->section' for time '$this->edittime' (article time '" .
                                $this->mArticle->getTimestamp() . "'\n" );
-                       $text = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
-                               $this->section, $this->textbox1, $this->summary, $this->edittime);
+                       $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $this->summary, $this->edittime);
                }
                else {
                        wfDebug( "EditPage::editForm getting section '$this->section'\n" );
-                       $text = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
-                               $this->section, $this->textbox1, $this->summary);
+                       $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $this->summary);
                }
 
-               # Suppress edit conflict with self
-               if ( ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
+               # Suppress edit conflict with self, except for section edits where merging is required.
+               if ( ( $this->section == '' ) && ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
                        wfDebug( "Suppressing edit conflict, same user.\n" );
                        $this->isConflict = false;
                } else {
@@ -501,10 +548,12 @@ class EditPage {
                }
                
                if ( $this->isConflict ) {
+                       wfProfileOut( $fname );
                        return true;
                }
                
                # All's well
+               wfProfileIn( "$fname-sectionanchor" );
                $sectionanchor = '';
                if( $this->section == 'new' ) {
                        if( $this->summary != '' ) {
@@ -512,7 +561,7 @@ class EditPage {
                        }
                } elseif( $this->section != '' ) {
                        # Try to get a section anchor from the section source, redirect to edited section if header found
-                       # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
+                       # XXX: might be better to integrate this into Article::replaceSection
                        # for duplicate heading checking and maybe parsing
                        $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1, $matches );
                        # we can't deal with anchors, includes, html etc in the header for now,
@@ -521,6 +570,7 @@ class EditPage {
                                $sectionanchor = $this->sectionAnchor( $matches[2] );
                        }
                }
+               wfProfileOut( "$fname-sectionanchor" );
 
                // Save errors may fall down to the edit form, but we've now
                // merged the section into full text. Clear the section field
@@ -529,23 +579,15 @@ class EditPage {
                $this->textbox1 = $text;
                $this->section = '';
 
-               if (wfRunHooks('ArticleSave', array(&$this->mArticle, &$wgUser, &$text,
-                       &$this->summary, &$this->minoredit,
-                       &$this->watchthis, &$sectionanchor)))
-               {
-                       # update the article here
-                       if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
-                               $this->watchthis, '', $sectionanchor ))
-                       {
-                               wfRunHooks('ArticleSaveComplete',
-                                       array(&$this->mArticle, &$wgUser, $text,
-                                       $this->summary, $this->minoredit,
-                                       $this->watchthis, $sectionanchor));
-                               return false;
-                       } else {
-                               $this->isConflict = true;
-                       }
+               # update the article here
+               if( $this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
+                       $this->watchthis, '', $sectionanchor ) ) {
+                       wfProfileOut( $fname );
+                       return false;
+               } else {
+                       $this->isConflict = true;
                }
+               wfProfileOut( $fname );
                return true;
        }
 
@@ -557,15 +599,21 @@ class EditPage {
                $this->edittime = $this->mArticle->getTimestamp();
                $this->textbox1 = $this->mArticle->getContent( true );
                $this->summary = '';
-               $this->proxyCheck();
+               wfProxyCheck();
        }
 
        /**
         * Send the edit form and related headers to $wgOut
+        * @param $formCallback Optional callable that takes an OutputPage
+        *                      parameter; will be called during form output
+        *                      near the top, for captchas and the like.
         */
-       function showEditForm() {
+       function showEditForm( $formCallback=null ) {
                global $wgOut, $wgUser, $wgAllowAnonymousMinor, $wgLang, $wgContLang;
 
+               $fname = 'EditPage::showEditForm';
+               wfProfileIn( $fname );
+
                $sk =& $wgUser->getSkin();
                
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
@@ -761,7 +809,7 @@ class EditPage {
                                // Add an confirmation checkbox and explanation.
                                $toolbar = '';
                                $hidden = 'type="hidden" style="display:none;"';
-                               $recreate = $wgOut->parse( wfMsg( 'confirmrecreate',  $query->user_name , $query->log_comment ));
+                               $recreate = $wgOut->parse( wfMsg( 'confirmrecreate',  $this->lastDelete->user_name , $this->lastDelete->log_comment ));
                                $recreate .=
                                        "<br /><input tabindex='1' type='checkbox' value='1' name='wpRecreate' id='wpRecreate' />".
                                        "<label for='wpRecreate' title='".wfMsg('tooltip-recreate')."'>". wfMsg('recreate')."</label>";
@@ -776,6 +824,12 @@ class EditPage {
 {$toolbar}
 <form id="editform" name="editform" method="post" action="$action"
 enctype="multipart/form-data">
+END
+);
+               if( is_callable( $formCallback ) ) {
+                       call_user_func_array( $formCallback, array( &$wgOut ) );
+               }
+               $wgOut->addHTML( <<<END
 $recreate
 {$commentsubject}
 <textarea tabindex='1' accesskey="," name="wpTextbox1" rows='{$rows}'
@@ -833,6 +887,8 @@ END
                        #$wgOut->addHTML( '<div id="wikiPreview">' . $difftext . '</div>' );
                        $wgOut->addHTML( $this->getDiff() );
                }
+
+               wfProfileOut( $fname );
        }
 
        /**
@@ -840,6 +896,10 @@ END
         */
        function getTemplatesUsed() {
                global $wgUser;
+
+               $fname = 'EditPage::getTemplatesUsed';
+               wfProfileIn( $fname );
+
                $sk =& $wgUser->getSkin();
 
                $templates = '';
@@ -853,6 +913,7 @@ END
                        }
                        $templates .= '</ul>';
                }
+               wfProfileOut( $fname );
                return $templates;
        }
 
@@ -892,10 +953,10 @@ END
                               'log_comment',
                               'log_params',
                               'user_name', ),
-                       array( 'log_namespace="' . $this->mTitle->getNamespace() . '"',
-                              'log_title="' . $this->mTitle->getDBkey() . '"',
-                              'log_type="delete"',
-                              'log_action="delete"',
+                       array( 'log_namespace' => $this->mTitle->getNamespace(),
+                              'log_title' => $this->mTitle->getDBkey(),
+                              'log_type' => 'delete',
+                              'log_action' => 'delete',
                               'user_id=log_user' ),
                        $fname,
                        array( 'LIMIT' => 1, 'ORDER BY' => 'log_timestamp DESC' ) );
@@ -915,6 +976,10 @@ END
         */
        function getPreviewText() {
                global $wgOut, $wgUser, $wgTitle, $wgParser, $wgAllowDiffPreview, $wgEnableDiffPreviewPreference;
+
+               $fname = 'EditPage::getPreviewText';
+               wfProfileIn( $fname );
+
                $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
                        "<p class='previewnote'>" . htmlspecialchars( wfMsg( 'previewnote' ) ) . "</p>\n";
                if ( $this->isConflict ) {
@@ -936,6 +1001,7 @@ END
                        }
                        $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
                        $wgOut->addHTML( $parserOutput->mText );
+                       wfProfileOut( $fname );
                        return $previewhead;
                } else {
                        # if user want to see preview when he edit an article
@@ -960,6 +1026,8 @@ END
 
                        $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
                        $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
+
+                       wfProfileOut( $fname );
                        return $previewhead . $previewHTML;
                }
        }
@@ -968,7 +1036,7 @@ END
         * @todo document
         */
        function blockedIPpage() {
-               global $wgOut, $wgUser, $wgContLang, $wgIP;
+               global $wgOut, $wgUser, $wgContLang;
 
                $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
@@ -976,7 +1044,7 @@ END
 
                $id = $wgUser->blockedBy();
                $reason = $wgUser->blockedFor();
-               $ip = $wgIP;
+               $ip = wfGetIP();
 
                if ( is_numeric( $id ) ) {
                        $name = User::whoIs( $id );
@@ -1021,50 +1089,6 @@ END
                $wgOut->returnToMain( false );
        }
 
-       /**
-        * Forks processes to scan the originating IP for an open proxy server
-        * MemCached can be used to skip IPs that have already been scanned
-        */
-       function proxyCheck() {
-               global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
-               global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
-
-               if ( !$wgBlockOpenProxies ) {
-                       return;
-               }
-
-               # Get MemCached key
-               $skip = false;
-               if ( $wgUseMemCached ) {
-                       $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
-                       $mcValue = $wgMemc->get( $mcKey );
-                       if ( $mcValue ) {
-                               $skip = true;
-                       }
-               }
-
-               # Fork the processes
-               if ( !$skip ) {
-                       $title = Title::makeTitle( NS_SPECIAL, 'Blockme' );
-                       $iphash = md5( $wgIP . $wgProxyKey );
-                       $url = $title->getFullURL( 'ip='.$iphash );
-
-                       foreach ( $wgProxyPorts as $port ) {
-                               $params = implode( ' ', array(
-                                                       escapeshellarg( $wgProxyScriptPath ),
-                                                       escapeshellarg( $wgIP ),
-                                                       escapeshellarg( $port ),
-                                                       escapeshellarg( $url )
-                                                       ));
-                               exec( "php $params &>/dev/null &" );
-                       }
-                       # Set MemCached key
-                       if ( $wgUseMemCached ) {
-                               $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
-                       }
-               }
-       }
-
        /**
         * @access private
         * @todo document
@@ -1256,10 +1280,7 @@ END
                        $toolbar.="addButton('$image','$tip','$open','$close','$sample');\n";
                }
 
-               $toolbar.="addInfobox('" . wfEscapeJsString( wfMsg( "infobox" ) ) .
-                       "','" . wfEscapeJsString( wfMsg( "infobox_alert" ) ) . "');\n";
                $toolbar.="document.writeln(\"</div>\");\n";
-
                $toolbar.="/*]]>*/\n</script>";
                return $toolbar;
        }
@@ -1296,13 +1317,15 @@ END
         * @return string HTML
         */
        function getDiff() {
+               global $wgUser;
+
                require_once( 'DifferenceEngine.php' );
                $oldtext = $this->mArticle->fetchContent();
-               $newtext = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
+               $newtext = $this->mArticle->replaceSection(
                        $this->section, $this->textbox1, $this->summary, $this->edittime );
                $oldtitle = wfMsg( 'currentrev' );
                $newtitle = wfMsg( 'yourtext' );
-               if ( $oldtext != wfMsg( 'noarticletext' ) || $newtext != '' ) {
+               if ( $oldtext != wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' ) || $newtext != '' ) {
                        $difftext = DifferenceEngine::getDiff( $oldtext, $newtext, $oldtitle, $newtitle );
                }