~/drupal/27-list-all-node-ids-which-do-not-have-images-my-domain.md

List all the Node ids which do not have images from my domain

2 min readSeptember 04, 2017by Gorav Singal#drupal#drupal-nodes#image-optimization
// TL;DR

Use a database query to scan node body fields for img tags with external URLs, identifying nodes that need image migration to your local domain.

List all the Node ids which do not have images from my domain

Problem Statement

I use drupal-7 in my website. I used to write articles and put images in that from google or from free image sources. Initially, I usually put image urls from their website only. i.e. I have img tag on my website, and source of image as external url of image from their website.

Example

``` My website: https://www.gyanbyte.com

Image tags (example image tag):


Now, manytimes, external sources removed their images, and my website/page left with no image, which is not a good impression for my users. This article is to find those articles/blogs which have this issue.

<h2>Code</h2>
```php
/**
 * See if a drupal node has images within my domain or not
 **/
function requireModification($nid) {
  $nd = node_load($nid);

  //read body of node
  $body = $nd->body['und'][0]['value'];

  $doc = new DOMDocument();
  $doc->loadHTML($body);

  //fetch all images in body of node
  $tags = $doc->getElementsByTagName('img');
  $imgArr = array();
  foreach ($tags as $tag) {
      //read src tag, source of image
      $imgSrc = $tag->getAttribute('src');

      //checking if image path is an http path and not in my domain
      if ((strpos($imgSrc, 'http') === 0) && (strpos($imgSrc, 'https://s3.amazonaws.com/images.MYDOMAIN.com') !== 0)) {
            return true;
      }
  }
  return false;
}

//My node type name
$node_type = "article";
$result = db_query("SELECT nid FROM node WHERE type = :nodeType ", array(':nodeType'=>$node_type));

$nids = array();
foreach ($result as $obj) {
  if (requireModification($obj->nid) === true) {
    $nids[] = $obj->nid;
  }
}

//printing node ids, which have images apart from my domain
print_r($nids);

Sample Output

``` ( [0] => 16 [1] => 26 [2] => 2235 [3] => 2236 [4] => 2349 [5] => 2350 [6] => 2351 [7] => 2352 [8] => 2353 [9] => 2354 [10] => 2355 [11] => 2356 [12] => 2357 [13] => 2358 [14] => 2359 [15] => 2360 [16] => 2361 [17] => 2362 [18] => 2363 [19] => 2364 [20] => 2365 ) ```

Drupal version

Drupal 7

See related articles

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Drupal&#58; How to block a user by email programatically

Drupal&#58; How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal 7&#58; How to save a node programmatically and add an image field from a public URL

Drupal 7&#58; How to save a node programmatically and add an image field from a public URL

Note: I have public URLs of these images, which I want to save. return…

How to add alt attribute of images in all of my drupal articles or other content type

How to add alt attribute of images in all of my drupal articles or other content type

I have a custom content type, and there are around 2000 number of nodes in my…

How to trim Meta Description tag value generated by Metatag module, to max 160 characters

How to trim Meta Description tag value generated by Metatag module, to max 160 characters

I was using On page optimization of the article pages, and found that meta…

How to get all image tags from an html - php code

How to get all image tags from an html - php code

I wanted to fetch all image tags from a big html, and wanted to perform some…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…