Merge "Correctly use $wgFeedLimit in page history feed"
[lhc/web/wiklou.git] / includes / Article.php
index a0d4438..732b1c2 100644 (file)
@@ -855,11 +855,11 @@ class Article implements Page {
        /**
         * Get the robot policy to be used for the current view
         * @param string $action the action= GET parameter
-        * @param $pOutput ParserOutput
+        * @param $pOutput ParserOutput|null
         * @return Array the policy that should be set
         * TODO: actions other than 'view'
         */
-       public function getRobotPolicy( $action, $pOutput ) {
+       public function getRobotPolicy( $action, $pOutput = null ) {
                global $wgArticleRobotPolicies, $wgNamespaceRobotPolicies, $wgDefaultRobotPolicy;
 
                $ns = $this->getTitle()->getNamespace();
@@ -1037,11 +1037,10 @@ class Article implements Page {
                        $this->getContext()->getOutput()->addWikiMsg( 'anontalkpagetext' );
                }
 
-               # If we have been passed an &rcid= parameter, we want to give the user a
-               # chance to mark this new article as patrolled.
-               $this->showPatrolFooter();
+               // Show a footer allowing the user to patrol the shown revision or page if possible
+               $patrolFooterShown = $this->showPatrolFooter();
 
-               wfRunHooks( 'ArticleViewFooter', array( $this ) );
+               wfRunHooks( 'ArticleViewFooter', array( $this, $patrolFooterShown ) );
 
        }
 
@@ -1051,21 +1050,85 @@ class Article implements Page {
         * desired, does nothing.
         * Side effect: When the patrol link is build, this method will call
         * OutputPage::preventClickjacking() and load mediawiki.page.patrol.ajax.
+        *
+        * @return bool
         */
        public function showPatrolFooter() {
-               $request = $this->getContext()->getRequest();
+               global $wgUseNPPatrol, $wgUseRCPatrol, $wgEnableAPI, $wgEnableWriteAPI;
+
                $outputPage = $this->getContext()->getOutput();
                $user = $this->getContext()->getUser();
-               $rcid = $request->getVal( 'rcid' );
+               $cache = wfGetMainCache();
+               $rc = false;
 
-               if ( !$rcid || !$this->getTitle()->quickUserCan( 'patrol', $user ) ) {
-                       return;
+               if ( !$this->getTitle()->quickUserCan( 'patrol', $user ) || !( $wgUseRCPatrol || $wgUseNPPatrol ) ) {
+                       // Patrolling is disabled or the user isn't allowed to
+                       return false;
+               }
+
+               wfProfileIn( __METHOD__ );
+
+               // New page patrol: Get the timestamp of the oldest revison which
+               // the revision table holds for the given page. Then we look
+               // whether it's within the RC lifespan and if it is, we try
+               // to get the recentchanges row belonging to that entry
+               // (with rc_new = 1).
+
+               // Check for cached results
+               if ( $cache->get( wfMemcKey( 'NotPatrollablePage', $this->getTitle()->getArticleID() ) ) ) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
                }
 
+               if ( $this->mRevision && !RecentChange::isInRCLifespan( $this->mRevision->getTimestamp(), 21600 ) ) {
+                       // The current revision is already older than what could be in the RC table
+                       // 6h tolerance because the RC might not be cleaned out regularly
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
+               $dbr = wfGetDB( DB_SLAVE );
+               $oldestRevisionTimestamp = $dbr->selectField(
+                       'revision',
+                       'MIN( rev_timestamp )',
+                       array( 'rev_page' => $this->getTitle()->getArticleID() ),
+                       __METHOD__
+               );
+
+               if ( $oldestRevisionTimestamp && RecentChange::isInRCLifespan( $oldestRevisionTimestamp, 21600 ) ) {
+                       // 6h tolerance because the RC might not be cleaned out regularly
+                       $rc = RecentChange::newFromConds(
+                               array(
+                                       'rc_new' => 1,
+                                       'rc_timestamp' => $oldestRevisionTimestamp,
+                                       'rc_namespace' => $this->getTitle()->getNamespace(),
+                                       'rc_cur_id' => $this->getTitle()->getArticleID(),
+                                       'rc_patrolled' => 0
+                               ),
+                               __METHOD__,
+                               array( 'USE INDEX' => 'new_name_timestamp' )
+                       );
+               }
+
+               if ( !$rc ) {
+                       // No RC entry around
+
+                       // Cache the information we gathered above in case we can't patrol
+                       // Don't cache in case we can patrol as this could change
+                       $cache->set( wfMemcKey( 'NotPatrollablePage', $this->getTitle()->getArticleID() ), '1' );
+
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
+               $rcid = $rc->getAttribute( 'rc_id' );
+
                $token = $user->getEditToken( $rcid );
 
                $outputPage->preventClickjacking();
-               $outputPage->addModules( 'mediawiki.page.patrol.ajax' );
+               if ( $wgEnableAPI && $wgEnableWriteAPI && $user->isAllowed( 'writeapi' ) ) {
+                       $outputPage->addModules( 'mediawiki.page.patrol.ajax' );
+               }
 
                $link = Linker::linkKnown(
                        $this->getTitle(),
@@ -1083,6 +1146,9 @@ class Article implements Page {
                                wfMessage( 'markaspatrolledlink' )->rawParams( $link )->escaped() .
                        '</div>'
                );
+
+               wfProfileOut( __METHOD__ );
+               return true;
        }
 
        /**
@@ -1142,6 +1208,13 @@ class Article implements Page {
                        $this->getContext()->getRequest()->response()->header( "HTTP/1.1 404 Not Found" );
                }
 
+               if ( $validUserPage ) {
+                       // Also apply the robot policy for nonexisting user pages (as those aren't served as 404)
+                       $policy = $this->getRobotPolicy( 'view' );
+                       $outputPage->setIndexPolicy( $policy['index'] );
+                       $outputPage->setFollowPolicy( $policy['follow'] );
+               }
+
                $hookResult = wfRunHooks( 'BeforeDisplayNoArticleText', array( $this ) );
 
                if ( ! $hookResult ) {
@@ -1473,13 +1546,7 @@ class Article implements Page {
 
                        $this->doDelete( $reason, $suppress );
 
-                       if ( $user->isLoggedIn() && $request->getCheck( 'wpWatch' ) != $user->isWatched( $title ) ) {
-                               if ( $request->getCheck( 'wpWatch' ) ) {
-                                       WatchAction::doWatch( $title, $user );
-                               } else {
-                                       WatchAction::doUnwatch( $title, $user );
-                               }
-                       }
+                       WatchAction::doWatchOrUnwatch( $request->getCheck( 'wpWatch' ), $title, $user );
 
                        return;
                }