- private static $annoyingQueryParams = array(
- // Facebook
- 'action_object_map=',
- 'action_ref_map=',
- 'action_type_map=',
- 'fb_',
- 'fb=',
-
- // Scoop.it
- '__scoop',
-
- // Google Analytics & FeedProxy
- 'utm_',
-
- // ATInternet
- 'xtor='
- );
-
- private static $annoyingFragments = array(
- // ATInternet
- 'xtor=RSS-',
-
- // Misc.
- 'tk.rss_all'
- );
-
- /*
- * URL parts represented as an array
- *
- * @see http://php.net/parse_url
- */
- protected $parts;
-
- /**
- * Parses a string containing a URL
- *
- * @param string $url a string containing a URL
- */
- public function __construct($url)
- {
- $this->parts = parse_url($url);
-
- if (!empty($url) && empty($this->parts['scheme'])) {
- $this->parts['scheme'] = 'http';
- }
- }
-
- /**
- * Returns a string representation of this URL
- */
- public function toString()
- {
- return unparse_url($this->parts);
- }
-
- /**
- * Removes undesired query parameters
- */
- protected function cleanupQuery()
- {
- if (! isset($this->parts['query'])) {
- return;
- }
-
- $queryParams = explode('&', $this->parts['query']);
-
- foreach (self::$annoyingQueryParams as $annoying) {
- foreach ($queryParams as $param) {
- if (startsWith($param, $annoying)) {
- $queryParams = array_diff($queryParams, array($param));
- continue;
- }
- }
- }
-
- if (count($queryParams) == 0) {
- unset($this->parts['query']);
- return;
- }
-
- $this->parts['query'] = implode('&', $queryParams);
- }
-
- /**
- * Removes undesired fragments
- */
- protected function cleanupFragment()
- {
- if (! isset($this->parts['fragment'])) {
- return;
- }
-
- foreach (self::$annoyingFragments as $annoying) {
- if (startsWith($this->parts['fragment'], $annoying)) {
- unset($this->parts['fragment']);
- break;
- }
- }
- }