C. Scott Ananian has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/314178

Change subject: Pass request source and user information to backend.
......................................................................

Pass request source and user information to backend.

This allows the backend to do rate-limiting, blacklisting, etc, based
on the user agent, wiki username, ip address, or article/collection title.

Bug: T97030
Bug: T147211
Change-Id: I5faae8e6caa77287ac4cfd6b249fd591837e6bd2
---
M Collection.body.php
M RenderingAPI.php
2 files changed, 56 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection 
refs/changes/78/314178/1

diff --git a/Collection.body.php b/Collection.body.php
index c41b0f2..d5fd051 100644
--- a/Collection.body.php
+++ b/Collection.body.php
@@ -498,6 +498,7 @@
                $collection = CollectionSession::getCollection();
                $collection['title'] = $title;
                $collection['subtitle'] = $subtitle;
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
        }
 
@@ -510,6 +511,7 @@
                        $collection['settings'] = array();
                }
                $collection['settings'] = $settings + $collection['settings'];
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
        }
 
@@ -542,6 +544,7 @@
                }
                usort( $articles, array( __CLASS__, 'title_cmp' ) );
                $collection['items'] = array_merge( $new_items, $articles );
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
        }
 
@@ -557,6 +560,7 @@
                        'type' => 'chapter',
                        'title' => $name,
                ) );
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
        }
 
@@ -573,6 +577,7 @@
                        return;
                }
                $collection['items'][$index]['title'] = $name;
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
        }
 
@@ -633,6 +638,7 @@
                );
 
                $collection['items'][] = $item;
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
                return true;
        }
@@ -662,6 +668,7 @@
                if ( $index != - 1 ) {
                        array_splice( $collection['items'], $index, 1 );
                }
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
                return true;
        }
@@ -738,6 +745,7 @@
                }
                $collection = CollectionSession::getCollection();
                array_splice( $collection['items'], $index, 1 );
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
                return true;
        }
@@ -755,6 +763,7 @@
                $saved = $collection['items'][$index + $delta];
                $collection['items'][$index + $delta] = 
$collection['items'][$index];
                $collection['items'][$index] = $saved;
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
                return true;
        }
@@ -773,6 +782,7 @@
                        $new_items[$new_index] = isset( $old_items[$old_index] 
) ? $old_items[$old_index] : null;
                }
                $collection['items'] = $new_items;
+               unset( $collection['source'] );
                CollectionSession::setCollection( $collection );
        }
 
@@ -905,6 +915,8 @@
                        }
                }
                $collection['items'] = $items;
+               // Mark the source of this multi-page collection.
+               $collection['source'] = $title->getCanonicalURL();
                return $collection;
        }
 
@@ -919,6 +931,7 @@
                        return false;
                }
                $collection = CollectionSession::getCollection();
+               $collection['source'] = $title->getCanonicalURL();
                $articleText = "{{" . $this->msg( 'coll-savedbook_template' 
)->inContentLanguage()->text();
                if ( !empty( $collection['settings'] ) ) {
                        $articleText .= "\n";
@@ -979,6 +992,8 @@
                );
                $api = new ApiMain( $req, true );
                $api->execute();
+               // Store new 'source' for this collection.
+               CollectionSession::setCollection( $collection );
                return true;
        }
 
@@ -997,7 +1012,8 @@
                }
 
                $api = CollectionRenderingAPI::instance( $writer );
