Let extensions add entries to Special:DeletedContributions
authorMatthias Mullie <git@mullie.eu>
Mon, 23 Mar 2015 13:47:58 +0000 (14:47 +0100)
committerMatthias Mullie <git@mullie.eu>
Wed, 25 Mar 2015 12:42:28 +0000 (13:42 +0100)
Bug: T90973
Change-Id: I0cb5340311262084a3120fdb0314fd26193c07a3

HISTORY
docs/hooks.txt
includes/specials/SpecialDeletedContributions.php

diff --git a/HISTORY b/HISTORY
index e5864fd..6ea5c2e 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -190,6 +190,10 @@ Change notes from older releases. For current info see RELEASE-NOTES-1.25.
   to multiple Config instances.
 * Update CSSJanus to v1.1.0.
 * Added FormatJson::parse() returning status with result or localized error message
+* Added DeletedContribsPager::reallyDoQuery hook allowing extensions to data to
+  Special:DeletedContributions
+* Added DeletedContributionsLineEnding hook allowing extensions to format
+  Special:DeletedContributions lines
 
 === Bug fixes in 1.24 ===
 * (bug 50572) MediaWiki:Blockip should support gender
index 20f5de8..f2c47ca 100644 (file)
@@ -1061,6 +1061,21 @@ etc.
 'DatabaseOraclePostInit': Called after initialising an Oracle database
 &$db: the DatabaseOracle object
 
+'DeletedContribsPager::reallyDoQuery': Called before really executing the query for Special:DeletedContributions
+Similar to ContribsPager::reallyDoQuery
+&$data: an array of results of all contribs queries
+$pager: The DeletedContribsPager object hooked into
+$offset: Index offset, inclusive
+$limit: Exact query limit
+$descending: Query direction, false for ascending, true for descending
+
+'DeletedContributionsLineEnding': Called before a DeletedContributions HTML line is finished.
+Similar to ContributionsLineEnding
+$page: SpecialPage object for DeletedContributions
+&$ret: the HTML line
+$row: the DB row for this line
+&$classes: the classes to add to the surrounding <li>
+
 'NewDifferenceEngine': Called when a new DifferenceEngine object is made
 $title: the diff page title (nullable)
 &$oldId: the actual old Id to use in the diff
index 680aa35..9e4bbbe 100644 (file)
@@ -78,6 +78,53 @@ class DeletedContribsPager extends IndexPager {
                );
        }
 
+       /**
+        * This method basically executes the exact same code as the parent class, though with
+        * a hook added, to allow extensions to add additional queries.
+        *
+        * @param string $offset Index offset, inclusive
+        * @param int $limit Exact query limit
+        * @param bool $descending Query direction, false for ascending, true for descending
+        * @return ResultWrapper
+        */
+       function reallyDoQuery( $offset, $limit, $descending ) {
+               $pager = $this;
+
+               $data = array( parent::reallyDoQuery( $offset, $limit, $descending ) );
+
+               // This hook will allow extensions to add in additional queries, nearly
+               // identical to ContribsPager::reallyDoQuery.
+               Hooks::run(
+                       'DeletedContribsPager::reallyDoQuery',
+                       array( &$data, $pager, $offset, $limit, $descending )
+               );
+
+               $result = array();
+
+               // loop all results and collect them in an array
+               foreach ( $data as $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 getUserCond() {
                $condition = array();
 
@@ -141,6 +188,50 @@ class DeletedContribsPager extends IndexPager {
        /**
         * Generates each row in the contributions list.
         *
+        * @todo This would probably look a lot nicer in a table.
+        * @param stdClass $row
+        * @return string
+        */
+       function formatRow( $row ) {
+               $ret = '';
+               $classes = array();
+
+               /*
+                * 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();
+               try {
+                       $rev = Revision::newFromArchiveRow( $row );
+                       $validRevision = (bool)$rev->getId();
+               } catch ( Exception $e ) {
+                       $validRevision = false;
+               }
+               wfRestoreWarnings();
+
+               if ( $validRevision ) {
+                       $ret = $this->formatRevisionRow( $row );
+               }
+
+               // Let extensions add data
+               Hooks::run( 'DeletedContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
+
+               if ( $classes === array() && $ret === '' ) {
+                       wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
+                       $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
+               } else {
+                       $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
+               }
+
+               return $ret;
+       }
+
+       /**
+        * Generates each row in the contributions list for archive entries.
+        *
         * Contributions which are marked "top" are currently on top of the history.
         * For these contributions, a [rollback] link is shown for users with sysop
         * privileges. The rollback link restores the most recent version that was not
@@ -150,8 +241,7 @@ class DeletedContribsPager extends IndexPager {
         * @param stdClass $row
         * @return string
         */
-       function formatRow( $row ) {
-
+       function formatRevisionRow( $row ) {
                $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
 
                $rev = new Revision( array(
@@ -255,8 +345,6 @@ class DeletedContribsPager extends IndexPager {
                        $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
                }
 
-               $ret = Html::rawElement( 'li', array(), $ret ) . "\n";
-
                return $ret;
        }