|
| 1 | +#!/bin/env bash |
| 2 | + |
| 3 | +# Search the media library post title on every network site. Return as |
| 4 | +# a CSV. |
| 5 | + |
| 6 | +source 'source/includes.sh'; |
| 7 | + |
| 8 | +# The text to search for in the title. |
| 9 | +search_regex=''; # Leave empty to return all. |
| 10 | + |
| 11 | +# The file to save results to. |
| 12 | +output_file='search-results.txt'; |
| 13 | + |
| 14 | +# Reset the output file contents. |
| 15 | +echo '' > $output_file; |
| 16 | + |
| 17 | +echo; |
| 18 | +echo 'Beginning search...'; |
| 19 | + |
| 20 | +# Get every network site. |
| 21 | +all_site_ids=$(wp_skip_all site list --field="blog_id"); |
| 22 | + |
| 23 | +# Loop over every network site. |
| 24 | +for site_id in $all_site_ids; do |
| 25 | + |
| 26 | + if [[ 1 -eq $site_id ]]; then |
| 27 | + continue; |
| 28 | + fi |
| 29 | + |
| 30 | + echo >> $output_file; |
| 31 | + echo "Results for Site ID ${site_id}" >> $output_file; |
| 32 | + |
| 33 | + # Search for multi term phrases. Cases sensitive. |
| 34 | + wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_type = 'attachment' AND post_REGEXP '${search_regex}';" >> $output_file; |
| 35 | + |
| 36 | + # The SQL query for searching the media library of a network site. |
| 37 | + sql_query="SELECT |
| 38 | + $site_id as site_id, |
| 39 | + p.ID, |
| 40 | + p.post_title, |
| 41 | + p.post_mime_type, |
| 42 | + pm1.meta_value AS file_path, |
| 43 | + p.guid AS URL |
| 44 | + FROM |
| 45 | + wp_${site_id}_posts p |
| 46 | + LEFT JOIN |
| 47 | + wp_3_postmeta pm1 ON (p.ID = pm1.post_id AND pm1.meta_key = '_wp_attached_file') |
| 48 | + WHERE |
| 49 | + p.post_type = 'attachment'" |
| 50 | + |
| 51 | + # Check if a search string was provided. |
| 52 | + if [ -n "$search_regex" ]; then |
| 53 | + QUERY="$QUERY AND p.post_title REGEXP '${search_regex}'" |
| 54 | + fi |
| 55 | + |
| 56 | + wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_type = 'attachment' AND post_REGEXP '${search_regex}';" tee -a $output_file; |
| 57 | + |
| 58 | +done; |
| 59 | + |
| 60 | +echo "Finished search. Results are in ${output_file}"; |
| 61 | +echo; |
0 commit comments