{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14ec1b2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Refactor PHP database connections to use shared `db.php`\n",
    "\n",
    "This notebook centralizes database credentials and connection logic in `db.php`, then updates PHP files to include it instead of using repeated connection code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e5234ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import re\n",
    "\n",
    "root = Path(r'c:\\Users\\UTILISATEUR\\Desktop\\htdocs')\n",
    "changed = []\n",
    "\n",
    "for p in root.rglob('*.php'):\n",
    "    if p.name == 'db.php':\n",
    "        continue\n",
    "    if 'med' in p.parts:\n",
    "        continue\n",
    "\n",
    "    text = p.read_text(encoding='utf8')\n",
    "    if '$servername' not in text or '$username_db' not in text or '$password_db' not in text or '$dbname' not in text:\n",
    "        continue\n",
    "    lines = text.splitlines()\n",
    "    start = None\n",
    "    for i, line in enumerate(lines):\n",
    "        if re.match(r'^\\s*\\$servername\\s*=\\s*', line):\n",
    "            start = i\n",
    "            break\n",
    "    if start is None:\n",
    "        continue\n",
    "\n",
    "    def find_block(lines, start):\n",
    "        pdo_line = None\n",
    "        mysqli_line = None\n",
    "        try_line = None\n",
    "        for i in range(start, len(lines)):\n",
    "            line = lines[i]\n",
    "            if pdo_line is None and re.search(r'new\\s+PDO', line):\n",
    "                pdo_line = i\n",
    "            if mysqli_line is None and re.search(r'new\\s+mysqli', line):\n",
    "                mysqli_line = i\n",
    "            if try_line is None and re.search(r'\\btry\\b', line):\n",
    "                try_line = i\n",
    "            if pdo_line is not None and mysqli_line is not None:\n",
    "                break\n",
    "        if pdo_line is not None:\n",
    "            if try_line is None or try_line > pdo_line:\n",
    "                return pdo_line, pdo_line\n",
    "            brace = 0\n",
    "            for i in range(try_line, len(lines)):\n",
    "                brace += lines[i].count('{') - lines[i].count('}')\n",
    "                if brace == 0 and i > try_line:\n",
    "                    return try_line, i\n",
    "            return try_line, len(lines) - 1\n",
    "        if mysqli_line is not None:\n",
    "            end = mysqli_line\n",
    "            if end + 1 < len(lines) and re.match(r'^\\s*if\\s*\\(\\s*\\$[a-zA-Z_]+\\s*->\\s*connect_error\\s*\\)', lines[end+1]):\n",
    "                brace = 0\n",
    "                for j in range(end + 1, len(lines)):\n",
    "                    brace += lines[j].count('{') - lines[j].count('}')\n",
    "                    if brace == 0 and j > end + 1:\n",
    "                        end = j\n",
    "                        break\n",
    "            return start, end\n",
    "        return None, None\n",
    "\n",
    "    end_start, end = find_block(lines, start)\n",
    "    if end_start is None:\n",
    "        continue\n",
    "    if 'admin' in p.parts:\n",
    "        include_line = \"require_once __DIR__ . '/../db.php';\"\n",
    "    else:\n",
    "        include_line = \"require_once __DIR__ . '/db.php';\"\n",
    "    new_lines = lines[:start] + [include_line] + lines[end+1:]\n",
    "    p.write_text('\\n'.join(new_lines) + ('\\n' if text.endswith('\\n') else ''), encoding='utf8')\n",
    "    changed.append(str(p.relative_to(root)))\n",
    "\n",
    "print('changed files:', len(changed))\n",
    "for f in changed:\n",
    "    print(f)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
