| 1 | package App::Netdisco::Util::SiteLocal; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use File::Find (); | ||
| 7 | |||
| 8 | use Dancer ':syntax'; | ||
| 9 | use Path::Class qw/dir file/; | ||
| 10 | |||
| 11 | use base 'Exporter'; | ||
| 12 | our @EXPORT = (); | ||
| 13 | our @EXPORT_OK = qw/ scan_site_local scan_shadowed_files | ||
| 14 | site_local_rules site_local_paths /; | ||
| 15 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | App::Netdisco::Util::SiteLocal - find site-local files broken by shipped changes | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Site-local templates override shipped ones by relative path, so they keep | ||
| 24 | calling APIs that later releases removed. Most of the removals below fail | ||
| 25 | silently in the browser, which is why this exists: nothing else reports them. | ||
| 26 | |||
| 27 | Detection only. This module never writes to a file and never logs. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | # One row per shipped removal. A later rung adds a row here and nothing else. | ||
| 32 | # | ||
| 33 | # `pattern` is matched against each line of each file under the scanned paths. | ||
| 34 | # `release` is the release that removed or deprecated the thing, for the report | ||
| 35 | # to cite. | ||
| 36 | my @RULES = ( | ||
| 37 | { | ||
| 38 | name => 'he-js', | ||
| 39 | release => '2.105002', | ||
| 40 | pattern => qr/\bhe\s*\.\s*(?:encode|decode)\s*\(/, | ||
| 41 | advice => 'he.js was removed. Use DataTable.util.escapeHtml(), which is ' | ||
| 42 | . 'loaded on every page already.', | ||
| 43 | }, | ||
| 44 | { | ||
| 45 | name => 'history-js', | ||
| 46 | release => '2.105004', | ||
| 47 | # History with a capital H is history.js. The browser's own object is | ||
| 48 | # lowercase, so the case distinction is the whole test. window.History | ||
| 49 | # still exists after the removal, as the DOM interface constructor, so a | ||
| 50 | # site's `window.History && ...` guard stops running its branch and never | ||
| 51 | # throws. That silence is why this rule matters more than it looks. | ||
| 52 | pattern => qr/\bHistory\s*\.\s*(?:pushState|replaceState|getState|Adapter|enabled|log)\b/, | ||
| 53 | advice => 'history.js was removed, and the browser no longer drives ' | ||
| 54 | . 'the address bar at all. Let the pane response say where it ' | ||
| 55 | . 'went, with an HX-Push-Url or HX-Replace-Url header, and drop ' | ||
| 56 | . 'the history call and any popstate listener beside it.', | ||
| 57 | }, | ||
| 58 | { | ||
| 59 | name => 'natural-js', | ||
| 60 | release => '2.105004', | ||
| 61 | # Both the registered sort-type names and the two ways a DataTables column | ||
| 62 | # asks for one. A column asking for a type nothing registers falls back to | ||
| 63 | # string sorting, so the rows are quietly in the wrong order. | ||
| 64 | # | ||
| 65 | # The key's own quotes are optional because both spellings are common: a | ||
| 66 | # JSON-shaped config writes "sType", a JavaScript object literal writes | ||
| 67 | # sType bare, and a pattern requiring one silently misses the other. | ||
| 68 | pattern => qr/natural-(?:asc|desc)\b|["']?(?:type|sType)["']?\s*:\s*["']natural["']/, | ||
| 69 | advice => 'natural.js was removed. Use a built-in DataTables type, or ' | ||
| 70 | . 'the portsort or versionsort plug-ins netdisco still ships.', | ||
| 71 | }, | ||
| 72 | { | ||
| 73 | name => 'do-search', | ||
| 74 | release => '2.105006', | ||
| 75 | # The only rule here whose subject still works: do_search forwards to htmx | ||
| 76 | # rather than throwing, so a site that ignores this keeps loading its tab. | ||
| 77 | # Reported anyway, because the console notice only reaches whoever opens | ||
| 78 | # devtools. | ||
| 79 | pattern => qr/\bdo_search\s*\(/, | ||
| 80 | advice => 'do_search() now only forwards to htmx and will be removed in a ' | ||
| 81 | . 'future release. Give the form the hx-get, hx-target, hx-headers, ' | ||
| 82 | . 'hx-indicator and hx-sync attributes that share/views/device.tt ' | ||
| 83 | . 'uses, ' | ||
| 84 | . 'then drop the do_search call from the submit handler.', | ||
| 85 | }, | ||
| 86 | { | ||
| 87 | name => 'datatabledefaults-include', | ||
| 88 | release => '2.109000', | ||
| 89 | pattern => qr/INCLUDE\s+['"]ajax\/datatabledefaults\.tt['"]/, | ||
| 90 | advice => 'ajax/datatabledefaults.tt was removed. Delete the INCLUDE and ' | ||
| 91 | . 'move the table options into the data-nd-table attribute, as ' | ||
| 92 | . 'share/views/ajax/device/ports.tt now does.', | ||
| 93 | }, | ||
| 94 | { | ||
| 95 | name => 'has-sidebar-global', | ||
| 96 | release => '2.109000', | ||
| 97 | pattern => qr/has_sidebar\s*\[/, | ||
| 98 | advice => 'the has_sidebar global was removed. The sidebar visibility ' | ||
| 99 | . 'marker is now a hidden input carrying data-nd-has-sidebar ' | ||
| 100 | . 'for the tab and a 0 or 1 value, as ' | ||
| 101 | . 'share/views/sidebar/report/portlog.tt shows.', | ||
| 102 | }, | ||
| 103 | { | ||
| 104 | name => 'page-script-include', | ||
| 105 | release => '2.109000', | ||
| 106 | pattern => qr/INCLUDE\s+['"]js\//, | ||
| 107 | advice => 'share/views/js/ was removed. The page scripts now live in ' | ||
| 108 | . 'share/public/javascripts/netdisco.js and a template must not ' | ||
| 109 | . 'include a script; re-copy the template from this release.', | ||
| 110 | }, | ||
| 111 | { | ||
| 112 | name => 'portcontrol-js-renamed', | ||
| 113 | release => '2.109000', | ||
| 114 | pattern => qr/netdisco_portcontrol\.js/, | ||
| 115 | advice => 'netdisco_portcontrol.js was renamed netdisco-portcontrol.js ' | ||
| 116 | . 'and a reference to the old name 404s. Update the script tag ' | ||
| 117 | . 'to the new file name.', | ||
| 118 | }, | ||
| 119 | { | ||
| 120 | name => 'nd-submit', | ||
| 121 | release => '2.109000', | ||
| 122 | pattern => qr/\bnd_submit\s*\(/, | ||
| 123 | advice => 'nd_submit() was removed. Ask htmx to submit the form, with ' | ||
| 124 | . 'htmx.trigger(\'#ports_form\', \'submit\'), which is what every ' | ||
| 125 | . 'shipped caller now does.', | ||
| 126 | }, | ||
| 127 | { | ||
| 128 | name => 'page-title-globals', | ||
| 129 | release => '2.109000', | ||
| 130 | # data-nd-title and its dataset spelling are here rather than in a file | ||
| 131 | # rule because a layout keeping the attribute is harmless; only reading it | ||
| 132 | # is broken, and a site-local layout is where the read usually lives. | ||
| 133 | pattern => qr/\bupdate_page_title\s*\(|\bdefault_pgtitle\b|\bndTitle\b|data-nd-title/, | ||
| 134 | advice => 'the page title now arrives as a <title> element at the top of ' | ||
| 135 | . 'the pane response and nothing in the browser sets it. Delete ' | ||
| 136 | . 'the call, and delete data-nd-title from a layout copy: no ' | ||
| 137 | . 'shipped code reads it.', | ||
| 138 | }, | ||
| 139 | { | ||
| 140 | name => 'history-replay', | ||
| 141 | release => '2.109000', | ||
| 142 | pattern => qr/\bupdate_browser_history\s*\(|\bis_from_state_event\b/, | ||
| 143 | advice => 'the browser no longer records or replays the search history. ' | ||
| 144 | . 'A pane response says where it went with an HX-Push-Url or ' | ||
| 145 | . 'HX-Replace-Url header and Back refetches that address, so ' | ||
| 146 | . 'delete the call and the replay flag beside it.', | ||
| 147 | }, | ||
| 148 | { | ||
| 149 | name => 'jquery-deserialize', | ||
| 150 | release => '2.109000', | ||
| 151 | # The plugin shipped for one caller, the popstate replay, so a site naming | ||
| 152 | # either the file or the method is running that replay by hand. | ||
| 153 | pattern => qr/jquery-deserialize\.js|\.\s*deserialize\s*\(/, | ||
| 154 | advice => 'jquery-deserialize.js was removed with the history replay it ' | ||
| 155 | . 'was loaded for. Delete the script tag from a layout copy and ' | ||
| 156 | . 'the .deserialize() call from the handler: Back now refetches ' | ||
| 157 | . 'the page and the form comes back filled in from the server.', | ||
| 158 | }, | ||
| 159 | { | ||
| 160 | name => 'csv-download-link', | ||
| 161 | release => '2.109000', | ||
| 162 | pattern => qr/\bupdate_csv_download_link\s*\(|data-nd-csv\b|\bndCsv\b/, | ||
| 163 | advice => 'the CSV download link now arrives with the pane as an ' | ||
| 164 | . 'hx-swap-oob anchor and the browser no longer rebuilds it. ' | ||
| 165 | . 'Delete the call and the data-nd-csv attribute from a sidebar ' | ||
| 166 | . 'form copy, and keep the id nd_csv-download on the anchor so ' | ||
| 167 | . 'the response has somewhere to land.', | ||
| 168 | }, | ||
| 169 | { | ||
| 170 | name => 'htmx-abort-trigger', | ||
| 171 | release => '2.109000', | ||
| 172 | # anchored on the trigger rather than the event name: the vendored htmx | ||
| 173 | # bundle listens for this event, so a site keeping its own copy of that | ||
| 174 | # file would otherwise be told to change the library | ||
| 175 | pattern => qr/trigger\s*\(\s*[^)]*['"]htmx:abort['"]/, | ||
| 176 | advice => 'a tab form no longer owns its own request, so triggering ' | ||
| 177 | . 'htmx:abort on one cancels nothing. Give each sidebar form ' | ||
| 178 | . 'hx-sync="closest .nd_sidebar:replace" and delete the trigger: ' | ||
| 179 | . 'htmx then cancels the request the tab being left started.', | ||
| 180 | }, | ||
| 181 | { | ||
| 182 | name => 'datatables-js-renamed', | ||
| 183 | release => '2.109000', | ||
| 184 | pattern => qr/jquery\.dataTables\.min\.js/, | ||
| 185 | advice => 'jquery.dataTables.min.js was renamed dataTables.min.js, the ' | ||
| 186 | . 'library no longer needing jQuery, and a reference to the ' | ||
| 187 | . 'old name 404s. Update the script tag in a layout copy to the ' | ||
| 188 | . 'new file name: nothing else loads the library, so until then ' | ||
| 189 | . 'no table on any page builds.', | ||
| 190 | }, | ||
| 191 | { | ||
| 192 | name => 'floatthead-js', | ||
| 193 | release => '2.109000', | ||
| 194 | # The file name and the method, because the two break differently: a | ||
| 195 | # layout copy still loading the plug-in 404s, and a script still calling | ||
| 196 | # it throws once the plug-in is not there to define the method. | ||
| 197 | pattern => qr/jquery\.floatThead\.js|\.\s*floatThead\s*\(/, | ||
| 198 | advice => 'jquery.floatThead.js was removed and the stylesheet holds the ' | ||
| 199 | . 'header still instead. Delete the script tag from a layout copy ' | ||
| 200 | . 'and the .floatThead() call beside the table, and give that ' | ||
| 201 | . 'table the class nd_floatinghead: netdisco.css sticks the ' | ||
| 202 | . 'header of a table.nd_floatinghead inside the tab content, ' | ||
| 203 | . 'which is what the job queue now relies on.', | ||
| 204 | }, | ||
| 205 | { | ||
| 206 | name => 'jquery-ui-autocomplete', | ||
| 207 | release => '2.109000', | ||
| 208 | # Both the constructor and the method form, since a copy that only calls | ||
| 209 | # the method still throws once the library is gone. | ||
| 210 | # the call alone, not the class the widget adds: a site keeping its own | ||
| 211 | # copy of the library would otherwise be told to change the library, and | ||
| 212 | # both the minified script and its stylesheet carry that class. A site | ||
| 213 | # stylesheet overriding it is left unreported, which is cosmetic; a layout | ||
| 214 | # copy still loading the theme is caught by the rule below. | ||
| 215 | pattern => qr/\.\s*autocomplete\s*\(/, | ||
| 216 | advice => 'jQuery UI was removed. A field asks for suggestions in its ' | ||
| 217 | . 'markup now: give the input a data-nd-typeahead attribute ' | ||
| 218 | . 'holding the endpoint path, with data-nd-typeahead-min, ' | ||
| 219 | . '-params, -open and -first beside it, and delete the script.', | ||
| 220 | }, | ||
| 221 | { | ||
| 222 | name => 'jquery-ui-removed', | ||
| 223 | release => '2.109000', | ||
| 224 | # A copied layout names the script and the theme on two separate lines, | ||
| 225 | # and both 404 silently rather than reporting anything. | ||
| 226 | pattern => qr{jquery-ui(?:\.min)?\.(?:js|css)|css/smoothness/}, | ||
| 227 | advice => 'jquery-ui.min.js and the smoothness theme were removed. Drop ' | ||
| 228 | . 'both lines from your copy of the layout.', | ||
| 229 | }, | ||
| 230 | { | ||
| 231 | name => 'jstree-removed', | ||
| 232 | release => '2.109000', | ||
| 233 | # the container, the selector that reaches it, and the vendored path. Not | ||
| 234 | # the bare method call: the library contains that itself, in its own jQuery | ||
| 235 | # bridge, so matching it told a site keeping a copy to change the library. | ||
| 236 | # Measured against the real minified file, which this pattern does not hit. | ||
| 237 | pattern => qr{id=["']jstree["']|["']\#jstree["']|javascripts/jstree/}, | ||
| 238 | advice => 'jsTree was removed and the SNMP browser is rendered by the ' | ||
| 239 | . 'server. A copy of the pane should hold a ul#nd_snmp-tree that ' | ||
| 240 | . 'includes ajax/device/snmptree.tt; delete the container, the ' | ||
| 241 | . 'script and stylesheet lines from a layout copy, and any ' | ||
| 242 | . 'jstree() call.', | ||
| 243 | }, | ||
| 244 | { | ||
| 245 | name => 'daterangepicker-removed', | ||
| 246 | release => '2.109000', | ||
| 247 | # The three file names together, since a copied layout loses each on its | ||
| 248 | # own line, and the call, since a script keeping it throws once the | ||
| 249 | # library is gone. Measured against the library's own source, which does | ||
| 250 | # not carry this call form: it names itself only inside namespaced event | ||
| 251 | # strings such as click.daterangepicker, never as a call. | ||
| 252 | pattern => qr{javascripts/(?:moment\.min|daterangepicker)\.js | ||
| 253 | |css/daterangepicker\.css | ||
| 254 | |\.\s*daterangepicker\s*\(}x, | ||
| 255 | advice => 'moment.js, daterangepicker.js and daterangepicker.css were ' | ||
| 256 | . 'removed. Give the field a container like ' | ||
| 257 | . 'share/views/sidebar/_daterange.tt and delete the ' | ||
| 258 | . '.daterangepicker() call and the two script tags and the ' | ||
| 259 | . 'stylesheet link from a layout copy: netdisco-daterange.js ' | ||
| 260 | . 'drives the control now, so add a script tag for it to the ' | ||
| 261 | . 'layout copy in their place.', | ||
| 262 | }, | ||
| 263 | { | ||
| 264 | name => 'moment-removed', | ||
| 265 | release => '2.109000', | ||
| 266 | # The rule above catches a layout still loading the file; this catches the | ||
| 267 | # call, since moment was a global on every page for a decade and a site | ||
| 268 | # script formatting a date with it throws once the library is gone. The | ||
| 269 | # vendored DataTables render helper of the same name as the second | ||
| 270 | # alternative requires moment or luxon internally, so a table column | ||
| 271 | # configured with it now fails with neither present. | ||
| 272 | pattern => qr/\bmoment\s*\(|DataTable\.render\.datetime\s*\(/, | ||
| 273 | advice => 'moment.js was removed and nothing replaces it as a global. ' | ||
| 274 | . 'Format the date with native Date or Intl.DateTimeFormat ' | ||
| 275 | . 'instead of calling moment, and give a datetime column a ' | ||
| 276 | . 'different render function: the DataTable.render.datetime ' | ||
| 277 | . 'helper needs moment or luxon and neither is loaded any more.', | ||
| 278 | }, | ||
| 279 | { | ||
| 280 | name => 'daterange-input-id', | ||
| 281 | release => '2.109000', | ||
| 282 | # A site that only customized the sidebar markup never wrote a | ||
| 283 | # .daterangepicker() call or named a removed script, so the two rules | ||
| 284 | # above stay quiet and this id is the only trace left to catch. The id | ||
| 285 | # and the type="text" that went with it sit on different lines in the | ||
| 286 | # old markup, so the id alone is the anchor rather than the pair on one | ||
| 287 | # line. No shipped template sets this id any more. | ||
| 288 | pattern => qr/id=["']daterange["']/, | ||
| 289 | advice => 'this field carried the daterangepicker widget by its id, ' | ||
| 290 | . 'which is gone along with the JavaScript that drove it, so ' | ||
| 291 | . 'the field is now a plain text box with nothing behind it. ' | ||
| 292 | . 'Replace it with a copy of share/views/sidebar/_daterange.tt: ' | ||
| 293 | . 'netdisco-daterange.js drives the control from its markup, ' | ||
| 294 | . 'not by id.', | ||
| 295 | }, | ||
| 296 | { | ||
| 297 | name => 'toastr-removed', | ||
| 298 | release => '2.109000', | ||
| 299 | # Anchored on the call rather than toastr.options, which the library's own | ||
| 300 | # source reads from itself, so a site keeping a copy of the library is not | ||
| 301 | # told to change the library. | ||
| 302 | pattern => qr{javascripts/toastr\.js|css/toastr\.css | ||
| 303 | |toastr\s*\.\s*(?:error|success|info|warning)\s*\(}x, | ||
| 304 | advice => 'toastr was removed. Use ndToast (success, error and info) ' | ||
| 305 | . 'instead: delete the toastr.js script and toastr.css ' | ||
| 306 | . 'stylesheet lines from a layout copy and add a script tag ' | ||
| 307 | . 'for netdisco-toast.js in their place.', | ||
| 308 | }, | ||
| 309 | { | ||
| 310 | name => 'jquery-removed', | ||
| 311 | release => '2.109000', | ||
| 312 | # Anchored on the exact vendored file name, not a generic jquery*.js glob, | ||
| 313 | # so a site's own copy of jquery-ui.min.js or jquery.dataTables.min.js is | ||
| 314 | # left to the rule that already covers it. Also on the literal jQuery | ||
| 315 | # global and on a "$." method call such as $.ajax( or $.get(, which | ||
| 316 | # covers a site-local script still making jQuery's own request calls. | ||
| 317 | # A bare $( is still not caught: the widget rules above each anchor on | ||
| 318 | # their own call, and a bare $( would report the same line twice against | ||
| 319 | # their own test fixtures. | ||
| 320 | pattern => qr{javascripts/jquery-latest(?:\.min)?\.js|\bjQuery\b|\$\s*\.\s*\w+\s*\(}, | ||
| 321 | advice => 'jQuery must not appear in a site-local script or a copied ' | ||
| 322 | . 'layout. Delete the script line naming jquery-latest.min.js ' | ||
| 323 | . 'from a layout copy, and rewrite a jQuery call with native ' | ||
| 324 | . 'DOM methods: netdisco-request.js carries the request and ' | ||
| 325 | . 'form helpers the shipped scripts use. If the reported file ' | ||
| 326 | . 'is a copy of a third-party library rather than the site\'s ' | ||
| 327 | . 'own script, update that library to a build that needs no ' | ||
| 328 | . 'jQuery, or drop it, rather than rewriting its calls.', | ||
| 329 | }, | ||
| 330 | ); | ||
| 331 | |||
| 332 | # Rules that fault what a file does NOT contain, so a finding has no line. | ||
| 333 | # | ||
| 334 | # `startup` marks the ones the web application warns about at every worker | ||
| 335 | # boot. The bar is that nothing else tells anyone: a shadowed tab page renders | ||
| 336 | # an empty pane in silence and a shadowed layout kills every page. The rest | ||
| 337 | # degrade something the person can see going wrong, and a warning on every boot | ||
| 338 | # for those is noise a site cannot turn off. | ||
| 339 | my @FILE_RULES = ( | ||
| 340 | { | ||
| 341 | name => 'tab-page-shadow', | ||
| 342 | release => '2.105006', | ||
| 343 | startup => 1, | ||
| 344 | paths => [qw/ device.tt search.tt report.tt /], | ||
| 345 | requires => qr/\bhx-get\b/, | ||
| 346 | advice => 'this copy predates the htmx tab transport, so its sidebar ' | ||
| 347 | . 'form never fetches the pane and the tab renders empty. Copy ' | ||
| 348 | . 'the hx-get, hx-target, hx-headers and hx-indicator attributes ' | ||
| 349 | . 'from the shipped template of the same name.', | ||
| 350 | }, | ||
| 351 | { | ||
| 352 | name => 'layout-shadow', | ||
| 353 | release => '2.109000', | ||
| 354 | startup => 1, | ||
| 355 | paths => ['layouts/main.tt'], | ||
| 356 | requires => qr/data-nd-uri-base/, | ||
| 357 | advice => 'this copy predates data-nd-uri-base and the other body ' | ||
| 358 | . 'attributes that carry the page JavaScript settings, so ' | ||
| 359 | . 'netdisco.js throws reading them and every page is dead. ' | ||
| 360 | . 'Re-copy layouts/main.tt from this release and re-apply the ' | ||
| 361 | . 'local branding: it now carries those settings as body ' | ||
| 362 | . 'attributes and loads the scripts at the end of the body.', | ||
| 363 | }, | ||
| 364 | { | ||
| 365 | name => 'csv-download-target', | ||
| 366 | release => '2.109000', | ||
| 367 | paths => [qw/ device.tt search.tt report.tt admintask.tt /], | ||
| 368 | requires => qr/nd_csv-download/, | ||
| 369 | advice => 'this copy carries no element with the id nd_csv-download, so ' | ||
| 370 | . 'the download link the pane response now sends alongside the ' | ||
| 371 | . 'results has nothing to swap into and the page reports the ' | ||
| 372 | . 'miss to the console instead of offering a download. Copy the ' | ||
| 373 | . 'anchor carrying that id from the shipped template of the same ' | ||
| 374 | . 'name.', | ||
| 375 | }, | ||
| 376 | { | ||
| 377 | name => 'sidebar-reset-target', | ||
| 378 | release => '2.109000', | ||
| 379 | paths => ['device.tt'], | ||
| 380 | requires => qr/nd_sidebar-reset-link/, | ||
| 381 | advice => 'this copy carries no element with the id ' | ||
| 382 | . 'nd_sidebar-reset-link, so the Reset to Defaults link the ' | ||
| 383 | . 'Ports and Network Map panes now send alongside the results ' | ||
| 384 | . 'has nothing to swap into. Copy the anchor carrying that id ' | ||
| 385 | . 'from the shipped device.tt.', | ||
| 386 | }, | ||
| 387 | { | ||
| 388 | name => 'ports-row-keys', | ||
| 389 | release => '2.117003', | ||
| 390 | paths => ['ajax/device/ports.tt'], | ||
| 391 | requires => qr/\bneighbor_net_mac\b/, | ||
| 392 | advice => 'this copy predates the keys the Ports tab now attaches to ' | ||
| 393 | . 'each row, and keeps working without them, at a cost. It ' | ||
| 394 | . 'looks for the neighbor\'s MAC among the port\'s nodes, so ' | ||
| 395 | . 'when Connected Devices is shown and Connected Nodes is not, ' | ||
| 396 | . 'the nodes of every port with a neighbor are still fetched ' | ||
| 397 | . 'for it. And row.power, row.ssid and row.last_comment each ' | ||
| 398 | . 'run one query per port now that the page no longer ' | ||
| 399 | . 'prefetches them. To avoid both, read row.neighbor_net_mac, ' | ||
| 400 | . 'row.port_power, row.port_ssids (a list of SSID names, not ' | ||
| 401 | . 'rows) and row.port_comment as the shipped template does. ' | ||
| 402 | . 'A copy that shows none of those columns can ignore this.', | ||
| 403 | }, | ||
| 404 | { | ||
| 405 | name => 'tab-sync-attribute', | ||
| 406 | release => '2.109000', | ||
| 407 | paths => [qw/ device.tt search.tt report.tt admintask.tt /], | ||
| 408 | requires => qr/\bhx-sync\b/, | ||
| 409 | advice => 'this copy predates hx-sync, so leaving a tab whose results ' | ||
| 410 | . 'are still loading neither cancels that request nor takes its ' | ||
| 411 | . 'loading indicator down, and the answer can arrive over the ' | ||
| 412 | . 'tab moved to. Add hx-sync="closest .nd_sidebar:replace" ' | ||
| 413 | . 'beside the hx-get on each sidebar form.', | ||
| 414 | }, | ||
| 415 | ); | ||
| 416 | |||
| 417 | =head2 scan_shadowed_files( \%args ) | ||
| 418 | |||
| 419 | Returns a finding for each file under C<< $args{paths} >> that shadows one of | ||
| 420 | one of the shipped tab pages without carrying what that page needs. | ||
| 421 | |||
| 422 | A finding is a hashref with keys C<kind> (always C<file>), C<path>, C<rule>, | ||
| 423 | C<release> and C<advice>. There is no C<line>: the fault is an absence, so | ||
| 424 | there is no line to point at. | ||
| 425 | |||
| 426 | C<< $args{rules} >> optionally restricts the scan to the named rules. | ||
| 427 | C<< $args{startup} >> restricts it to the rules the web application warns | ||
| 428 | about at every worker boot, which is how L<App::Netdisco::Web> asks for them: | ||
| 429 | the rest are for the person who ran C<checksitelocal> and asked. | ||
| 430 | |||
| 431 | Bounded on purpose. This opens at most one file per shipped tab page per | ||
| 432 | configured path, because L<App::Netdisco::Web> runs it at every worker startup, | ||
| 433 | where C<scan_site_local>'s walk of the whole tree would not be welcome. | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | sub scan_shadowed_files { | ||
| 438 | my $args = shift || {}; | ||
| 439 | my @paths = @{ $args->{paths} || [] }; | ||
| 440 | my @findings = (); | ||
| 441 | |||
| 442 | my %wanted = map {($_ => 1)} @{ $args->{rules} || [] }; | ||
| 443 | my @rules = (keys %wanted) | ||
| 444 | ? (grep { $wanted{ $_->{name} } } @FILE_RULES) : @FILE_RULES; | ||
| 445 | @rules = grep { $_->{startup} } @rules if $args->{startup}; | ||
| 446 | |||
| 447 | foreach my $rule (@rules) { | ||
| 448 | foreach my $relative (@{ $rule->{paths} }) { | ||
| 449 | foreach my $path (@paths) { | ||
| 450 | next unless defined $path and length $path; | ||
| 451 | my $shadow = file($path, $relative)->stringify; | ||
| 452 | next unless -f $shadow; | ||
| 453 | next if _file_contains($shadow, $rule->{requires}); | ||
| 454 | push @findings, { | ||
| 455 | kind => 'file', | ||
| 456 | path => $shadow, | ||
| 457 | rule => $rule->{name}, | ||
| 458 | release => $rule->{release}, | ||
| 459 | advice => $rule->{advice}, | ||
| 460 | }; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | } | ||
| 464 | |||
| 465 | # sort returns undef in scalar context, not a count | ||
| 466 | my @sorted = sort { $a->{path} cmp $b->{path} } @findings; | ||
| 467 | return @sorted; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub _file_contains { | ||
| 471 | my ($file, $pattern) = @_; | ||
| 472 | open my $fh, '<', $file or return 0; | ||
| 473 | while (my $line = <$fh>) { | ||
| 474 | next unless $line =~ $pattern; | ||
| 475 | close $fh; | ||
| 476 | return 1; | ||
| 477 | } | ||
| 478 | close $fh; | ||
| 479 | return 0; | ||
| 480 | } | ||
| 481 | |||
| 482 | =head2 site_local_rules | ||
| 483 | |||
| 484 | Returns the rule table as a list of hashrefs with keys C<name>, C<release> and | ||
| 485 | C<advice>. Lists what is checked without scanning anything. | ||
| 486 | |||
| 487 | =cut | ||
| 488 | |||
| 489 | sub site_local_rules { | ||
| 490 | return map {; +{ name => $_->{name}, release => $_->{release}, | ||
| 491 | advice => $_->{advice} } } (@RULES, @FILE_RULES); | ||
| 492 | } | ||
| 493 | |||
| 494 | =head2 site_local_paths | ||
| 495 | |||
| 496 | Returns the directories a scan should cover: C<template_paths> from | ||
| 497 | C<deployment.yml>, plus the two C<nd-site-local> directories when | ||
| 498 | C<site_local_files> is on. | ||
| 499 | |||
| 500 | These are the same directories L<App::Netdisco::Web> unshifts onto the Template | ||
| 501 | Toolkit C<INCLUDE_PATH>. Derived here rather than passed in, so the | ||
| 502 | C<checksitelocal> action and the web app's startup check cannot drift apart. | ||
| 503 | |||
| 504 | The shipped C<share/views> is deliberately absent: it is what site-local files | ||
| 505 | override, not something to scan. | ||
| 506 | |||
| 507 | =cut | ||
| 508 | |||
| 509 | sub site_local_paths { | ||
| 510 | my @paths = (); | ||
| 511 | my $configured = setting('template_paths'); | ||
| 512 | push @paths, @$configured if $configured and ref [] eq ref $configured; | ||
| 513 | |||
| 514 | if (setting('site_local_files')) { | ||
| 515 | my $home = ($ENV{NETDISCO_HOME} || $ENV{HOME}); | ||
| 516 | push @paths, | ||
| 517 | dir($home, 'nd-site-local', 'share')->stringify, | ||
| 518 | dir($home, 'nd-site-local', 'share', 'views')->stringify; | ||
| 519 | } | ||
| 520 | |||
| 521 | return @paths; | ||
| 522 | } | ||
| 523 | |||
| 524 | =head2 scan_site_local( \%args ) | ||
| 525 | |||
| 526 | Scans every file under each directory in C<< $args{paths} >> and returns the | ||
| 527 | findings, sorted by path then line. C<< $args{rules} >> optionally restricts the | ||
| 528 | scan to the named rules. | ||
| 529 | |||
| 530 | A finding is a hashref with keys C<path>, C<line>, C<rule>, C<release>, | ||
| 531 | C<excerpt> and C<advice>. | ||
| 532 | |||
| 533 | A directory that does not exist is skipped rather than fatal: C<template_paths> | ||
| 534 | routinely names directories a given install has not created. | ||
| 535 | |||
| 536 | =cut | ||
| 537 | |||
| 538 | sub scan_site_local { | ||
| 539 | my $args = shift || {}; | ||
| 540 | my @paths = @{ $args->{paths} || [] }; | ||
| 541 | |||
| 542 | my %wanted = map {($_ => 1)} @{ $args->{rules} || [] }; | ||
| 543 | my @rules = (keys %wanted) | ||
| 544 | ? (grep { $wanted{ $_->{name} } } @RULES) : @RULES; | ||
| 545 | return () unless scalar @rules; | ||
| 546 | |||
| 547 | # site_local_paths returns nd-site-local/share and nd-site-local/share/views, | ||
| 548 | # and the second is inside the first, so every file below views is reached | ||
| 549 | # twice. Read each file once, keyed on the name File::Find gives it. | ||
| 550 | my %seen = (); | ||
| 551 | my @findings = (); | ||
| 552 | |||
| 553 | foreach my $path (@paths) { | ||
| 554 | next unless defined $path and length $path and -d $path; | ||
| 555 | File::Find::find({ no_chdir => 1, wanted => sub { | ||
| 556 | return unless -f $File::Find::name; | ||
| 557 | return if $seen{ $File::Find::name }++; | ||
| 558 | push @findings, _scan_file($File::Find::name, \@rules); | ||
| 559 | }}, $path); | ||
| 560 | } | ||
| 561 | |||
| 562 | # Assigned rather than returned straight from sort, which yields undef in | ||
| 563 | # scalar context instead of a count. | ||
| 564 | my @sorted = | ||
| 565 | sort { $a->{path} cmp $b->{path} or $a->{line} <=> $b->{line} } @findings; | ||
| 566 | |||
| 567 | return @sorted; | ||
| 568 | } | ||
| 569 | |||
| 570 | # Read once, test every rule against every line. A line can match more than one | ||
| 571 | # rule and each match is its own finding, because each carries its own advice. | ||
| 572 | # The line number is counted here rather than read from $., which is not reset | ||
| 573 | # between files and would number the second file from where the first ended. | ||
| 574 | sub _scan_file { | ||
| 575 | my ($file, $rules) = @_; | ||
| 576 | |||
| 577 | open my $fh, '<', $file or return (); | ||
| 578 | my @findings = (); | ||
| 579 | my $lineno = 0; | ||
| 580 | |||
| 581 | while (my $line = <$fh>) { | ||
| 582 | $lineno++; | ||
| 583 | chomp $line; | ||
| 584 | foreach my $rule (@$rules) { | ||
| 585 | next unless $line =~ $rule->{pattern}; | ||
| 586 | my $excerpt = $line; | ||
| 587 | $excerpt =~ s/^\s+|\s+$//g; | ||
| 588 | push @findings, { | ||
| 589 | path => $file, | ||
| 590 | line => $lineno, | ||
| 591 | rule => $rule->{name}, | ||
| 592 | release => $rule->{release}, | ||
| 593 | excerpt => $excerpt, | ||
| 594 | advice => $rule->{advice}, | ||
| 595 | }; | ||
| 596 | } | ||
| 597 | } | ||
| 598 | close $fh; | ||
| 599 | |||
| 600 | return @findings; | ||
| 601 | } | ||
| 602 | |||
| 603 | 1; |