add tests for {{#special:}}
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
index 695c8c2..299cb23 100644 (file)
@@ -77,7 +77,6 @@ class PageArchive {
         * @fixme Does this belong in Image for fuller encapsulation?
         */
        function listFiles() {
-               $fname = __CLASS__ . '::' . __FUNCTION__;
                if( $this->title->getNamespace() == NS_IMAGE ) {
                        $dbr =& wfGetDB( DB_SLAVE );
                        $res = $dbr->select( 'filearchive',
@@ -93,7 +92,7 @@ class PageArchive {
                                        'fa_user_text',
                                        'fa_timestamp' ),
                                array( 'fa_name' => $this->title->getDbKey() ),
-                               $fname,
+                               __METHOD__,
                                array( 'ORDER BY' => 'fa_timestamp DESC' ) );
                        $ret = $dbr->resultObject( $res );
                        return $ret;
@@ -106,18 +105,47 @@ class PageArchive {
         * revision of the page with the given timestamp.
         *
         * @return string
+        * @deprecated Use getRevision() for more flexible information
         */
        function getRevisionText( $timestamp ) {
-               $fname = 'PageArchive::getRevisionText';
+               $rev = $this->getRevision( $timestamp );
+               return $rev ? $rev->getText() : null;
+       }
+
+       /**
+        * Return a Revision object containing data for the deleted revision.
+        * Note that the result *may* or *may not* have a null page ID.
+        * @param string $timestamp
+        * @return Revision
+        */
+       function getRevision( $timestamp ) {
                $dbr =& wfGetDB( DB_SLAVE );
                $row = $dbr->selectRow( 'archive',
-                       array( 'ar_text', 'ar_flags', 'ar_text_id' ),
+                       array(
+                               'ar_rev_id',
+                               'ar_text',
+                               'ar_comment',
+                               'ar_user',
+                               'ar_user_text',
+                               'ar_timestamp',
+                               'ar_minor_edit',
+                               'ar_flags',
+                               'ar_text_id' ),
                        array( 'ar_namespace' => $this->title->getNamespace(),
                               'ar_title' => $this->title->getDbkey(),
                               'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
-                       $fname );
+                       __METHOD__ );
                if( $row ) {
-                       return $this->getTextFromRow( $row );
+                       return new Revision( array(
+                               'page'       => $this->title->getArticleId(),
+                               'id'         => $row->ar_rev_id,
+                               'text'       => ($row->ar_text_id ? null : $row->ar_text),
+                               'comment'    => $row->ar_comment,
+                               'user'       => $row->ar_user,
+                               'user_text'  => $row->ar_user_text,
+                               'timestamp'  => $row->ar_timestamp,
+                               'minor_edit' => $row->ar_minor_edit,
+                               'text_id'    => $row->ar_text_id ) );
                } else {
                        return null;
                }
@@ -127,8 +155,6 @@ class PageArchive {
         * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
         */
        function getTextFromRow( $row ) {
-               $fname = 'PageArchive::getTextFromRow';
-
                if( is_null( $row->ar_text_id ) ) {
                        // An old row from MediaWiki 1.4 or previous.
                        // Text is embedded in this row in classic compression format.
@@ -139,7 +165,7 @@ class PageArchive {
                        $text = $dbr->selectRow( 'text',
                                array( 'old_text', 'old_flags' ),
                                array( 'old_id' => $row->ar_text_id ),
-                               $fname );
+                               __METHOD__ );
                        return Revision::getRevisionText( $text );
                }
        }
@@ -252,7 +278,6 @@ class PageArchive {
        private function undeleteRevisions( $timestamps ) {
                global $wgParser, $wgDBtype;
 
-               $fname = __CLASS__ . '::' . __FUNCTION__;
                $restoreAll = empty( $timestamps );
                
                $dbw =& wfGetDB( DB_MASTER );
@@ -267,7 +292,7 @@ class PageArchive {
                        array( 'page_id', 'page_latest' ),
                        array( 'page_namespace' => $this->title->getNamespace(),
                               'page_title'     => $this->title->getDBkey() ),
-                       $fname,
+                       __METHOD__,
                        $options );
                if( $page ) {
                        # Page already exists. Import the history, and if necessary
@@ -311,12 +336,12 @@ class PageArchive {
                                'ar_namespace' => $this->title->getNamespace(),
                                'ar_title'     => $this->title->getDBkey(),
                                $oldones ),
-                       $fname,
+                       __METHOD__,
                        /* options */ array(
                                'ORDER BY' => 'ar_timestamp' )
                        );
                if( $dbw->numRows( $result ) < count( $timestamps ) ) {
-                       wfDebug( "$fname: couldn't find all requested rows\n" );
+                       wfDebug( __METHOD__.": couldn't find all requested rows\n" );
                        return false;
                }
                
@@ -355,17 +380,11 @@ class PageArchive {
                if( $revision ) {
                        # FIXME: Update latest if newer as well...
                        if( $newid ) {
-                               # FIXME: update article count if changed...
+                               // Attach the latest revision to the page...
                                $article->updateRevisionOn( $dbw, $revision, $previousRevId );
-
-                               # Finally, clean up the link tables
-                               $options = new ParserOptions;
-                               $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options,
-                                       true, true, $newRevId );
-                               $u = new LinksUpdate( $this->title, $parserOutput );
-                               $u->doUpdate();
-
-                               #TODO: SearchUpdate, etc.
+                               
+                               // Update site stats, link tables, etc
+                               $article->createUpdates( $revision );
                        }
 
                        if( $newid ) {
@@ -383,7 +402,7 @@ class PageArchive {
                                'ar_namespace' => $this->title->getNamespace(),
                                'ar_title' => $this->title->getDBkey(),
                                $oldones ),
-                       $fname );
+                       __METHOD__ );
 
                return $restored;
        }
@@ -401,9 +420,10 @@ class UndeleteForm {
 
        function UndeleteForm( &$request, $par = "" ) {
                global $wgUser;
-               $this->mAction = $request->getText( 'action' );
-               $this->mTarget = $request->getText( 'target' );
-               $this->mTimestamp = $request->getText( 'timestamp' );
+               $this->mAction = $request->getVal( 'action' );
+               $this->mTarget = $request->getVal( 'target' );
+               $time = $request->getVal( 'timestamp' );
+               $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
                $this->mFile = $request->getVal( 'file' );
                
                $posted = $request->wasPosted() &&
@@ -463,7 +483,6 @@ class UndeleteForm {
 
        /* private */ function showList() {
                global $wgLang, $wgContLang, $wgUser, $wgOut;
-               $fname = "UndeleteForm::showList";
 
                # List undeletable articles
                $result = PageArchive::listAllPages();
@@ -476,17 +495,13 @@ class UndeleteForm {
                $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
 
                $sk = $wgUser->getSkin();
-               $undelete =& Title::makeTitle( NS_SPECIAL, 'Undelete' );
+               $undelete =& SpecialPage::getTitleFor( 'Undelete' );
                $wgOut->addHTML( "<ul>\n" );
                while( $row = $result->fetchObject() ) {
-                       $n = ($row->ar_namespace ?
-                               ($wgContLang->getNsText( $row->ar_namespace ) . ":") : "").
-                               $row->ar_title;
-                       $link = $sk->makeKnownLinkObj( $undelete,
-                               htmlspecialchars( $n ), "target=" . urlencode( $n ) );
-                       $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
-                               $wgLang->formatNum( $row->count ) ) );
-                       $wgOut->addHTML( "<li>$link ($revisions)</li>\n" );
+                       $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
+                       $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
+                       $revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
+                       $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
                }
                $result->free();
                $wgOut->addHTML( "</ul>\n" );
@@ -496,30 +511,36 @@ class UndeleteForm {
 
        /* private */ function showRevision( $timestamp ) {
                global $wgLang, $wgUser, $wgOut;
-               $fname = "UndeleteForm::showRevision";
 
                if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
 
-               $archive =& new PageArchive( $this->mTargetObj );
-               $text = $archive->getRevisionText( $timestamp );
-
+               $archive = new PageArchive( $this->mTargetObj );
+               $rev = $archive->getRevision( $timestamp );
+               
                $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
                $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
                        $wgLang->date( $timestamp ) ) . ")\n" );
                
+               if( !$rev ) {
+                       $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
+                       return;
+               }
+               
+               wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
+               
                if( $this->mPreview ) {
                        $wgOut->addHtml( "<hr />\n" );
-                       $wgOut->addWikiText( $text );
+                       $wgOut->addWikiText( $rev->getText() );
                }
                
-               $self = Title::makeTitle( NS_SPECIAL, "Undelete" );
+               $self = SpecialPage::getTitleFor( "Undelete" );
                
                $wgOut->addHtml(
                        wfElement( 'textarea', array(
                                        'readonly' => true,
                                        'cols' => intval( $wgUser->getOption( 'cols' ) ),
                                        'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
-                               $text . "\n" ) .
+                               $rev->getText() . "\n" ) .
                        wfOpenElement( 'div' ) .
                        wfOpenElement( 'form', array(
                                'method' => 'post',
@@ -610,7 +631,7 @@ class UndeleteForm {
                }
 
                if ( $this->mAllowed ) {
-                       $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
+                       $titleObj = SpecialPage::getTitleFor( "Undelete" );
                        $action = $titleObj->getLocalURL( "action=submit" );
                        # Start the form here
                        $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
@@ -619,8 +640,7 @@ class UndeleteForm {
 
                # Show relevant lines from the deletion log:
                $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
-               require_once( 'SpecialLog.php' );
-               $logViewer =& new LogViewer(
+               $logViewer = new LogViewer(
                        new LogReader(
                                new FauxRequest(
                                        array( 'page' => $this->mTargetObj->getPrefixedText(),