-               $response = $api->render( $collection );
+               $source = isset( $collection['source'] ) ? 
$collection['source'] : null;
+               $response = $api->render( $collection, 
$this->collectRequestInfo( $source ) );
 
                if ( !$this->handleResult( $response ) ) {
                        return;
@@ -1021,7 +1037,7 @@
                $writer = $request->getVal( 'writer', 'rl' );
 
                $api = CollectionRenderingAPI::instance( $writer );
-               $response = $api->forceRender( $collectionID );
+               $response = $api->forceRender( $collectionID, 
$this->collectRequestInfo( null ) );
 
                if ( !$response ) {
                        return;
@@ -1206,6 +1222,29 @@
        }
 
        /**
+        * Returns an array containing various bits of information we'd like
+        * to log when a render request is initiated, in case the backend
+        * needs to throttle or reject the job.  The source title is also
+        * included here, so that failing jobs can be traced back to the
+        * collection or article URL they come from.
+        * @param $source String|null the original collection or article title,
+        *  if known.
+        */
+       public function collectRequestInfo( $source ) {
+               return array(
+                       // Encode as URL of user talk page, so that it includes
+                       // the site information as well.
+                       'user' => 
$this->getUser()->getUserPage()->getCanonicalURL(),
+                       // The backend may wish to IP-block.
+                       'ip' => $this->getRequest()->getIP(),
+                       // URL of the original article or collection for 
debugging.
+                       'source' => $source,
+                       // User-Agent string.
+                       'userAgent' => $this->getRequest()->getHeader( 
'User-agent' ),
+               );
+       }
+
+       /**
         * Render a single page: fetch page name and revision information, then
         * assemble and feed to renderCollection() a single-item $collection.
         * @param $title Title needs to be full page name aka prefixed title.
@@ -1231,7 +1270,11 @@
                if ( $revision ) {
                        $article['timestamp'] = wfTimestamp( TS_UNIX, 
$revision->getTimestamp() );
                }
-               return array( 'items' => array( $article ) );
+               return array(
+                       'items' => array( $article ),
+                       // Mark the source of this single-page render.
+                       'source' => $title->getCanonicalURL(),
+               );
        }
 
        /**
@@ -1275,7 +1318,8 @@
                }
 
                $api = CollectionRenderingAPI::instance();
-               $result = $api->postZip( $collection, 
$this->mPODPartners[$partner]['posturl'] );
+               $source = isset( $collection['source'] ) ? 
$collection['source'] : null;
+               $result = $api->postZip( $collection, 
$this->collectRequestInfo( $source ), $this->mPODPartners[$partner]['posturl'] 
);
                if ( !$this->handleResult( $result ) ) {
                        return;
                }
diff --git a/RenderingAPI.php b/RenderingAPI.php
index fc427ac..85f2816 100644
--- a/RenderingAPI.php
+++ b/RenderingAPI.php
@@ -49,11 +49,10 @@
         *
         * @return CollectionAPIResult
         */
-       public function render( $collection ) {
+       public function render( $collection, array $source ) {
                return $this->doRender( array(
                                'metabook' => $this->buildJSONCollection( 
$collection ),
-                       )
-               );
+               ), $source );
        }
 
        /**
@@ -62,20 +61,20 @@
         * @param $collectionId
         * @return CollectionAPIResult
         */
-       public function forceRender( $collectionId ) {
+       public function forceRender( $collectionId, array $source ) {
                return $this->doRender( array(
                                'collection_id' => $collectionId,
                                'force_render' => true
-                       )
-               );
+               ), $source );
        }
 
-       protected function doRender( array $params ) {
+       protected function doRender( array $params, array $source ) {
                global $wgContLang;
 
                $params['base_url'] = $this->getBaseURL();
                $params['script_extension'] = '.php';
                $params['language'] = $wgContLang->getCode();
+               $params['source'] = FormatJson::encode( $source );
                return $this->makeRequest( 'render', $params );
        }
 
@@ -88,13 +87,14 @@
         *
         * @return CollectionAPIResult
         */
-       public function postZip( $collection, $url ) {
+       public function postZip( $collection, array $source, $url ) {
                return $this->makeRequest( 'zip_post',
                        array(
                                'metabook' => $this->buildJSONCollection( 
$collection ),
                                'base_url' => $this->getBaseURL(),
                                'script_extension' => '.php',
                                'pod_api_url' => $url,
+                               'source' => FormatJson::encode( $source ),
                        )
                );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/314178
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5faae8e6caa77287ac4cfd6b249fd591837e6bd2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Collection
Gerrit-Branch: master
Gerrit-Owner: C. Scott Ananian <canan...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to