Merge changes Idfc2e2b6,I9b30d602
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
index 49aa7e6..689e096 100644 (file)
@@ -399,7 +399,7 @@ class SpecialContributions extends SpecialPage {
                        $this->opts['topOnly'] = false;
                }
 
-               $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
+               $form = Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
 
                # Add hidden params for tracking except for parameters in $skipParameters
                $skipParameters = array( 'namespace', 'nsInvert', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly', 'associated' );
@@ -414,13 +414,13 @@ class SpecialContributions extends SpecialPage {
 
                if ( $tagFilter ) {
                        $filterSelection =
-                               Xml::tags( 'td', array( 'class' => 'mw-label' ), array_shift( $tagFilter ) ) .
-                               Xml::tags( 'td', array( 'class' => 'mw-input' ), implode( '&#160', $tagFilter ) );
+                               Html::rawElement( 'td', array( 'class' => 'mw-label' ), array_shift( $tagFilter ) ) .
+                               Html::rawElement( 'td', array( 'class' => 'mw-input' ), implode( '&#160', $tagFilter ) );
                } else {
-                       $filterSelection = Xml::tags( 'td', array( 'colspan' => 2 ), '' );
+                       $filterSelection = Html::rawElement( 'td', array( 'colspan' => 2 ), '' );
                }
 
-               $targetSelection = Xml::tags( 'td', array( 'colspan' => 2 ),
+               $targetSelection = Html::rawElement( 'td', array( 'colspan' => 2 ),
                        Xml::radioLabel(
                                $this->msg( 'sp-contributions-newbies' )->text(),
                                'contribs',
@@ -455,7 +455,7 @@ class SpecialContributions extends SpecialPage {
                                        ''
                                )
                        ) .
-                       Xml::tags( 'td', null,
+                       Html::rawElement( 'td', null,
                                Html::namespaceSelector( array(
                                        'selected' => $this->opts['namespace'],
                                        'all'      => '',
@@ -485,8 +485,8 @@ class SpecialContributions extends SpecialPage {
                                )
                        ) ;
 
-               $extraOptions = Xml::tags( 'td', array( 'colspan' => 2 ),
-                       Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
+               if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
+                       $deletedOnlyCheck = Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
                                Xml::checkLabel(
                                        $this->msg( 'history-show-deleted' )->text(),
                                        'deletedOnly',
@@ -494,7 +494,13 @@ class SpecialContributions extends SpecialPage {
                                        $this->opts['deletedOnly'],
                                        array( 'class' => 'mw-input' )
                                )
-                       ) .
+                       );
+               } else {
+                       $deletedOnlyCheck = '';
+               }
+
+               $extraOptions = Html::rawElement( 'td', array( 'colspan' => 2 ),
+                       $deletedOnlyCheck .
                        Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
                                Xml::checkLabel(
                                        $this->msg( 'sp-contributions-toponly' )->text(),
@@ -504,7 +510,7 @@ class SpecialContributions extends SpecialPage {
                                        array( 'class' => 'mw-input' )
                                )
                        )
-               ) ;
+               );
 
                $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
                        Xml::dateMenu(
@@ -519,23 +525,13 @@ class SpecialContributions extends SpecialPage {
 
                $form .=
                        Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() ) .
-                       Xml::openElement( 'table', array( 'class' => 'mw-contributions-table' ) ) .
-                               Xml::openElement( 'tr' ) .
-                                       $targetSelection .
-                               Xml::closeElement( 'tr' ) .
-                               Xml::openElement( 'tr' ) .
-                                       $namespaceSelection .
-                               Xml::closeElement( 'tr' ) .
-                               Xml::openElement( 'tr' ) .
-                                       $filterSelection .
-                               Xml::closeElement( 'tr' ) .
-                               Xml::openElement( 'tr' ) .
-                                       $extraOptions .
-                               Xml::closeElement( 'tr' ) .
-                               Xml::openElement( 'tr' ) .
-                                       $dateSelectionAndSubmit .
-                               Xml::closeElement( 'tr' ) .
-                       Xml::closeElement( 'table' );
+                       Html::rawElement( 'table', array( 'class' => 'mw-contributions-table' ), "\n" .
+                               Html::rawElement( 'tr', array(), $targetSelection ) . "\n" .
+                               Html::rawElement( 'tr', array(), $namespaceSelection ) . "\n" .
+                               Html::rawElement( 'tr', array(), $filterSelection ) . "\n" .
+                               Html::rawElement( 'tr', array(), $extraOptions ) . "\n" .
+                               Html::rawElement( 'tr', array(), $dateSelectionAndSubmit ) . "\n"
+                       );
 
                $explain = $this->msg( 'sp-contributions-explain' );
                if ( $explain->exists() ) {
@@ -594,6 +590,66 @@ class ContribsPager extends ReverseChronologicalPager {
                return $query;
        }
 
+       /**
+        * This method basically executes the exact same code as the parent class, though with
+        * a hook added, to allow extentions to add additional queries.
+        *
+        * @param $offset String: index offset, inclusive
+        * @param $limit Integer: exact query limit
+        * @param $descending Boolean: query direction, false for ascending, true for descending
+        * @return ResultWrapper
+        */
+       function reallyDoQuery( $offset, $limit, $descending ) {
+               list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $descending );
+               $pager = $this;
+
+               /*
+                * This hook will allow extensions to add in additional queries, so they can get their data
+                * in My Contributions as well. Extensions should append their results to the $data array.
+                *
+                * Extension queries have to implement the navbar requirement as well. They should
+                * - have a column aliased as $pager->getIndexField()
+                * - have LIMIT set
+                * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
+                * - have the ORDER BY specified based upon the details provided by the navbar
+                *
+                * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
+                *
+                * &$data: an array of results of all contribs queries
+                * $pager: the ContribsPager object hooked into
+                * $offset: see phpdoc above
+                * $limit: see phpdoc above
+                * $descending: see phpdoc above
+                */
+               $data = array( $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds ) );
+               wfRunHooks( 'ContribsPager::reallyDoQuery', array( &$data, $pager, $offset, $limit, $descending ) );
+
+               $result = array();
+
+               // loop all results and collect them in an array
+               foreach ( $data as $j => $query ) {
+                       foreach ( $query as $i => $row ) {
+                               // use index column as key, allowing us to easily sort in PHP
+                               $result[$row->{$this->getIndexField()} . "-$i"] = $row;
+                       }
+               }
+
+               // sort results
+               if ( $descending ) {
+                       ksort( $result );
+               } else {
+                       krsort( $result );
+               }
+
+               // enforce limit
+               $result = array_slice( $result, 0, $limit );
+
+               // get rid of array keys
+               $result = array_values( $result );
+
+               return new FakeResultWrapper( $result );
+       }
+
        function getQueryInfo() {
                list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
 
@@ -644,13 +700,22 @@ class ContribsPager extends ReverseChronologicalPager {
                $join_conds = array();
                $tables = array( 'revision', 'page', 'user' );
                if ( $this->contribs == 'newbie' ) {
-                       $tables[] = 'user_groups';
                        $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
                        $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
-                       $condition[] = 'ug_group IS NULL';
                        $index = 'user_timestamp';
-                       # @todo FIXME: Other groups may have 'bot' rights
-                       $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
+                       # ignore local groups with the bot right
+                       # @todo FIXME: Global groups may have 'bot' rights
+                       $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
+                       if( count( $groupsWithBotPermission ) ) {
+                               $tables[] = 'user_groups';
+                               $condition[] = 'ug_group IS NULL';
+                               $join_conds['user_groups'] = array(
+                                       'LEFT JOIN', array(
+                                               'ug_user = rev_user',
+                                               'ug_group' => $groupsWithBotPermission
+                                       )
+                               );
+                       }
                } else {
                        $uid = User::idFromName( $this->target );
                        if ( $uid ) {
@@ -699,53 +764,28 @@ class ContribsPager extends ReverseChronologicalPager {
        }
 
        function doBatchLookups() {
-               $this->mResult->rewind();
-               $revIds = array();
-               foreach ( $this->mResult as $row ) {
-                       if( $row->rev_parent_id ) {
-                               $revIds[] = $row->rev_parent_id;
-                       }
-               }
-               $this->mParentLens = $this->getParentLengths( $revIds );
-               $this->mResult->rewind(); // reset
-
                # Do a link batch query
                $this->mResult->seek( 0 );
+               $revIds = array();
                $batch = new LinkBatch();
                # Give some pointers to make (last) links
                foreach ( $this->mResult as $row ) {
-                       if ( $this->contribs === 'newbie' ) { // multiple users
-                               $batch->add( NS_USER, $row->user_name );
-                               $batch->add( NS_USER_TALK, $row->user_name );
+                       if( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
+                               $revIds[] = $row->rev_parent_id;
+                       }
+                       if ( isset( $row->rev_id ) ) {
+                               if ( $this->contribs === 'newbie' ) { // multiple users
+                                       $batch->add( NS_USER, $row->user_name );
+                                       $batch->add( NS_USER_TALK, $row->user_name );
+                               }
+                               $batch->add( $row->page_namespace, $row->page_title );
                        }
-                       $batch->add( $row->page_namespace, $row->page_title );
                }
+               $this->mParentLens = Revision::getParentLengths( $this->getDatabase(), $revIds );
                $batch->execute();
                $this->mResult->seek( 0 );
        }
 
-       /**
-        * Do a batched query to get the parent revision lengths
-        * @param $revIds array
-        * @return array
-        */
-       private function getParentLengths( array $revIds ) {
-               $revLens = array();
-               if ( !$revIds ) {
-                       return $revLens; // empty
-               }
-               wfProfileIn( __METHOD__ );
-               $res = $this->getDatabase()->select( 'revision',
-                       array( 'rev_id', 'rev_len' ),
-                       array( 'rev_id' => $revIds ),
-                       __METHOD__ );
-               foreach ( $res as $row ) {
-                       $revLens[$row->rev_id] = $row->rev_len;
-               }
-               wfProfileOut( __METHOD__ );
-               return $revLens;
-       }
-
        /**
         * @return string
         */
@@ -775,136 +815,144 @@ class ContribsPager extends ReverseChronologicalPager {
        function formatRow( $row ) {
                wfProfileIn( __METHOD__ );
 
-               $rev = new Revision( $row );
+               $ret = '';
                $classes = array();
 
-               $page = Title::newFromRow( $row );
-               $link = Linker::link(
-                       $page,
-                       htmlspecialchars( $page->getPrefixedText() ),
-                       array(),
-                       $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
-               );
-               # Mark current revisions
-               $topmarktext = '';
-               if ( $row->rev_id == $row->page_latest ) {
-                       $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
-                       # Add rollback link
-                       if ( !$row->page_is_new && $page->quickUserCan( 'rollback' )
-                               && $page->quickUserCan( 'edit' ) )
-                       {
-                               $this->preventClickjacking();
-                               $topmarktext .= ' ' . Linker::generateRollback( $rev );
+               /*
+                * There may be more than just revision rows. To make sure that we'll only be processing
+                * revisions here, let's _try_ to build a revision out of our row (without displaying
+                * notices though) and then trying to grab data from the built object. If we succeed,
+                * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
+                * to extensions to subscribe to the hook to parse the row.
+                */
+               wfSuppressWarnings();
+               $rev = new Revision( $row );
+               $validRevision = $rev->getParentId() !== null;
+               wfRestoreWarnings();
+
+               if ( $validRevision ) {
+                       $classes = array();
+
+                       $page = Title::newFromRow( $row );
+                       $link = Linker::link(
+                               $page,
+                               htmlspecialchars( $page->getPrefixedText() ),
+                               array( 'class' => 'mw-contributions-title' ),
+                               $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
+                       );
+                       # Mark current revisions
+                       $topmarktext = '';
+                       $user = $this->getUser();
+                       if ( $row->rev_id == $row->page_latest ) {
+                               $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
+                               # Add rollback link
+                               if ( !$row->page_is_new && $page->quickUserCan( 'rollback', $user )
+                                       && $page->quickUserCan( 'edit', $user ) )
+                               {
+                                       $this->preventClickjacking();
+                                       $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
+                               }
                        }
-               }
-               $user = $this->getUser();
-               # Is there a visible previous revision?
-               if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
-                       $difftext = Linker::linkKnown(
+                       # Is there a visible previous revision?
+                       if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
+                               $difftext = Linker::linkKnown(
+                                       $page,
+                                       $this->messages['diff'],
+                                       array(),
+                                       array(
+                                               'diff' => 'prev',
+                                               'oldid' => $row->rev_id
+                                       )
+                               );
+                       } else {
+                               $difftext = $this->messages['diff'];
+                       }
+                       $histlink = Linker::linkKnown(
                                $page,
-                               $this->messages['diff'],
+                               $this->messages['hist'],
                                array(),
-                               array(
-                                       'diff' => 'prev',
-                                       'oldid' => $row->rev_id
-                               )
+                               array( 'action' => 'history' )
                        );
-               } else {
-                       $difftext = $this->messages['diff'];
-               }
-               $histlink = Linker::linkKnown(
-                       $page,
-                       $this->messages['hist'],
-                       array(),
-                       array( 'action' => 'history' )
-               );
 
-               if ( $row->rev_parent_id === null ) {
-                       // For some reason rev_parent_id isn't populated for this row.
-                       // Its rumoured this is true on wikipedia for some revisions (bug 34922).
-                       // Next best thing is to have the total number of bytes.
-                       $chardiff = ' . . ' . Linker::formatRevisionSize( $row->rev_len ) . ' . . ';
-               } else {
-                       $parentLen = isset( $this->mParentLens[$row->rev_parent_id] ) ? $this->mParentLens[$row->rev_parent_id] : 0;
-                       $chardiff = ' . . ' . ChangesList::showCharacterDifference(
-                                       $parentLen, $row->rev_len ) . ' . . ';
-               }
+                       if ( $row->rev_parent_id === null ) {
+                               // For some reason rev_parent_id isn't populated for this row.
+                               // Its rumoured this is true on wikipedia for some revisions (bug 34922).
+                               // Next best thing is to have the total number of bytes.
+                               $chardiff = ' <span class="mw-changeslist-separator">. .</span> ' . Linker::formatRevisionSize( $row->rev_len ) . ' <span class="mw-changeslist-separator">. .</span> ';
+                       } else {
+                               $parentLen = isset( $this->mParentLens[$row->rev_parent_id] ) ? $this->mParentLens[$row->rev_parent_id] : 0;
+                               $chardiff = ' <span class="mw-changeslist-separator">. .</span> ' . ChangesList::showCharacterDifference(
+                                               $parentLen, $row->rev_len, $this->getContext() ) . ' <span class="mw-changeslist-separator">. .</span> ';
+                       }
 
-               $lang = $this->getLanguage();
-               $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
-               $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
-               if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
-                       $d = Linker::linkKnown(
-                               $page,
-                               htmlspecialchars( $date ),
-                               array(),
-                               array( 'oldid' => intval( $row->rev_id ) )
-                       );
-               } else {
-                       $d = htmlspecialchars( $date );
-               }
-               if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
-                       $d = '<span class="history-deleted">' . $d . '</span>';
-               }
+                       $lang = $this->getLanguage();
+                       $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
+                       $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
+                       if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
+                               $d = Linker::linkKnown(
+                                       $page,
+                                       htmlspecialchars( $date ),
+                                       array( 'class' => 'mw-changeslist-date' ),
+                                       array( 'oldid' => intval( $row->rev_id ) )
+                               );
+                       } else {
+                               $d = htmlspecialchars( $date );
+                       }
+                       if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
+                               $d = '<span class="history-deleted">' . $d . '</span>';
+                       }
 
-               # Show user names for /newbies as there may be different users.
-               # Note that we already excluded rows with hidden user names.
-               if ( $this->contribs == 'newbie' ) {
-                       $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
-                       $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
-                               Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
-               } else {
-                       $userlink = '';
-               }
+                       # Show user names for /newbies as there may be different users.
+                       # Note that we already excluded rows with hidden user names.
+                       if ( $this->contribs == 'newbie' ) {
+                               $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
+                               $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
+                                       Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
+                       } else {
+                               $userlink = '';
+                       }
 
-               if ( $rev->getParentId() === 0 ) {
-                       $nflag = ChangesList::flag( 'newpage' );
-               } else {
-                       $nflag = '';
-               }
+                       if ( $rev->getParentId() === 0 ) {
+                               $nflag = ChangesList::flag( 'newpage' );
+                       } else {
+                               $nflag = '';
+                       }
 
-               if ( $rev->isMinor() ) {
-                       $mflag = ChangesList::flag( 'minor' );
-               } else {
-                       $mflag = '';
-               }
+                       if ( $rev->isMinor() ) {
+                               $mflag = ChangesList::flag( 'minor' );
+                       } else {
+                               $mflag = '';
+                       }
 
-               $del = Linker::getRevDeleteLink( $user, $rev, $page );
-               if ( $del !== '' ) {
-                       $del .= ' ';
-               }
+                       $del = Linker::getRevDeleteLink( $user, $rev, $page );
+                       if ( $del !== '' ) {
+                               $del .= ' ';
+                       }
 
-               $diffHistLinks = $this->msg( 'parentheses' )->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )->escaped();
-               $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
+                       $diffHistLinks = $this->msg( 'parentheses' )->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )->escaped();
+                       $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
 
-               # Denote if username is redacted for this edit
-               if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
-                       $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
-               }
+                       # Denote if username is redacted for this edit
+                       if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
+                               $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
+                       }
 
-               # Tags, if any.
-               list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
-               $classes = array_merge( $classes, $newClasses );
-               $ret .= " $tagSummary";
+                       # Tags, if any.
+                       list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
+                       $classes = array_merge( $classes, $newClasses );
+                       $ret .= " $tagSummary";
+               }
 
                // Let extensions add data
-               wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
+               wfRunHooks( 'ContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
 
                $classes = implode( ' ', $classes );
                $ret = "<li class=\"$classes\">$ret</li>\n";
+
                wfProfileOut( __METHOD__ );
                return $ret;
        }
 
-       /**
-        * Get the Database object in use
-        *
-        * @return DatabaseBase
-        */
-       public function getDatabase() {
-               return $this->mDb;
-       }
-
        /**
         * Overwrite Pager function and return a helpful comment
         * @return string