| 285 | } |
| 286 | |
| 287 | std::string MetadataTagProcessorImpl::ValidateAndFormat_wikipedia(std::string v) |
| 288 | { |
| 289 | using std::string; |
| 290 | strings::Trim(v); |
| 291 | // Normalize by converting full URL to "lang:title" if necessary |
| 292 | // (some OSMers do not follow standard for wikipedia tag). |
| 293 | static string const base = ".wikipedia.org/wiki/"; |
| 294 | auto const baseIndex = v.find(base); |
| 295 | if (baseIndex != string::npos) |
| 296 | { |
| 297 | auto const baseSize = base.size(); |
| 298 | // Do not allow urls without article name after /wiki/. |
| 299 | if (v.size() > baseIndex + baseSize) |
| 300 | { |
| 301 | auto const slashIndex = v.rfind('/', baseIndex); |
| 302 | if (slashIndex != string::npos && slashIndex + 1 != baseIndex) |
| 303 | { |
| 304 | // Normalize article title according to OSM standards. |
| 305 | string title = url::UrlDecode(v.substr(baseIndex + baseSize)); |
| 306 | replace(title.begin(), title.end(), '_', ' '); |
| 307 | return v.substr(slashIndex + 1, baseIndex - slashIndex - 1) + ":" + title; |
| 308 | } |
| 309 | } |
| 310 | LOG(LDEBUG, ("Invalid Wikipedia tag value:", v)); |
| 311 | return {}; |
| 312 | } |
| 313 | // Standard case: "lang:Article Name With Spaces". |
| 314 | // Language and article are at least 2 chars each. |
| 315 | auto const colonIndex = v.find(':'); |
| 316 | if (colonIndex == string::npos || colonIndex < 2 || colonIndex + 2 > v.size()) |
| 317 | { |
| 318 | LOG(LDEBUG, ("Invalid Wikipedia tag value:", v)); |
| 319 | return {}; |
| 320 | } |
| 321 | // Check if it's not a random/invalid link. |
| 322 | if (v.find("//") != string::npos || v.find(".org") != string::npos) |
| 323 | { |
| 324 | LOG(LDEBUG, ("Invalid Wikipedia tag value:", v)); |
| 325 | return {}; |
| 326 | } |
| 327 | // Normalize to OSM standards. |
| 328 | replace(v.begin() + colonIndex, v.end(), '_', ' '); |
| 329 | return v; |
| 330 | } |
| 331 | |
| 332 | std::string MetadataTagProcessorImpl::ValidateAndFormat_wikimedia_commons(std::string v) |
| 333 | { |